File tree Expand file tree Collapse file tree 4 files changed +23
-10
lines changed
packages-exp/auth-exp/src/platform_browser Expand file tree Collapse file tree 4 files changed +23
-10
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ describe('core/auth/auth_impl', () => {
71
71
apiHost : DefaultConfig . API_HOST ,
72
72
apiScheme : DefaultConfig . API_SCHEME ,
73
73
tokenApiHost : DefaultConfig . TOKEN_API_HOST ,
74
+ clientPlatform : ClientPlatform . BROWSER ,
74
75
sdkClientVersion : 'v'
75
76
} ) ;
76
77
@@ -137,6 +138,7 @@ describe('core/auth/initializeAuth', () => {
137
138
apiScheme : DefaultConfig . API_SCHEME ,
138
139
tokenApiHost : DefaultConfig . TOKEN_API_HOST ,
139
140
authDomain,
141
+ clientPlatform : ClientPlatform . BROWSER ,
140
142
sdkClientVersion : _getClientVersion ( ClientPlatform . BROWSER )
141
143
} ) ;
142
144
@@ -312,6 +314,7 @@ describe('core/auth/initializeAuth', () => {
312
314
apiHost : DefaultConfig . API_HOST ,
313
315
apiScheme : DefaultConfig . API_SCHEME ,
314
316
tokenApiHost : DefaultConfig . TOKEN_API_HOST ,
317
+ clientPlatform : ClientPlatform . BROWSER ,
315
318
sdkClientVersion : _getClientVersion ( ClientPlatform . BROWSER )
316
319
} ) ;
317
320
} ) ;
Original file line number Diff line number Diff line change @@ -128,6 +128,11 @@ describe('platform_browser/persistence/indexed_db', () => {
128
128
clock . restore ( ) ;
129
129
} ) ;
130
130
131
+ it ( 'should not trigger a listener when there are no changes' , async ( ) => {
132
+ await waitUntilPoll ( clock ) ;
133
+ expect ( callback ) . not . to . have . been . called ;
134
+ } ) ;
135
+
131
136
it ( 'should trigger a listener when the key changes' , async ( ) => {
132
137
await _putObject ( db , key , newValue ) ;
133
138
Original file line number Diff line number Diff line change @@ -373,7 +373,7 @@ class IndexedDBLocalPersistence implements InternalPersistence {
373
373
}
374
374
}
375
375
for ( const localKey of Object . keys ( this . localCache ) ) {
376
- if ( ! keysInResult . has ( localKey ) ) {
376
+ if ( this . localCache [ localKey ] && ! keysInResult . has ( localKey ) ) {
377
377
// Deleted
378
378
this . notifyListeners ( localKey , null ) ;
379
379
keys . push ( localKey ) ;
@@ -387,8 +387,11 @@ class IndexedDBLocalPersistence implements InternalPersistence {
387
387
newValue : PersistenceValue | null
388
388
) : void {
389
389
this . localCache [ key ] = newValue ;
390
- for ( const listener of Array . from ( this . listeners [ key ] ) ) {
391
- listener ( newValue ) ;
390
+ const listeners = this . listeners [ key ] ;
391
+ if ( listeners ) {
392
+ for ( const listener of Array . from ( listeners ) ) {
393
+ listener ( newValue ) ;
394
+ }
392
395
}
393
396
}
394
397
@@ -414,6 +417,8 @@ class IndexedDBLocalPersistence implements InternalPersistence {
414
417
}
415
418
this . listeners [ key ] = this . listeners [ key ] || new Set ( ) ;
416
419
this . listeners [ key ] . add ( listener ) ;
420
+ // Populate the cache to avoid spuriously triggering on first poll.
421
+ void this . _get ( key ) ; // This can happen in the background async and we can return immediately.
417
422
}
418
423
419
424
_removeListener ( key : string , listener : StorageEventListener ) : void {
Original file line number Diff line number Diff line change @@ -99,11 +99,6 @@ class BrowserLocalPersistence
99
99
100
100
const key = event . key ;
101
101
102
- // Ignore keys that have no listeners.
103
- if ( ! this . listeners [ key ] ) {
104
- return ;
105
- }
106
-
107
102
// Check the mechanism how this event was detected.
108
103
// The first event will dictate the mechanism to be used.
109
104
if ( poll ) {
@@ -166,8 +161,11 @@ class BrowserLocalPersistence
166
161
167
162
private notifyListeners ( key : string , value : string | null ) : void {
168
163
this . localCache [ key ] = value ;
169
- for ( const listener of Array . from ( this . listeners [ key ] ) ) {
170
- listener ( value ? JSON . parse ( value ) : value ) ;
164
+ const listeners = this . listeners [ key ] ;
165
+ if ( listeners ) {
166
+ for ( const listener of Array . from ( listeners ) ) {
167
+ listener ( value ? JSON . parse ( value ) : value ) ;
168
+ }
171
169
}
172
170
}
173
171
@@ -206,6 +204,8 @@ class BrowserLocalPersistence
206
204
}
207
205
208
206
_addListener ( key : string , listener : StorageEventListener ) : void {
207
+ // Populate the cache to avoid spuriously triggering on first poll.
208
+ this . localCache [ key ] = this . storage . getItem ( key ) ;
209
209
if ( Object . keys ( this . listeners ) . length === 0 ) {
210
210
// Whether browser can detect storage event when it had already been pushed to the background.
211
211
// This may happen in some mobile browsers. A localStorage change in the foreground window
You can’t perform that action at this time.
0 commit comments