Skip to content

Commit

Permalink
fix: crash on storage synchronization attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillzyusko committed Mar 6, 2024
1 parent d6f8171 commit c461b99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/storage/InstanceSync/index.web.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable no-invalid-this */
/**
* The InstancesSync object provides data-changed events like the ones that exist
* when using LocalStorage APIs in the browser. These events are great because multiple tabs can listen for when
* data changes and then stay up-to-date with everything happening in Onyx.
*/
import type {KeyList, Key, OnStorageKeyChanged, Value} from '../providers/types';
import NoopProvider from '../providers/NoopProvider';
import type {KeyList, Key, OnStorageKeyChanged} from '../providers/types';
import StorageProvider from '../providers/types';

Check failure on line 8 in lib/storage/InstanceSync/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

const SYNC_ONYX = 'SYNC_ONYX';

Expand All @@ -23,11 +24,15 @@ function raiseStorageSyncManyKeysEvent(onyxKeys: KeyList) {
});
}

let storage = NoopProvider;

const InstanceSync = {
/**
* @param {Function} onStorageKeyChanged Storage synchronization mechanism keeping all opened tabs in sync
*/
init: (onStorageKeyChanged: OnStorageKeyChanged) => {
init: (onStorageKeyChanged: OnStorageKeyChanged, store: StorageProvider) => {
storage = store;

// This listener will only be triggered by events coming from other tabs
global.addEventListener('storage', (event) => {
// Ignore events that don't originate from the SYNC_ONYX logic
Expand All @@ -36,8 +41,8 @@ const InstanceSync = {
}

const onyxKey = event.newValue;
// @ts-expect-error `this` will be substituted later in actual function call
this.getItem(onyxKey).then((value: Value) => onStorageKeyChanged(onyxKey, value));

storage.getItem(onyxKey).then((value) => onStorageKeyChanged(onyxKey, value));
});
},
setItem: raiseStorageSyncEvent,
Expand All @@ -48,8 +53,8 @@ const InstanceSync = {
let allKeys: KeyList;

// The keys must be retrieved before storage is cleared or else the list of keys would be empty
// @ts-expect-error `this` will be substituted later in actual function call
return this.getAllKeys()
return storage
.getAllKeys()
.then((keys: KeyList) => {
allKeys = keys;
})
Expand Down
2 changes: 1 addition & 1 deletion lib/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const Storage: Storage = {
if (InstanceSync === null) return;

shouldKeepInstancesSync = true;
InstanceSync.init(onStorageKeyChanged);
InstanceSync.init(onStorageKeyChanged, this);
},
};

Expand Down

0 comments on commit c461b99

Please sign in to comment.