Skip to content

Commit 76dfbf6

Browse files
committed
Removed unnecessary changes
1 parent 57966f2 commit 76dfbf6

File tree

2 files changed

+28
-97
lines changed

2 files changed

+28
-97
lines changed

packages/database-compat/test/query.test.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ import {
3030
} from '../../database/test/helpers/EventAccumulator';
3131
import { DataSnapshot, Query, Reference } from '../src/api/Reference';
3232

33-
import {
34-
createTestApp,
35-
getFreshRepo,
36-
getPath,
37-
getRandomNode,
38-
pause
39-
} from './helpers/util';
33+
import { getFreshRepo, getPath, getRandomNode, pause } from './helpers/util';
4034

4135
use(chaiAsPromised);
4236

@@ -4670,39 +4664,6 @@ describe('Query Tests', () => {
46704664
});
46714665
});
46724666
});
4673-
it.only('can properly handle unknown deep merges', async () => {
4674-
const app = createTestApp();
4675-
const root = app.database().ref().child('testing');
4676-
// TODO(mtewani): This repro requires an index to be created.
4677-
await root.remove();
4678-
4679-
const query = root.orderByChild('testIndex').limitToFirst(2);
4680-
4681-
await root
4682-
.child('i|1')
4683-
.set({ testIndex: 3, timestamp: Date.now(), action: 'test' });
4684-
await root
4685-
.child('i|2')
4686-
.set({ testIndex: 1, timestamp: Date.now(), action: 'test' });
4687-
await root
4688-
.child('i|3')
4689-
.set({ testIndex: 2, timestamp: Date.now(), action: 'test' });
4690-
const childAdded = query.on('child_added', snap => {
4691-
const value = snap.val();
4692-
expect(value).to.haveOwnProperty('timestamp');
4693-
expect(value).to.haveOwnProperty('action');
4694-
expect(value).to.haveOwnProperty('testIndex');
4695-
});
4696-
const onValue = root.child('i|1').on('value', snap => {
4697-
console.log('onValue', snap.val());
4698-
//no-op
4699-
});
4700-
await root.child('i|1').update({ timestamp: `${Date.now()}|1` });
4701-
console.log('wrote value');
4702-
await new Promise(resolve => setTimeout(resolve, 4000));
4703-
root.child('i|1').off('value', onValue);
4704-
query.off('child_added', childAdded);
4705-
});
47064667

