|
|
|
@ -10,8 +10,6 @@ export class PubSubRedis {
|
|
|
|
|
|
|
|
|
|
public static redisClient: Redis; |
|
|
|
|
public static redisSubscriber: Redis; |
|
|
|
|
private static unsubscribeCallbacks: { [key: string]: () => Promise<void> } = |
|
|
|
|
{}; |
|
|
|
|
|
|
|
|
|
public static async init() { |
|
|
|
|
if (!PubSubRedis.available) { |
|
|
|
@ -43,25 +41,16 @@ export class PubSubRedis {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static async unsubscribe(channel: string) { |
|
|
|
|
if (!PubSubRedis.initialized) { |
|
|
|
|
if (!PubSubRedis.available) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await PubSubRedis.init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (PubSubRedis.unsubscribeCallbacks[channel]) { |
|
|
|
|
await PubSubRedis.unsubscribeCallbacks[channel](); |
|
|
|
|
delete PubSubRedis.unsubscribeCallbacks[channel]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
*
|
|
|
|
|
* @param channel
|
|
|
|
|
* @param callback
|
|
|
|
|
* @returns Returns a callback to unsubscribe |
|
|
|
|
*/ |
|
|
|
|
static async subscribe( |
|
|
|
|
channel: string, |
|
|
|
|
callback: (message: any) => Promise<void>, |
|
|
|
|
) { |
|
|
|
|
): Promise<(keepRedisChannel?: boolean) => Promise<void>> { |
|
|
|
|
if (!PubSubRedis.initialized) { |
|
|
|
|
if (!PubSubRedis.available) { |
|
|
|
|
return; |
|
|
|
@ -83,8 +72,9 @@ export class PubSubRedis {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
PubSubRedis.redisSubscriber.on('message', onMessage); |
|
|
|
|
PubSubRedis.unsubscribeCallbacks[channel] = async () => { |
|
|
|
|
await PubSubRedis.redisSubscriber.unsubscribe(channel); |
|
|
|
|
return async (keepRedisChannel = false) => { |
|
|
|
|
// keepRedisChannel is used to keep the channel open for other subscribers
|
|
|
|
|
if (!keepRedisChannel) await PubSubRedis.redisSubscriber.unsubscribe(channel); |
|
|
|
|
PubSubRedis.redisSubscriber.off('message', onMessage); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|