Skip to content

Commit

Permalink
Handle storage events correctly with memstore and other edge cases.
Browse files Browse the repository at this point in the history
Fixes #345.
  • Loading branch information
aboodman committed Mar 30, 2021
1 parent 655926e commit f7af1ee
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/replicache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,16 @@ export class Replicache implements ReadTransaction {
});
if (hasBroadcastChannel) {
this._broadcastChannel = new BroadcastChannel(storageKeyName(this._name));
this._broadcastChannel.onmessage = e => {
this._checkChange(e.data);
};
this._broadcastChannel.onmessage = () => this._onStorage();
} else {
window.addEventListener('storage', this._onStorage);
window.addEventListener('storage', (e: StorageEvent) => {
if (e.key === storageKeyName(this._name)) {
this._onStorage();
}
});
}
this._setRoot(this._getRoot());
await this._root;
window.addEventListener('storage', this._onStorage);
}

/**
Expand Down Expand Up @@ -358,11 +359,12 @@ export class Replicache implements ReadTransaction {

// Callback for when window.onstorage fires which happens when a different tab
// changes the db.
private _onStorage = (e: StorageEvent): void => {
if (e.key === storageKeyName(this._name)) {
this._checkChange(e.newValue as string);
}
};
private async _onStorage() {
// Cannot just use the value from the other tab, because it can be behind us.
// Also, in the case of memstore, it will have a totally different, unrelated
// hash chain.
this._checkChange(await this._getRoot());
}

private async _checkChange(root: string | undefined): Promise<void> {
const currentRoot = await this._root; // instantaneous except maybe first time
Expand Down

0 comments on commit f7af1ee

Please sign in to comment.