47074668
it('Can JSON serialize refs', () => {
47084669
const ref = getRandomNode() as Reference;

packages/database/src/core/Repo.ts

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import {
2424
stringify
2525
} from '@firebase/util';
2626

27-
import { ValueEventRegistration } from '../api/Reference_impl';
28-
2927
import { AppCheckTokenProvider } from './AppCheckTokenProvider';
3028
import { AuthTokenProvider } from './AuthTokenProvider';
3129
import { PersistentConnection } from './PersistentConnection';
@@ -63,7 +61,7 @@ import {
6361
syncTreeCalcCompleteEventCache,
6462
syncTreeGetServerValue,
6563
syncTreeRemoveEventRegistration,
66-
syncTreeTagForQuery
64+
syncTreeRegisterQuery
6765
} from './SyncTree';
6866
import { Indexable } from './util/misc';
6967
import {
@@ -445,6 +443,7 @@ function repoUpdateInfo(repo: Repo, pathString: string, value: unknown): void {
445443
function repoGetNextWriteId(repo: Repo): number {
446444
return repo.nextWriteId_++;
447445
}
446+
448447
/**
449448
* The purpose of `getValue` is to return the latest known value
450449
* satisfying `query`.
@@ -453,18 +452,14 @@ function repoGetNextWriteId(repo: Repo): number {
453452
* belonging to active listeners. If they are found, such values
454453
* are considered to be the most up-to-date.
455454
*
456-
* If the client is not connected, this method will wait until the
457-
* repo has established a connection and then request the value for `query`.
458-
* If the client is not able to retrieve the query result for another reason,
459-
* it reports an error.
455+
* If the client is not connected, this method will try to
456+
* establish a connection and request the value for `query`. If
457+
* the client is not able to retrieve the query result, it reports
458+
* an error.
460459
*
461460
* @param query - The query to surface a value for.
462461
*/
463-
export function repoGetValue(
464-
repo: Repo,
465-
query: QueryContext,
466-
eventRegistration: ValueEventRegistration
467-
): Promise<Node> {
462+
export function repoGetValue(repo: Repo, query: QueryContext): Promise<Node> {
468463
// Only active queries are cached. There is no persisted cache.
469464
const cached = syncTreeGetServerValue(repo.serverSyncTree_, query);
470465
if (cached != null) {
@@ -475,57 +470,32 @@ export function repoGetValue(
475470
const node = nodeFromJSON(payload).withIndex(
476471
query._queryParams.getIndex()
477472
);
478-
/**
479-
* Below we simulate the actions of an `onlyOnce` `onValue()` event where:
480-
* Add an event registration,
481-
* Update data at the path,
482-
* Raise any events,
483-
* Cleanup the SyncTree
484-
*/
485-
syncTreeAddEventRegistration(
486-
repo.serverSyncTree_,
487-
query,
488-
eventRegistration,
489-
true
490-
);
491-
let events: Event[];
473+
// if this is a filtered query, then overwrite at path
492474
if (query._queryParams.loadsAllData()) {
493-
events = syncTreeApplyServerOverwrite(
494-
repo.serverSyncTree_,
495-
query._path,
496-
node
497-
);
475+
syncTreeApplyServerOverwrite(repo.serverSyncTree_, query._path, node);
498476
} else {
499-
const tag = syncTreeTagForQuery(repo.serverSyncTree_, query);
500-
events = syncTreeApplyTaggedQueryOverwrite(
477+
// Simulate `syncTreeAddEventRegistration` without events/listener setup.
478+
// We do this (along with the syncTreeRemoveEventRegistration` below) so that
479+
// `repoGetValue` results have the same cache effects as initial listener(s)
480+
// updates.
481+
const tag = syncTreeRegisterQuery(repo.serverSyncTree_, query);
482+
syncTreeApplyTaggedQueryOverwrite(
501483
repo.serverSyncTree_,
502484
query._path,
503485
node,
504486
tag
505487
);
488+
// Call `syncTreeRemoveEventRegistration` with a null event registration, since there is none.
489+
// Note: The below code essentially unregisters the query and cleans up any views/syncpoints temporarily created above.
506490
}
507-
/*
508-
* We need to raise events in the scenario where `get()` is called at a parent path, and
509-
* while the `get()` is pending, `onValue` is called at a child location. While get() is waiting
510-
* for the data, `onValue` will register a new event. Then, get() will come back, and update the syncTree
511-
* and its corresponding serverCache, including the child location where `onValue` is called. Then,
512-
* `onValue` will receive the event from the server, but look at the syncTree and see that the data received
513-
* from the server is already at the SyncPoint, and so the `onValue` callback will never get fired.
514-
* Calling `eventQueueRaiseEventsForChangedPath()` is the correct way to propagate the events and
515-
* ensure the corresponding child events will get fired.
516-
*/
517-
eventQueueRaiseEventsForChangedPath(
518-
repo.eventQueue_,
519-
query._path,
520-
events
521-
);
522-
syncTreeRemoveEventRegistration(
491+
const cancels = syncTreeRemoveEventRegistration(
523492
repo.serverSyncTree_,
524493
query,
525-
eventRegistration,
526-
null,
527-
true
494+
null
528495
);
496+
if (cancels.length > 0) {
497+
repoLog(repo, 'unexpected cancel events in repoGetValue');
498+
}
529499
return node;
530500
},
531501
err => {
@@ -982,7 +952,7 @@ export function repoStartTransaction(
982952
// Mark as run and add to our queue.
983953
transaction.status = TransactionStatus.RUN;
984954
const queueNode = treeSubTree(repo.transactionQueueTree_, path);
985-
const nodeQueue = treeGetValue(queueNode) ?? [];
955+
const nodeQueue = treeGetValue(queueNode) || [];
986956
nodeQueue.push(transaction);
987957

988958
treeSetValue(queueNode, nodeQueue);
@@ -1067,7 +1037,7 @@ function repoSendReadyTransactions(
10671037
repoPruneCompletedTransactionsBelowNode(repo, node);
10681038
}
10691039

1070-
if (treeGetValue(node) !== undefined) {
1040+
if (treeGetValue(node)) {
10711041
const queue = repoBuildTransactionQueue(repo, node);
10721042
assert(queue.length > 0, 'Sending zero length transaction queue');
10731043

@@ -1443,7 +1413,7 @@ function repoAggregateTransactionQueuesForNode(
14431413
queue: Transaction[]
14441414
): void {
14451415
const nodeQueue = treeGetValue(node);
1446-
if (nodeQueue !== undefined) {
1416+
if (nodeQueue) {
14471417
for (let i = 0; i < nodeQueue.length; i++) {
14481418
queue.push(nodeQueue[i]);
14491419
}
@@ -1462,7 +1432,7 @@ function repoPruneCompletedTransactionsBelowNode(
14621432
node: Tree<Transaction[]>
14631433
): void {
14641434
const queue = treeGetValue(node);
1465-
if (queue !== undefined) {
1435+
if (queue) {
14661436
let to = 0;
14671437
for (let from = 0; from < queue.length; from++) {
14681438
if (queue[from].status !== TransactionStatus.COMPLETED) {
@@ -1514,7 +1484,7 @@ function repoAbortTransactionsOnNode(
15141484
node: Tree<Transaction[]>
15151485
): void {
15161486
const queue = treeGetValue(node);
1517-
if (queue !== undefined) {
1487+
if (queue) {
15181488
// Queue up the callbacks and fire them after cleaning up all of our
15191489
// transaction state, since the callback could trigger more transactions
15201490
// or sets.

0 commit comments

Comments
 (0)