16
16
*/
17
17
18
18
import firebase from '@firebase/app' ;
19
- import { forEach , contains , isEmpty , getCount , safeGet } from '@firebase/util' ;
19
+ import { contains , isEmpty , safeGet } from '@firebase/util' ;
20
20
import { stringify } from '@firebase/util' ;
21
21
import { assert } from '@firebase/util' ;
22
22
import { error , log , logWrapper , warn , ObjectToUniqueKey } from './util/util' ;
@@ -76,9 +76,8 @@ export class PersistentConnection extends ServerActions {
76
76
id = PersistentConnection . nextPersistentConnectionId_ ++ ;
77
77
private log_ = logWrapper ( 'p:' + this . id + ':' ) ;
78
78
79
- /** @private {Object} */
80
79
private interruptReasons_ : { [ reason : string ] : boolean } = { } ;
81
- private listens_ : { [ path : string ] : { [ queryId : string ] : ListenSpec } } = { } ;
80
+ private readonly listens : Map < string , Map < string , ListenSpec > > = new Map ( ) ;
82
81
private outstandingPuts_ : OutstandingPut [ ] = [ ] ;
83
82
private outstandingPutCount_ = 0 ;
84
83
private onDisconnectRequestQueue_ : OnDisconnectRequest [ ] = [ ] ;
@@ -88,26 +87,19 @@ export class PersistentConnection extends ServerActions {
88
87
private securityDebugCallback_ : ( ( a : Object ) => void ) | null = null ;
89
88
lastSessionId : string | null = null ;
90
89
91
- /** @private {number|null} */
92
90
private establishConnectionTimer_ : number | null = null ;
93
91
94
- /** @private {boolean} */
95
92
private visible_ : boolean = false ;
96
93
97
94
// Before we get connected, we keep a queue of pending messages to send.
98
95
private requestCBHash_ : { [ k : number ] : ( a : any ) => void } = { } ;
99
96
private requestNumber_ = 0 ;
100
97
101
- /** @private {?{
102
- * sendRequest(Object),
103
- * close()
104
- * }} */
105
98
private realtime_ : {
106
99
sendRequest ( a : Object ) : void ;
107
100
close ( ) : void ;
108
101
} | null = null ;
109
102
110
- /** @private {string|null} */
111
103
private authToken_ : string | null = null ;
112
104
private forceTokenRefresh_ = false ;
113
105
private invalidAuthTokenCount_ = 0 ;
@@ -116,26 +108,17 @@ export class PersistentConnection extends ServerActions {
116
108
private lastConnectionAttemptTime_ : number | null = null ;
117
109
private lastConnectionEstablishedTime_ : number | null = null ;
118
110
119
- /**
120
- * @private
121
- */
122
111
private static nextPersistentConnectionId_ = 0 ;
123
112
124
113
/**
125
114
* Counter for number of connections created. Mainly used for tagging in the logs
126
- * @type {number }
127
- * @private
128
115
*/
129
116
private static nextConnectionId_ = 0 ;
130
117
131
118
/**
132
119
* @implements {ServerActions}
133
- * @param {!RepoInfo } repoInfo_ Data about the namespace we are connecting to
134
- * @param {function(string, *, boolean, ?number) } onDataUpdate_ A callback for new data from the server
135
- * @param onConnectStatus_
136
- * @param onServerInfoUpdate_
137
- * @param authTokenProvider_
138
- * @param authOverride_
120
+ * @param repoInfo_ Data about the namespace we are connecting to
121
+ * @param onDataUpdate_ A callback for new data from the server
139
122
*/
140
123
constructor (
141
124
private repoInfo_ : RepoInfo ,
@@ -166,12 +149,6 @@ export class PersistentConnection extends ServerActions {
166
149
}
167
150
}
168
151
169
- /**
170
- * @param {!string } action
171
- * @param {* } body
172
- * @param {function(*)= } onResponse
173
- * @protected
174
- */
175
152
protected sendRequest (
176
153
action : string ,
177
154
body : any ,
@@ -203,14 +180,16 @@ export class PersistentConnection extends ServerActions {
203
180
const queryId = query . queryIdentifier ( ) ;
204
181
const pathString = query . path . toString ( ) ;
205
182
this . log_ ( 'Listen called for ' + pathString + ' ' + queryId ) ;
206
- this . listens_ [ pathString ] = this . listens_ [ pathString ] || { } ;
183
+ if ( ! this . listens . has ( pathString ) ) {
184
+ this . listens . set ( pathString , new Map ( ) ) ;
185
+ }
207
186
assert (
208
187
query . getQueryParams ( ) . isDefault ( ) ||
209
188
! query . getQueryParams ( ) . loadsAllData ( ) ,
210
189
'listen() called for non-default but complete query'
211
190
) ;
212
191
assert (
213
- ! this . listens_ [ pathString ] [ queryId ] ,
192
+ ! this . listens . get ( pathString ) ! . has ( queryId ) ,
214
193
'listen() called twice for same path/queryId.'
215
194
) ;
216
195
const listenSpec : ListenSpec = {
@@ -219,20 +198,13 @@ export class PersistentConnection extends ServerActions {
219
198
query : query ,
220
199
tag : tag
221
200
} ;
222
- this . listens_ [ pathString ] [ queryId ] = listenSpec ;
201
+ this . listens . get ( pathString ) ! . set ( queryId , listenSpec ) ;
223
202
224
203
if ( this . connected_ ) {
225
204
this . sendListen_ ( listenSpec ) ;
226
205
}
227
206
}
228
207
229
- /**
230
- * @param {!{onComplete(),
231
- * hashFn():!string,
232
- * query: !Query,
233
- * tag: ?number}} listenSpec
234
- * @private
235
- */
236
208
private sendListen_ ( listenSpec : ListenSpec ) {
237
209
const query = listenSpec . query ;
238
210
const pathString = query . path . toString ( ) ;
@@ -258,7 +230,8 @@ export class PersistentConnection extends ServerActions {
258
230
PersistentConnection . warnOnListenWarnings_ ( payload , query ) ;
259
231
260
232
const currentListenSpec =
261
- this . listens_ [ pathString ] && this . listens_ [ pathString ] [ queryId ] ;
233
+ this . listens . get ( pathString ) &&
234
+ this . listens . get ( pathString ) ! . get ( queryId ) ;
262
235
// only trigger actions if the listen hasn't been removed and readded
263
236
if ( currentListenSpec === listenSpec ) {
264
237
this . log_ ( 'listen response' , message ) ;
@@ -274,11 +247,6 @@ export class PersistentConnection extends ServerActions {
274
247
} ) ;
275
248
}
276
249
277
- /**
278
- * @param {* } payload
279
- * @param {!Query } query
280
- * @private
281
- */
282
250
private static warnOnListenWarnings_ ( payload : any , query : Query ) {
283
251
if ( payload && typeof payload === 'object' && contains ( payload , 'w' ) ) {
284
252
const warnings = safeGet ( payload , 'w' ) ;
@@ -319,10 +287,6 @@ export class PersistentConnection extends ServerActions {
319
287
this . reduceReconnectDelayIfAdminCredential_ ( token ) ;
320
288
}
321
289
322
- /**
323
- * @param {!string } credential
324
- * @private
325
- */
326
290
private reduceReconnectDelayIfAdminCredential_ ( credential : string ) {
327
291
// NOTE: This isn't intended to be bulletproof (a malicious developer can always just modify the client).
328
292
// Additionally, we don't bother resetting the max delay back to the default if auth fails / expires.
@@ -576,10 +540,6 @@ export class PersistentConnection extends ServerActions {
576
540
}
577
541
}
578
542
579
- /**
580
- * @param {* } message
581
- * @private
582
- */
583
543
private onDataMessage_ ( message : { [ k : string ] : any } ) {
584
544
if ( 'r' in message ) {
585
545
// this is a response
@@ -663,10 +623,6 @@ export class PersistentConnection extends ServerActions {
663
623
} , Math . floor ( timeout ) ) as any ;
664
624
}
665
625
666
- /**
667
- * @param {boolean } visible
668
- * @private
669
- */
670
626
private onVisible_ ( visible : boolean ) {
671
627
// NOTE: Tabbing away and back to a window will defeat our reconnect backoff, but I think that's fine.
672
628
if (
@@ -819,9 +775,6 @@ export class PersistentConnection extends ServerActions {
819
775
}
820
776
}
821
777
822
- /**
823
- * @param {string } reason
824
- */
825
778
interrupt ( reason : string ) {
826
779
log ( 'Interrupting connection for reason: ' + reason ) ;
827
780
this . interruptReasons_ [ reason ] = true ;
@@ -838,9 +791,6 @@ export class PersistentConnection extends ServerActions {
838
791
}
839
792
}
840
793
841
- /**
842
- * @param {string } reason
843
- */
844
794
resume ( reason : string ) {
845
795
log ( 'Resuming connection for reason: ' + reason ) ;
846
796
delete this . interruptReasons_ [ reason ] ;
@@ -872,11 +822,6 @@ export class PersistentConnection extends ServerActions {
872
822
if ( this . outstandingPutCount_ === 0 ) this . outstandingPuts_ = [ ] ;
873
823
}
874
824
875
- /**
876
- * @param {!string } pathString
877
- * @param {Array.<*>= } query
878
- * @private
879
- */
880
825
private onListenRevoked_ ( pathString : string , query ?: any [ ] ) {
881
826
// Remove the listen and manufacture a "permission_denied" error for the failed listen.
882
827
let queryId ;
@@ -889,20 +834,15 @@ export class PersistentConnection extends ServerActions {
889
834
if ( listen && listen . onComplete ) listen . onComplete ( 'permission_denied' ) ;
890
835
}
891
836
892
- /**
893
- * @param {!string } pathString
894
- * @param {!string } queryId
895
- * @return {{queries:Array.<Query>, onComplete:function(string)} }
896
- * @private
897
- */
898
837
private removeListen_ ( pathString : string , queryId : string ) : ListenSpec {
899
838
const normalizedPathString = new Path ( pathString ) . toString ( ) ; // normalize path.
900
839
let listen ;
901
- if ( this . listens_ [ normalizedPathString ] !== undefined ) {
902
- listen = this . listens_ [ normalizedPathString ] [ queryId ] ;
903
- delete this . listens_ [ normalizedPathString ] [ queryId ] ;
904
- if ( getCount ( this . listens_ [ normalizedPathString ] ) === 0 ) {
905
- delete this . listens_ [ normalizedPathString ] ;
840
+ if ( this . listens . has ( normalizedPathString ) ) {
841
+ const map = this . listens . get ( normalizedPathString ) ! ;
842
+ listen = map . get ( queryId ) ;
843
+ map . delete ( queryId ) ;
844
+ if ( map . size === 0 ) {
845
+ this . listens . delete ( normalizedPathString ) ;
906
846
}
907
847
} else {
908
848
// all listens for this path has already been removed
@@ -948,11 +888,11 @@ export class PersistentConnection extends ServerActions {
948
888
949
889
// Puts depend on having received the corresponding data update from the server before they complete, so we must
950
890
// make sure to send listens before puts.
951
- forEach ( this . listens_ , ( _pathString , queries ) => {
952
- forEach ( queries , ( _key , listenSpec ) => {
891
+ for ( const queries of this . listens . values ( ) ) {
892
+ for ( const listenSpec of queries . values ( ) ) {
953
893
this . sendListen_ ( listenSpec ) ;
954
- } ) ;
955
- } ) ;
894
+ }
895
+ }
956
896
957
897
for ( let i = 0 ; i < this . outstandingPuts_ . length ; i ++ ) {
958
898
if ( this . outstandingPuts_ [ i ] ) this . sendPut_ ( i ) ;
@@ -971,7 +911,6 @@ export class PersistentConnection extends ServerActions {
971
911
972
912
/**
973
913
* Sends client stats for first connection
974
- * @private
975
914
*/
976
915
private sendConnectStats_ ( ) {
977
916
const stats : { [ k : string ] : number } = { } ;
@@ -995,10 +934,6 @@ export class PersistentConnection extends ServerActions {
995
934
this . reportStats ( stats ) ;
996
935
}
997
936
998
- /**
999
- * @return {boolean }
1000
- * @private
1001
- */
1002
937
private shouldReconnect_ ( ) : boolean {
1003
938
const online = OnlineMonitor . getInstance ( ) . currentlyOnline ( ) ;
1004
939
return isEmpty ( this . interruptReasons_ ) && online ;
0 commit comments