| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 |
12x
12x
12x
12x
12x
12x
12x
12x
12x
12x
108x
108x
256x
12x
159x
12x
1303x
1303x
425x
425x
425x
878x
878x
901x
878x
12x
410x
410x
410x
305x
305x
305x
23x
282x
282x
282x
305x
305x
305x
410x
410x
12x
159x
159x
159x
159x
159x
134x
134x
141x
141x
112x
112x
3x
25x
25x
24x
24x
21x
21x
18x
159x
108x
159x
153x
154x
153x
154x
12x
1358x
1358x
1578x
1358x
12x
726x
726x
381x
345x
345x
12x
436x
12x
675x
12x
1071x
1113x
1071x
12x
| /**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CacheNode } from './view/CacheNode';
import { ChildrenNode } from './snap/ChildrenNode';
import { assert } from '@firebase/util';
import { isEmpty, forEach, findValue, safeGet } from '@firebase/util';
import { ViewCache } from './view/ViewCache';
import { View } from './view/View';
import { Operation } from './operation/Operation';
import { WriteTreeRef } from './WriteTree';
import { Query } from '../api/Query';
import { EventRegistration } from './view/EventRegistration';
import { Node } from './snap/Node';
import { Path } from './util/Path';
import { Event } from './view/Event';
import { Reference, ReferenceConstructor } from '../api/Reference';
let __referenceConstructor: ReferenceConstructor;
/**
* SyncPoint represents a single location in a SyncTree with 1 or more event registrations, meaning we need to
* maintain 1 or more Views at this location to cache server data and raise appropriate events for server changes
* and user writes (set, transaction, update).
*
* It's responsible for:
* - Maintaining the set of 1 or more views necessary at this location (a SyncPoint with 0 views should be removed).
* - Proxying user / server operations to the views as appropriate (i.e. applyServerOverwrite,
* applyUserOverwrite, etc.)
*/
export class SyncPoint {
static set __referenceConstructor(val: ReferenceConstructor) {
assert(
!__referenceConstructor,
'__referenceConstructor has already been defined'
);
__referenceConstructor = val;
}
static get __referenceConstructor() {
assert(__referenceConstructor, 'Reference.ts has not been loaded');
return __referenceConstructor;
}
/**
* The Views being tracked at this location in the tree, stored as a map where the key is a
* queryId and the value is the View for that query.
*
* NOTE: This list will be quite small (usually 1, but perhaps 2 or 3; any more is an odd use case).
*
* @type {!Object.<!string, !View>}
* @private
*/
private views_: { [k: string]: View } = {};
/**
* @return {boolean}
*/
isEmpty(): boolean {
return isEmpty(this.views_);
}
/**
*
* @param {!Operation} operation
* @param {!WriteTreeRef} writesCache
* @param {?Node} optCompleteServerCache
* @return {!Array.<!Event>}
*/
applyOperation(
operation: Operation,
writesCache: WriteTreeRef,
optCompleteServerCache: Node | null
): Event[] {
const queryId = operation.source.queryId;
if (queryId !== null) {
const view = safeGet(this.views_, queryId);
assert(view != null, 'SyncTree gave us an op for an invalid query.');
return view.applyOperation(
operation,
writesCache,
optCompleteServerCache
);
} else {
let events: Event[] = [];
forEach(this.views_, function(key: string, view: View) {
events = events.concat(
view.applyOperation(operation, writesCache, optCompleteServerCache)
);
});
return events;
}
}
/**
* Add an event callback for the specified query.
*
* @param {!Query} query
* @param {!EventRegistration} eventRegistration
* @param {!WriteTreeRef} writesCache
* @param {?Node} serverCache Complete server cache, if we have it.
* @param {boolean} serverCacheComplete
* @return {!Array.<!Event>} Events to raise.
*/
addEventRegistration(
query: Query,
eventRegistration: EventRegistration,
writesCache: WriteTreeRef,
serverCache: Node | null,
serverCacheComplete: boolean
): Event[] {
const queryId = query.queryIdentifier();
let view = safeGet(this.views_, queryId);
if (!view) {
// TODO: make writesCache take flag for complete server node
let eventCache = writesCache.calcCompleteEventCache(
serverCacheComplete ? serverCache : null
);
let eventCacheComplete = false;
if (eventCache) {
eventCacheComplete = true;
} else Eif (serverCache instanceof ChildrenNode) {
eventCache = writesCache.calcCompleteEventChildren(serverCache);
eventCacheComplete = false;
} else {
eventCache = ChildrenNode.EMPTY_NODE;
eventCacheComplete = false;
}
const viewCache = new ViewCache(
new CacheNode(
/** @type {!Node} */ (eventCache),
eventCacheComplete,
false
),
new CacheNode(
/** @type {!Node} */ (serverCache),
serverCacheComplete,
false
)
);
view = new View(query, viewCache);
this.views_[queryId] = view;
}
// This is guaranteed to exist now, we just created anything that was missing
view.addEventRegistration(eventRegistration);
return view.getInitialEvents(eventRegistration);
}
/**
* Remove event callback(s). Return cancelEvents if a cancelError is specified.
*
* If query is the default query, we'll check all views for the specified eventRegistration.
* If eventRegistration is null, we'll remove all callbacks for the specified view(s).
*
* @param {!Query} query
* @param {?EventRegistration} eventRegistration If null, remove all callbacks.
* @param {Error=} cancelError If a cancelError is provided, appropriate cancel events will be returned.
* @return {{removed:!Array.<!Query>, events:!Array.<!Event>}} removed queries and any cancel events
*/
removeEventRegistration(
query: Query,
eventRegistration: EventRegistration | null,
cancelError?: Error
): { removed: Query[]; events: Event[] } {
const queryId = query.queryIdentifier();
const removed: Query[] = [];
let cancelEvents: Event[] = [];
const hadCompleteView = this.hasCompleteView();
if (queryId === 'default') {
// When you do ref.off(...), we search all views for the registration to remove.
const self = this;
forEach(this.views_, function(viewQueryId: string, view: View) {
cancelEvents = cancelEvents.concat(
view.removeEventRegistration(eventRegistration, cancelError)
);
if (view.isEmpty()) {
delete self.views_[viewQueryId];
// We'll deal with complete views later.
if (
!view
.getQuery()
.getQueryParams()
.loadsAllData()
) {
removed.push(view.getQuery());
}
}
});
} else {
// remove the callback from the specific view.
const view = safeGet(this.views_, queryId);
if (view) {
cancelEvents = cancelEvents.concat(
view.removeEventRegistration(eventRegistration, cancelError)
);
if (view.isEmpty()) {
delete this.views_[queryId];
// We'll deal with complete views later.
if (
!view
.getQuery()
.getQueryParams()
.loadsAllData()
) {
removed.push(view.getQuery());
}
}
}
}
if (hadCompleteView && !this.hasCompleteView()) {
// We removed our last complete view.
removed.push(
new SyncPoint.__referenceConstructor(query.repo, query.path)
);
}
return { removed: removed, events: cancelEvents };
}
/**
* @return {!Array.<!View>}
*/
getQueryViews(): View[] {
const values = Object.keys(this.views_).map(key => this.views_[key]);
return values.filter(function(view) {
return !view
.getQuery()
.getQueryParams()
.loadsAllData();
});
}
/**
*
* @param {!Path} path The path to the desired complete snapshot
* @return {?Node} A complete cache, if it exists
*/
getCompleteServerCache(path: Path): Node | null {
let serverCache: Node | null = null;
forEach(this.views_, (key: string, view: View) => {
serverCache = serverCache || view.getCompleteServerCache(path);
});
return serverCache;
}
/**
* @param {!Query} query
* @return {?View}
*/
viewForQuery(query: Query): View | null {
const params = query.getQueryParams();
if (params.loadsAllData()) {
return this.getCompleteView();
} else {
const queryId = query.queryIdentifier();
return safeGet(this.views_, queryId);
}
}
/**
* @param {!Query} query
* @return {boolean}
*/
viewExistsForQuery(query: Query): boolean {
return this.viewForQuery(query) != null;
}
/**
* @return {boolean}
*/
hasCompleteView(): boolean {
return this.getCompleteView() != null;
}
/**
* @return {?View}
*/
getCompleteView(): View | null {
const completeView = findValue(this.views_, (view: View) =>
view
.getQuery()
.getQueryParams()
.loadsAllData()
);
return completeView || null;
}
}
|