@@ -79,7 +79,7 @@ export class RedisPubSub implements PubSubEngine {
79
79
this . redisSubscriber . on ( messageEventName , this . onMessage . bind ( this , undefined ) ) ;
80
80
81
81
this . subscriptionMap = { } ;
82
- this . subsRefsMap = { } ;
82
+ this . subsRefsMap = new Map < string , Set < number > > ( ) ;
83
83
this . currentSubscriptionId = 0 ;
84
84
}
85
85
@@ -97,9 +97,13 @@ export class RedisPubSub implements PubSubEngine {
97
97
const id = this . currentSubscriptionId ++ ;
98
98
this . subscriptionMap [ id ] = [ triggerName , onMessage ] ;
99
99
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 ) ;
103
107
return Promise . resolve ( id ) ;
104
108
} else {
105
109
return new Promise < number > ( ( resolve , reject ) => {
@@ -109,10 +113,7 @@ export class RedisPubSub implements PubSubEngine {
109
113
if ( err ) {
110
114
reject ( err ) ;
111
115
} else {
112
- this . subsRefsMap [ triggerName ] = [
113
- ...( this . subsRefsMap [ triggerName ] || [ ] ) ,
114
- id ,
115
- ] ;
116
+ refs . add ( id ) ;
116
117
resolve ( id ) ;
117
118
}
118
119
} ) ;
@@ -122,21 +123,18 @@ export class RedisPubSub implements PubSubEngine {
122
123
123
124
public unsubscribe ( subId : number ) : void {
124
125
const [ triggerName = null ] = this . subscriptionMap [ subId ] || [ ] ;
125
- const refs = this . subsRefsMap [ triggerName ] ;
126
+ const refs = this . subsRefsMap . get ( triggerName ) ;
126
127
127
128
if ( ! refs ) throw new Error ( `There is no subscription of id "${ subId } "` ) ;
128
129
129
- if ( refs . length === 1 ) {
130
+ if ( refs . size === 1 ) {
130
131
// unsubscribe from specific channel and pattern match
131
132
this . redisSubscriber . unsubscribe ( triggerName ) ;
132
133
this . redisSubscriber . punsubscribe ( triggerName ) ;
133
134
134
- delete this . subsRefsMap [ triggerName ] ;
135
+ this . subsRefsMap . delete ( triggerName ) ;
135
136
} 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 ) ;
140
138
}
141
139
delete this . subscriptionMap [ subId ] ;
142
140
}
@@ -168,14 +166,14 @@ export class RedisPubSub implements PubSubEngine {
168
166
private readonly reviver : Reviver ;
169
167
170
168
private readonly subscriptionMap : { [ subId : number ] : [ string , OnMessage < unknown > ] } ;
171
- private readonly subsRefsMap : { [ trigger : string ] : Array < number > } ;
169
+ private readonly subsRefsMap : Map < string , Set < number > > ;
172
170
private currentSubscriptionId : number ;
173
171
174
172
private onMessage ( pattern : string , channel : string , message : string ) {
175
- const subscribers = this . subsRefsMap [ pattern || channel ] ;
173
+ const subscribers = this . subsRefsMap . get ( pattern || channel ) ;
176
174
177
175
// Don't work for nothing..
178
- if ( ! subscribers || ! subscribers . length ) return ;
176
+ if ( ! subscribers ?. size ) return ;
179
177
180
178
let parsedMessage ;
181
179
try {
@@ -186,10 +184,10 @@ export class RedisPubSub implements PubSubEngine {
186
184
parsedMessage = message ;
187
185
}
188
186
189
- for ( const subId of subscribers ) {
187
+ subscribers . forEach ( subId => {
190
188
const [ , listener ] = this . subscriptionMap [ subId ] ;
191
189
listener ( parsedMessage ) ;
192
- }
190
+ } ) ;
193
191
}
194
192
}
195
193
0 commit comments