UNPKG

7.25 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17import { ReferenceConstructor } from '../exp/Reference';
18import { Node } from './snap/Node';
19import { SyncPoint } from './SyncPoint';
20import { ImmutableTree } from './util/ImmutableTree';
21import { Path } from './util/Path';
22import { Event } from './view/Event';
23import { EventRegistration, QueryContext } from './view/EventRegistration';
24import { WriteTree } from './WriteTree';
25export declare function syncTreeSetReferenceConstructor(val: ReferenceConstructor): void;
26export interface ListenProvider {
27 startListening(query: QueryContext, tag: number | null, hashFn: () => string, onComplete: (a: string, b?: unknown) => Event[]): Event[];
28 stopListening(a: QueryContext, b: number | null): void;
29}
30/**
31 * SyncTree is the central class for managing event callback registration, data caching, views
32 * (query processing), and event generation. There are typically two SyncTree instances for
33 * each Repo, one for the normal Firebase data, and one for the .info data.
34 *
35 * It has a number of responsibilities, including:
36 * - Tracking all user event callbacks (registered via addEventRegistration() and removeEventRegistration()).
37 * - Applying and caching data changes for user set(), transaction(), and update() calls
38 * (applyUserOverwrite(), applyUserMerge()).
39 * - Applying and caching data changes for server data changes (applyServerOverwrite(),
40 * applyServerMerge()).
41 * - Generating user-facing events for server and user changes (all of the apply* methods
42 * return the set of events that need to be raised as a result).
43 * - Maintaining the appropriate set of server listens to ensure we are always subscribed
44 * to the correct set of paths and queries to satisfy the current set of user event
45 * callbacks (listens are started/stopped using the provided listenProvider).
46 *
47 * NOTE: Although SyncTree tracks event callbacks and calculates events to raise, the actual
48 * events are returned to the caller rather than raised synchronously.
49 *
50 */
51export declare class SyncTree {
52 listenProvider_: ListenProvider;
53 /**
54 * Tree of SyncPoints. There's a SyncPoint at any location that has 1 or more views.
55 */
56 syncPointTree_: ImmutableTree<SyncPoint>;
57 /**
58 * A tree of all pending user writes (user-initiated set()'s, transaction()'s, update()'s, etc.).
59 */
60 pendingWriteTree_: WriteTree;
61 readonly tagToQueryMap: Map<number, string>;
62 readonly queryToTagMap: Map<string, number>;
63 /**
64 * @param listenProvider_ - Used by SyncTree to start / stop listening
65 * to server data.
66 */
67 constructor(listenProvider_: ListenProvider);
68}
69/**
70 * Apply the data changes for a user-generated set() or transaction() call.
71 *
72 * @returns Events to raise.
73 */
74export declare function syncTreeApplyUserOverwrite(syncTree: SyncTree, path: Path, newData: Node, writeId: number, visible?: boolean): Event[];
75/**
76 * Apply the data from a user-generated update() call
77 *
78 * @returns Events to raise.
79 */
80export declare function syncTreeApplyUserMerge(syncTree: SyncTree, path: Path, changedChildren: {
81 [k: string]: Node;
82}, writeId: number): Event[];
83/**
84 * Acknowledge a pending user write that was previously registered with applyUserOverwrite() or applyUserMerge().
85 *
86 * @param revert - True if the given write failed and needs to be reverted
87 * @returns Events to raise.
88 */
89export declare function syncTreeAckUserWrite(syncTree: SyncTree, writeId: number, revert?: boolean): Event[];
90/**
91 * Apply new server data for the specified path..
92 *
93 * @returns Events to raise.
94 */
95export declare function syncTreeApplyServerOverwrite(syncTree: SyncTree, path: Path, newData: Node): Event[];
96/**
97 * Apply new server data to be merged in at the specified path.
98 *
99 * @returns Events to raise.
100 */
101export declare function syncTreeApplyServerMerge(syncTree: SyncTree, path: Path, changedChildren: {
102 [k: string]: Node;
103}): Event[];
104/**
105 * Apply a listen complete for a query
106 *
107 * @returns Events to raise.
108 */
109export declare function syncTreeApplyListenComplete(syncTree: SyncTree, path: Path): Event[];
110/**
111 * Apply a listen complete for a tagged query
112 *
113 * @returns Events to raise.
114 */
115export declare function syncTreeApplyTaggedListenComplete(syncTree: SyncTree, path: Path, tag: number): Event[];
116/**
117 * Remove event callback(s).
118 *
119 * If query is the default query, we'll check all queries for the specified eventRegistration.
120 * If eventRegistration is null, we'll remove all callbacks for the specified query/queries.
121 *
122 * @param eventRegistration - If null, all callbacks are removed.
123 * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
124 * @returns Cancel events, if cancelError was provided.
125 */
126export declare function syncTreeRemoveEventRegistration(syncTree: SyncTree, query: QueryContext, eventRegistration: EventRegistration | null, cancelError?: Error): Event[];
127/**
128 * Apply new server data for the specified tagged query.
129 *
130 * @returns Events to raise.
131 */
132export declare function syncTreeApplyTaggedQueryOverwrite(syncTree: SyncTree, path: Path, snap: Node, tag: number): Event[];
133/**
134 * Apply server data to be merged in for the specified tagged query.
135 *
136 * @returns Events to raise.
137 */
138export declare function syncTreeApplyTaggedQueryMerge(syncTree: SyncTree, path: Path, changedChildren: {
139 [k: string]: Node;
140}, tag: number): Event[];
141/**
142 * Add an event callback for the specified query.
143 *
144 * @returns Events to raise.
145 */
146export declare function syncTreeAddEventRegistration(syncTree: SyncTree, query: QueryContext, eventRegistration: EventRegistration): Event[];
147/**
148 * Returns a complete cache, if we have one, of the data at a particular path. If the location does not have a
149 * listener above it, we will get a false "null". This shouldn't be a problem because transactions will always
150 * have a listener above, and atomic operations would correctly show a jitter of <increment value> ->
151 * <incremented total> as the write is applied locally and then acknowledged at the server.
152 *
153 * Note: this method will *include* hidden writes from transaction with applyLocally set to false.
154 *
155 * @param path - The path to the data we want
156 * @param writeIdsToExclude - A specific set to be excluded
157 */
158export declare function syncTreeCalcCompleteEventCache(syncTree: SyncTree, path: Path, writeIdsToExclude?: number[]): Node;
159export declare function syncTreeGetServerValue(syncTree: SyncTree, query: QueryContext): Node | null;