Skip to content

Tree-shake View #4612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions packages/database/src/core/SyncPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ import { CacheNode } from './view/CacheNode';
import { ChildrenNode } from './snap/ChildrenNode';
import { assert } from '@firebase/util';
import { ViewCache } from './view/ViewCache';
import { View } from './view/View';
import {
View,
viewAddEventRegistration,
viewApplyOperation,
viewGetCompleteServerCache,
viewGetInitialEvents,
viewIsEmpty,
viewRemoveEventRegistration
} from './view/View';
import { Operation } from './operation/Operation';
import { WriteTreeRef } from './WriteTree';
import { Query } from '../api/Query';
Expand Down Expand Up @@ -80,13 +88,18 @@ export function syncPointApplyOperation(
if (queryId !== null) {
const view = syncPoint.views.get(queryId);
assert(view != null, 'SyncTree gave us an op for an invalid query.');
return view.applyOperation(operation, writesCache, optCompleteServerCache);
return viewApplyOperation(
view,
operation,
writesCache,
optCompleteServerCache
);
} else {
let events: Event[] = [];

for (const view of syncPoint.views.values()) {
events = events.concat(
view.applyOperation(operation, writesCache, optCompleteServerCache)
viewApplyOperation(view, operation, writesCache, optCompleteServerCache)
);
}

Expand Down Expand Up @@ -165,8 +178,8 @@ export function syncPointAddEventRegistration(
syncPoint.views.set(query.queryIdentifier(), view);
}
// This is guaranteed to exist now, we just created anything that was missing
view.addEventRegistration(eventRegistration);
return view.getInitialEvents(eventRegistration);
viewAddEventRegistration(view, eventRegistration);
return viewGetInitialEvents(view, eventRegistration);
}

/**
Expand All @@ -193,14 +206,14 @@ export function syncPointRemoveEventRegistration(
// When you do ref.off(...), we search all views for the registration to remove.
for (const [viewQueryId, view] of syncPoint.views.entries()) {
cancelEvents = cancelEvents.concat(
view.removeEventRegistration(eventRegistration, cancelError)
viewRemoveEventRegistration(view, eventRegistration, cancelError)
);
if (view.isEmpty()) {
if (viewIsEmpty(view)) {
syncPoint.views.delete(viewQueryId);

// We'll deal with complete views later.
if (!view.getQuery().getQueryParams().loadsAllData()) {
removed.push(view.getQuery());
if (!view.query.getQueryParams().loadsAllData()) {
removed.push(view.query);
}
}
}
Expand All @@ -209,14 +222,14 @@ export function syncPointRemoveEventRegistration(
const view = syncPoint.views.get(queryId);
if (view) {
cancelEvents = cancelEvents.concat(
view.removeEventRegistration(eventRegistration, cancelError)
viewRemoveEventRegistration(view, eventRegistration, cancelError)
);
if (view.isEmpty()) {
if (viewIsEmpty(view)) {
syncPoint.views.delete(queryId);

// We'll deal with complete views later.
if (!view.getQuery().getQueryParams().loadsAllData()) {
removed.push(view.getQuery());
if (!view.query.getQueryParams().loadsAllData()) {
removed.push(view.query);
}
}
}
Expand All @@ -235,7 +248,7 @@ export function syncPointRemoveEventRegistration(
export function syncPointGetQueryViews(syncPoint: SyncPoint): View[] {
const result = [];
for (const view of syncPoint.views.values()) {
if (!view.getQuery().getQueryParams().loadsAllData()) {
if (!view.query.getQueryParams().loadsAllData()) {
result.push(view);
}
}
Expand All @@ -252,7 +265,7 @@ export function syncPointGetCompleteServerCache(
): Node | null {
let serverCache: Node | null = null;
for (const view of syncPoint.views.values()) {
serverCache = serverCache || view.getCompleteServerCache(path);
serverCache = serverCache || viewGetCompleteServerCache(view, path);
}
return serverCache;
}
Expand Down Expand Up @@ -283,7 +296,7 @@ export function syncPointHasCompleteView(syncPoint: SyncPoint): boolean {

export function syncPointGetCompleteView(syncPoint: SyncPoint): View | null {
for (const view of syncPoint.views.values()) {
if (view.getQuery().getQueryParams().loadsAllData()) {
if (view.query.getQueryParams().loadsAllData()) {
return view;
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/database/src/core/SyncTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { Query } from '../api/Query';
import { Node } from './snap/Node';
import { Event } from './view/Event';
import { EventRegistration } from './view/EventRegistration';
import { View } from './view/View';
import { View, viewGetCompleteNode, viewGetServerCache } from './view/View';
import { CacheNode } from './view/CacheNode';

/**
Expand Down Expand Up @@ -350,7 +350,7 @@ export function syncTreeRemoveEventRegistration(
// Ok, we've collected all the listens we need. Set them up.
for (let i = 0; i < newViews.length; ++i) {
const view = newViews[i],
newQuery = view.getQuery();
newQuery = view.query;
const listener = syncTreeCreateListenerForView_(syncTree, view);
syncTree.listenProvider_.startListening(
syncTreeQueryForListening_(newQuery),
Expand Down Expand Up @@ -612,7 +612,7 @@ export function syncTreeGetServerValue(
serverCacheComplete ? serverCacheNode.getNode() : ChildrenNode.EMPTY_NODE,
serverCacheComplete
);
return view.getCompleteNode();
return viewGetCompleteNode(view);
}

/**
Expand Down Expand Up @@ -741,12 +741,12 @@ function syncTreeCreateListenerForView_(
syncTree: SyncTree,
view: View
): { hashFn(): string; onComplete(a: string, b?: unknown): Event[] } {
const query = view.getQuery();
const query = view.query;
const tag = syncTreeTagForQuery_(syncTree, query);

return {
hashFn: () => {
const cache = view.getServerCache() || ChildrenNode.EMPTY_NODE;
const cache = viewGetServerCache(view) || ChildrenNode.EMPTY_NODE;
return cache.hash();
},
onComplete: (status: string): Event[] => {
Expand Down Expand Up @@ -929,14 +929,14 @@ function syncTreeSetupListener_(
maybeChildSyncPoint &&
syncPointHasCompleteView(maybeChildSyncPoint)
) {
return [syncPointGetCompleteView(maybeChildSyncPoint).getQuery()];
return [syncPointGetCompleteView(maybeChildSyncPoint).query];
} else {
// No default listener here, flatten any deeper queries into an array
let queries: Query[] = [];
if (maybeChildSyncPoint) {
queries = queries.concat(
syncPointGetQueryViews(maybeChildSyncPoint).map(view =>
view.getQuery()
syncPointGetQueryViews(maybeChildSyncPoint).map(
view => view.query
)
);
}
Expand Down
Loading