Skip to content

Commit 6abb55f

Browse files
Don't look up TargetId in notifyLocalViewChanges (#1065)
1 parent d9e432e commit 6abb55f

File tree

5 files changed

+31
-43
lines changed

5 files changed

+31
-43
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ export class SyncEngine implements RemoteSyncer {
598598
if (viewChange.snapshot) {
599599
newSnaps.push(viewChange.snapshot);
600600
const docChanges = LocalViewChanges.fromSnapshot(
601+
queryView.targetId,
601602
viewChange.snapshot
602603
);
603604
docChangesInAllViews.push(docChanges);
@@ -607,14 +608,11 @@ export class SyncEngine implements RemoteSyncer {
607608
);
608609
});
609610

610-
return Promise.all(queriesProcessed)
611-
.then(() => {
612-
this.viewHandler!(newSnaps);
613-
return this.localStore.notifyLocalViewChanges(docChangesInAllViews);
614-
})
615-
.then(() => {
616-
return this.localStore.collectGarbage();
617-
});
611+
return Promise.all(queriesProcessed).then(() => {
612+
this.viewHandler!(newSnaps);
613+
this.localStore.notifyLocalViewChanges(docChangesInAllViews);
614+
return this.localStore.collectGarbage();
615+
});
618616
}
619617

620618
private assertSubscribed(fnName: string): void {

packages/firestore/src/local/local_store.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -553,29 +553,17 @@ export class LocalStore {
553553
/**
554554
* Notify local store of the changed views to locally pin documents.
555555
*/
556-
notifyLocalViewChanges(viewChanges: LocalViewChanges[]): Promise<void> {
557-
return this.persistence.runTransaction('Notify local view changes', txn => {
558-
const promises = [] as Array<PersistencePromise<void>>;
559-
for (const view of viewChanges) {
560-
promises.push(
561-
this.queryCache
562-
.getQueryData(txn, view.query)
563-
.next((queryData: QueryData | null) => {
564-
assert(
565-
queryData !== null,
566-
'Local view changes contain unallocated query.'
567-
);
568-
const targetId = queryData!.targetId;
569-
this.localViewReferences.addReferences(view.addedKeys, targetId);
570-
this.localViewReferences.removeReferences(
571-
view.removedKeys,
572-
targetId
573-
);
574-
})
575-
);
576-
}
577-
return PersistencePromise.waitFor(promises);
578-
});
556+
notifyLocalViewChanges(viewChanges: LocalViewChanges[]): void {
557+
for (const viewChange of viewChanges) {
558+
this.localViewReferences.addReferences(
559+
viewChange.addedKeys,
560+
viewChange.targetId
561+
);
562+
this.localViewReferences.removeReferences(
563+
viewChange.removedKeys,
564+
viewChange.targetId
565+
);
566+
}
579567
}
580568

581569
/**

packages/firestore/src/local/local_view_changes.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Query } from '../core/query';
1817
import { ChangeType, ViewSnapshot } from '../core/view_snapshot';
1918
import { documentKeySet, DocumentKeySet } from '../model/collections';
19+
import { TargetId } from '../core/types';
2020

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

33-
static fromSnapshot(viewSnapshot: ViewSnapshot): LocalViewChanges {
33+
static fromSnapshot(
34+
targetId: TargetId,
35+
viewSnapshot: ViewSnapshot
36+
): LocalViewChanges {
3437
let addedKeys = documentKeySet();
3538
let removedKeys = documentKeySet();
3639

@@ -47,6 +50,6 @@ export class LocalViewChanges {
4750
}
4851
}
4952

50-
return new LocalViewChanges(viewSnapshot.query, addedKeys, removedKeys);
53+
return new LocalViewChanges(targetId, addedKeys, removedKeys);
5154
}
5255
}

packages/firestore/test/unit/local/local_store.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ class LocalStoreTester {
115115
}
116116

117117
afterViewChanges(viewChanges: LocalViewChanges): LocalStoreTester {
118-
this.promiseChain = this.promiseChain.then(() => {
119-
return this.localStore.notifyLocalViewChanges([viewChanges]);
120-
});
118+
this.promiseChain = this.promiseChain.then(() =>
119+
this.localStore.notifyLocalViewChanges([viewChanges])
120+
);
121121
return this;
122122
}
123123

@@ -786,14 +786,14 @@ function genericLocalStoreTests(
786786
.afterGC()
787787
.toContain(doc('foo/bar', 1, { foo: 'bar' }))
788788
.toContain(doc('foo/baz', 0, { foo: 'baz' }, { hasLocalMutations: true }))
789-
.after(localViewChanges(query, { added: ['foo/bar', 'foo/baz'] }))
789+
.after(localViewChanges(2, { added: ['foo/bar', 'foo/baz'] }))
790790
.after(docUpdateRemoteEvent(doc('foo/bar', 1, { foo: 'bar' }), [], [2]))
791791
.after(docUpdateRemoteEvent(doc('foo/baz', 2, { foo: 'baz' }), [2]))
792792
.afterAcknowledgingMutation({ documentVersion: 2 })
793793
.afterGC()
794794
.toContain(doc('foo/bar', 1, { foo: 'bar' }))
795795
.toContain(doc('foo/baz', 2, { foo: 'baz' }))
796-
.after(localViewChanges(query, { removed: ['foo/bar', 'foo/baz'] }))
796+
.after(localViewChanges(2, { removed: ['foo/bar', 'foo/baz'] }))
797797
.afterReleasingQuery(query)
798798
.afterGC()
799799
.toNotContain('foo/bar')

packages/firestore/test/util/helpers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
Direction,
3131
Filter,
3232
OrderBy,
33-
Query,
3433
RelationOp
3534
} from '../../src/core/query';
3635
import { SnapshotVersion } from '../../src/core/snapshot_version';
@@ -371,7 +370,7 @@ export function limboChanges(changes: {
371370
}
372371

373372
export function localViewChanges(
374-
query: Query,
373+
targetId: TargetId,
375374
changes: { added?: string[]; removed?: string[] }
376375
): LocalViewChanges {
377376
if (!changes.added) changes.added = [];
@@ -386,7 +385,7 @@ export function localViewChanges(
386385
keyStr => (removedKeys = removedKeys.add(key(keyStr)))
387386
);
388387

389-
return new LocalViewChanges(query, addedKeys, removedKeys);
388+
return new LocalViewChanges(targetId, addedKeys, removedKeys);
390389
}
391390

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

0 commit comments

Comments
 (0)