Skip to content

Commit 7d87f15

Browse files
authored
Merge pull request #295 from tjmehta/subRefsMap-use-maps-and-sets
make pubsub subRefsMap use maps and sets
2 parents 7fbd63a + 5f0151b commit 7d87f15

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/redis-pubsub.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class RedisPubSub implements PubSubEngine {
7979
this.redisSubscriber.on(messageEventName, this.onMessage.bind(this, undefined));
8080

8181
this.subscriptionMap = {};
82-
this.subsRefsMap = {};
82+
this.subsRefsMap = new Map<string, Set<number>>();
8383
this.currentSubscriptionId = 0;
8484
}
8585

@@ -97,9 +97,13 @@ export class RedisPubSub implements PubSubEngine {
9797
const id = this.currentSubscriptionId++;
9898
this.subscriptionMap[id] = [triggerName, onMessage];
9999

100-
const refs = this.subsRefsMap[triggerName];
101-
if (refs && refs.length > 0) {
102-
this.subsRefsMap[triggerName] = [...refs, id];
100+
if (!this.subsRefsMap.has(triggerName)) {
101+
this.subsRefsMap.set(triggerName, new Set());
102+
}
103+
104+
const refs = this.subsRefsMap.get(triggerName);
105+
if (refs.size > 0) {
106+
refs.add(id);
103107
return Promise.resolve(id);
104108
} else {
105109
return new Promise<number>((resolve, reject) => {
@@ -109,10 +113,7 @@ export class RedisPubSub implements PubSubEngine {
109113
if (err) {
110114
reject(err);
111115
} else {
112-
this.subsRefsMap[triggerName] = [
113-
...(this.subsRefsMap[triggerName] || []),
114-
id,
115-
];
116+
refs.add(id);
116117
resolve(id);
117118
}
118119
});
@@ -122,21 +123,18 @@ export class RedisPubSub implements PubSubEngine {
122123

123124
public unsubscribe(subId: number): void {
124125
const [triggerName = null] = this.subscriptionMap[subId] || [];
125-
const refs = this.subsRefsMap[triggerName];
126+
const refs = this.subsRefsMap.get(triggerName);
126127

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

129-
if (refs.length === 1) {
130+
if (refs.size === 1) {
130131
// unsubscribe from specific channel and pattern match
131132
this.redisSubscriber.unsubscribe(triggerName);
132133
this.redisSubscriber.punsubscribe(triggerName);
133134

134-
delete this.subsRefsMap[triggerName];
135+
this.subsRefsMap.delete(triggerName);
135136
} else {
136-
const index = refs.indexOf(subId);
137-
this.subsRefsMap[triggerName] = index === -1
138-
? refs
139-
: [...refs.slice(0, index), ...refs.slice(index + 1)];
137+
refs.delete(subId);
140138
}
141139
delete this.subscriptionMap[subId];
142140
}
@@ -168,14 +166,14 @@ export class RedisPubSub implements PubSubEngine {
168166
private readonly reviver: Reviver;
169167

170168
private readonly subscriptionMap: { [subId: number]: [string, OnMessage<unknown>] };
171-
private readonly subsRefsMap: { [trigger: string]: Array<number> };
169+
private readonly subsRefsMap: Map<string, Set<number>>;
172170
private currentSubscriptionId: number;
173171

174172
private onMessage(pattern: string, channel: string, message: string) {
175-
const subscribers = this.subsRefsMap[pattern || channel];
173+
const subscribers = this.subsRefsMap.get(pattern || channel);
176174

177175
// Don't work for nothing..
178-
if (!subscribers || !subscribers.length) return;
176+
if (!subscribers?.size) return;
179177

180178
let parsedMessage;
181179
try {
@@ -186,10 +184,10 @@ export class RedisPubSub implements PubSubEngine {
186184
parsedMessage = message;
187185
}
188186

189-
for (const subId of subscribers) {
187+
subscribers.forEach(subId => {
190188
const [, listener] = this.subscriptionMap[subId];
191189
listener(parsedMessage);
192-
}
190+
});
193191
}
194192
}
195193

0 commit comments

Comments
 (0)