Skip to content

Commit 4f8cfcf

Browse files
Treeshake Tree (#4582)
1 parent 8ba98cb commit 4f8cfcf

File tree

3 files changed

+219
-221
lines changed

3 files changed

+219
-221
lines changed

packages/database/src/core/Repo.ts

+46-34
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ import {
3838
import {
3939
SyncTree,
4040
syncTreeAckUserWrite,
41+
syncTreeAddEventRegistration,
42+
syncTreeApplyServerMerge,
4143
syncTreeApplyServerOverwrite,
4244
syncTreeApplyTaggedQueryMerge,
4345
syncTreeApplyTaggedQueryOverwrite,
44-
syncTreeApplyServerMerge,
45-
syncTreeGetServerValue,
46-
syncTreeCalcCompleteEventCache,
47-
syncTreeApplyUserOverwrite,
4846
syncTreeApplyUserMerge,
49-
syncTreeAddEventRegistration,
47+
syncTreeApplyUserOverwrite,
48+
syncTreeCalcCompleteEventCache,
49+
syncTreeGetServerValue,
5050
syncTreeRemoveEventRegistration
5151
} from './SyncTree';
5252
import { SnapshotHolder } from './SnapshotHolder';
@@ -92,7 +92,17 @@ import { StatsCollection } from './stats/StatsCollection';
9292
import { Event } from './view/Event';
9393
import { Node } from './snap/Node';
9494
import { Indexable } from './util/misc';
95-
import { Tree } from './util/Tree';
95+
import {
96+
Tree,
97+
treeForEachAncestor,
98+
treeForEachChild,
99+
treeForEachDescendant,
100+
treeGetPath,
101+
treeGetValue,
102+
treeHasChildren,
103+
treeSetValue,
104+
treeSubTree
105+
} from './util/Tree';
96106
import { isValidPriority, validateFirebaseData } from './util/validation';
97107
import { ChildrenNode } from './snap/ChildrenNode';
98108
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex';
@@ -937,11 +947,11 @@ export function repoStartTransaction(
937947

938948
// Mark as run and add to our queue.
939949
transaction.status = TransactionStatus.RUN;
940-
const queueNode = repo.transactionQueueTree_.subTree(path);
941-
const nodeQueue = queueNode.getValue() || [];
950+
const queueNode = treeSubTree(repo.transactionQueueTree_, path);
951+
const nodeQueue = treeGetValue(queueNode) || [];
942952
nodeQueue.push(transaction);
943953

944-
queueNode.setValue(nodeQueue);
954+
treeSetValue(queueNode, nodeQueue);
945955

946956
// Update visibleData and raise events
947957
// Note: We intentionally raise events after updating all of our
@@ -1023,7 +1033,7 @@ function repoSendReadyTransactions(
10231033
repoPruneCompletedTransactionsBelowNode(repo, node);
10241034
}
10251035

1026-
if (node.getValue() !== null) {
1036+
if (treeGetValue(node)) {
10271037
const queue = repoBuildTransactionQueue(repo, node);
10281038
assert(queue.length > 0, 'Sending zero length transaction queue');
10291039

@@ -1033,10 +1043,10 @@ function repoSendReadyTransactions(
10331043

10341044
// If they're all run (and not sent), we can send them. Else, we must wait.
10351045
if (allRun) {
1036-
repoSendTransactionQueue(repo, node.path(), queue);
1046+
repoSendTransactionQueue(repo, treeGetPath(node), queue);
10371047
}
1038-
} else if (node.hasChildren()) {
1039-
node.forEachChild(childNode => {
1048+
} else if (treeHasChildren(node)) {
1049+
treeForEachChild(node, childNode => {
10401050
repoSendReadyTransactions(repo, childNode);
10411051
});
10421052
}
@@ -1117,7 +1127,7 @@ function repoSendTransactionQueue(
11171127
// Now remove the completed transactions.
11181128
repoPruneCompletedTransactionsBelowNode(
11191129
repo,
1120-
repo.transactionQueueTree_.subTree(path)
1130+
treeSubTree(repo.transactionQueueTree_, path)
11211131
);
11221132
// There may be pending transactions that we can now send.
11231133
repoSendReadyTransactions(repo, repo.transactionQueueTree_);
@@ -1171,7 +1181,7 @@ function repoRerunTransactions(repo: Repo, changedPath: Path): Path {
11711181
repo,
11721182
changedPath
11731183
);
1174-
const path = rootMostTransactionNode.path();
1184+
const path = treeGetPath(rootMostTransactionNode);
11751185

11761186
const queue = repoBuildTransactionQueue(repo, rootMostTransactionNode);
11771187
repoRerunTransactionQueue(repo, queue, path);
@@ -1360,8 +1370,8 @@ function repoGetAncestorTransactionNode(
13601370
// find a node with pending transactions.
13611371
let transactionNode = repo.transactionQueueTree_;
13621372
front = pathGetFront(path);
1363-
while (front !== null && transactionNode.getValue() === null) {
1364-
transactionNode = transactionNode.subTree(front);
1373+
while (front !== null && treeGetValue(transactionNode) === undefined) {
1374+
transactionNode = treeSubTree(transactionNode, front);
13651375
path = pathPopFront(path);
13661376
front = pathGetFront(path);
13671377
}
@@ -1389,9 +1399,7 @@ function repoBuildTransactionQueue(
13891399
);
13901400

13911401
// Sort them by the order the transactions were created.
1392-
transactionQueue.sort((a, b) => {
1393-
return a.order - b.order;
1394-
});
1402+
transactionQueue.sort((a, b) => a.order - b.order);
13951403

13961404
return transactionQueue;
13971405
}
@@ -1401,14 +1409,14 @@ function repoAggregateTransactionQueuesForNode(
14011409
node: Tree<Transaction[]>,
14021410
queue: Transaction[]
14031411
): void {
1404-
const nodeQueue = node.getValue();
1405-
if (nodeQueue !== null) {
1412+
const nodeQueue = treeGetValue(node);
1413+
if (nodeQueue) {
14061414
for (let i = 0; i < nodeQueue.length; i++) {
14071415
queue.push(nodeQueue[i]);
14081416
}
14091417
}
14101418

1411-
node.forEachChild(child => {
1419+
treeForEachChild(node, child => {
14121420
repoAggregateTransactionQueuesForNode(repo, child, queue);
14131421
});
14141422
}
@@ -1420,7 +1428,7 @@ function repoPruneCompletedTransactionsBelowNode(
14201428
repo: Repo,
14211429
node: Tree<Transaction[]>
14221430
): void {
1423-
const queue = node.getValue();
1431+
const queue = treeGetValue(node);
14241432
if (queue) {
14251433
let to = 0;
14261434
for (let from = 0; from < queue.length; from++) {
@@ -1430,10 +1438,10 @@ function repoPruneCompletedTransactionsBelowNode(
14301438
}
14311439
}
14321440
queue.length = to;
1433-
node.setValue(queue.length > 0 ? queue : null);
1441+
treeSetValue(node, queue.length > 0 ? queue : undefined);
14341442
}
14351443

1436-
node.forEachChild(childNode => {
1444+
treeForEachChild(node, childNode => {
14371445
repoPruneCompletedTransactionsBelowNode(repo, childNode);
14381446
});
14391447
}
@@ -1446,17 +1454,17 @@ function repoPruneCompletedTransactionsBelowNode(
14461454
* @param path Path for which we want to abort related transactions.
14471455
*/
14481456
function repoAbortTransactions(repo: Repo, path: Path): Path {
1449-
const affectedPath = repoGetAncestorTransactionNode(repo, path).path();
1457+
const affectedPath = treeGetPath(repoGetAncestorTransactionNode(repo, path));
14501458

1451-
const transactionNode = repo.transactionQueueTree_.subTree(path);
1459+
const transactionNode = treeSubTree(repo.transactionQueueTree_, path);
14521460

1453-
transactionNode.forEachAncestor((node: Tree<Transaction[]>) => {
1461+
treeForEachAncestor(transactionNode, (node: Tree<Transaction[]>) => {
14541462
repoAbortTransactionsOnNode(repo, node);
14551463
});
14561464

14571465
repoAbortTransactionsOnNode(repo, transactionNode);
14581466

1459-
transactionNode.forEachDescendant((node: Tree<Transaction[]>) => {
1467+
treeForEachDescendant(transactionNode, (node: Tree<Transaction[]>) => {
14601468
repoAbortTransactionsOnNode(repo, node);
14611469
});
14621470

@@ -1472,8 +1480,8 @@ function repoAbortTransactionsOnNode(
14721480
repo: Repo,
14731481
node: Tree<Transaction[]>
14741482
): void {
1475-
const queue = node.getValue();
1476-
if (queue !== null) {
1483+
const queue = treeGetValue(node);
1484+
if (queue) {
14771485
// Queue up the callbacks and fire them after cleaning up all of our
14781486
// transaction state, since the callback could trigger more transactions
14791487
// or sets.
@@ -1519,14 +1527,18 @@ function repoAbortTransactionsOnNode(
15191527
}
15201528
if (lastSent === -1) {
15211529
// We're not waiting for any sent transactions. We can clear the queue.
1522-
node.setValue(null);
1530+
treeSetValue(node, undefined);
15231531
} else {
15241532
// Remove the transactions we aborted.
15251533
queue.length = lastSent + 1;
15261534
}
15271535

15281536
// Now fire the callbacks.
1529-
eventQueueRaiseEventsForChangedPath(repo.eventQueue_, node.path(), events);
1537+
eventQueueRaiseEventsForChangedPath(
1538+
repo.eventQueue_,
1539+
treeGetPath(node),
1540+
events
1541+
);
15301542
for (let i = 0; i < callbacks.length; i++) {
15311543
exceptionGuard(callbacks[i]);
15321544
}

0 commit comments

Comments
 (0)