Skip to content

Always update LocalStorage state #1282

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
Oct 3, 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
12 changes: 9 additions & 3 deletions packages/firestore/src/local/shared_client_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export interface SharedClientState {
interface MutationMetadataSchema {
state: MutationBatchState;
error?: { code: string; message: string }; // Only set when state === 'rejected'
updateTimeMs: number;
}

/**
Expand Down Expand Up @@ -260,7 +261,8 @@ export class MutationMetadata {

toWebStorageJSON(): string {
const batchMetadata: MutationMetadataSchema = {
state: this.state
state: this.state,
updateTimeMs: Date.now() // Modify the existing value to trigger update.
};

if (this.error) {
Expand All @@ -281,6 +283,7 @@ export class MutationMetadata {
interface QueryTargetStateSchema {
state: QueryTargetState;
error?: { code: string; message: string }; // Only set when state === 'rejected'
updateTimeMs: number;
}

/**
Expand Down Expand Up @@ -348,7 +351,8 @@ export class QueryTargetMetadata {

toWebStorageJSON(): string {
const targetState: QueryTargetStateSchema = {
state: this.state
state: this.state,
updateTimeMs: Date.now() // Modify the existing value to trigger update.
};

if (this.error) {
Expand All @@ -369,6 +373,7 @@ export class QueryTargetMetadata {
*/
interface ClientStateSchema {
activeTargetIds: number[];
updateTimeMs: number;
}

/**
Expand Down Expand Up @@ -503,7 +508,8 @@ export class LocalClientState implements ClientState {
*/
toWebStorageJSON(): string {
const data: ClientStateSchema = {
activeTargetIds: this.activeTargetIds.toArray()
activeTargetIds: this.activeTargetIds.toArray(),
updateTimeMs: Date.now() // Modify the existing value to trigger update.
};
return JSON.stringify(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ describe('WebStorageSharedClientState', () => {
)!
);

expect(Object.keys(actual)).to.have.members(['activeTargetIds']);
expect(Object.keys(actual)).to.have.members([
'activeTargetIds',
'updateTimeMs'
]);
expect(actual.activeTargetIds)
.to.be.an('array')
.and.have.members(activeTargetIds);
Expand All @@ -250,7 +253,7 @@ describe('WebStorageSharedClientState', () => {

expect(actual.state).to.equal(mutationBatchState);

const expectedMembers = ['state'];
const expectedMembers = ['state', 'updateTimeMs'];

if (mutationBatchState === 'rejected') {
expectedMembers.push('error');
Expand Down Expand Up @@ -304,7 +307,7 @@ describe('WebStorageSharedClientState', () => {
const actual = JSON.parse(webStorage.getItem(targetKey(targetId))!);
expect(actual.state).to.equal(queryTargetState);

const expectedMembers = ['state'];
const expectedMembers = ['state', 'updateTimeMs'];
if (queryTargetState === 'rejected') {
expectedMembers.push('error');
expect(actual.error.code).to.equal(err!.code);
Expand Down