Skip to content

Commit 0895631

Browse files
committed
make pubsub subRefsMap use maps and sets
1 parent e2a165e commit 0895631

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/redis-pubsub.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Cluster, Ok, Redis, RedisOptions} from 'ioredis';
2-
import {PubSubEngine} from 'graphql-subscriptions';
2+
33
import {PubSubAsyncIterator} from './pubsub-async-iterator';
4+
import {PubSubEngine} from 'graphql-subscriptions';
45

56
type RedisClient = Redis | Cluster;
67
type OnMessage<T> = (message: T) => void;
@@ -74,7 +75,7 @@ export class RedisPubSub implements PubSubEngine {
7475
this.redisSubscriber.on('message', this.onMessage.bind(this, undefined));
7576

7677
this.subscriptionMap = {};
77-
this.subsRefsMap = {};
78+
this.subsRefsMap = new Map<string, Set<number>>();
7879
this.currentSubscriptionId = 0;
7980
}
8081

@@ -92,9 +93,9 @@ export class RedisPubSub implements PubSubEngine {
9293
const id = this.currentSubscriptionId++;
9394
this.subscriptionMap[id] = [triggerName, onMessage];
9495

95-
const refs = this.subsRefsMap[triggerName];
96-
if (refs && refs.length > 0) {
97-
this.subsRefsMap[triggerName] = [...refs, id];
96+
const refs = this.subsRefsMap.get(triggerName);
97+
if (refs?.size > 0) {
98+
refs.add(id)
9899
return Promise.resolve(id);
99100
} else {
100101
return new Promise<number>((resolve, reject) => {
@@ -104,10 +105,7 @@ export class RedisPubSub implements PubSubEngine {
104105
if (err) {
105106
reject(err);
106107
} else {
107-
this.subsRefsMap[triggerName] = [
108-
...(this.subsRefsMap[triggerName] || []),
109-
id,
110-
];
108+
refs.add(id)
111109
resolve(id);
112110
}
113111
});
@@ -117,21 +115,18 @@ export class RedisPubSub implements PubSubEngine {
117115

118116
public unsubscribe(subId: number): void {
119117
const [triggerName = null] = this.subscriptionMap[subId] || [];
120-
const refs = this.subsRefsMap[triggerName];
118+
const refs = this.subsRefsMap.get(triggerName);
121119

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

124-
if (refs.length === 1) {
122+
if (refs.size === 1) {
125123
// unsubscribe from specific channel and pattern match
126124
this.redisSubscriber.unsubscribe(triggerName);
127125
this.redisSubscriber.punsubscribe(triggerName);
128126

129-
delete this.subsRefsMap[triggerName];
127+
this.subsRefsMap.delete(triggerName);
130128
} else {
131-
const index = refs.indexOf(subId);
132-
this.subsRefsMap[triggerName] = index === -1
133-
? refs
134-
: [...refs.slice(0, index), ...refs.slice(index + 1)];
129+
refs.delete(subId)
135130
}
136131
delete this.subscriptionMap[subId];
137132
}
@@ -163,14 +158,14 @@ export class RedisPubSub implements PubSubEngine {
163158
private readonly reviver: Reviver;
164159

165160
private readonly subscriptionMap: { [subId: number]: [string, OnMessage<unknown>] };
166-
private readonly subsRefsMap: { [trigger: string]: Array<number> };
161+
private readonly subsRefsMap: Map<string, Set<number>>;
167162
private currentSubscriptionId: number;
168163

169164
private onMessage(pattern: string, channel: string, message: string) {
170-
const subscribers = this.subsRefsMap[pattern || channel];
165+
const subscribers = this.subsRefsMap.get(pattern || channel);
171166

172167
// Don't work for nothing..
173-
if (!subscribers || !subscribers.length) return;
168+
if (!subscribers?.size) return;
174169

175170
let parsedMessage;
176171
try {
@@ -179,7 +174,7 @@ export class RedisPubSub implements PubSubEngine {
179174
parsedMessage = message;
180175
}
181176

182-
for (const subId of subscribers) {
177+
for (const subId in subscribers) {
183178
const [, listener] = this.subscriptionMap[subId];
184179
listener(parsedMessage);
185180
}

0 commit comments

Comments
 (0)