Skip to content

Commit 13686cb

Browse files
authored
Tree-shake View (#4612)
1 parent ec95df3 commit 13686cb

File tree

3 files changed

+191
-164
lines changed

3 files changed

+191
-164
lines changed

packages/database/src/core/SyncPoint.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ import { CacheNode } from './view/CacheNode';
1919
import { ChildrenNode } from './snap/ChildrenNode';
2020
import { assert } from '@firebase/util';
2121
import { ViewCache } from './view/ViewCache';
22-
import { View } from './view/View';
22+
import {
23+
View,
24+
viewAddEventRegistration,
25+
viewApplyOperation,
26+
viewGetCompleteServerCache,
27+
viewGetInitialEvents,
28+
viewIsEmpty,
29+
viewRemoveEventRegistration
30+
} from './view/View';
2331
import { Operation } from './operation/Operation';
2432
import { WriteTreeRef } from './WriteTree';
2533
import { Query } from '../api/Query';
@@ -80,13 +88,18 @@ export function syncPointApplyOperation(
8088
if (queryId !== null) {
8189
const view = syncPoint.views.get(queryId);
8290
assert(view != null, 'SyncTree gave us an op for an invalid query.');
83-
return view.applyOperation(operation, writesCache, optCompleteServerCache);
91+
return viewApplyOperation(
92+
view,
93+
operation,
94+
writesCache,
95+
optCompleteServerCache
96+
);
8497
} else {
8598
let events: Event[] = [];
8699

87100
for (const view of syncPoint.views.values()) {
88101
events = events.concat(
89-
view.applyOperation(operation, writesCache, optCompleteServerCache)
102+
viewApplyOperation(view, operation, writesCache, optCompleteServerCache)
90103
);
91104
}
92105

@@ -165,8 +178,8 @@ export function syncPointAddEventRegistration(
165178
syncPoint.views.set(query.queryIdentifier(), view);
166179
}
167180
// This is guaranteed to exist now, we just created anything that was missing
168-
view.addEventRegistration(eventRegistration);
169-
return view.getInitialEvents(eventRegistration);
181+
viewAddEventRegistration(view, eventRegistration);
182+
return viewGetInitialEvents(view, eventRegistration);
170183
}
171184

172185
/**
@@ -193,14 +206,14 @@ export function syncPointRemoveEventRegistration(
193206
// When you do ref.off(...), we search all views for the registration to remove.
194207
for (const [viewQueryId, view] of syncPoint.views.entries()) {
195208
cancelEvents = cancelEvents.concat(
196-
view.removeEventRegistration(eventRegistration, cancelError)
209+
viewRemoveEventRegistration(view, eventRegistration, cancelError)
197210
);
198-
if (view.isEmpty()) {
211+
if (viewIsEmpty(view)) {
199212
syncPoint.views.delete(viewQueryId);
200213

201214
// We'll deal with complete views later.
202-
if (!view.getQuery().getQueryParams().loadsAllData()) {
203-
removed.push(view.getQuery());
215+
if (!view.query.getQueryParams().loadsAllData()) {
216+
removed.push(view.query);
204217
}
205218
}
206219
}
@@ -209,14 +222,14 @@ export function syncPointRemoveEventRegistration(
209222
const view = syncPoint.views.get(queryId);
210223
if (view) {
211224
cancelEvents = cancelEvents.concat(
212-
view.removeEventRegistration(eventRegistration, cancelError)
225+
viewRemoveEventRegistration(view, eventRegistration, cancelError)
213226
);
214-
if (view.isEmpty()) {
227+
if (viewIsEmpty(view)) {
215228
syncPoint.views.delete(queryId);
216229

217230
// We'll deal with complete views later.
218-
if (!view.getQuery().getQueryParams().loadsAllData()) {
219-
removed.push(view.getQuery());
231+
if (!view.query.getQueryParams().loadsAllData()) {
232+
removed.push(view.query);
220233
}
221234
}
222235
}
@@ -235,7 +248,7 @@ export function syncPointRemoveEventRegistration(
235248
export function syncPointGetQueryViews(syncPoint: SyncPoint): View[] {
236249
const result = [];
237250
for (const view of syncPoint.views.values()) {
238-
if (!view.getQuery().getQueryParams().loadsAllData()) {
251+
if (!view.query.getQueryParams().loadsAllData()) {
239252
result.push(view);
240253
}
241254
}
@@ -252,7 +265,7 @@ export function syncPointGetCompleteServerCache(
252265
): Node | null {
253266
let serverCache: Node | null = null;
254267
for (const view of syncPoint.views.values()) {
255-
serverCache = serverCache || view.getCompleteServerCache(path);
268+
serverCache = serverCache || viewGetCompleteServerCache(view, path);
256269
}
257270
return serverCache;
258271
}
@@ -283,7 +296,7 @@ export function syncPointHasCompleteView(syncPoint: SyncPoint): boolean {
283296

284297
export function syncPointGetCompleteView(syncPoint: SyncPoint): View | null {
285298
for (const view of syncPoint.views.values()) {
286-
if (view.getQuery().getQueryParams().loadsAllData()) {
299+
if (view.query.getQueryParams().loadsAllData()) {
287300
return view;
288301
}
289302
}

packages/database/src/core/SyncTree.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { Query } from '../api/Query';
5555
import { Node } from './snap/Node';
5656
import { Event } from './view/Event';
5757
import { EventRegistration } from './view/EventRegistration';
58-
import { View } from './view/View';
58+
import { View, viewGetCompleteNode, viewGetServerCache } from './view/View';
5959
import { CacheNode } from './view/CacheNode';
6060

6161
/**
@@ -350,7 +350,7 @@ export function syncTreeRemoveEventRegistration(
350350
// Ok, we've collected all the listens we need. Set them up.
351351
for (let i = 0; i < newViews.length; ++i) {
352352
const view = newViews[i],
353-
newQuery = view.getQuery();
353+
newQuery = view.query;
354354
const listener = syncTreeCreateListenerForView_(syncTree, view);
355355
syncTree.listenProvider_.startListening(
356356
syncTreeQueryForListening_(newQuery),
@@ -612,7 +612,7 @@ export function syncTreeGetServerValue(
612612
serverCacheComplete ? serverCacheNode.getNode() : ChildrenNode.EMPTY_NODE,
613613
serverCacheComplete
614614
);
615-
return view.getCompleteNode();
615+
return viewGetCompleteNode(view);
616616
}
617617

618618
/**
@@ -741,12 +741,12 @@ function syncTreeCreateListenerForView_(
741741
syncTree: SyncTree,
742742
view: View
743743
): { hashFn(): string; onComplete(a: string, b?: unknown): Event[] } {
744-
const query = view.getQuery();
744+
const query = view.query;
745745
const tag = syncTreeTagForQuery_(syncTree, query);
746746

747747
return {
748748
hashFn: () => {
749-
const cache = view.getServerCache() || ChildrenNode.EMPTY_NODE;
749+
const cache = viewGetServerCache(view) || ChildrenNode.EMPTY_NODE;
750750
return cache.hash();
751751
},
752752
onComplete: (status: string): Event[] => {
@@ -929,14 +929,14 @@ function syncTreeSetupListener_(
929929
maybeChildSyncPoint &&
930930
syncPointHasCompleteView(maybeChildSyncPoint)
931931
) {
932-
return [syncPointGetCompleteView(maybeChildSyncPoint).getQuery()];
932+
return [syncPointGetCompleteView(maybeChildSyncPoint).query];
933933
} else {
934934
// No default listener here, flatten any deeper queries into an array
935935
let queries: Query[] = [];
936936
if (maybeChildSyncPoint) {
937937
queries = queries.concat(
938-
syncPointGetQueryViews(maybeChildSyncPoint).map(view =>
939-
view.getQuery()
938+
syncPointGetQueryViews(maybeChildSyncPoint).map(
939+
view => view.query
940940
)
941941
);
942942
}

0 commit comments

Comments
 (0)