Skip to content

Add option to skip punsubscribe call #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/redis-pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface PubSubRedisOptions {
deserializer?: Deserializer;
messageEventName?: string;
pmessageEventName?: string;
skipPunsubscribe?: boolean;
}

export class RedisPubSub implements PubSubEngine {
Expand All @@ -33,6 +34,7 @@ export class RedisPubSub implements PubSubEngine {
deserializer,
messageEventName = 'message',
pmessageEventName = 'pmessage',
skipPunsubscribe = false,
} = options;

this.triggerTransform = triggerTransform || (trigger => trigger as string);
Expand All @@ -44,6 +46,7 @@ export class RedisPubSub implements PubSubEngine {
this.reviver = reviver;
this.serializer = serializer;
this.deserializer = deserializer;
this.skipPunsubscribe = skipPunsubscribe;

if (subscriber && publisher) {
this.redisPublisher = publisher;
Expand Down Expand Up @@ -159,7 +162,9 @@ export class RedisPubSub implements PubSubEngine {
if (refs.size === 1) {
// unsubscribe from specific channel and pattern match
this.redisSubscriber.unsubscribe(triggerName);
this.redisSubscriber.punsubscribe(triggerName);
if (!this.skipPunsubscribe) {
this.redisSubscriber.punsubscribe(triggerName);
}

this.subsRefsMap.delete(triggerName);
} else {
Expand Down Expand Up @@ -197,6 +202,7 @@ export class RedisPubSub implements PubSubEngine {
private readonly redisSubscriber: RedisClient;
private readonly redisPublisher: RedisClient;
private readonly reviver: Reviver;
private readonly skipPunsubscribe: boolean;

private readonly subscriptionMap: { [subId: number]: [string, OnMessage<unknown>] };
private readonly subsRefsMap: Map<string, Set<number>>;
Expand Down
20 changes: 20 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ describe('RedisPubSub', () => {
});
});

it('skips punsubscribe when skipPunsubscribe is set to true', done => {
const pubSub = new RedisPubSub({...mockOptions, skipPunsubscribe: true});
pubSub.subscribe('Posts', () => null).then(subId => {
pubSub.unsubscribe(subId);

try {

expect(unsubscribeSpy.callCount).to.equals(1);
expect(unsubscribeSpy.lastCall.args).to.have.members(['Posts']);

expect(punsubscribeSpy.callCount).to.equals(0);

done();

} catch (e) {
done(e);
}
});
});

it('cleans up correctly the memory when unsubscribing', done => {
const pubSub = new RedisPubSub(mockOptions);
Promise.all([
Expand Down