Skip to content

Commit 753fed7

Browse files
committed
Updated tests
1 parent 022bf11 commit 753fed7

File tree

3 files changed

+198
-73
lines changed

3 files changed

+198
-73
lines changed

packages/database/src/core/Repo.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import {
6363
syncTreeCalcCompleteEventCache,
6464
syncTreeGetServerValue,
6565
syncTreeRemoveEventRegistration,
66-
syncTreeAddGetRegistration
66+
syncTreeTagForQuery
6767
} from './SyncTree';
6868
import { Indexable } from './util/misc';
6969
import {
@@ -478,17 +478,33 @@ export function repoGetValue(
478478
);
479479
/**
480480
* Below we simulate the actions of an `onlyOnce` `onValue()` event where:
481-
* we add an event registration,
482-
* update data at the path,
483-
* trigger the events,
484-
* and then cleanup the SyncTree
481+
* Add an event registration,
482+
* Update data at the path,
483+
* Raise any events,
484+
* Cleanup the SyncTree
485485
*/
486-
const events = syncTreeAddGetRegistration(
487-
repo,
486+
syncTreeAddEventRegistration(
487+
repo.serverSyncTree_,
488488
query,
489-
node,
490-
eventRegistration
489+
eventRegistration,
490+
true
491491
);
492+
let events: Event[];
493+
if (query._queryParams.loadsAllData()) {
494+
events = syncTreeApplyServerOverwrite(
495+
repo.serverSyncTree_,
496+
query._path,
497+
node
498+
);
499+
} else {
500+
const tag = syncTreeTagForQuery(repo.serverSyncTree_, query);
501+
events = syncTreeApplyTaggedQueryOverwrite(
502+
repo.serverSyncTree_,
503+
query._path,
504+
node,
505+
tag
506+
);
507+
}
492508
/*
493509
* We need to raise events in the scenario where `get()` is called at a parent path, and
494510
* while the `get()` is pending, `onValue` is called at a child location. While get() is waiting
@@ -505,7 +521,6 @@ export function repoGetValue(
505521
events
506522
);
507523
syncTreeRemoveEventRegistration(
508-
// syncTreeAddGetRegistration calls syncTreeAddEventRegistration. We need to clean that up here.
509524
repo.serverSyncTree_,
510525
query,
511526
eventRegistration,

packages/database/src/core/SyncTree.ts

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import { assert } from '@firebase/util';
1919

2020
import { ReferenceConstructor } from '../api/Reference';
21-
import { ValueEventRegistration } from '../api/Reference_impl';
2221

2322
import { AckUserWrite } from './operation/AckUserWrite';
2423
import { ListenComplete } from './operation/ListenComplete';
@@ -30,7 +29,6 @@ import {
3029
Operation
3130
} from './operation/Operation';
3231
import { Overwrite } from './operation/Overwrite';
33-
import { Repo } from './Repo';
3432
import { ChildrenNode } from './snap/ChildrenNode';
3533
import { Node } from './snap/Node';
3634
import {
@@ -355,10 +353,12 @@ export function syncTreeRemoveEventRegistration(
355353
cancelEvents = removedAndEvents.events;
356354

357355
if (!skipListenerDedup) {
358-
// We may have just removed one of many listeners and can short-circuit this whole process
359-
// We may also not have removed a default listener, in which case all of the descendant listeners should already be
360-
// properly set up.
361-
//
356+
/**
357+
* We may have just removed one of many listeners and can short-circuit this whole process
358+
* We may also not have removed a default listener, in which case all of the descendant listeners should already be
359+
* properly set up.
360+
*/
361+
362362
// Since indexed queries can shadow if they don't have other query constraints, check for loadsAllData(), instead of
363363
// queryId === 'default'
364364
const removingDefault =
@@ -387,7 +387,7 @@ export function syncTreeRemoveEventRegistration(
387387
const listener = syncTreeCreateListenerForView_(syncTree, view);
388388
syncTree.listenProvider_.startListening(
389389
syncTreeQueryForListening_(newQuery),
390-
syncTreeTagForQuery_(syncTree, newQuery),
390+
syncTreeTagForQuery(syncTree, newQuery),
391391
listener.hashFn,
392392
listener.onComplete
393393
);
@@ -779,7 +779,7 @@ function syncTreeCreateListenerForView_(
779779
view: View
780780
): { hashFn(): string; onComplete(a: string, b?: unknown): Event[] } {
781781
const query = view.query;
782-
const tag = syncTreeTagForQuery_(syncTree, query);
782+
const tag = syncTreeTagForQuery(syncTree, query);
783783

784784
return {
785785
hashFn: () => {
@@ -811,7 +811,7 @@ function syncTreeCreateListenerForView_(
811811
/**
812812
* Return the tag associated with the given query.
813813
*/
814-
export function syncTreeTagForQuery_(
814+
export function syncTreeTagForQuery(
815815
syncTree: SyncTree,
816816
query: QueryContext
817817
): number | null {
@@ -943,7 +943,7 @@ function syncTreeSetupListener_(
943943
view: View
944944
): Event[] {
945945
const path = query._path;
946-
const tag = syncTreeTagForQuery_(syncTree, query);
946+
const tag = syncTreeTagForQuery(syncTree, query);
947947
const listener = syncTreeCreateListenerForView_(syncTree, view);
948948

949949
const events = syncTree.listenProvider_.startListening(
@@ -992,49 +992,9 @@ function syncTreeSetupListener_(
992992
const queryToStop = queriesToStop[i];
993993
syncTree.listenProvider_.stopListening(
994994
syncTreeQueryForListening_(queryToStop),
995-
syncTreeTagForQuery_(syncTree, queryToStop)
995+
syncTreeTagForQuery(syncTree, queryToStop)
996996
);
997997
}
998998
}
999999
return events;
10001000
}
1001-
1002-
/**
1003-
* Added to support non-listener data queries (Used by `get()`).
1004-
* Simulates `onValue` `onlyOnce` where it adds the event registration,
1005-
* and then applies the changes to the tree.
1006-
* @param repo - repo reference to the overall Repo.
1007-
* @param query - query to be added.
1008-
* @param node - Data needed to be added to the tree
1009-
* @param eventRegistration - a dummy event registration to add to the SyncTree
1010-
*/
1011-
export function syncTreeAddGetRegistration(
1012-
repo: Repo,
1013-
query: QueryContext,
1014-
node: Node,
1015-
eventRegistration: ValueEventRegistration
1016-
) {
1017-
syncTreeAddEventRegistration(
1018-
repo.serverSyncTree_,
1019-
query,
1020-
eventRegistration,
1021-
true
1022-
);
1023-
let events: Event[];
1024-
if (query._queryParams.loadsAllData()) {
1025-
events = syncTreeApplyServerOverwrite(
1026-
repo.serverSyncTree_,
1027-
query._path,
1028-
node
1029-
);
1030-
} else {
1031-
const tag = syncTreeTagForQuery_(repo.serverSyncTree_, query);
1032-
events = syncTreeApplyTaggedQueryOverwrite(
1033-
repo.serverSyncTree_,
1034-
query._path,
1035-
node,
1036-
tag
1037-
);
1038-
}
1039-
return events;
1040-
}

0 commit comments

Comments
 (0)