Skip to content

Commit cb1a4dd

Browse files
committed
Fix spurious triggers.
1 parent 04f61fc commit cb1a4dd

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

packages-exp/auth-exp/src/platform_browser/auth.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe('core/auth/auth_impl', () => {
7171
apiHost: DefaultConfig.API_HOST,
7272
apiScheme: DefaultConfig.API_SCHEME,
7373
tokenApiHost: DefaultConfig.TOKEN_API_HOST,
74+
clientPlatform: ClientPlatform.BROWSER,
7475
sdkClientVersion: 'v'
7576
});
7677

@@ -137,6 +138,7 @@ describe('core/auth/initializeAuth', () => {
137138
apiScheme: DefaultConfig.API_SCHEME,
138139
tokenApiHost: DefaultConfig.TOKEN_API_HOST,
139140
authDomain,
141+
clientPlatform: ClientPlatform.BROWSER,
140142
sdkClientVersion: _getClientVersion(ClientPlatform.BROWSER)
141143
});
142144

@@ -312,6 +314,7 @@ describe('core/auth/initializeAuth', () => {
312314
apiHost: DefaultConfig.API_HOST,
313315
apiScheme: DefaultConfig.API_SCHEME,
314316
tokenApiHost: DefaultConfig.TOKEN_API_HOST,
317+
clientPlatform: ClientPlatform.BROWSER,
315318
sdkClientVersion: _getClientVersion(ClientPlatform.BROWSER)
316319
});
317320
});

packages-exp/auth-exp/src/platform_browser/persistence/indexed_db.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ describe('platform_browser/persistence/indexed_db', () => {
128128
clock.restore();
129129
});
130130

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+
131136
it('should trigger a listener when the key changes', async () => {
132137
await _putObject(db, key, newValue);
133138

packages-exp/auth-exp/src/platform_browser/persistence/indexed_db.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class IndexedDBLocalPersistence implements InternalPersistence {
373373
}
374374
}
375375
for (const localKey of Object.keys(this.localCache)) {
376-
if (!keysInResult.has(localKey)) {
376+
if (this.localCache[localKey] && !keysInResult.has(localKey)) {
377377
// Deleted
378378
this.notifyListeners(localKey, null);
379379
keys.push(localKey);
@@ -387,8 +387,11 @@ class IndexedDBLocalPersistence implements InternalPersistence {
387387
newValue: PersistenceValue | null
388388
): void {
389389
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+
}
392395
}
393396
}
394397

@@ -414,6 +417,8 @@ class IndexedDBLocalPersistence implements InternalPersistence {
414417
}
415418
this.listeners[key] = this.listeners[key] || new Set();
416419
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.
417422
}
418423

419424
_removeListener(key: string, listener: StorageEventListener): void {

packages-exp/auth-exp/src/platform_browser/persistence/local_storage.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ class BrowserLocalPersistence
9999

100100
const key = event.key;
101101

102-
// Ignore keys that have no listeners.
103-
if (!this.listeners[key]) {
104-
return;
105-
}
106-
107102
// Check the mechanism how this event was detected.
108103
// The first event will dictate the mechanism to be used.
109104
if (poll) {
@@ -166,8 +161,11 @@ class BrowserLocalPersistence
166161

167162
private notifyListeners(key: string, value: string | null): void {
168163
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+
}
171169
}
172170
}
173171

@@ -206,6 +204,8 @@ class BrowserLocalPersistence
206204
}
207205

208206
_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);
209209
if (Object.keys(this.listeners).length === 0) {
210210
// Whether browser can detect storage event when it had already been pushed to the background.
211211
// This may happen in some mobile browsers. A localStorage change in the foreground window

0 commit comments

Comments
 (0)