Skip to content

Attempt to delete client metadata row #1131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/firestore/src/local/shared_client_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@ export class WebStorageSharedClientState implements SharedClientState {
}

this.earlyEvents = [];

// Register a window unload hook to remove the client metadata entry from
// LocalStorage even if `shutdown()` was not called.
this.platform.window.addEventListener('unload', () => this.shutdown());

this.started = true;
}

Expand Down Expand Up @@ -752,13 +757,11 @@ export class WebStorageSharedClientState implements SharedClientState {
}

shutdown(): void {
assert(
this.started,
'WebStorageSharedClientState.shutdown() called when not started'
);
this.platform.window.removeEventListener('storage', this.storageListener);
this.removeItem(this.localClientStorageKey);
this.started = false;
if (this.started) {
this.platform.window.removeEventListener('storage', this.storageListener);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the 'unload' hook too? (FWIW that might mean we don't need the this.started check, though I don't know if that's guaranteed or not... so probably fine to leave it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted not to. It provides very little gain and adds more state keeping (I would have to keep a reference to the original function pointer for it to work consistently in all browsers).

this.removeItem(this.localClientStorageKey);
this.started = false;
}
}

private getItem(key: string): string | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ describe('WebStorageSharedClientState', () => {
// client. If we directly relied on LocalStorage listeners, we would not
// receive events for local writes.
window.addEventListener = (type, callback) => {
expect(type).to.equal('storage');
localStorageCallbacks.push(callback);
if (type === 'storage') {
localStorageCallbacks.push(callback);
}
};
window.removeEventListener = () => {};

Expand Down