Skip to content

Don't look up TargetId in notifyLocalViewChanges #1065

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 5 commits into from
Jul 30, 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
14 changes: 6 additions & 8 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ export class SyncEngine implements RemoteSyncer {
if (viewChange.snapshot) {
newSnaps.push(viewChange.snapshot);
const docChanges = LocalViewChanges.fromSnapshot(
queryView.targetId,
viewChange.snapshot
);
docChangesInAllViews.push(docChanges);
Expand All @@ -607,14 +608,11 @@ export class SyncEngine implements RemoteSyncer {
);
});

return Promise.all(queriesProcessed)
.then(() => {
this.viewHandler!(newSnaps);
return this.localStore.notifyLocalViewChanges(docChangesInAllViews);
})
.then(() => {
return this.localStore.collectGarbage();
});
return Promise.all(queriesProcessed).then(() => {
this.viewHandler!(newSnaps);
this.localStore.notifyLocalViewChanges(docChangesInAllViews);
return this.localStore.collectGarbage();
});
}

private assertSubscribed(fnName: string): void {
Expand Down
34 changes: 11 additions & 23 deletions packages/firestore/src/local/local_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,29 +553,17 @@ export class LocalStore {
/**
* Notify local store of the changed views to locally pin documents.
*/
notifyLocalViewChanges(viewChanges: LocalViewChanges[]): Promise<void> {
return this.persistence.runTransaction('Notify local view changes', txn => {
const promises = [] as Array<PersistencePromise<void>>;
for (const view of viewChanges) {
promises.push(
this.queryCache
.getQueryData(txn, view.query)
.next((queryData: QueryData | null) => {
assert(
queryData !== null,
'Local view changes contain unallocated query.'
);
const targetId = queryData!.targetId;
this.localViewReferences.addReferences(view.addedKeys, targetId);
this.localViewReferences.removeReferences(
view.removedKeys,
targetId
);
})
);
}
return PersistencePromise.waitFor(promises);
});
notifyLocalViewChanges(viewChanges: LocalViewChanges[]): void {
for (const viewChange of viewChanges) {
this.localViewReferences.addReferences(
viewChange.addedKeys,
viewChange.targetId
);
this.localViewReferences.removeReferences(
viewChange.removedKeys,
viewChange.targetId
);
}
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/firestore/src/local/local_view_changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import { Query } from '../core/query';
import { ChangeType, ViewSnapshot } from '../core/view_snapshot';
import { documentKeySet, DocumentKeySet } from '../model/collections';
import { TargetId } from '../core/types';

/**
* A set of changes to what documents are currently in view and out of view for
Expand All @@ -25,12 +25,15 @@ import { documentKeySet, DocumentKeySet } from '../model/collections';
*/
export class LocalViewChanges {
constructor(
readonly query: Query,
readonly targetId: TargetId,
readonly addedKeys: DocumentKeySet,
readonly removedKeys: DocumentKeySet
) {}

static fromSnapshot(viewSnapshot: ViewSnapshot): LocalViewChanges {
static fromSnapshot(
targetId: TargetId,
viewSnapshot: ViewSnapshot
): LocalViewChanges {
let addedKeys = documentKeySet();
let removedKeys = documentKeySet();

Expand All @@ -47,6 +50,6 @@ export class LocalViewChanges {
}
}

return new LocalViewChanges(viewSnapshot.query, addedKeys, removedKeys);
return new LocalViewChanges(targetId, addedKeys, removedKeys);
}
}
10 changes: 5 additions & 5 deletions packages/firestore/test/unit/local/local_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ class LocalStoreTester {
}

afterViewChanges(viewChanges: LocalViewChanges): LocalStoreTester {
this.promiseChain = this.promiseChain.then(() => {
return this.localStore.notifyLocalViewChanges([viewChanges]);
});
this.promiseChain = this.promiseChain.then(() =>
this.localStore.notifyLocalViewChanges([viewChanges])
);
return this;
}

Expand Down Expand Up @@ -786,14 +786,14 @@ function genericLocalStoreTests(
.afterGC()
.toContain(doc('foo/bar', 1, { foo: 'bar' }))
.toContain(doc('foo/baz', 0, { foo: 'baz' }, { hasLocalMutations: true }))
.after(localViewChanges(query, { added: ['foo/bar', 'foo/baz'] }))
.after(localViewChanges(2, { added: ['foo/bar', 'foo/baz'] }))
.after(docUpdateRemoteEvent(doc('foo/bar', 1, { foo: 'bar' }), [], [2]))
.after(docUpdateRemoteEvent(doc('foo/baz', 2, { foo: 'baz' }), [2]))
.afterAcknowledgingMutation({ documentVersion: 2 })
.afterGC()
.toContain(doc('foo/bar', 1, { foo: 'bar' }))
.toContain(doc('foo/baz', 2, { foo: 'baz' }))
.after(localViewChanges(query, { removed: ['foo/bar', 'foo/baz'] }))
.after(localViewChanges(2, { removed: ['foo/bar', 'foo/baz'] }))
.afterReleasingQuery(query)
.afterGC()
.toNotContain('foo/bar')
Expand Down
5 changes: 2 additions & 3 deletions packages/firestore/test/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
Direction,
Filter,
OrderBy,
Query,
RelationOp
} from '../../src/core/query';
import { SnapshotVersion } from '../../src/core/snapshot_version';
Expand Down Expand Up @@ -371,7 +370,7 @@ export function limboChanges(changes: {
}

export function localViewChanges(
query: Query,
targetId: TargetId,
changes: { added?: string[]; removed?: string[] }
): LocalViewChanges {
if (!changes.added) changes.added = [];
Expand All @@ -386,7 +385,7 @@ export function localViewChanges(
keyStr => (removedKeys = removedKeys.add(key(keyStr)))
);

return new LocalViewChanges(query, addedKeys, removedKeys);
return new LocalViewChanges(targetId, addedKeys, removedKeys);
}

/** Creates a resume token to match the given snapshot version. */
Expand Down