Skip to content
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

fix(unsubscribe): only punsubscribe when a pattern matcher actually has been used #669

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
16 changes: 10 additions & 6 deletions src/redis-pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class RedisPubSub implements PubSubEngine {

const triggerName: string = this.triggerTransform(trigger, options);
const id = this.currentSubscriptionId++;
this.subscriptionMap[id] = [triggerName, onMessage];
const patternSubscription: boolean = !!options['pattern'];
this.subscriptionMap[id] = [triggerName, onMessage, patternSubscription];

if (!this.subsRefsMap.has(triggerName)) {
this.subsRefsMap.set(triggerName, new Set());
Expand All @@ -127,7 +128,7 @@ export class RedisPubSub implements PubSubEngine {
subsPendingRefsMap.set(triggerName, { refs: [], pending });

const sub = new Promise<number>((resolve, reject) => {
const subscribeFn = options['pattern'] ? this.redisSubscriber.psubscribe : this.redisSubscriber.subscribe;
const subscribeFn = patternSubscription ? this.redisSubscriber.psubscribe : this.redisSubscriber.subscribe;

subscribeFn.call(this.redisSubscriber, triggerName, err => {
if (err) {
Expand All @@ -151,15 +152,18 @@ export class RedisPubSub implements PubSubEngine {
}

public unsubscribe(subId: number): void {
const [triggerName = null] = this.subscriptionMap[subId] || [];
const [triggerName = null,, patternSubscription] = this.subscriptionMap[subId] || [];
const refs = this.subsRefsMap.get(triggerName);

if (!refs) throw new Error(`There is no subscription of id "${subId}"`);

if (refs.size === 1) {
// unsubscribe from specific channel and pattern match
this.redisSubscriber.unsubscribe(triggerName);
this.redisSubscriber.punsubscribe(triggerName);
if (patternSubscription) {
this.redisSubscriber.punsubscribe(triggerName);
} else {
this.redisSubscriber.unsubscribe(triggerName);
}

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

private readonly subscriptionMap: { [subId: number]: [string, OnMessage<unknown>] };
private readonly subscriptionMap: { [subId: number]: [string, OnMessage<unknown>, patternSubscription: boolean] };
private readonly subsRefsMap: Map<string, Set<number>>;
private readonly subsPendingRefsMap: Map<string, { refs: number[], pending: Promise<number> }>;
private currentSubscriptionId: number;
Expand Down
21 changes: 20 additions & 1 deletion src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('RedisPubSub', () => {
});
});

it('can unsubscribe from specific redis channel', done => {
it('can unsubscribe from specific redis channel with pattern matching disabled', done => {
const pubSub = new RedisPubSub(mockOptions);
pubSub.subscribe('Posts', () => null).then(subId => {
pubSub.unsubscribe(subId);
Expand All @@ -106,6 +106,25 @@ describe('RedisPubSub', () => {
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('can unsubscribe from specific redis channel with pattern matching enabled', done => {
const pubSub = new RedisPubSub(mockOptions);
pubSub.subscribe('Posts', () => null, { pattern: true }).then(subId => {
pubSub.unsubscribe(subId);

try {

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

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

Expand Down