1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var Websocket = require('faye-websocket');
|
6 | var util = require('@firebase/util');
|
7 | var tslib = require('tslib');
|
8 | var logger$1 = require('@firebase/logger');
|
9 |
|
10 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
11 |
|
12 | var Websocket__default = _interopDefaultLegacy(Websocket);
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var PROTOCOL_VERSION = '5';
|
31 | var VERSION_PARAM = 'v';
|
32 | var TRANSPORT_SESSION_PARAM = 's';
|
33 | var REFERER_PARAM = 'r';
|
34 | var FORGE_REF = 'f';
|
35 |
|
36 |
|
37 | var FORGE_DOMAIN_RE = /(console\.firebase|firebase-console-\w+\.corp|firebase\.corp)\.google\.com/;
|
38 | var LAST_SESSION_PARAM = 'ls';
|
39 | var APPLICATION_ID_PARAM = 'p';
|
40 | var APP_CHECK_TOKEN_PARAM = 'ac';
|
41 | var WEBSOCKET = 'websocket';
|
42 | var LONG_POLLING = 'long_polling';
|
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 | var DOMStorageWrapper = (function () {
|
70 | |
71 |
|
72 |
|
73 | function DOMStorageWrapper(domStorage_) {
|
74 | this.domStorage_ = domStorage_;
|
75 |
|
76 | this.prefix_ = 'firebase:';
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 | DOMStorageWrapper.prototype.set = function (key, value) {
|
83 | if (value == null) {
|
84 | this.domStorage_.removeItem(this.prefixedName_(key));
|
85 | }
|
86 | else {
|
87 | this.domStorage_.setItem(this.prefixedName_(key), util.stringify(value));
|
88 | }
|
89 | };
|
90 | |
91 |
|
92 |
|
93 | DOMStorageWrapper.prototype.get = function (key) {
|
94 | var storedVal = this.domStorage_.getItem(this.prefixedName_(key));
|
95 | if (storedVal == null) {
|
96 | return null;
|
97 | }
|
98 | else {
|
99 | return util.jsonEval(storedVal);
|
100 | }
|
101 | };
|
102 | DOMStorageWrapper.prototype.remove = function (key) {
|
103 | this.domStorage_.removeItem(this.prefixedName_(key));
|
104 | };
|
105 | DOMStorageWrapper.prototype.prefixedName_ = function (name) {
|
106 | return this.prefix_ + name;
|
107 | };
|
108 | DOMStorageWrapper.prototype.toString = function () {
|
109 | return this.domStorage_.toString();
|
110 | };
|
111 | return DOMStorageWrapper;
|
112 | }());
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | var MemoryStorage = (function () {
|
135 | function MemoryStorage() {
|
136 | this.cache_ = {};
|
137 | this.isInMemoryStorage = true;
|
138 | }
|
139 | MemoryStorage.prototype.set = function (key, value) {
|
140 | if (value == null) {
|
141 | delete this.cache_[key];
|
142 | }
|
143 | else {
|
144 | this.cache_[key] = value;
|
145 | }
|
146 | };
|
147 | MemoryStorage.prototype.get = function (key) {
|
148 | if (util.contains(this.cache_, key)) {
|
149 | return this.cache_[key];
|
150 | }
|
151 | return null;
|
152 | };
|
153 | MemoryStorage.prototype.remove = function (key) {
|
154 | delete this.cache_[key];
|
155 | };
|
156 | return MemoryStorage;
|
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 | var createStoragefor = function (domStorageName) {
|
185 | try {
|
186 |
|
187 |
|
188 | if (typeof window !== 'undefined' &&
|
189 | typeof window[domStorageName] !== 'undefined') {
|
190 |
|
191 | var domStorage = window[domStorageName];
|
192 | domStorage.setItem('firebase:sentinel', 'cache');
|
193 | domStorage.removeItem('firebase:sentinel');
|
194 | return new DOMStorageWrapper(domStorage);
|
195 | }
|
196 | }
|
197 | catch (e) { }
|
198 |
|
199 |
|
200 | return new MemoryStorage();
|
201 | };
|
202 |
|
203 | var PersistentStorage = createStoragefor('localStorage');
|
204 |
|
205 | var SessionStorage = createStoragefor('sessionStorage');
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | var logClient = new logger$1.Logger('@firebase/database');
|
224 |
|
225 |
|
226 |
|
227 | var LUIDGenerator = (function () {
|
228 | var id = 1;
|
229 | return function () {
|
230 | return id++;
|
231 | };
|
232 | })();
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | var sha1 = function (str) {
|
239 | var utf8Bytes = util.stringToByteArray(str);
|
240 | var sha1 = new util.Sha1();
|
241 | sha1.update(utf8Bytes);
|
242 | var sha1Bytes = sha1.digest();
|
243 | return util.base64.encodeByteArray(sha1Bytes);
|
244 | };
|
245 | var buildLogMessage_ = function () {
|
246 | var varArgs = [];
|
247 | for (var _i = 0; _i < arguments.length; _i++) {
|
248 | varArgs[_i] = arguments[_i];
|
249 | }
|
250 | var message = '';
|
251 | for (var i = 0; i < varArgs.length; i++) {
|
252 | var arg = varArgs[i];
|
253 | if (Array.isArray(arg) ||
|
254 | (arg &&
|
255 | typeof arg === 'object' &&
|
256 |
|
257 | typeof arg.length === 'number')) {
|
258 | message += buildLogMessage_.apply(null, arg);
|
259 | }
|
260 | else if (typeof arg === 'object') {
|
261 | message += util.stringify(arg);
|
262 | }
|
263 | else {
|
264 | message += arg;
|
265 | }
|
266 | message += ' ';
|
267 | }
|
268 | return message;
|
269 | };
|
270 |
|
271 |
|
272 |
|
273 | var logger = null;
|
274 |
|
275 |
|
276 |
|
277 | var firstLog_ = true;
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | var enableLogging$1 = function (logger_, persistent) {
|
284 | util.assert(!persistent || logger_ === true || logger_ === false, "Can't turn on custom loggers persistently.");
|
285 | if (logger_ === true) {
|
286 | logClient.logLevel = logger$1.LogLevel.VERBOSE;
|
287 | logger = logClient.log.bind(logClient);
|
288 | if (persistent) {
|
289 | SessionStorage.set('logging_enabled', true);
|
290 | }
|
291 | }
|
292 | else if (typeof logger_ === 'function') {
|
293 | logger = logger_;
|
294 | }
|
295 | else {
|
296 | logger = null;
|
297 | SessionStorage.remove('logging_enabled');
|
298 | }
|
299 | };
|
300 | var log = function () {
|
301 | var varArgs = [];
|
302 | for (var _i = 0; _i < arguments.length; _i++) {
|
303 | varArgs[_i] = arguments[_i];
|
304 | }
|
305 | if (firstLog_ === true) {
|
306 | firstLog_ = false;
|
307 | if (logger === null && SessionStorage.get('logging_enabled') === true) {
|
308 | enableLogging$1(true);
|
309 | }
|
310 | }
|
311 | if (logger) {
|
312 | var message = buildLogMessage_.apply(null, varArgs);
|
313 | logger(message);
|
314 | }
|
315 | };
|
316 | var logWrapper = function (prefix) {
|
317 | return function () {
|
318 | var varArgs = [];
|
319 | for (var _i = 0; _i < arguments.length; _i++) {
|
320 | varArgs[_i] = arguments[_i];
|
321 | }
|
322 | log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs)));
|
323 | };
|
324 | };
|
325 | var error = function () {
|
326 | var varArgs = [];
|
327 | for (var _i = 0; _i < arguments.length; _i++) {
|
328 | varArgs[_i] = arguments[_i];
|
329 | }
|
330 | var message = 'FIREBASE INTERNAL ERROR: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs)));
|
331 | logClient.error(message);
|
332 | };
|
333 | var fatal = function () {
|
334 | var varArgs = [];
|
335 | for (var _i = 0; _i < arguments.length; _i++) {
|
336 | varArgs[_i] = arguments[_i];
|
337 | }
|
338 | var message = "FIREBASE FATAL ERROR: " + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs)));
|
339 | logClient.error(message);
|
340 | throw new Error(message);
|
341 | };
|
342 | var warn = function () {
|
343 | var varArgs = [];
|
344 | for (var _i = 0; _i < arguments.length; _i++) {
|
345 | varArgs[_i] = arguments[_i];
|
346 | }
|
347 | var message = 'FIREBASE WARNING: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs)));
|
348 | logClient.warn(message);
|
349 | };
|
350 |
|
351 |
|
352 |
|
353 |
|
354 | var warnIfPageIsSecure = function () {
|
355 |
|
356 | if (typeof window !== 'undefined' &&
|
357 | window.location &&
|
358 | window.location.protocol &&
|
359 | window.location.protocol.indexOf('https:') !== -1) {
|
360 | warn('Insecure Firebase access from a secure page. ' +
|
361 | 'Please use https in calls to new Firebase().');
|
362 | }
|
363 | };
|
364 |
|
365 |
|
366 |
|
367 | var isInvalidJSONNumber = function (data) {
|
368 | return (typeof data === 'number' &&
|
369 | (data !== data ||
|
370 | data === Number.POSITIVE_INFINITY ||
|
371 | data === Number.NEGATIVE_INFINITY));
|
372 | };
|
373 | var executeWhenDOMReady = function (fn) {
|
374 | if (util.isNodeSdk() || document.readyState === 'complete') {
|
375 | fn();
|
376 | }
|
377 | else {
|
378 |
|
379 |
|
380 | var called_1 = false;
|
381 | var wrappedFn_1 = function () {
|
382 | if (!document.body) {
|
383 | setTimeout(wrappedFn_1, Math.floor(10));
|
384 | return;
|
385 | }
|
386 | if (!called_1) {
|
387 | called_1 = true;
|
388 | fn();
|
389 | }
|
390 | };
|
391 | if (document.addEventListener) {
|
392 | document.addEventListener('DOMContentLoaded', wrappedFn_1, false);
|
393 |
|
394 | window.addEventListener('load', wrappedFn_1, false);
|
395 |
|
396 | }
|
397 | else if (document.attachEvent) {
|
398 |
|
399 |
|
400 | document.attachEvent('onreadystatechange', function () {
|
401 | if (document.readyState === 'complete') {
|
402 | wrappedFn_1();
|
403 | }
|
404 | });
|
405 |
|
406 |
|
407 | window.attachEvent('onload', wrappedFn_1);
|
408 |
|
409 |
|
410 |
|
411 | }
|
412 | }
|
413 | };
|
414 |
|
415 |
|
416 |
|
417 | var MIN_NAME = '[MIN_NAME]';
|
418 |
|
419 |
|
420 |
|
421 | var MAX_NAME = '[MAX_NAME]';
|
422 |
|
423 |
|
424 |
|
425 | var nameCompare = function (a, b) {
|
426 | if (a === b) {
|
427 | return 0;
|
428 | }
|
429 | else if (a === MIN_NAME || b === MAX_NAME) {
|
430 | return -1;
|
431 | }
|
432 | else if (b === MIN_NAME || a === MAX_NAME) {
|
433 | return 1;
|
434 | }
|
435 | else {
|
436 | var aAsInt = tryParseInt(a), bAsInt = tryParseInt(b);
|
437 | if (aAsInt !== null) {
|
438 | if (bAsInt !== null) {
|
439 | return aAsInt - bAsInt === 0 ? a.length - b.length : aAsInt - bAsInt;
|
440 | }
|
441 | else {
|
442 | return -1;
|
443 | }
|
444 | }
|
445 | else if (bAsInt !== null) {
|
446 | return 1;
|
447 | }
|
448 | else {
|
449 | return a < b ? -1 : 1;
|
450 | }
|
451 | }
|
452 | };
|
453 |
|
454 |
|
455 |
|
456 | var stringCompare = function (a, b) {
|
457 | if (a === b) {
|
458 | return 0;
|
459 | }
|
460 | else if (a < b) {
|
461 | return -1;
|
462 | }
|
463 | else {
|
464 | return 1;
|
465 | }
|
466 | };
|
467 | var requireKey = function (key, obj) {
|
468 | if (obj && key in obj) {
|
469 | return obj[key];
|
470 | }
|
471 | else {
|
472 | throw new Error('Missing required key (' + key + ') in object: ' + util.stringify(obj));
|
473 | }
|
474 | };
|
475 | var ObjectToUniqueKey = function (obj) {
|
476 | if (typeof obj !== 'object' || obj === null) {
|
477 | return util.stringify(obj);
|
478 | }
|
479 | var keys = [];
|
480 |
|
481 | for (var k in obj) {
|
482 | keys.push(k);
|
483 | }
|
484 |
|
485 | keys.sort();
|
486 | var key = '{';
|
487 | for (var i = 0; i < keys.length; i++) {
|
488 | if (i !== 0) {
|
489 | key += ',';
|
490 | }
|
491 | key += util.stringify(keys[i]);
|
492 | key += ':';
|
493 | key += ObjectToUniqueKey(obj[keys[i]]);
|
494 | }
|
495 | key += '}';
|
496 | return key;
|
497 | };
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 | var splitStringBySize = function (str, segsize) {
|
505 | var len = str.length;
|
506 | if (len <= segsize) {
|
507 | return [str];
|
508 | }
|
509 | var dataSegs = [];
|
510 | for (var c = 0; c < len; c += segsize) {
|
511 | if (c + segsize > len) {
|
512 | dataSegs.push(str.substring(c, len));
|
513 | }
|
514 | else {
|
515 | dataSegs.push(str.substring(c, c + segsize));
|
516 | }
|
517 | }
|
518 | return dataSegs;
|
519 | };
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 | function each(obj, fn) {
|
527 | for (var key in obj) {
|
528 | if (obj.hasOwnProperty(key)) {
|
529 | fn(key, obj[key]);
|
530 | }
|
531 | }
|
532 | }
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 | var doubleToIEEE754String = function (v) {
|
541 | util.assert(!isInvalidJSONNumber(v), 'Invalid JSON number');
|
542 | var ebits = 11, fbits = 52;
|
543 | var bias = (1 << (ebits - 1)) - 1;
|
544 | var s, e, f, ln, i;
|
545 |
|
546 |
|
547 | if (v === 0) {
|
548 | e = 0;
|
549 | f = 0;
|
550 | s = 1 / v === -Infinity ? 1 : 0;
|
551 | }
|
552 | else {
|
553 | s = v < 0;
|
554 | v = Math.abs(v);
|
555 | if (v >= Math.pow(2, 1 - bias)) {
|
556 |
|
557 | ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
|
558 | e = ln + bias;
|
559 | f = Math.round(v * Math.pow(2, fbits - ln) - Math.pow(2, fbits));
|
560 | }
|
561 | else {
|
562 |
|
563 | e = 0;
|
564 | f = Math.round(v / Math.pow(2, 1 - bias - fbits));
|
565 | }
|
566 | }
|
567 |
|
568 | var bits = [];
|
569 | for (i = fbits; i; i -= 1) {
|
570 | bits.push(f % 2 ? 1 : 0);
|
571 | f = Math.floor(f / 2);
|
572 | }
|
573 | for (i = ebits; i; i -= 1) {
|
574 | bits.push(e % 2 ? 1 : 0);
|
575 | e = Math.floor(e / 2);
|
576 | }
|
577 | bits.push(s ? 1 : 0);
|
578 | bits.reverse();
|
579 | var str = bits.join('');
|
580 |
|
581 | var hexByteString = '';
|
582 | for (i = 0; i < 64; i += 8) {
|
583 | var hexByte = parseInt(str.substr(i, 8), 2).toString(16);
|
584 | if (hexByte.length === 1) {
|
585 | hexByte = '0' + hexByte;
|
586 | }
|
587 | hexByteString = hexByteString + hexByte;
|
588 | }
|
589 | return hexByteString.toLowerCase();
|
590 | };
|
591 |
|
592 |
|
593 |
|
594 |
|
595 | var isChromeExtensionContentScript = function () {
|
596 | return !!(typeof window === 'object' &&
|
597 | window['chrome'] &&
|
598 | window['chrome']['extension'] &&
|
599 | !/^chrome/.test(window.location.href));
|
600 | };
|
601 |
|
602 |
|
603 |
|
604 | var isWindowsStoreApp = function () {
|
605 |
|
606 | return typeof Windows === 'object' && typeof Windows.UI === 'object';
|
607 | };
|
608 |
|
609 |
|
610 |
|
611 | function errorForServerCode(code, query) {
|
612 | var reason = 'Unknown Error';
|
613 | if (code === 'too_big') {
|
614 | reason =
|
615 | 'The data requested exceeds the maximum size ' +
|
616 | 'that can be accessed with a single request.';
|
617 | }
|
618 | else if (code === 'permission_denied') {
|
619 | reason = "Client doesn't have permission to access the desired data.";
|
620 | }
|
621 | else if (code === 'unavailable') {
|
622 | reason = 'The service is unavailable';
|
623 | }
|
624 | var error = new Error(code + ' at ' + query._path.toString() + ': ' + reason);
|
625 |
|
626 | error.code = code.toUpperCase();
|
627 | return error;
|
628 | }
|
629 |
|
630 |
|
631 |
|
632 | var INTEGER_REGEXP_ = new RegExp('^-?(0*)\\d{1,10}$');
|
633 |
|
634 |
|
635 |
|
636 | var INTEGER_32_MIN = -2147483648;
|
637 |
|
638 |
|
639 |
|
640 | var INTEGER_32_MAX = 2147483647;
|
641 |
|
642 |
|
643 |
|
644 | var tryParseInt = function (str) {
|
645 | if (INTEGER_REGEXP_.test(str)) {
|
646 | var intVal = Number(str);
|
647 | if (intVal >= INTEGER_32_MIN && intVal <= INTEGER_32_MAX) {
|
648 | return intVal;
|
649 | }
|
650 | }
|
651 | return null;
|
652 | };
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | var exceptionGuard = function (fn) {
|
671 | try {
|
672 | fn();
|
673 | }
|
674 | catch (e) {
|
675 |
|
676 | setTimeout(function () {
|
677 |
|
678 |
|
679 |
|
680 |
|
681 | var stack = e.stack || '';
|
682 | warn('Exception was thrown by user callback.', stack);
|
683 | throw e;
|
684 | }, Math.floor(0));
|
685 | }
|
686 | };
|
687 |
|
688 |
|
689 |
|
690 | var beingCrawled = function () {
|
691 | var userAgent = (typeof window === 'object' &&
|
692 | window['navigator'] &&
|
693 | window['navigator']['userAgent']) ||
|
694 | '';
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | return (userAgent.search(/googlebot|google webmaster tools|bingbot|yahoo! slurp|baiduspider|yandexbot|duckduckbot/i) >= 0);
|
700 | };
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 | var setTimeoutNonBlocking = function (fn, time) {
|
711 | var timeout = setTimeout(fn, time);
|
712 |
|
713 | if (typeof timeout === 'object' && timeout['unref']) {
|
714 |
|
715 | timeout['unref']();
|
716 | }
|
717 | return timeout;
|
718 | };
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 | var RepoInfo = (function () {
|
740 | |
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 | function RepoInfo(host, secure, namespace, webSocketOnly, nodeAdmin, persistenceKey, includeNamespaceInQueryParams) {
|
749 | if (nodeAdmin === void 0) { nodeAdmin = false; }
|
750 | if (persistenceKey === void 0) { persistenceKey = ''; }
|
751 | if (includeNamespaceInQueryParams === void 0) { includeNamespaceInQueryParams = false; }
|
752 | this.secure = secure;
|
753 | this.namespace = namespace;
|
754 | this.webSocketOnly = webSocketOnly;
|
755 | this.nodeAdmin = nodeAdmin;
|
756 | this.persistenceKey = persistenceKey;
|
757 | this.includeNamespaceInQueryParams = includeNamespaceInQueryParams;
|
758 | this._host = host.toLowerCase();
|
759 | this._domain = this._host.substr(this._host.indexOf('.') + 1);
|
760 | this.internalHost =
|
761 | PersistentStorage.get('host:' + host) || this._host;
|
762 | }
|
763 | RepoInfo.prototype.isCacheableHost = function () {
|
764 | return this.internalHost.substr(0, 2) === 's-';
|
765 | };
|
766 | RepoInfo.prototype.isCustomHost = function () {
|
767 | return (this._domain !== 'firebaseio.com' &&
|
768 | this._domain !== 'firebaseio-demo.com');
|
769 | };
|
770 | Object.defineProperty(RepoInfo.prototype, "host", {
|
771 | get: function () {
|
772 | return this._host;
|
773 | },
|
774 | set: function (newHost) {
|
775 | if (newHost !== this.internalHost) {
|
776 | this.internalHost = newHost;
|
777 | if (this.isCacheableHost()) {
|
778 | PersistentStorage.set('host:' + this._host, this.internalHost);
|
779 | }
|
780 | }
|
781 | },
|
782 | enumerable: false,
|
783 | configurable: true
|
784 | });
|
785 | RepoInfo.prototype.toString = function () {
|
786 | var str = this.toURLString();
|
787 | if (this.persistenceKey) {
|
788 | str += '<' + this.persistenceKey + '>';
|
789 | }
|
790 | return str;
|
791 | };
|
792 | RepoInfo.prototype.toURLString = function () {
|
793 | var protocol = this.secure ? 'https://' : 'http://';
|
794 | var query = this.includeNamespaceInQueryParams
|
795 | ? "?ns=" + this.namespace
|
796 | : '';
|
797 | return "" + protocol + this.host + "/" + query;
|
798 | };
|
799 | return RepoInfo;
|
800 | }());
|
801 | function repoInfoNeedsQueryParam(repoInfo) {
|
802 | return (repoInfo.host !== repoInfo.internalHost ||
|
803 | repoInfo.isCustomHost() ||
|
804 | repoInfo.includeNamespaceInQueryParams);
|
805 | }
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 | function repoInfoConnectionURL(repoInfo, type, params) {
|
814 | util.assert(typeof type === 'string', 'typeof type must == string');
|
815 | util.assert(typeof params === 'object', 'typeof params must == object');
|
816 | var connURL;
|
817 | if (type === WEBSOCKET) {
|
818 | connURL =
|
819 | (repoInfo.secure ? 'wss://' : 'ws://') + repoInfo.internalHost + '/.ws?';
|
820 | }
|
821 | else if (type === LONG_POLLING) {
|
822 | connURL =
|
823 | (repoInfo.secure ? 'https://' : 'http://') +
|
824 | repoInfo.internalHost +
|
825 | '/.lp?';
|
826 | }
|
827 | else {
|
828 | throw new Error('Unknown connection type: ' + type);
|
829 | }
|
830 | if (repoInfoNeedsQueryParam(repoInfo)) {
|
831 | params['ns'] = repoInfo.namespace;
|
832 | }
|
833 | var pairs = [];
|
834 | each(params, function (key, value) {
|
835 | pairs.push(key + '=' + value);
|
836 | });
|
837 | return connURL + pairs.join('&');
|
838 | }
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 | var StatsCollection = (function () {
|
860 | function StatsCollection() {
|
861 | this.counters_ = {};
|
862 | }
|
863 | StatsCollection.prototype.incrementCounter = function (name, amount) {
|
864 | if (amount === void 0) { amount = 1; }
|
865 | if (!util.contains(this.counters_, name)) {
|
866 | this.counters_[name] = 0;
|
867 | }
|
868 | this.counters_[name] += amount;
|
869 | };
|
870 | StatsCollection.prototype.get = function () {
|
871 | return util.deepCopy(this.counters_);
|
872 | };
|
873 | return StatsCollection;
|
874 | }());
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 | var collections = {};
|
893 | var reporters = {};
|
894 | function statsManagerGetCollection(repoInfo) {
|
895 | var hashString = repoInfo.toString();
|
896 | if (!collections[hashString]) {
|
897 | collections[hashString] = new StatsCollection();
|
898 | }
|
899 | return collections[hashString];
|
900 | }
|
901 | function statsManagerGetOrCreateReporter(repoInfo, creatorFunction) {
|
902 | var hashString = repoInfo.toString();
|
903 | if (!reporters[hashString]) {
|
904 | reporters[hashString] = creatorFunction();
|
905 | }
|
906 | return reporters[hashString];
|
907 | }
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 | var SDK_VERSION = '';
|
927 |
|
928 |
|
929 |
|
930 |
|
931 | function setSDKVersion(version) {
|
932 | SDK_VERSION = version;
|
933 | }
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 | var WEBSOCKET_MAX_FRAME_SIZE = 16384;
|
952 | var WEBSOCKET_KEEPALIVE_INTERVAL = 45000;
|
953 | var WebSocketImpl = null;
|
954 | if (typeof MozWebSocket !== 'undefined') {
|
955 | WebSocketImpl = MozWebSocket;
|
956 | }
|
957 | else if (typeof WebSocket !== 'undefined') {
|
958 | WebSocketImpl = WebSocket;
|
959 | }
|
960 | function setWebSocketImpl(impl) {
|
961 | WebSocketImpl = impl;
|
962 | }
|
963 |
|
964 |
|
965 |
|
966 | var WebSocketConnection = (function () {
|
967 | |
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 | function WebSocketConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
|
979 | this.connId = connId;
|
980 | this.applicationId = applicationId;
|
981 | this.appCheckToken = appCheckToken;
|
982 | this.authToken = authToken;
|
983 | this.keepaliveTimer = null;
|
984 | this.frames = null;
|
985 | this.totalFrames = 0;
|
986 | this.bytesSent = 0;
|
987 | this.bytesReceived = 0;
|
988 | this.log_ = logWrapper(this.connId);
|
989 | this.stats_ = statsManagerGetCollection(repoInfo);
|
990 | this.connURL = WebSocketConnection.connectionURL_(repoInfo, transportSessionId, lastSessionId, appCheckToken, applicationId);
|
991 | this.nodeAdmin = repoInfo.nodeAdmin;
|
992 | }
|
993 | |
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 | WebSocketConnection.connectionURL_ = function (repoInfo, transportSessionId, lastSessionId, appCheckToken, applicationId) {
|
1001 | var urlParams = {};
|
1002 | urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
|
1003 | if (!util.isNodeSdk() &&
|
1004 | typeof location !== 'undefined' &&
|
1005 | location.hostname &&
|
1006 | FORGE_DOMAIN_RE.test(location.hostname)) {
|
1007 | urlParams[REFERER_PARAM] = FORGE_REF;
|
1008 | }
|
1009 | if (transportSessionId) {
|
1010 | urlParams[TRANSPORT_SESSION_PARAM] = transportSessionId;
|
1011 | }
|
1012 | if (lastSessionId) {
|
1013 | urlParams[LAST_SESSION_PARAM] = lastSessionId;
|
1014 | }
|
1015 | if (appCheckToken) {
|
1016 | urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
|
1017 | }
|
1018 | if (applicationId) {
|
1019 | urlParams[APPLICATION_ID_PARAM] = applicationId;
|
1020 | }
|
1021 | return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
|
1022 | };
|
1023 | |
1024 |
|
1025 |
|
1026 |
|
1027 | WebSocketConnection.prototype.open = function (onMessage, onDisconnect) {
|
1028 | var _this = this;
|
1029 | this.onDisconnect = onDisconnect;
|
1030 | this.onMessage = onMessage;
|
1031 | this.log_('Websocket connecting to ' + this.connURL);
|
1032 | this.everConnected_ = false;
|
1033 |
|
1034 | PersistentStorage.set('previous_websocket_failure', true);
|
1035 | try {
|
1036 | var options = void 0;
|
1037 | if (util.isNodeSdk()) {
|
1038 | var device = this.nodeAdmin ? 'AdminNode' : 'Node';
|
1039 |
|
1040 | var options_1 = {
|
1041 | headers: {
|
1042 | 'User-Agent': "Firebase/" + PROTOCOL_VERSION + "/" + SDK_VERSION + "/" + process.platform + "/" + device,
|
1043 | 'X-Firebase-GMPID': this.applicationId || ''
|
1044 | }
|
1045 | };
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 | if (this.authToken) {
|
1052 | options_1.headers['Authorization'] = "Bearer " + this.authToken;
|
1053 | }
|
1054 | if (this.appCheckToken) {
|
1055 | options_1.headers['X-Firebase-AppCheck'] = this.appCheckToken;
|
1056 | }
|
1057 |
|
1058 | var env = process['env'];
|
1059 | var proxy = this.connURL.indexOf('wss://') === 0
|
1060 | ? env['HTTPS_PROXY'] || env['https_proxy']
|
1061 | : env['HTTP_PROXY'] || env['http_proxy'];
|
1062 | if (proxy) {
|
1063 | options_1['proxy'] = { origin: proxy };
|
1064 | }
|
1065 | }
|
1066 | this.mySock = new WebSocketImpl(this.connURL, [], options);
|
1067 | }
|
1068 | catch (e) {
|
1069 | this.log_('Error instantiating WebSocket.');
|
1070 | var error = e.message || e.data;
|
1071 | if (error) {
|
1072 | this.log_(error);
|
1073 | }
|
1074 | this.onClosed_();
|
1075 | return;
|
1076 | }
|
1077 | this.mySock.onopen = function () {
|
1078 | _this.log_('Websocket connected.');
|
1079 | _this.everConnected_ = true;
|
1080 | };
|
1081 | this.mySock.onclose = function () {
|
1082 | _this.log_('Websocket connection was disconnected.');
|
1083 | _this.mySock = null;
|
1084 | _this.onClosed_();
|
1085 | };
|
1086 | this.mySock.onmessage = function (m) {
|
1087 | _this.handleIncomingFrame(m);
|
1088 | };
|
1089 | this.mySock.onerror = function (e) {
|
1090 | _this.log_('WebSocket error. Closing connection.');
|
1091 |
|
1092 | var error = e.message || e.data;
|
1093 | if (error) {
|
1094 | _this.log_(error);
|
1095 | }
|
1096 | _this.onClosed_();
|
1097 | };
|
1098 | };
|
1099 | |
1100 |
|
1101 |
|
1102 | WebSocketConnection.prototype.start = function () { };
|
1103 | WebSocketConnection.forceDisallow = function () {
|
1104 | WebSocketConnection.forceDisallow_ = true;
|
1105 | };
|
1106 | WebSocketConnection.isAvailable = function () {
|
1107 | var isOldAndroid = false;
|
1108 | if (typeof navigator !== 'undefined' && navigator.userAgent) {
|
1109 | var oldAndroidRegex = /Android ([0-9]{0,}\.[0-9]{0,})/;
|
1110 | var oldAndroidMatch = navigator.userAgent.match(oldAndroidRegex);
|
1111 | if (oldAndroidMatch && oldAndroidMatch.length > 1) {
|
1112 | if (parseFloat(oldAndroidMatch[1]) < 4.4) {
|
1113 | isOldAndroid = true;
|
1114 | }
|
1115 | }
|
1116 | }
|
1117 | return (!isOldAndroid &&
|
1118 | WebSocketImpl !== null &&
|
1119 | !WebSocketConnection.forceDisallow_);
|
1120 | };
|
1121 | |
1122 |
|
1123 |
|
1124 | WebSocketConnection.previouslyFailed = function () {
|
1125 |
|
1126 |
|
1127 | return (PersistentStorage.isInMemoryStorage ||
|
1128 | PersistentStorage.get('previous_websocket_failure') === true);
|
1129 | };
|
1130 | WebSocketConnection.prototype.markConnectionHealthy = function () {
|
1131 | PersistentStorage.remove('previous_websocket_failure');
|
1132 | };
|
1133 | WebSocketConnection.prototype.appendFrame_ = function (data) {
|
1134 | this.frames.push(data);
|
1135 | if (this.frames.length === this.totalFrames) {
|
1136 | var fullMess = this.frames.join('');
|
1137 | this.frames = null;
|
1138 | var jsonMess = util.jsonEval(fullMess);
|
1139 |
|
1140 | this.onMessage(jsonMess);
|
1141 | }
|
1142 | };
|
1143 | |
1144 |
|
1145 |
|
1146 | WebSocketConnection.prototype.handleNewFrameCount_ = function (frameCount) {
|
1147 | this.totalFrames = frameCount;
|
1148 | this.frames = [];
|
1149 | };
|
1150 | |
1151 |
|
1152 |
|
1153 |
|
1154 | WebSocketConnection.prototype.extractFrameCount_ = function (data) {
|
1155 | util.assert(this.frames === null, 'We already have a frame buffer');
|
1156 |
|
1157 |
|
1158 | if (data.length <= 6) {
|
1159 | var frameCount = Number(data);
|
1160 | if (!isNaN(frameCount)) {
|
1161 | this.handleNewFrameCount_(frameCount);
|
1162 | return null;
|
1163 | }
|
1164 | }
|
1165 | this.handleNewFrameCount_(1);
|
1166 | return data;
|
1167 | };
|
1168 | |
1169 |
|
1170 |
|
1171 |
|
1172 | WebSocketConnection.prototype.handleIncomingFrame = function (mess) {
|
1173 | if (this.mySock === null) {
|
1174 | return;
|
1175 | }
|
1176 | var data = mess['data'];
|
1177 | this.bytesReceived += data.length;
|
1178 | this.stats_.incrementCounter('bytes_received', data.length);
|
1179 | this.resetKeepAlive();
|
1180 | if (this.frames !== null) {
|
1181 |
|
1182 | this.appendFrame_(data);
|
1183 | }
|
1184 | else {
|
1185 |
|
1186 | var remainingData = this.extractFrameCount_(data);
|
1187 | if (remainingData !== null) {
|
1188 | this.appendFrame_(remainingData);
|
1189 | }
|
1190 | }
|
1191 | };
|
1192 | |
1193 |
|
1194 |
|
1195 |
|
1196 | WebSocketConnection.prototype.send = function (data) {
|
1197 | this.resetKeepAlive();
|
1198 | var dataStr = util.stringify(data);
|
1199 | this.bytesSent += dataStr.length;
|
1200 | this.stats_.incrementCounter('bytes_sent', dataStr.length);
|
1201 |
|
1202 |
|
1203 | var dataSegs = splitStringBySize(dataStr, WEBSOCKET_MAX_FRAME_SIZE);
|
1204 |
|
1205 | if (dataSegs.length > 1) {
|
1206 | this.sendString_(String(dataSegs.length));
|
1207 | }
|
1208 |
|
1209 | for (var i = 0; i < dataSegs.length; i++) {
|
1210 | this.sendString_(dataSegs[i]);
|
1211 | }
|
1212 | };
|
1213 | WebSocketConnection.prototype.shutdown_ = function () {
|
1214 | this.isClosed_ = true;
|
1215 | if (this.keepaliveTimer) {
|
1216 | clearInterval(this.keepaliveTimer);
|
1217 | this.keepaliveTimer = null;
|
1218 | }
|
1219 | if (this.mySock) {
|
1220 | this.mySock.close();
|
1221 | this.mySock = null;
|
1222 | }
|
1223 | };
|
1224 | WebSocketConnection.prototype.onClosed_ = function () {
|
1225 | if (!this.isClosed_) {
|
1226 | this.log_('WebSocket is closing itself');
|
1227 | this.shutdown_();
|
1228 |
|
1229 | if (this.onDisconnect) {
|
1230 | this.onDisconnect(this.everConnected_);
|
1231 | this.onDisconnect = null;
|
1232 | }
|
1233 | }
|
1234 | };
|
1235 | |
1236 |
|
1237 |
|
1238 |
|
1239 | WebSocketConnection.prototype.close = function () {
|
1240 | if (!this.isClosed_) {
|
1241 | this.log_('WebSocket is being closed');
|
1242 | this.shutdown_();
|
1243 | }
|
1244 | };
|
1245 | |
1246 |
|
1247 |
|
1248 |
|
1249 | WebSocketConnection.prototype.resetKeepAlive = function () {
|
1250 | var _this = this;
|
1251 | clearInterval(this.keepaliveTimer);
|
1252 | this.keepaliveTimer = setInterval(function () {
|
1253 |
|
1254 | if (_this.mySock) {
|
1255 | _this.sendString_('0');
|
1256 | }
|
1257 | _this.resetKeepAlive();
|
1258 |
|
1259 | }, Math.floor(WEBSOCKET_KEEPALIVE_INTERVAL));
|
1260 | };
|
1261 | |
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 | WebSocketConnection.prototype.sendString_ = function (str) {
|
1267 |
|
1268 |
|
1269 |
|
1270 | try {
|
1271 | this.mySock.send(str);
|
1272 | }
|
1273 | catch (e) {
|
1274 | this.log_('Exception thrown from WebSocket.send():', e.message || e.data, 'Closing connection.');
|
1275 | setTimeout(this.onClosed_.bind(this), 0);
|
1276 | }
|
1277 | };
|
1278 | |
1279 |
|
1280 |
|
1281 | WebSocketConnection.responsesRequiredToBeHealthy = 2;
|
1282 | |
1283 |
|
1284 |
|
1285 | WebSocketConnection.healthyTimeout = 30000;
|
1286 | return WebSocketConnection;
|
1287 | }());
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 | var AppCheckTokenProvider = (function () {
|
1309 | function AppCheckTokenProvider(appName_, appCheckProvider) {
|
1310 | var _this = this;
|
1311 | this.appName_ = appName_;
|
1312 | this.appCheckProvider = appCheckProvider;
|
1313 | this.appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
|
1314 | if (!this.appCheck) {
|
1315 | appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); });
|
1316 | }
|
1317 | }
|
1318 | AppCheckTokenProvider.prototype.getToken = function (forceRefresh) {
|
1319 | var _this = this;
|
1320 | if (!this.appCheck) {
|
1321 | return new Promise(function (resolve, reject) {
|
1322 |
|
1323 |
|
1324 |
|
1325 |
|
1326 | setTimeout(function () {
|
1327 | if (_this.appCheck) {
|
1328 | _this.getToken(forceRefresh).then(resolve, reject);
|
1329 | }
|
1330 | else {
|
1331 | resolve(null);
|
1332 | }
|
1333 | }, 0);
|
1334 | });
|
1335 | }
|
1336 | return this.appCheck.getToken(forceRefresh);
|
1337 | };
|
1338 | AppCheckTokenProvider.prototype.addTokenChangeListener = function (listener) {
|
1339 | var _a;
|
1340 | (_a = this.appCheckProvider) === null || _a === void 0 ? void 0 : _a.get().then(function (appCheck) { return appCheck.addTokenListener(listener); });
|
1341 | };
|
1342 | AppCheckTokenProvider.prototype.notifyForInvalidToken = function () {
|
1343 | warn("Provided AppCheck credentials for the app named \"" + this.appName_ + "\" " +
|
1344 | 'are invalid. This usually indicates your app was not initialized correctly.');
|
1345 | };
|
1346 | return AppCheckTokenProvider;
|
1347 | }());
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 |
|
1368 | var FirebaseAuthTokenProvider = (function () {
|
1369 | function FirebaseAuthTokenProvider(appName_, firebaseOptions_, authProvider_) {
|
1370 | var _this = this;
|
1371 | this.appName_ = appName_;
|
1372 | this.firebaseOptions_ = firebaseOptions_;
|
1373 | this.authProvider_ = authProvider_;
|
1374 | this.auth_ = null;
|
1375 | this.auth_ = authProvider_.getImmediate({ optional: true });
|
1376 | if (!this.auth_) {
|
1377 | authProvider_.onInit(function (auth) { return (_this.auth_ = auth); });
|
1378 | }
|
1379 | }
|
1380 | FirebaseAuthTokenProvider.prototype.getToken = function (forceRefresh) {
|
1381 | var _this = this;
|
1382 | if (!this.auth_) {
|
1383 | return new Promise(function (resolve, reject) {
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 | setTimeout(function () {
|
1389 | if (_this.auth_) {
|
1390 | _this.getToken(forceRefresh).then(resolve, reject);
|
1391 | }
|
1392 | else {
|
1393 | resolve(null);
|
1394 | }
|
1395 | }, 0);
|
1396 | });
|
1397 | }
|
1398 | return this.auth_.getToken(forceRefresh).catch(function (error) {
|
1399 |
|
1400 |
|
1401 | if (error && error.code === 'auth/token-not-initialized') {
|
1402 | log('Got auth/token-not-initialized error. Treating as null token.');
|
1403 | return null;
|
1404 | }
|
1405 | else {
|
1406 | return Promise.reject(error);
|
1407 | }
|
1408 | });
|
1409 | };
|
1410 | FirebaseAuthTokenProvider.prototype.addTokenChangeListener = function (listener) {
|
1411 |
|
1412 |
|
1413 | if (this.auth_) {
|
1414 | this.auth_.addAuthTokenListener(listener);
|
1415 | }
|
1416 | else {
|
1417 | this.authProvider_
|
1418 | .get()
|
1419 | .then(function (auth) { return auth.addAuthTokenListener(listener); });
|
1420 | }
|
1421 | };
|
1422 | FirebaseAuthTokenProvider.prototype.removeTokenChangeListener = function (listener) {
|
1423 | this.authProvider_
|
1424 | .get()
|
1425 | .then(function (auth) { return auth.removeAuthTokenListener(listener); });
|
1426 | };
|
1427 | FirebaseAuthTokenProvider.prototype.notifyForInvalidToken = function () {
|
1428 | var errorMessage = 'Provided authentication credentials for the app named "' +
|
1429 | this.appName_ +
|
1430 | '" are invalid. This usually indicates your app was not ' +
|
1431 | 'initialized correctly. ';
|
1432 | if ('credential' in this.firebaseOptions_) {
|
1433 | errorMessage +=
|
1434 | 'Make sure the "credential" property provided to initializeApp() ' +
|
1435 | 'is authorized to access the specified "databaseURL" and is from the correct ' +
|
1436 | 'project.';
|
1437 | }
|
1438 | else if ('serviceAccount' in this.firebaseOptions_) {
|
1439 | errorMessage +=
|
1440 | 'Make sure the "serviceAccount" property provided to initializeApp() ' +
|
1441 | 'is authorized to access the specified "databaseURL" and is from the correct ' +
|
1442 | 'project.';
|
1443 | }
|
1444 | else {
|
1445 | errorMessage +=
|
1446 | 'Make sure the "apiKey" and "databaseURL" properties provided to ' +
|
1447 | 'initializeApp() match the values provided for your app at ' +
|
1448 | 'https://console.firebase.google.com/.';
|
1449 | }
|
1450 | warn(errorMessage);
|
1451 | };
|
1452 | return FirebaseAuthTokenProvider;
|
1453 | }());
|
1454 |
|
1455 | var EmulatorTokenProvider = (function () {
|
1456 | function EmulatorTokenProvider(accessToken) {
|
1457 | this.accessToken = accessToken;
|
1458 | }
|
1459 | EmulatorTokenProvider.prototype.getToken = function (forceRefresh) {
|
1460 | return Promise.resolve({
|
1461 | accessToken: this.accessToken
|
1462 | });
|
1463 | };
|
1464 | EmulatorTokenProvider.prototype.addTokenChangeListener = function (listener) {
|
1465 |
|
1466 |
|
1467 | listener(this.accessToken);
|
1468 | };
|
1469 | EmulatorTokenProvider.prototype.removeTokenChangeListener = function (listener) { };
|
1470 | EmulatorTokenProvider.prototype.notifyForInvalidToken = function () { };
|
1471 |
|
1472 | EmulatorTokenProvider.OWNER = 'owner';
|
1473 | return EmulatorTokenProvider;
|
1474 | }());
|
1475 |
|
1476 |
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 | var PacketReceiver = (function () {
|
1497 | |
1498 |
|
1499 |
|
1500 | function PacketReceiver(onMessage_) {
|
1501 | this.onMessage_ = onMessage_;
|
1502 | this.pendingResponses = [];
|
1503 | this.currentResponseNum = 0;
|
1504 | this.closeAfterResponse = -1;
|
1505 | this.onClose = null;
|
1506 | }
|
1507 | PacketReceiver.prototype.closeAfter = function (responseNum, callback) {
|
1508 | this.closeAfterResponse = responseNum;
|
1509 | this.onClose = callback;
|
1510 | if (this.closeAfterResponse < this.currentResponseNum) {
|
1511 | this.onClose();
|
1512 | this.onClose = null;
|
1513 | }
|
1514 | };
|
1515 | |
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 | PacketReceiver.prototype.handleResponse = function (requestNum, data) {
|
1521 | var _this = this;
|
1522 | this.pendingResponses[requestNum] = data;
|
1523 | var _loop_1 = function () {
|
1524 | var toProcess = this_1.pendingResponses[this_1.currentResponseNum];
|
1525 | delete this_1.pendingResponses[this_1.currentResponseNum];
|
1526 | var _loop_2 = function (i) {
|
1527 | if (toProcess[i]) {
|
1528 | exceptionGuard(function () {
|
1529 | _this.onMessage_(toProcess[i]);
|
1530 | });
|
1531 | }
|
1532 | };
|
1533 | for (var i = 0; i < toProcess.length; ++i) {
|
1534 | _loop_2(i);
|
1535 | }
|
1536 | if (this_1.currentResponseNum === this_1.closeAfterResponse) {
|
1537 | if (this_1.onClose) {
|
1538 | this_1.onClose();
|
1539 | this_1.onClose = null;
|
1540 | }
|
1541 | return "break";
|
1542 | }
|
1543 | this_1.currentResponseNum++;
|
1544 | };
|
1545 | var this_1 = this;
|
1546 | while (this.pendingResponses[this.currentResponseNum]) {
|
1547 | var state_1 = _loop_1();
|
1548 | if (state_1 === "break")
|
1549 | break;
|
1550 | }
|
1551 | };
|
1552 | return PacketReceiver;
|
1553 | }());
|
1554 |
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 | var FIREBASE_LONGPOLL_START_PARAM = 'start';
|
1573 | var FIREBASE_LONGPOLL_CLOSE_COMMAND = 'close';
|
1574 | var FIREBASE_LONGPOLL_COMMAND_CB_NAME = 'pLPCommand';
|
1575 | var FIREBASE_LONGPOLL_DATA_CB_NAME = 'pRTLPCB';
|
1576 | var FIREBASE_LONGPOLL_ID_PARAM = 'id';
|
1577 | var FIREBASE_LONGPOLL_PW_PARAM = 'pw';
|
1578 | var FIREBASE_LONGPOLL_SERIAL_PARAM = 'ser';
|
1579 | var FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = 'cb';
|
1580 | var FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = 'seg';
|
1581 | var FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = 'ts';
|
1582 | var FIREBASE_LONGPOLL_DATA_PARAM = 'd';
|
1583 | var FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = 'dframe';
|
1584 |
|
1585 |
|
1586 |
|
1587 | var MAX_URL_DATA_SIZE = 1870;
|
1588 | var SEG_HEADER_SIZE = 30;
|
1589 | var MAX_PAYLOAD_SIZE = MAX_URL_DATA_SIZE - SEG_HEADER_SIZE;
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 | var KEEPALIVE_REQUEST_INTERVAL = 25000;
|
1596 |
|
1597 |
|
1598 |
|
1599 | var LP_CONNECT_TIMEOUT = 30000;
|
1600 |
|
1601 |
|
1602 |
|
1603 | var BrowserPollConnection = (function () {
|
1604 | |
1605 |
|
1606 |
|
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 | function BrowserPollConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
|
1616 | var _this = this;
|
1617 | this.connId = connId;
|
1618 | this.repoInfo = repoInfo;
|
1619 | this.applicationId = applicationId;
|
1620 | this.appCheckToken = appCheckToken;
|
1621 | this.authToken = authToken;
|
1622 | this.transportSessionId = transportSessionId;
|
1623 | this.lastSessionId = lastSessionId;
|
1624 | this.bytesSent = 0;
|
1625 | this.bytesReceived = 0;
|
1626 | this.everConnected_ = false;
|
1627 | this.log_ = logWrapper(connId);
|
1628 | this.stats_ = statsManagerGetCollection(repoInfo);
|
1629 | this.urlFn = function (params) {
|
1630 |
|
1631 | if (_this.appCheckToken) {
|
1632 | params[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
|
1633 | }
|
1634 | return repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
|
1635 | };
|
1636 | }
|
1637 | |
1638 |
|
1639 |
|
1640 |
|
1641 | BrowserPollConnection.prototype.open = function (onMessage, onDisconnect) {
|
1642 | var _this = this;
|
1643 | this.curSegmentNum = 0;
|
1644 | this.onDisconnect_ = onDisconnect;
|
1645 | this.myPacketOrderer = new PacketReceiver(onMessage);
|
1646 | this.isClosed_ = false;
|
1647 | this.connectTimeoutTimer_ = setTimeout(function () {
|
1648 | _this.log_('Timed out trying to connect.');
|
1649 |
|
1650 | _this.onClosed_();
|
1651 | _this.connectTimeoutTimer_ = null;
|
1652 |
|
1653 | }, Math.floor(LP_CONNECT_TIMEOUT));
|
1654 |
|
1655 | executeWhenDOMReady(function () {
|
1656 | if (_this.isClosed_) {
|
1657 | return;
|
1658 | }
|
1659 |
|
1660 | _this.scriptTagHolder = new FirebaseIFrameScriptHolder(function () {
|
1661 | var args = [];
|
1662 | for (var _i = 0; _i < arguments.length; _i++) {
|
1663 | args[_i] = arguments[_i];
|
1664 | }
|
1665 | var _a = tslib.__read(args, 5), command = _a[0], arg1 = _a[1], arg2 = _a[2]; _a[3]; _a[4];
|
1666 | _this.incrementIncomingBytes_(args);
|
1667 | if (!_this.scriptTagHolder) {
|
1668 | return;
|
1669 | }
|
1670 | if (_this.connectTimeoutTimer_) {
|
1671 | clearTimeout(_this.connectTimeoutTimer_);
|
1672 | _this.connectTimeoutTimer_ = null;
|
1673 | }
|
1674 | _this.everConnected_ = true;
|
1675 | if (command === FIREBASE_LONGPOLL_START_PARAM) {
|
1676 | _this.id = arg1;
|
1677 | _this.password = arg2;
|
1678 | }
|
1679 | else if (command === FIREBASE_LONGPOLL_CLOSE_COMMAND) {
|
1680 |
|
1681 | if (arg1) {
|
1682 |
|
1683 |
|
1684 | _this.scriptTagHolder.sendNewPolls = false;
|
1685 |
|
1686 |
|
1687 | _this.myPacketOrderer.closeAfter(arg1, function () {
|
1688 | _this.onClosed_();
|
1689 | });
|
1690 | }
|
1691 | else {
|
1692 | _this.onClosed_();
|
1693 | }
|
1694 | }
|
1695 | else {
|
1696 | throw new Error('Unrecognized command received: ' + command);
|
1697 | }
|
1698 | }, function () {
|
1699 | var args = [];
|
1700 | for (var _i = 0; _i < arguments.length; _i++) {
|
1701 | args[_i] = arguments[_i];
|
1702 | }
|
1703 | var _a = tslib.__read(args, 2), pN = _a[0], data = _a[1];
|
1704 | _this.incrementIncomingBytes_(args);
|
1705 | _this.myPacketOrderer.handleResponse(pN, data);
|
1706 | }, function () {
|
1707 | _this.onClosed_();
|
1708 | }, _this.urlFn);
|
1709 |
|
1710 |
|
1711 | var urlParams = {};
|
1712 | urlParams[FIREBASE_LONGPOLL_START_PARAM] = 't';
|
1713 | urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = Math.floor(Math.random() * 100000000);
|
1714 | if (_this.scriptTagHolder.uniqueCallbackIdentifier) {
|
1715 | urlParams[FIREBASE_LONGPOLL_CALLBACK_ID_PARAM] =
|
1716 | _this.scriptTagHolder.uniqueCallbackIdentifier;
|
1717 | }
|
1718 | urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
|
1719 | if (_this.transportSessionId) {
|
1720 | urlParams[TRANSPORT_SESSION_PARAM] = _this.transportSessionId;
|
1721 | }
|
1722 | if (_this.lastSessionId) {
|
1723 | urlParams[LAST_SESSION_PARAM] = _this.lastSessionId;
|
1724 | }
|
1725 | if (_this.applicationId) {
|
1726 | urlParams[APPLICATION_ID_PARAM] = _this.applicationId;
|
1727 | }
|
1728 | if (_this.appCheckToken) {
|
1729 | urlParams[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
|
1730 | }
|
1731 | if (typeof location !== 'undefined' &&
|
1732 | location.hostname &&
|
1733 | FORGE_DOMAIN_RE.test(location.hostname)) {
|
1734 | urlParams[REFERER_PARAM] = FORGE_REF;
|
1735 | }
|
1736 | var connectURL = _this.urlFn(urlParams);
|
1737 | _this.log_('Connecting via long-poll to ' + connectURL);
|
1738 | _this.scriptTagHolder.addTag(connectURL, function () {
|
1739 |
|
1740 | });
|
1741 | });
|
1742 | };
|
1743 | |
1744 |
|
1745 |
|
1746 | BrowserPollConnection.prototype.start = function () {
|
1747 | this.scriptTagHolder.startLongPoll(this.id, this.password);
|
1748 | this.addDisconnectPingFrame(this.id, this.password);
|
1749 | };
|
1750 | |
1751 |
|
1752 |
|
1753 | BrowserPollConnection.forceAllow = function () {
|
1754 | BrowserPollConnection.forceAllow_ = true;
|
1755 | };
|
1756 | |
1757 |
|
1758 |
|
1759 | BrowserPollConnection.forceDisallow = function () {
|
1760 | BrowserPollConnection.forceDisallow_ = true;
|
1761 | };
|
1762 |
|
1763 | BrowserPollConnection.isAvailable = function () {
|
1764 | if (util.isNodeSdk()) {
|
1765 | return false;
|
1766 | }
|
1767 | else if (BrowserPollConnection.forceAllow_) {
|
1768 | return true;
|
1769 | }
|
1770 | else {
|
1771 |
|
1772 |
|
1773 | return (!BrowserPollConnection.forceDisallow_ &&
|
1774 | typeof document !== 'undefined' &&
|
1775 | document.createElement != null &&
|
1776 | !isChromeExtensionContentScript() &&
|
1777 | !isWindowsStoreApp());
|
1778 | }
|
1779 | };
|
1780 | |
1781 |
|
1782 |
|
1783 | BrowserPollConnection.prototype.markConnectionHealthy = function () { };
|
1784 | |
1785 |
|
1786 |
|
1787 | BrowserPollConnection.prototype.shutdown_ = function () {
|
1788 | this.isClosed_ = true;
|
1789 | if (this.scriptTagHolder) {
|
1790 | this.scriptTagHolder.close();
|
1791 | this.scriptTagHolder = null;
|
1792 | }
|
1793 |
|
1794 | if (this.myDisconnFrame) {
|
1795 | document.body.removeChild(this.myDisconnFrame);
|
1796 | this.myDisconnFrame = null;
|
1797 | }
|
1798 | if (this.connectTimeoutTimer_) {
|
1799 | clearTimeout(this.connectTimeoutTimer_);
|
1800 | this.connectTimeoutTimer_ = null;
|
1801 | }
|
1802 | };
|
1803 | |
1804 |
|
1805 |
|
1806 | BrowserPollConnection.prototype.onClosed_ = function () {
|
1807 | if (!this.isClosed_) {
|
1808 | this.log_('Longpoll is closing itself');
|
1809 | this.shutdown_();
|
1810 | if (this.onDisconnect_) {
|
1811 | this.onDisconnect_(this.everConnected_);
|
1812 | this.onDisconnect_ = null;
|
1813 | }
|
1814 | }
|
1815 | };
|
1816 | |
1817 |
|
1818 |
|
1819 |
|
1820 | BrowserPollConnection.prototype.close = function () {
|
1821 | if (!this.isClosed_) {
|
1822 | this.log_('Longpoll is being closed.');
|
1823 | this.shutdown_();
|
1824 | }
|
1825 | };
|
1826 | |
1827 |
|
1828 |
|
1829 |
|
1830 |
|
1831 | BrowserPollConnection.prototype.send = function (data) {
|
1832 | var dataStr = util.stringify(data);
|
1833 | this.bytesSent += dataStr.length;
|
1834 | this.stats_.incrementCounter('bytes_sent', dataStr.length);
|
1835 |
|
1836 | var base64data = util.base64Encode(dataStr);
|
1837 |
|
1838 |
|
1839 | var dataSegs = splitStringBySize(base64data, MAX_PAYLOAD_SIZE);
|
1840 |
|
1841 |
|
1842 | for (var i = 0; i < dataSegs.length; i++) {
|
1843 | this.scriptTagHolder.enqueueSegment(this.curSegmentNum, dataSegs.length, dataSegs[i]);
|
1844 | this.curSegmentNum++;
|
1845 | }
|
1846 | };
|
1847 | |
1848 |
|
1849 |
|
1850 |
|
1851 |
|
1852 | BrowserPollConnection.prototype.addDisconnectPingFrame = function (id, pw) {
|
1853 | if (util.isNodeSdk()) {
|
1854 | return;
|
1855 | }
|
1856 | this.myDisconnFrame = document.createElement('iframe');
|
1857 | var urlParams = {};
|
1858 | urlParams[FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM] = 't';
|
1859 | urlParams[FIREBASE_LONGPOLL_ID_PARAM] = id;
|
1860 | urlParams[FIREBASE_LONGPOLL_PW_PARAM] = pw;
|
1861 | this.myDisconnFrame.src = this.urlFn(urlParams);
|
1862 | this.myDisconnFrame.style.display = 'none';
|
1863 | document.body.appendChild(this.myDisconnFrame);
|
1864 | };
|
1865 | |
1866 |
|
1867 |
|
1868 | BrowserPollConnection.prototype.incrementIncomingBytes_ = function (args) {
|
1869 |
|
1870 | var bytesReceived = util.stringify(args).length;
|
1871 | this.bytesReceived += bytesReceived;
|
1872 | this.stats_.incrementCounter('bytes_received', bytesReceived);
|
1873 | };
|
1874 | return BrowserPollConnection;
|
1875 | }());
|
1876 |
|
1877 |
|
1878 |
|
1879 | var FirebaseIFrameScriptHolder = (function () {
|
1880 | |
1881 |
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 | function FirebaseIFrameScriptHolder(commandCB, onMessageCB, onDisconnect, urlFn) {
|
1887 | this.onDisconnect = onDisconnect;
|
1888 | this.urlFn = urlFn;
|
1889 |
|
1890 |
|
1891 | this.outstandingRequests = new Set();
|
1892 |
|
1893 | this.pendingSegs = [];
|
1894 |
|
1895 |
|
1896 |
|
1897 |
|
1898 |
|
1899 | this.currentSerial = Math.floor(Math.random() * 100000000);
|
1900 |
|
1901 |
|
1902 | this.sendNewPolls = true;
|
1903 | if (!util.isNodeSdk()) {
|
1904 |
|
1905 |
|
1906 |
|
1907 |
|
1908 | this.uniqueCallbackIdentifier = LUIDGenerator();
|
1909 | window[FIREBASE_LONGPOLL_COMMAND_CB_NAME + this.uniqueCallbackIdentifier] = commandCB;
|
1910 | window[FIREBASE_LONGPOLL_DATA_CB_NAME + this.uniqueCallbackIdentifier] =
|
1911 | onMessageCB;
|
1912 |
|
1913 | this.myIFrame = FirebaseIFrameScriptHolder.createIFrame_();
|
1914 |
|
1915 | var script = '';
|
1916 |
|
1917 |
|
1918 | if (this.myIFrame.src &&
|
1919 | this.myIFrame.src.substr(0, 'javascript:'.length) === 'javascript:') {
|
1920 | var currentDomain = document.domain;
|
1921 | script = '<script>document.domain="' + currentDomain + '";</script>';
|
1922 | }
|
1923 | var iframeContents = '<html><body>' + script + '</body></html>';
|
1924 | try {
|
1925 | this.myIFrame.doc.open();
|
1926 | this.myIFrame.doc.write(iframeContents);
|
1927 | this.myIFrame.doc.close();
|
1928 | }
|
1929 | catch (e) {
|
1930 | log('frame writing exception');
|
1931 | if (e.stack) {
|
1932 | log(e.stack);
|
1933 | }
|
1934 | log(e);
|
1935 | }
|
1936 | }
|
1937 | else {
|
1938 | this.commandCB = commandCB;
|
1939 | this.onMessageCB = onMessageCB;
|
1940 | }
|
1941 | }
|
1942 | |
1943 |
|
1944 |
|
1945 |
|
1946 | FirebaseIFrameScriptHolder.createIFrame_ = function () {
|
1947 | var iframe = document.createElement('iframe');
|
1948 | iframe.style.display = 'none';
|
1949 |
|
1950 | if (document.body) {
|
1951 | document.body.appendChild(iframe);
|
1952 | try {
|
1953 |
|
1954 |
|
1955 |
|
1956 | var a = iframe.contentWindow.document;
|
1957 | if (!a) {
|
1958 |
|
1959 | log('No IE domain setting required');
|
1960 | }
|
1961 | }
|
1962 | catch (e) {
|
1963 | var domain = document.domain;
|
1964 | iframe.src =
|
1965 | "javascript:void((function(){document.open();document.domain='" +
|
1966 | domain +
|
1967 | "';document.close();})())";
|
1968 | }
|
1969 | }
|
1970 | else {
|
1971 |
|
1972 |
|
1973 | throw 'Document body has not initialized. Wait to initialize Firebase until after the document is ready.';
|
1974 | }
|
1975 |
|
1976 | if (iframe.contentDocument) {
|
1977 | iframe.doc = iframe.contentDocument;
|
1978 | }
|
1979 | else if (iframe.contentWindow) {
|
1980 | iframe.doc = iframe.contentWindow.document;
|
1981 |
|
1982 | }
|
1983 | else if (iframe.document) {
|
1984 |
|
1985 | iframe.doc = iframe.document;
|
1986 | }
|
1987 | return iframe;
|
1988 | };
|
1989 | |
1990 |
|
1991 |
|
1992 | FirebaseIFrameScriptHolder.prototype.close = function () {
|
1993 | var _this = this;
|
1994 |
|
1995 | this.alive = false;
|
1996 | if (this.myIFrame) {
|
1997 |
|
1998 |
|
1999 |
|
2000 | this.myIFrame.doc.body.innerHTML = '';
|
2001 | setTimeout(function () {
|
2002 | if (_this.myIFrame !== null) {
|
2003 | document.body.removeChild(_this.myIFrame);
|
2004 | _this.myIFrame = null;
|
2005 | }
|
2006 | }, Math.floor(0));
|
2007 | }
|
2008 |
|
2009 | var onDisconnect = this.onDisconnect;
|
2010 | if (onDisconnect) {
|
2011 | this.onDisconnect = null;
|
2012 | onDisconnect();
|
2013 | }
|
2014 | };
|
2015 | |
2016 |
|
2017 |
|
2018 |
|
2019 |
|
2020 | FirebaseIFrameScriptHolder.prototype.startLongPoll = function (id, pw) {
|
2021 | this.myID = id;
|
2022 | this.myPW = pw;
|
2023 | this.alive = true;
|
2024 |
|
2025 | while (this.newRequest_()) { }
|
2026 | };
|
2027 | |
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 | FirebaseIFrameScriptHolder.prototype.newRequest_ = function () {
|
2035 |
|
2036 |
|
2037 |
|
2038 | if (this.alive &&
|
2039 | this.sendNewPolls &&
|
2040 | this.outstandingRequests.size < (this.pendingSegs.length > 0 ? 2 : 1)) {
|
2041 |
|
2042 | this.currentSerial++;
|
2043 | var urlParams = {};
|
2044 | urlParams[FIREBASE_LONGPOLL_ID_PARAM] = this.myID;
|
2045 | urlParams[FIREBASE_LONGPOLL_PW_PARAM] = this.myPW;
|
2046 | urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = this.currentSerial;
|
2047 | var theURL = this.urlFn(urlParams);
|
2048 |
|
2049 | var curDataString = '';
|
2050 | var i = 0;
|
2051 | while (this.pendingSegs.length > 0) {
|
2052 |
|
2053 | var nextSeg = this.pendingSegs[0];
|
2054 | if (nextSeg.d.length +
|
2055 | SEG_HEADER_SIZE +
|
2056 | curDataString.length <=
|
2057 | MAX_URL_DATA_SIZE) {
|
2058 |
|
2059 | var theSeg = this.pendingSegs.shift();
|
2060 | curDataString =
|
2061 | curDataString +
|
2062 | '&' +
|
2063 | FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM +
|
2064 | i +
|
2065 | '=' +
|
2066 | theSeg.seg +
|
2067 | '&' +
|
2068 | FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET +
|
2069 | i +
|
2070 | '=' +
|
2071 | theSeg.ts +
|
2072 | '&' +
|
2073 | FIREBASE_LONGPOLL_DATA_PARAM +
|
2074 | i +
|
2075 | '=' +
|
2076 | theSeg.d;
|
2077 | i++;
|
2078 | }
|
2079 | else {
|
2080 | break;
|
2081 | }
|
2082 | }
|
2083 | theURL = theURL + curDataString;
|
2084 | this.addLongPollTag_(theURL, this.currentSerial);
|
2085 | return true;
|
2086 | }
|
2087 | else {
|
2088 | return false;
|
2089 | }
|
2090 | };
|
2091 | |
2092 |
|
2093 |
|
2094 |
|
2095 |
|
2096 |
|
2097 | FirebaseIFrameScriptHolder.prototype.enqueueSegment = function (segnum, totalsegs, data) {
|
2098 |
|
2099 | this.pendingSegs.push({ seg: segnum, ts: totalsegs, d: data });
|
2100 |
|
2101 |
|
2102 | if (this.alive) {
|
2103 | this.newRequest_();
|
2104 | }
|
2105 | };
|
2106 | |
2107 |
|
2108 |
|
2109 |
|
2110 |
|
2111 | FirebaseIFrameScriptHolder.prototype.addLongPollTag_ = function (url, serial) {
|
2112 | var _this = this;
|
2113 |
|
2114 | this.outstandingRequests.add(serial);
|
2115 | var doNewRequest = function () {
|
2116 | _this.outstandingRequests.delete(serial);
|
2117 | _this.newRequest_();
|
2118 | };
|
2119 |
|
2120 |
|
2121 | var keepaliveTimeout = setTimeout(doNewRequest, Math.floor(KEEPALIVE_REQUEST_INTERVAL));
|
2122 | var readyStateCB = function () {
|
2123 |
|
2124 | clearTimeout(keepaliveTimeout);
|
2125 |
|
2126 | doNewRequest();
|
2127 | };
|
2128 | this.addTag(url, readyStateCB);
|
2129 | };
|
2130 | |
2131 |
|
2132 |
|
2133 |
|
2134 |
|
2135 | FirebaseIFrameScriptHolder.prototype.addTag = function (url, loadCB) {
|
2136 | var _this = this;
|
2137 | if (util.isNodeSdk()) {
|
2138 |
|
2139 | this.doNodeLongPoll(url, loadCB);
|
2140 | }
|
2141 | else {
|
2142 | setTimeout(function () {
|
2143 | try {
|
2144 |
|
2145 | if (!_this.sendNewPolls) {
|
2146 | return;
|
2147 | }
|
2148 | var newScript_1 = _this.myIFrame.doc.createElement('script');
|
2149 | newScript_1.type = 'text/javascript';
|
2150 | newScript_1.async = true;
|
2151 | newScript_1.src = url;
|
2152 |
|
2153 | newScript_1.onload = newScript_1.onreadystatechange =
|
2154 | function () {
|
2155 |
|
2156 | var rstate = newScript_1.readyState;
|
2157 | if (!rstate || rstate === 'loaded' || rstate === 'complete') {
|
2158 |
|
2159 | newScript_1.onload = newScript_1.onreadystatechange = null;
|
2160 | if (newScript_1.parentNode) {
|
2161 | newScript_1.parentNode.removeChild(newScript_1);
|
2162 | }
|
2163 | loadCB();
|
2164 | }
|
2165 | };
|
2166 | newScript_1.onerror = function () {
|
2167 | log('Long-poll script failed to load: ' + url);
|
2168 | _this.sendNewPolls = false;
|
2169 | _this.close();
|
2170 | };
|
2171 | _this.myIFrame.doc.body.appendChild(newScript_1);
|
2172 | }
|
2173 | catch (e) {
|
2174 |
|
2175 | }
|
2176 | }, Math.floor(1));
|
2177 | }
|
2178 | };
|
2179 | return FirebaseIFrameScriptHolder;
|
2180 | }());
|
2181 |
|
2182 |
|
2183 |
|
2184 |
|
2185 |
|
2186 |
|
2187 |
|
2188 |
|
2189 |
|
2190 |
|
2191 |
|
2192 |
|
2193 |
|
2194 |
|
2195 |
|
2196 |
|
2197 |
|
2198 |
|
2199 |
|
2200 |
|
2201 |
|
2202 |
|
2203 |
|
2204 |
|
2205 | var TransportManager = (function () {
|
2206 | |
2207 |
|
2208 |
|
2209 | function TransportManager(repoInfo) {
|
2210 | this.initTransports_(repoInfo);
|
2211 | }
|
2212 | Object.defineProperty(TransportManager, "ALL_TRANSPORTS", {
|
2213 | get: function () {
|
2214 | return [BrowserPollConnection, WebSocketConnection];
|
2215 | },
|
2216 | enumerable: false,
|
2217 | configurable: true
|
2218 | });
|
2219 | Object.defineProperty(TransportManager, "IS_TRANSPORT_INITIALIZED", {
|
2220 | |
2221 |
|
2222 |
|
2223 |
|
2224 | get: function () {
|
2225 | return this.globalTransportInitialized_;
|
2226 | },
|
2227 | enumerable: false,
|
2228 | configurable: true
|
2229 | });
|
2230 | TransportManager.prototype.initTransports_ = function (repoInfo) {
|
2231 | var e_1, _a;
|
2232 | var isWebSocketsAvailable = WebSocketConnection && WebSocketConnection['isAvailable']();
|
2233 | var isSkipPollConnection = isWebSocketsAvailable && !WebSocketConnection.previouslyFailed();
|
2234 | if (repoInfo.webSocketOnly) {
|
2235 | if (!isWebSocketsAvailable) {
|
2236 | warn("wss:// URL used, but browser isn't known to support websockets. Trying anyway.");
|
2237 | }
|
2238 | isSkipPollConnection = true;
|
2239 | }
|
2240 | if (isSkipPollConnection) {
|
2241 | this.transports_ = [WebSocketConnection];
|
2242 | }
|
2243 | else {
|
2244 | var transports = (this.transports_ = []);
|
2245 | try {
|
2246 | for (var _b = tslib.__values(TransportManager.ALL_TRANSPORTS), _c = _b.next(); !_c.done; _c = _b.next()) {
|
2247 | var transport = _c.value;
|
2248 | if (transport && transport['isAvailable']()) {
|
2249 | transports.push(transport);
|
2250 | }
|
2251 | }
|
2252 | }
|
2253 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
2254 | finally {
|
2255 | try {
|
2256 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
2257 | }
|
2258 | finally { if (e_1) throw e_1.error; }
|
2259 | }
|
2260 | TransportManager.globalTransportInitialized_ = true;
|
2261 | }
|
2262 | };
|
2263 | |
2264 |
|
2265 |
|
2266 | TransportManager.prototype.initialTransport = function () {
|
2267 | if (this.transports_.length > 0) {
|
2268 | return this.transports_[0];
|
2269 | }
|
2270 | else {
|
2271 | throw new Error('No transports available');
|
2272 | }
|
2273 | };
|
2274 | |
2275 |
|
2276 |
|
2277 | TransportManager.prototype.upgradeTransport = function () {
|
2278 | if (this.transports_.length > 1) {
|
2279 | return this.transports_[1];
|
2280 | }
|
2281 | else {
|
2282 | return null;
|
2283 | }
|
2284 | };
|
2285 |
|
2286 | TransportManager.globalTransportInitialized_ = false;
|
2287 | return TransportManager;
|
2288 | }());
|
2289 |
|
2290 |
|
2291 |
|
2292 |
|
2293 |
|
2294 |
|
2295 |
|
2296 |
|
2297 |
|
2298 |
|
2299 |
|
2300 |
|
2301 |
|
2302 |
|
2303 |
|
2304 |
|
2305 |
|
2306 |
|
2307 | var UPGRADE_TIMEOUT = 60000;
|
2308 |
|
2309 |
|
2310 | var DELAY_BEFORE_SENDING_EXTRA_REQUESTS = 5000;
|
2311 |
|
2312 |
|
2313 |
|
2314 | var BYTES_SENT_HEALTHY_OVERRIDE = 10 * 1024;
|
2315 | var BYTES_RECEIVED_HEALTHY_OVERRIDE = 100 * 1024;
|
2316 | var MESSAGE_TYPE = 't';
|
2317 | var MESSAGE_DATA = 'd';
|
2318 | var CONTROL_SHUTDOWN = 's';
|
2319 | var CONTROL_RESET = 'r';
|
2320 | var CONTROL_ERROR = 'e';
|
2321 | var CONTROL_PONG = 'o';
|
2322 | var SWITCH_ACK = 'a';
|
2323 | var END_TRANSMISSION = 'n';
|
2324 | var PING = 'p';
|
2325 | var SERVER_HELLO = 'h';
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 | var Connection = (function () {
|
2331 | |
2332 |
|
2333 |
|
2334 |
|
2335 |
|
2336 |
|
2337 |
|
2338 |
|
2339 |
|
2340 |
|
2341 |
|
2342 |
|
2343 | function Connection(id, repoInfo_, applicationId_, appCheckToken_, authToken_, onMessage_, onReady_, onDisconnect_, onKill_, lastSessionId) {
|
2344 | this.id = id;
|
2345 | this.repoInfo_ = repoInfo_;
|
2346 | this.applicationId_ = applicationId_;
|
2347 | this.appCheckToken_ = appCheckToken_;
|
2348 | this.authToken_ = authToken_;
|
2349 | this.onMessage_ = onMessage_;
|
2350 | this.onReady_ = onReady_;
|
2351 | this.onDisconnect_ = onDisconnect_;
|
2352 | this.onKill_ = onKill_;
|
2353 | this.lastSessionId = lastSessionId;
|
2354 | this.connectionCount = 0;
|
2355 | this.pendingDataMessages = [];
|
2356 | this.state_ = 0 ;
|
2357 | this.log_ = logWrapper('c:' + this.id + ':');
|
2358 | this.transportManager_ = new TransportManager(repoInfo_);
|
2359 | this.log_('Connection created');
|
2360 | this.start_();
|
2361 | }
|
2362 | |
2363 |
|
2364 |
|
2365 | Connection.prototype.start_ = function () {
|
2366 | var _this = this;
|
2367 | var conn = this.transportManager_.initialTransport();
|
2368 | this.conn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, null, this.lastSessionId);
|
2369 |
|
2370 |
|
2371 | this.primaryResponsesRequired_ = conn['responsesRequiredToBeHealthy'] || 0;
|
2372 | var onMessageReceived = this.connReceiver_(this.conn_);
|
2373 | var onConnectionLost = this.disconnReceiver_(this.conn_);
|
2374 | this.tx_ = this.conn_;
|
2375 | this.rx_ = this.conn_;
|
2376 | this.secondaryConn_ = null;
|
2377 | this.isHealthy_ = false;
|
2378 | |
2379 |
|
2380 |
|
2381 |
|
2382 |
|
2383 |
|
2384 | setTimeout(function () {
|
2385 |
|
2386 | _this.conn_ && _this.conn_.open(onMessageReceived, onConnectionLost);
|
2387 | }, Math.floor(0));
|
2388 | var healthyTimeoutMS = conn['healthyTimeout'] || 0;
|
2389 | if (healthyTimeoutMS > 0) {
|
2390 | this.healthyTimeout_ = setTimeoutNonBlocking(function () {
|
2391 | _this.healthyTimeout_ = null;
|
2392 | if (!_this.isHealthy_) {
|
2393 | if (_this.conn_ &&
|
2394 | _this.conn_.bytesReceived > BYTES_RECEIVED_HEALTHY_OVERRIDE) {
|
2395 | _this.log_('Connection exceeded healthy timeout but has received ' +
|
2396 | _this.conn_.bytesReceived +
|
2397 | ' bytes. Marking connection healthy.');
|
2398 | _this.isHealthy_ = true;
|
2399 | _this.conn_.markConnectionHealthy();
|
2400 | }
|
2401 | else if (_this.conn_ &&
|
2402 | _this.conn_.bytesSent > BYTES_SENT_HEALTHY_OVERRIDE) {
|
2403 | _this.log_('Connection exceeded healthy timeout but has sent ' +
|
2404 | _this.conn_.bytesSent +
|
2405 | ' bytes. Leaving connection alive.');
|
2406 |
|
2407 |
|
2408 | }
|
2409 | else {
|
2410 | _this.log_('Closing unhealthy connection after timeout.');
|
2411 | _this.close();
|
2412 | }
|
2413 | }
|
2414 |
|
2415 | }, Math.floor(healthyTimeoutMS));
|
2416 | }
|
2417 | };
|
2418 | Connection.prototype.nextTransportId_ = function () {
|
2419 | return 'c:' + this.id + ':' + this.connectionCount++;
|
2420 | };
|
2421 | Connection.prototype.disconnReceiver_ = function (conn) {
|
2422 | var _this = this;
|
2423 | return function (everConnected) {
|
2424 | if (conn === _this.conn_) {
|
2425 | _this.onConnectionLost_(everConnected);
|
2426 | }
|
2427 | else if (conn === _this.secondaryConn_) {
|
2428 | _this.log_('Secondary connection lost.');
|
2429 | _this.onSecondaryConnectionLost_();
|
2430 | }
|
2431 | else {
|
2432 | _this.log_('closing an old connection');
|
2433 | }
|
2434 | };
|
2435 | };
|
2436 | Connection.prototype.connReceiver_ = function (conn) {
|
2437 | var _this = this;
|
2438 | return function (message) {
|
2439 | if (_this.state_ !== 2 ) {
|
2440 | if (conn === _this.rx_) {
|
2441 | _this.onPrimaryMessageReceived_(message);
|
2442 | }
|
2443 | else if (conn === _this.secondaryConn_) {
|
2444 | _this.onSecondaryMessageReceived_(message);
|
2445 | }
|
2446 | else {
|
2447 | _this.log_('message on old connection');
|
2448 | }
|
2449 | }
|
2450 | };
|
2451 | };
|
2452 | |
2453 |
|
2454 |
|
2455 | Connection.prototype.sendRequest = function (dataMsg) {
|
2456 |
|
2457 | var msg = { t: 'd', d: dataMsg };
|
2458 | this.sendData_(msg);
|
2459 | };
|
2460 | Connection.prototype.tryCleanupConnection = function () {
|
2461 | if (this.tx_ === this.secondaryConn_ && this.rx_ === this.secondaryConn_) {
|
2462 | this.log_('cleaning up and promoting a connection: ' + this.secondaryConn_.connId);
|
2463 | this.conn_ = this.secondaryConn_;
|
2464 | this.secondaryConn_ = null;
|
2465 |
|
2466 | }
|
2467 | };
|
2468 | Connection.prototype.onSecondaryControl_ = function (controlData) {
|
2469 | if (MESSAGE_TYPE in controlData) {
|
2470 | var cmd = controlData[MESSAGE_TYPE];
|
2471 | if (cmd === SWITCH_ACK) {
|
2472 | this.upgradeIfSecondaryHealthy_();
|
2473 | }
|
2474 | else if (cmd === CONTROL_RESET) {
|
2475 |
|
2476 | this.log_('Got a reset on secondary, closing it');
|
2477 | this.secondaryConn_.close();
|
2478 |
|
2479 | if (this.tx_ === this.secondaryConn_ ||
|
2480 | this.rx_ === this.secondaryConn_) {
|
2481 | this.close();
|
2482 | }
|
2483 | }
|
2484 | else if (cmd === CONTROL_PONG) {
|
2485 | this.log_('got pong on secondary.');
|
2486 | this.secondaryResponsesRequired_--;
|
2487 | this.upgradeIfSecondaryHealthy_();
|
2488 | }
|
2489 | }
|
2490 | };
|
2491 | Connection.prototype.onSecondaryMessageReceived_ = function (parsedData) {
|
2492 | var layer = requireKey('t', parsedData);
|
2493 | var data = requireKey('d', parsedData);
|
2494 | if (layer === 'c') {
|
2495 | this.onSecondaryControl_(data);
|
2496 | }
|
2497 | else if (layer === 'd') {
|
2498 |
|
2499 | this.pendingDataMessages.push(data);
|
2500 | }
|
2501 | else {
|
2502 | throw new Error('Unknown protocol layer: ' + layer);
|
2503 | }
|
2504 | };
|
2505 | Connection.prototype.upgradeIfSecondaryHealthy_ = function () {
|
2506 | if (this.secondaryResponsesRequired_ <= 0) {
|
2507 | this.log_('Secondary connection is healthy.');
|
2508 | this.isHealthy_ = true;
|
2509 | this.secondaryConn_.markConnectionHealthy();
|
2510 | this.proceedWithUpgrade_();
|
2511 | }
|
2512 | else {
|
2513 |
|
2514 | this.log_('sending ping on secondary.');
|
2515 | this.secondaryConn_.send({ t: 'c', d: { t: PING, d: {} } });
|
2516 | }
|
2517 | };
|
2518 | Connection.prototype.proceedWithUpgrade_ = function () {
|
2519 |
|
2520 | this.secondaryConn_.start();
|
2521 |
|
2522 | this.log_('sending client ack on secondary');
|
2523 | this.secondaryConn_.send({ t: 'c', d: { t: SWITCH_ACK, d: {} } });
|
2524 |
|
2525 |
|
2526 | this.log_('Ending transmission on primary');
|
2527 | this.conn_.send({ t: 'c', d: { t: END_TRANSMISSION, d: {} } });
|
2528 | this.tx_ = this.secondaryConn_;
|
2529 | this.tryCleanupConnection();
|
2530 | };
|
2531 | Connection.prototype.onPrimaryMessageReceived_ = function (parsedData) {
|
2532 |
|
2533 | var layer = requireKey('t', parsedData);
|
2534 | var data = requireKey('d', parsedData);
|
2535 | if (layer === 'c') {
|
2536 | this.onControl_(data);
|
2537 | }
|
2538 | else if (layer === 'd') {
|
2539 | this.onDataMessage_(data);
|
2540 | }
|
2541 | };
|
2542 | Connection.prototype.onDataMessage_ = function (message) {
|
2543 | this.onPrimaryResponse_();
|
2544 |
|
2545 | this.onMessage_(message);
|
2546 | };
|
2547 | Connection.prototype.onPrimaryResponse_ = function () {
|
2548 | if (!this.isHealthy_) {
|
2549 | this.primaryResponsesRequired_--;
|
2550 | if (this.primaryResponsesRequired_ <= 0) {
|
2551 | this.log_('Primary connection is healthy.');
|
2552 | this.isHealthy_ = true;
|
2553 | this.conn_.markConnectionHealthy();
|
2554 | }
|
2555 | }
|
2556 | };
|
2557 | Connection.prototype.onControl_ = function (controlData) {
|
2558 | var cmd = requireKey(MESSAGE_TYPE, controlData);
|
2559 | if (MESSAGE_DATA in controlData) {
|
2560 | var payload = controlData[MESSAGE_DATA];
|
2561 | if (cmd === SERVER_HELLO) {
|
2562 | this.onHandshake_(payload);
|
2563 | }
|
2564 | else if (cmd === END_TRANSMISSION) {
|
2565 | this.log_('recvd end transmission on primary');
|
2566 | this.rx_ = this.secondaryConn_;
|
2567 | for (var i = 0; i < this.pendingDataMessages.length; ++i) {
|
2568 | this.onDataMessage_(this.pendingDataMessages[i]);
|
2569 | }
|
2570 | this.pendingDataMessages = [];
|
2571 | this.tryCleanupConnection();
|
2572 | }
|
2573 | else if (cmd === CONTROL_SHUTDOWN) {
|
2574 |
|
2575 |
|
2576 | this.onConnectionShutdown_(payload);
|
2577 | }
|
2578 | else if (cmd === CONTROL_RESET) {
|
2579 |
|
2580 | this.onReset_(payload);
|
2581 | }
|
2582 | else if (cmd === CONTROL_ERROR) {
|
2583 | error('Server Error: ' + payload);
|
2584 | }
|
2585 | else if (cmd === CONTROL_PONG) {
|
2586 | this.log_('got pong on primary.');
|
2587 | this.onPrimaryResponse_();
|
2588 | this.sendPingOnPrimaryIfNecessary_();
|
2589 | }
|
2590 | else {
|
2591 | error('Unknown control packet command: ' + cmd);
|
2592 | }
|
2593 | }
|
2594 | };
|
2595 | |
2596 |
|
2597 |
|
2598 | Connection.prototype.onHandshake_ = function (handshake) {
|
2599 | var timestamp = handshake.ts;
|
2600 | var version = handshake.v;
|
2601 | var host = handshake.h;
|
2602 | this.sessionId = handshake.s;
|
2603 | this.repoInfo_.host = host;
|
2604 |
|
2605 | if (this.state_ === 0 ) {
|
2606 | this.conn_.start();
|
2607 | this.onConnectionEstablished_(this.conn_, timestamp);
|
2608 | if (PROTOCOL_VERSION !== version) {
|
2609 | warn('Protocol version mismatch detected');
|
2610 | }
|
2611 |
|
2612 | this.tryStartUpgrade_();
|
2613 | }
|
2614 | };
|
2615 | Connection.prototype.tryStartUpgrade_ = function () {
|
2616 | var conn = this.transportManager_.upgradeTransport();
|
2617 | if (conn) {
|
2618 | this.startUpgrade_(conn);
|
2619 | }
|
2620 | };
|
2621 | Connection.prototype.startUpgrade_ = function (conn) {
|
2622 | var _this = this;
|
2623 | this.secondaryConn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, this.sessionId);
|
2624 |
|
2625 |
|
2626 | this.secondaryResponsesRequired_ =
|
2627 | conn['responsesRequiredToBeHealthy'] || 0;
|
2628 | var onMessage = this.connReceiver_(this.secondaryConn_);
|
2629 | var onDisconnect = this.disconnReceiver_(this.secondaryConn_);
|
2630 | this.secondaryConn_.open(onMessage, onDisconnect);
|
2631 |
|
2632 | setTimeoutNonBlocking(function () {
|
2633 | if (_this.secondaryConn_) {
|
2634 | _this.log_('Timed out trying to upgrade.');
|
2635 | _this.secondaryConn_.close();
|
2636 | }
|
2637 | }, Math.floor(UPGRADE_TIMEOUT));
|
2638 | };
|
2639 | Connection.prototype.onReset_ = function (host) {
|
2640 | this.log_('Reset packet received. New host: ' + host);
|
2641 | this.repoInfo_.host = host;
|
2642 |
|
2643 |
|
2644 | if (this.state_ === 1 ) {
|
2645 | this.close();
|
2646 | }
|
2647 | else {
|
2648 |
|
2649 | this.closeConnections_();
|
2650 | this.start_();
|
2651 | }
|
2652 | };
|
2653 | Connection.prototype.onConnectionEstablished_ = function (conn, timestamp) {
|
2654 | var _this = this;
|
2655 | this.log_('Realtime connection established.');
|
2656 | this.conn_ = conn;
|
2657 | this.state_ = 1 ;
|
2658 | if (this.onReady_) {
|
2659 | this.onReady_(timestamp, this.sessionId);
|
2660 | this.onReady_ = null;
|
2661 | }
|
2662 |
|
2663 |
|
2664 | if (this.primaryResponsesRequired_ === 0) {
|
2665 | this.log_('Primary connection is healthy.');
|
2666 | this.isHealthy_ = true;
|
2667 | }
|
2668 | else {
|
2669 | setTimeoutNonBlocking(function () {
|
2670 | _this.sendPingOnPrimaryIfNecessary_();
|
2671 | }, Math.floor(DELAY_BEFORE_SENDING_EXTRA_REQUESTS));
|
2672 | }
|
2673 | };
|
2674 | Connection.prototype.sendPingOnPrimaryIfNecessary_ = function () {
|
2675 |
|
2676 | if (!this.isHealthy_ && this.state_ === 1 ) {
|
2677 | this.log_('sending ping on primary.');
|
2678 | this.sendData_({ t: 'c', d: { t: PING, d: {} } });
|
2679 | }
|
2680 | };
|
2681 | Connection.prototype.onSecondaryConnectionLost_ = function () {
|
2682 | var conn = this.secondaryConn_;
|
2683 | this.secondaryConn_ = null;
|
2684 | if (this.tx_ === conn || this.rx_ === conn) {
|
2685 |
|
2686 | this.close();
|
2687 | }
|
2688 | };
|
2689 | |
2690 |
|
2691 |
|
2692 |
|
2693 | Connection.prototype.onConnectionLost_ = function (everConnected) {
|
2694 | this.conn_ = null;
|
2695 |
|
2696 |
|
2697 | if (!everConnected && this.state_ === 0 ) {
|
2698 | this.log_('Realtime connection failed.');
|
2699 |
|
2700 | if (this.repoInfo_.isCacheableHost()) {
|
2701 | PersistentStorage.remove('host:' + this.repoInfo_.host);
|
2702 |
|
2703 | this.repoInfo_.internalHost = this.repoInfo_.host;
|
2704 | }
|
2705 | }
|
2706 | else if (this.state_ === 1 ) {
|
2707 | this.log_('Realtime connection lost.');
|
2708 | }
|
2709 | this.close();
|
2710 | };
|
2711 | Connection.prototype.onConnectionShutdown_ = function (reason) {
|
2712 | this.log_('Connection shutdown command received. Shutting down...');
|
2713 | if (this.onKill_) {
|
2714 | this.onKill_(reason);
|
2715 | this.onKill_ = null;
|
2716 | }
|
2717 |
|
2718 |
|
2719 | this.onDisconnect_ = null;
|
2720 | this.close();
|
2721 | };
|
2722 | Connection.prototype.sendData_ = function (data) {
|
2723 | if (this.state_ !== 1 ) {
|
2724 | throw 'Connection is not connected';
|
2725 | }
|
2726 | else {
|
2727 | this.tx_.send(data);
|
2728 | }
|
2729 | };
|
2730 | |
2731 |
|
2732 |
|
2733 | Connection.prototype.close = function () {
|
2734 | if (this.state_ !== 2 ) {
|
2735 | this.log_('Closing realtime connection.');
|
2736 | this.state_ = 2 ;
|
2737 | this.closeConnections_();
|
2738 | if (this.onDisconnect_) {
|
2739 | this.onDisconnect_();
|
2740 | this.onDisconnect_ = null;
|
2741 | }
|
2742 | }
|
2743 | };
|
2744 | Connection.prototype.closeConnections_ = function () {
|
2745 | this.log_('Shutting down all connections');
|
2746 | if (this.conn_) {
|
2747 | this.conn_.close();
|
2748 | this.conn_ = null;
|
2749 | }
|
2750 | if (this.secondaryConn_) {
|
2751 | this.secondaryConn_.close();
|
2752 | this.secondaryConn_ = null;
|
2753 | }
|
2754 | if (this.healthyTimeout_) {
|
2755 | clearTimeout(this.healthyTimeout_);
|
2756 | this.healthyTimeout_ = null;
|
2757 | }
|
2758 | };
|
2759 | return Connection;
|
2760 | }());
|
2761 |
|
2762 |
|
2763 |
|
2764 |
|
2765 |
|
2766 |
|
2767 |
|
2768 |
|
2769 |
|
2770 |
|
2771 |
|
2772 |
|
2773 |
|
2774 |
|
2775 |
|
2776 |
|
2777 |
|
2778 |
|
2779 |
|
2780 |
|
2781 |
|
2782 |
|
2783 |
|
2784 | var ServerActions = (function () {
|
2785 | function ServerActions() {
|
2786 | }
|
2787 | ServerActions.prototype.put = function (pathString, data, onComplete, hash) { };
|
2788 | ServerActions.prototype.merge = function (pathString, data, onComplete, hash) { };
|
2789 | |
2790 |
|
2791 |
|
2792 |
|
2793 | ServerActions.prototype.refreshAuthToken = function (token) { };
|
2794 | |
2795 |
|
2796 |
|
2797 |
|
2798 | ServerActions.prototype.refreshAppCheckToken = function (token) { };
|
2799 | ServerActions.prototype.onDisconnectPut = function (pathString, data, onComplete) { };
|
2800 | ServerActions.prototype.onDisconnectMerge = function (pathString, data, onComplete) { };
|
2801 | ServerActions.prototype.onDisconnectCancel = function (pathString, onComplete) { };
|
2802 | ServerActions.prototype.reportStats = function (stats) { };
|
2803 | return ServerActions;
|
2804 | }());
|
2805 |
|
2806 |
|
2807 |
|
2808 |
|
2809 |
|
2810 |
|
2811 |
|
2812 |
|
2813 |
|
2814 |
|
2815 |
|
2816 |
|
2817 |
|
2818 |
|
2819 |
|
2820 |
|
2821 |
|
2822 |
|
2823 |
|
2824 |
|
2825 |
|
2826 | var EventEmitter = (function () {
|
2827 | function EventEmitter(allowedEvents_) {
|
2828 | this.allowedEvents_ = allowedEvents_;
|
2829 | this.listeners_ = {};
|
2830 | util.assert(Array.isArray(allowedEvents_) && allowedEvents_.length > 0, 'Requires a non-empty array');
|
2831 | }
|
2832 | |
2833 |
|
2834 |
|
2835 | EventEmitter.prototype.trigger = function (eventType) {
|
2836 | var varArgs = [];
|
2837 | for (var _i = 1; _i < arguments.length; _i++) {
|
2838 | varArgs[_i - 1] = arguments[_i];
|
2839 | }
|
2840 | if (Array.isArray(this.listeners_[eventType])) {
|
2841 |
|
2842 | var listeners = tslib.__spreadArray([], tslib.__read(this.listeners_[eventType]));
|
2843 | for (var i = 0; i < listeners.length; i++) {
|
2844 | listeners[i].callback.apply(listeners[i].context, varArgs);
|
2845 | }
|
2846 | }
|
2847 | };
|
2848 | EventEmitter.prototype.on = function (eventType, callback, context) {
|
2849 | this.validateEventType_(eventType);
|
2850 | this.listeners_[eventType] = this.listeners_[eventType] || [];
|
2851 | this.listeners_[eventType].push({ callback: callback, context: context });
|
2852 | var eventData = this.getInitialEvent(eventType);
|
2853 | if (eventData) {
|
2854 | callback.apply(context, eventData);
|
2855 | }
|
2856 | };
|
2857 | EventEmitter.prototype.off = function (eventType, callback, context) {
|
2858 | this.validateEventType_(eventType);
|
2859 | var listeners = this.listeners_[eventType] || [];
|
2860 | for (var i = 0; i < listeners.length; i++) {
|
2861 | if (listeners[i].callback === callback &&
|
2862 | (!context || context === listeners[i].context)) {
|
2863 | listeners.splice(i, 1);
|
2864 | return;
|
2865 | }
|
2866 | }
|
2867 | };
|
2868 | EventEmitter.prototype.validateEventType_ = function (eventType) {
|
2869 | util.assert(this.allowedEvents_.find(function (et) {
|
2870 | return et === eventType;
|
2871 | }), 'Unknown event: ' + eventType);
|
2872 | };
|
2873 | return EventEmitter;
|
2874 | }());
|
2875 |
|
2876 |
|
2877 |
|
2878 |
|
2879 |
|
2880 |
|
2881 |
|
2882 |
|
2883 |
|
2884 |
|
2885 |
|
2886 |
|
2887 |
|
2888 |
|
2889 |
|
2890 |
|
2891 |
|
2892 |
|
2893 |
|
2894 |
|
2895 |
|
2896 |
|
2897 |
|
2898 |
|
2899 | var OnlineMonitor = (function (_super) {
|
2900 | tslib.__extends(OnlineMonitor, _super);
|
2901 | function OnlineMonitor() {
|
2902 | var _this = _super.call(this, ['online']) || this;
|
2903 | _this.online_ = true;
|
2904 |
|
2905 |
|
2906 |
|
2907 |
|
2908 | if (typeof window !== 'undefined' &&
|
2909 | typeof window.addEventListener !== 'undefined' &&
|
2910 | !util.isMobileCordova()) {
|
2911 | window.addEventListener('online', function () {
|
2912 | if (!_this.online_) {
|
2913 | _this.online_ = true;
|
2914 | _this.trigger('online', true);
|
2915 | }
|
2916 | }, false);
|
2917 | window.addEventListener('offline', function () {
|
2918 | if (_this.online_) {
|
2919 | _this.online_ = false;
|
2920 | _this.trigger('online', false);
|
2921 | }
|
2922 | }, false);
|
2923 | }
|
2924 | return _this;
|
2925 | }
|
2926 | OnlineMonitor.getInstance = function () {
|
2927 | return new OnlineMonitor();
|
2928 | };
|
2929 | OnlineMonitor.prototype.getInitialEvent = function (eventType) {
|
2930 | util.assert(eventType === 'online', 'Unknown event type: ' + eventType);
|
2931 | return [this.online_];
|
2932 | };
|
2933 | OnlineMonitor.prototype.currentlyOnline = function () {
|
2934 | return this.online_;
|
2935 | };
|
2936 | return OnlineMonitor;
|
2937 | }(EventEmitter));
|
2938 |
|
2939 |
|
2940 |
|
2941 |
|
2942 |
|
2943 |
|
2944 |
|
2945 |
|
2946 |
|
2947 |
|
2948 |
|
2949 |
|
2950 |
|
2951 |
|
2952 |
|
2953 |
|
2954 |
|
2955 |
|
2956 | var MAX_PATH_DEPTH = 32;
|
2957 |
|
2958 | var MAX_PATH_LENGTH_BYTES = 768;
|
2959 |
|
2960 |
|
2961 |
|
2962 |
|
2963 |
|
2964 | var Path = (function () {
|
2965 | |
2966 |
|
2967 |
|
2968 |
|
2969 | function Path(pathOrString, pieceNum) {
|
2970 | if (pieceNum === void 0) {
|
2971 | this.pieces_ = pathOrString.split('/');
|
2972 |
|
2973 | var copyTo = 0;
|
2974 | for (var i = 0; i < this.pieces_.length; i++) {
|
2975 | if (this.pieces_[i].length > 0) {
|
2976 | this.pieces_[copyTo] = this.pieces_[i];
|
2977 | copyTo++;
|
2978 | }
|
2979 | }
|
2980 | this.pieces_.length = copyTo;
|
2981 | this.pieceNum_ = 0;
|
2982 | }
|
2983 | else {
|
2984 | this.pieces_ = pathOrString;
|
2985 | this.pieceNum_ = pieceNum;
|
2986 | }
|
2987 | }
|
2988 | Path.prototype.toString = function () {
|
2989 | var pathString = '';
|
2990 | for (var i = this.pieceNum_; i < this.pieces_.length; i++) {
|
2991 | if (this.pieces_[i] !== '') {
|
2992 | pathString += '/' + this.pieces_[i];
|
2993 | }
|
2994 | }
|
2995 | return pathString || '/';
|
2996 | };
|
2997 | return Path;
|
2998 | }());
|
2999 | function newEmptyPath() {
|
3000 | return new Path('');
|
3001 | }
|
3002 | function pathGetFront(path) {
|
3003 | if (path.pieceNum_ >= path.pieces_.length) {
|
3004 | return null;
|
3005 | }
|
3006 | return path.pieces_[path.pieceNum_];
|
3007 | }
|
3008 |
|
3009 |
|
3010 |
|
3011 | function pathGetLength(path) {
|
3012 | return path.pieces_.length - path.pieceNum_;
|
3013 | }
|
3014 | function pathPopFront(path) {
|
3015 | var pieceNum = path.pieceNum_;
|
3016 | if (pieceNum < path.pieces_.length) {
|
3017 | pieceNum++;
|
3018 | }
|
3019 | return new Path(path.pieces_, pieceNum);
|
3020 | }
|
3021 | function pathGetBack(path) {
|
3022 | if (path.pieceNum_ < path.pieces_.length) {
|
3023 | return path.pieces_[path.pieces_.length - 1];
|
3024 | }
|
3025 | return null;
|
3026 | }
|
3027 | function pathToUrlEncodedString(path) {
|
3028 | var pathString = '';
|
3029 | for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
|
3030 | if (path.pieces_[i] !== '') {
|
3031 | pathString += '/' + encodeURIComponent(String(path.pieces_[i]));
|
3032 | }
|
3033 | }
|
3034 | return pathString || '/';
|
3035 | }
|
3036 |
|
3037 |
|
3038 |
|
3039 |
|
3040 | function pathSlice(path, begin) {
|
3041 | if (begin === void 0) { begin = 0; }
|
3042 | return path.pieces_.slice(path.pieceNum_ + begin);
|
3043 | }
|
3044 | function pathParent(path) {
|
3045 | if (path.pieceNum_ >= path.pieces_.length) {
|
3046 | return null;
|
3047 | }
|
3048 | var pieces = [];
|
3049 | for (var i = path.pieceNum_; i < path.pieces_.length - 1; i++) {
|
3050 | pieces.push(path.pieces_[i]);
|
3051 | }
|
3052 | return new Path(pieces, 0);
|
3053 | }
|
3054 | function pathChild(path, childPathObj) {
|
3055 | var pieces = [];
|
3056 | for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
|
3057 | pieces.push(path.pieces_[i]);
|
3058 | }
|
3059 | if (childPathObj instanceof Path) {
|
3060 | for (var i = childPathObj.pieceNum_; i < childPathObj.pieces_.length; i++) {
|
3061 | pieces.push(childPathObj.pieces_[i]);
|
3062 | }
|
3063 | }
|
3064 | else {
|
3065 | var childPieces = childPathObj.split('/');
|
3066 | for (var i = 0; i < childPieces.length; i++) {
|
3067 | if (childPieces[i].length > 0) {
|
3068 | pieces.push(childPieces[i]);
|
3069 | }
|
3070 | }
|
3071 | }
|
3072 | return new Path(pieces, 0);
|
3073 | }
|
3074 |
|
3075 |
|
3076 |
|
3077 | function pathIsEmpty(path) {
|
3078 | return path.pieceNum_ >= path.pieces_.length;
|
3079 | }
|
3080 |
|
3081 |
|
3082 |
|
3083 | function newRelativePath(outerPath, innerPath) {
|
3084 | var outer = pathGetFront(outerPath), inner = pathGetFront(innerPath);
|
3085 | if (outer === null) {
|
3086 | return innerPath;
|
3087 | }
|
3088 | else if (outer === inner) {
|
3089 | return newRelativePath(pathPopFront(outerPath), pathPopFront(innerPath));
|
3090 | }
|
3091 | else {
|
3092 | throw new Error('INTERNAL ERROR: innerPath (' +
|
3093 | innerPath +
|
3094 | ') is not within ' +
|
3095 | 'outerPath (' +
|
3096 | outerPath +
|
3097 | ')');
|
3098 | }
|
3099 | }
|
3100 |
|
3101 |
|
3102 |
|
3103 | function pathCompare(left, right) {
|
3104 | var leftKeys = pathSlice(left, 0);
|
3105 | var rightKeys = pathSlice(right, 0);
|
3106 | for (var i = 0; i < leftKeys.length && i < rightKeys.length; i++) {
|
3107 | var cmp = nameCompare(leftKeys[i], rightKeys[i]);
|
3108 | if (cmp !== 0) {
|
3109 | return cmp;
|
3110 | }
|
3111 | }
|
3112 | if (leftKeys.length === rightKeys.length) {
|
3113 | return 0;
|
3114 | }
|
3115 | return leftKeys.length < rightKeys.length ? -1 : 1;
|
3116 | }
|
3117 |
|
3118 |
|
3119 |
|
3120 | function pathEquals(path, other) {
|
3121 | if (pathGetLength(path) !== pathGetLength(other)) {
|
3122 | return false;
|
3123 | }
|
3124 | for (var i = path.pieceNum_, j = other.pieceNum_; i <= path.pieces_.length; i++, j++) {
|
3125 | if (path.pieces_[i] !== other.pieces_[j]) {
|
3126 | return false;
|
3127 | }
|
3128 | }
|
3129 | return true;
|
3130 | }
|
3131 |
|
3132 |
|
3133 |
|
3134 | function pathContains(path, other) {
|
3135 | var i = path.pieceNum_;
|
3136 | var j = other.pieceNum_;
|
3137 | if (pathGetLength(path) > pathGetLength(other)) {
|
3138 | return false;
|
3139 | }
|
3140 | while (i < path.pieces_.length) {
|
3141 | if (path.pieces_[i] !== other.pieces_[j]) {
|
3142 | return false;
|
3143 | }
|
3144 | ++i;
|
3145 | ++j;
|
3146 | }
|
3147 | return true;
|
3148 | }
|
3149 |
|
3150 |
|
3151 |
|
3152 |
|
3153 |
|
3154 |
|
3155 |
|
3156 |
|
3157 |
|
3158 |
|
3159 | var ValidationPath = (function () {
|
3160 | |
3161 |
|
3162 |
|
3163 |
|
3164 | function ValidationPath(path, errorPrefix_) {
|
3165 | this.errorPrefix_ = errorPrefix_;
|
3166 | this.parts_ = pathSlice(path, 0);
|
3167 |
|
3168 | this.byteLength_ = Math.max(1, this.parts_.length);
|
3169 | for (var i = 0; i < this.parts_.length; i++) {
|
3170 | this.byteLength_ += util.stringLength(this.parts_[i]);
|
3171 | }
|
3172 | validationPathCheckValid(this);
|
3173 | }
|
3174 | return ValidationPath;
|
3175 | }());
|
3176 | function validationPathPush(validationPath, child) {
|
3177 |
|
3178 | if (validationPath.parts_.length > 0) {
|
3179 | validationPath.byteLength_ += 1;
|
3180 | }
|
3181 | validationPath.parts_.push(child);
|
3182 | validationPath.byteLength_ += util.stringLength(child);
|
3183 | validationPathCheckValid(validationPath);
|
3184 | }
|
3185 | function validationPathPop(validationPath) {
|
3186 | var last = validationPath.parts_.pop();
|
3187 | validationPath.byteLength_ -= util.stringLength(last);
|
3188 |
|
3189 | if (validationPath.parts_.length > 0) {
|
3190 | validationPath.byteLength_ -= 1;
|
3191 | }
|
3192 | }
|
3193 | function validationPathCheckValid(validationPath) {
|
3194 | if (validationPath.byteLength_ > MAX_PATH_LENGTH_BYTES) {
|
3195 | throw new Error(validationPath.errorPrefix_ +
|
3196 | 'has a key path longer than ' +
|
3197 | MAX_PATH_LENGTH_BYTES +
|
3198 | ' bytes (' +
|
3199 | validationPath.byteLength_ +
|
3200 | ').');
|
3201 | }
|
3202 | if (validationPath.parts_.length > MAX_PATH_DEPTH) {
|
3203 | throw new Error(validationPath.errorPrefix_ +
|
3204 | 'path specified exceeds the maximum depth that can be written (' +
|
3205 | MAX_PATH_DEPTH +
|
3206 | ') or object contains a cycle ' +
|
3207 | validationPathToErrorString(validationPath));
|
3208 | }
|
3209 | }
|
3210 |
|
3211 |
|
3212 |
|
3213 | function validationPathToErrorString(validationPath) {
|
3214 | if (validationPath.parts_.length === 0) {
|
3215 | return '';
|
3216 | }
|
3217 | return "in property '" + validationPath.parts_.join('.') + "'";
|
3218 | }
|
3219 |
|
3220 |
|
3221 |
|
3222 |
|
3223 |
|
3224 |
|
3225 |
|
3226 |
|
3227 |
|
3228 |
|
3229 |
|
3230 |
|
3231 |
|
3232 |
|
3233 |
|
3234 |
|
3235 |
|
3236 | var VisibilityMonitor = (function (_super) {
|
3237 | tslib.__extends(VisibilityMonitor, _super);
|
3238 | function VisibilityMonitor() {
|
3239 | var _this = _super.call(this, ['visible']) || this;
|
3240 | var hidden;
|
3241 | var visibilityChange;
|
3242 | if (typeof document !== 'undefined' &&
|
3243 | typeof document.addEventListener !== 'undefined') {
|
3244 | if (typeof document['hidden'] !== 'undefined') {
|
3245 |
|
3246 | visibilityChange = 'visibilitychange';
|
3247 | hidden = 'hidden';
|
3248 | }
|
3249 | else if (typeof document['mozHidden'] !== 'undefined') {
|
3250 | visibilityChange = 'mozvisibilitychange';
|
3251 | hidden = 'mozHidden';
|
3252 | }
|
3253 | else if (typeof document['msHidden'] !== 'undefined') {
|
3254 | visibilityChange = 'msvisibilitychange';
|
3255 | hidden = 'msHidden';
|
3256 | }
|
3257 | else if (typeof document['webkitHidden'] !== 'undefined') {
|
3258 | visibilityChange = 'webkitvisibilitychange';
|
3259 | hidden = 'webkitHidden';
|
3260 | }
|
3261 | }
|
3262 |
|
3263 |
|
3264 |
|
3265 |
|
3266 | _this.visible_ = true;
|
3267 | if (visibilityChange) {
|
3268 | document.addEventListener(visibilityChange, function () {
|
3269 | var visible = !document[hidden];
|
3270 | if (visible !== _this.visible_) {
|
3271 | _this.visible_ = visible;
|
3272 | _this.trigger('visible', visible);
|
3273 | }
|
3274 | }, false);
|
3275 | }
|
3276 | return _this;
|
3277 | }
|
3278 | VisibilityMonitor.getInstance = function () {
|
3279 | return new VisibilityMonitor();
|
3280 | };
|
3281 | VisibilityMonitor.prototype.getInitialEvent = function (eventType) {
|
3282 | util.assert(eventType === 'visible', 'Unknown event type: ' + eventType);
|
3283 | return [this.visible_];
|
3284 | };
|
3285 | return VisibilityMonitor;
|
3286 | }(EventEmitter));
|
3287 |
|
3288 |
|
3289 |
|
3290 |
|
3291 |
|
3292 |
|
3293 |
|
3294 |
|
3295 |
|
3296 |
|
3297 |
|
3298 |
|
3299 |
|
3300 |
|
3301 |
|
3302 |
|
3303 |
|
3304 | var RECONNECT_MIN_DELAY = 1000;
|
3305 | var RECONNECT_MAX_DELAY_DEFAULT = 60 * 5 * 1000;
|
3306 | var GET_CONNECT_TIMEOUT = 3 * 1000;
|
3307 | var RECONNECT_MAX_DELAY_FOR_ADMINS = 30 * 1000;
|
3308 | var RECONNECT_DELAY_MULTIPLIER = 1.3;
|
3309 | var RECONNECT_DELAY_RESET_TIMEOUT = 30000;
|
3310 | var SERVER_KILL_INTERRUPT_REASON = 'server_kill';
|
3311 |
|
3312 | var INVALID_TOKEN_THRESHOLD = 3;
|
3313 |
|
3314 |
|
3315 |
|
3316 |
|
3317 |
|
3318 |
|
3319 | var PersistentConnection = (function (_super) {
|
3320 | tslib.__extends(PersistentConnection, _super);
|
3321 | |
3322 |
|
3323 |
|
3324 |
|
3325 |
|
3326 | function PersistentConnection(repoInfo_, applicationId_, onDataUpdate_, onConnectStatus_, onServerInfoUpdate_, authTokenProvider_, appCheckTokenProvider_, authOverride_) {
|
3327 | var _this = _super.call(this) || this;
|
3328 | _this.repoInfo_ = repoInfo_;
|
3329 | _this.applicationId_ = applicationId_;
|
3330 | _this.onDataUpdate_ = onDataUpdate_;
|
3331 | _this.onConnectStatus_ = onConnectStatus_;
|
3332 | _this.onServerInfoUpdate_ = onServerInfoUpdate_;
|
3333 | _this.authTokenProvider_ = authTokenProvider_;
|
3334 | _this.appCheckTokenProvider_ = appCheckTokenProvider_;
|
3335 | _this.authOverride_ = authOverride_;
|
3336 |
|
3337 | _this.id = PersistentConnection.nextPersistentConnectionId_++;
|
3338 | _this.log_ = logWrapper('p:' + _this.id + ':');
|
3339 | _this.interruptReasons_ = {};
|
3340 | _this.listens = new Map();
|
3341 | _this.outstandingPuts_ = [];
|
3342 | _this.outstandingGets_ = [];
|
3343 | _this.outstandingPutCount_ = 0;
|
3344 | _this.outstandingGetCount_ = 0;
|
3345 | _this.onDisconnectRequestQueue_ = [];
|
3346 | _this.connected_ = false;
|
3347 | _this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3348 | _this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_DEFAULT;
|
3349 | _this.securityDebugCallback_ = null;
|
3350 | _this.lastSessionId = null;
|
3351 | _this.establishConnectionTimer_ = null;
|
3352 | _this.visible_ = false;
|
3353 |
|
3354 | _this.requestCBHash_ = {};
|
3355 | _this.requestNumber_ = 0;
|
3356 | _this.realtime_ = null;
|
3357 | _this.authToken_ = null;
|
3358 | _this.appCheckToken_ = null;
|
3359 | _this.forceTokenRefresh_ = false;
|
3360 | _this.invalidAuthTokenCount_ = 0;
|
3361 | _this.invalidAppCheckTokenCount_ = 0;
|
3362 | _this.firstConnection_ = true;
|
3363 | _this.lastConnectionAttemptTime_ = null;
|
3364 | _this.lastConnectionEstablishedTime_ = null;
|
3365 | if (authOverride_ && !util.isNodeSdk()) {
|
3366 | throw new Error('Auth override specified in options, but not supported on non Node.js platforms');
|
3367 | }
|
3368 | VisibilityMonitor.getInstance().on('visible', _this.onVisible_, _this);
|
3369 | if (repoInfo_.host.indexOf('fblocal') === -1) {
|
3370 | OnlineMonitor.getInstance().on('online', _this.onOnline_, _this);
|
3371 | }
|
3372 | return _this;
|
3373 | }
|
3374 | PersistentConnection.prototype.sendRequest = function (action, body, onResponse) {
|
3375 | var curReqNum = ++this.requestNumber_;
|
3376 | var msg = { r: curReqNum, a: action, b: body };
|
3377 | this.log_(util.stringify(msg));
|
3378 | util.assert(this.connected_, "sendRequest call when we're not connected not allowed.");
|
3379 | this.realtime_.sendRequest(msg);
|
3380 | if (onResponse) {
|
3381 | this.requestCBHash_[curReqNum] = onResponse;
|
3382 | }
|
3383 | };
|
3384 | PersistentConnection.prototype.get = function (query) {
|
3385 | var _this = this;
|
3386 | this.initConnection_();
|
3387 | var deferred = new util.Deferred();
|
3388 | var request = {
|
3389 | p: query._path.toString(),
|
3390 | q: query._queryObject
|
3391 | };
|
3392 | var outstandingGet = {
|
3393 | action: 'g',
|
3394 | request: request,
|
3395 | onComplete: function (message) {
|
3396 | var payload = message['d'];
|
3397 | if (message['s'] === 'ok') {
|
3398 | deferred.resolve(payload);
|
3399 | }
|
3400 | else {
|
3401 | deferred.reject(payload);
|
3402 | }
|
3403 | }
|
3404 | };
|
3405 | this.outstandingGets_.push(outstandingGet);
|
3406 | this.outstandingGetCount_++;
|
3407 | var index = this.outstandingGets_.length - 1;
|
3408 | if (!this.connected_) {
|
3409 | setTimeout(function () {
|
3410 | var get = _this.outstandingGets_[index];
|
3411 | if (get === undefined || outstandingGet !== get) {
|
3412 | return;
|
3413 | }
|
3414 | delete _this.outstandingGets_[index];
|
3415 | _this.outstandingGetCount_--;
|
3416 | if (_this.outstandingGetCount_ === 0) {
|
3417 | _this.outstandingGets_ = [];
|
3418 | }
|
3419 | _this.log_('get ' + index + ' timed out on connection');
|
3420 | deferred.reject(new Error('Client is offline.'));
|
3421 | }, GET_CONNECT_TIMEOUT);
|
3422 | }
|
3423 | if (this.connected_) {
|
3424 | this.sendGet_(index);
|
3425 | }
|
3426 | return deferred.promise;
|
3427 | };
|
3428 | PersistentConnection.prototype.listen = function (query, currentHashFn, tag, onComplete) {
|
3429 | this.initConnection_();
|
3430 | var queryId = query._queryIdentifier;
|
3431 | var pathString = query._path.toString();
|
3432 | this.log_('Listen called for ' + pathString + ' ' + queryId);
|
3433 | if (!this.listens.has(pathString)) {
|
3434 | this.listens.set(pathString, new Map());
|
3435 | }
|
3436 | util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'listen() called for non-default but complete query');
|
3437 | util.assert(!this.listens.get(pathString).has(queryId), "listen() called twice for same path/queryId.");
|
3438 | var listenSpec = {
|
3439 | onComplete: onComplete,
|
3440 | hashFn: currentHashFn,
|
3441 | query: query,
|
3442 | tag: tag
|
3443 | };
|
3444 | this.listens.get(pathString).set(queryId, listenSpec);
|
3445 | if (this.connected_) {
|
3446 | this.sendListen_(listenSpec);
|
3447 | }
|
3448 | };
|
3449 | PersistentConnection.prototype.sendGet_ = function (index) {
|
3450 | var _this = this;
|
3451 | var get = this.outstandingGets_[index];
|
3452 | this.sendRequest('g', get.request, function (message) {
|
3453 | delete _this.outstandingGets_[index];
|
3454 | _this.outstandingGetCount_--;
|
3455 | if (_this.outstandingGetCount_ === 0) {
|
3456 | _this.outstandingGets_ = [];
|
3457 | }
|
3458 | if (get.onComplete) {
|
3459 | get.onComplete(message);
|
3460 | }
|
3461 | });
|
3462 | };
|
3463 | PersistentConnection.prototype.sendListen_ = function (listenSpec) {
|
3464 | var _this = this;
|
3465 | var query = listenSpec.query;
|
3466 | var pathString = query._path.toString();
|
3467 | var queryId = query._queryIdentifier;
|
3468 | this.log_('Listen on ' + pathString + ' for ' + queryId);
|
3469 | var req = { p: pathString };
|
3470 | var action = 'q';
|
3471 |
|
3472 | if (listenSpec.tag) {
|
3473 | req['q'] = query._queryObject;
|
3474 | req['t'] = listenSpec.tag;
|
3475 | }
|
3476 | req[ 'h'] = listenSpec.hashFn();
|
3477 | this.sendRequest(action, req, function (message) {
|
3478 | var payload = message[ 'd'];
|
3479 | var status = message[ 's'];
|
3480 |
|
3481 | PersistentConnection.warnOnListenWarnings_(payload, query);
|
3482 | var currentListenSpec = _this.listens.get(pathString) &&
|
3483 | _this.listens.get(pathString).get(queryId);
|
3484 |
|
3485 | if (currentListenSpec === listenSpec) {
|
3486 | _this.log_('listen response', message);
|
3487 | if (status !== 'ok') {
|
3488 | _this.removeListen_(pathString, queryId);
|
3489 | }
|
3490 | if (listenSpec.onComplete) {
|
3491 | listenSpec.onComplete(status, payload);
|
3492 | }
|
3493 | }
|
3494 | });
|
3495 | };
|
3496 | PersistentConnection.warnOnListenWarnings_ = function (payload, query) {
|
3497 | if (payload && typeof payload === 'object' && util.contains(payload, 'w')) {
|
3498 |
|
3499 | var warnings = util.safeGet(payload, 'w');
|
3500 | if (Array.isArray(warnings) && ~warnings.indexOf('no_index')) {
|
3501 | var indexSpec = '".indexOn": "' + query._queryParams.getIndex().toString() + '"';
|
3502 | var indexPath = query._path.toString();
|
3503 | warn("Using an unspecified index. Your data will be downloaded and " +
|
3504 | ("filtered on the client. Consider adding " + indexSpec + " at ") +
|
3505 | (indexPath + " to your security rules for better performance."));
|
3506 | }
|
3507 | }
|
3508 | };
|
3509 | PersistentConnection.prototype.refreshAuthToken = function (token) {
|
3510 | this.authToken_ = token;
|
3511 | this.log_('Auth token refreshed');
|
3512 | if (this.authToken_) {
|
3513 | this.tryAuth();
|
3514 | }
|
3515 | else {
|
3516 |
|
3517 |
|
3518 | if (this.connected_) {
|
3519 | this.sendRequest('unauth', {}, function () { });
|
3520 | }
|
3521 | }
|
3522 | this.reduceReconnectDelayIfAdminCredential_(token);
|
3523 | };
|
3524 | PersistentConnection.prototype.reduceReconnectDelayIfAdminCredential_ = function (credential) {
|
3525 |
|
3526 |
|
3527 | var isFirebaseSecret = credential && credential.length === 40;
|
3528 | if (isFirebaseSecret || util.isAdmin(credential)) {
|
3529 | this.log_('Admin auth credential detected. Reducing max reconnect time.');
|
3530 | this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
|
3531 | }
|
3532 | };
|
3533 | PersistentConnection.prototype.refreshAppCheckToken = function (token) {
|
3534 | this.appCheckToken_ = token;
|
3535 | this.log_('App check token refreshed');
|
3536 | if (this.appCheckToken_) {
|
3537 | this.tryAppCheck();
|
3538 | }
|
3539 | else {
|
3540 |
|
3541 |
|
3542 |
|
3543 | if (this.connected_) {
|
3544 | this.sendRequest('unappeck', {}, function () { });
|
3545 | }
|
3546 | }
|
3547 | };
|
3548 | |
3549 |
|
3550 |
|
3551 |
|
3552 | PersistentConnection.prototype.tryAuth = function () {
|
3553 | var _this = this;
|
3554 | if (this.connected_ && this.authToken_) {
|
3555 | var token_1 = this.authToken_;
|
3556 | var authMethod = util.isValidFormat(token_1) ? 'auth' : 'gauth';
|
3557 | var requestData = { cred: token_1 };
|
3558 | if (this.authOverride_ === null) {
|
3559 | requestData['noauth'] = true;
|
3560 | }
|
3561 | else if (typeof this.authOverride_ === 'object') {
|
3562 | requestData['authvar'] = this.authOverride_;
|
3563 | }
|
3564 | this.sendRequest(authMethod, requestData, function (res) {
|
3565 | var status = res[ 's'];
|
3566 | var data = res[ 'd'] || 'error';
|
3567 | if (_this.authToken_ === token_1) {
|
3568 | if (status === 'ok') {
|
3569 | _this.invalidAuthTokenCount_ = 0;
|
3570 | }
|
3571 | else {
|
3572 |
|
3573 | _this.onAuthRevoked_(status, data);
|
3574 | }
|
3575 | }
|
3576 | });
|
3577 | }
|
3578 | };
|
3579 | |
3580 |
|
3581 |
|
3582 |
|
3583 |
|
3584 | PersistentConnection.prototype.tryAppCheck = function () {
|
3585 | var _this = this;
|
3586 | if (this.connected_ && this.appCheckToken_) {
|
3587 | this.sendRequest('appcheck', { 'token': this.appCheckToken_ }, function (res) {
|
3588 | var status = res[ 's'];
|
3589 | var data = res[ 'd'] || 'error';
|
3590 | if (status === 'ok') {
|
3591 | _this.invalidAppCheckTokenCount_ = 0;
|
3592 | }
|
3593 | else {
|
3594 | _this.onAppCheckRevoked_(status, data);
|
3595 | }
|
3596 | });
|
3597 | }
|
3598 | };
|
3599 | |
3600 |
|
3601 |
|
3602 | PersistentConnection.prototype.unlisten = function (query, tag) {
|
3603 | var pathString = query._path.toString();
|
3604 | var queryId = query._queryIdentifier;
|
3605 | this.log_('Unlisten called for ' + pathString + ' ' + queryId);
|
3606 | util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'unlisten() called for non-default but complete query');
|
3607 | var listen = this.removeListen_(pathString, queryId);
|
3608 | if (listen && this.connected_) {
|
3609 | this.sendUnlisten_(pathString, queryId, query._queryObject, tag);
|
3610 | }
|
3611 | };
|
3612 | PersistentConnection.prototype.sendUnlisten_ = function (pathString, queryId, queryObj, tag) {
|
3613 | this.log_('Unlisten on ' + pathString + ' for ' + queryId);
|
3614 | var req = { p: pathString };
|
3615 | var action = 'n';
|
3616 |
|
3617 | if (tag) {
|
3618 | req['q'] = queryObj;
|
3619 | req['t'] = tag;
|
3620 | }
|
3621 | this.sendRequest(action, req);
|
3622 | };
|
3623 | PersistentConnection.prototype.onDisconnectPut = function (pathString, data, onComplete) {
|
3624 | this.initConnection_();
|
3625 | if (this.connected_) {
|
3626 | this.sendOnDisconnect_('o', pathString, data, onComplete);
|
3627 | }
|
3628 | else {
|
3629 | this.onDisconnectRequestQueue_.push({
|
3630 | pathString: pathString,
|
3631 | action: 'o',
|
3632 | data: data,
|
3633 | onComplete: onComplete
|
3634 | });
|
3635 | }
|
3636 | };
|
3637 | PersistentConnection.prototype.onDisconnectMerge = function (pathString, data, onComplete) {
|
3638 | this.initConnection_();
|
3639 | if (this.connected_) {
|
3640 | this.sendOnDisconnect_('om', pathString, data, onComplete);
|
3641 | }
|
3642 | else {
|
3643 | this.onDisconnectRequestQueue_.push({
|
3644 | pathString: pathString,
|
3645 | action: 'om',
|
3646 | data: data,
|
3647 | onComplete: onComplete
|
3648 | });
|
3649 | }
|
3650 | };
|
3651 | PersistentConnection.prototype.onDisconnectCancel = function (pathString, onComplete) {
|
3652 | this.initConnection_();
|
3653 | if (this.connected_) {
|
3654 | this.sendOnDisconnect_('oc', pathString, null, onComplete);
|
3655 | }
|
3656 | else {
|
3657 | this.onDisconnectRequestQueue_.push({
|
3658 | pathString: pathString,
|
3659 | action: 'oc',
|
3660 | data: null,
|
3661 | onComplete: onComplete
|
3662 | });
|
3663 | }
|
3664 | };
|
3665 | PersistentConnection.prototype.sendOnDisconnect_ = function (action, pathString, data, onComplete) {
|
3666 | var request = { p: pathString, d: data };
|
3667 | this.log_('onDisconnect ' + action, request);
|
3668 | this.sendRequest(action, request, function (response) {
|
3669 | if (onComplete) {
|
3670 | setTimeout(function () {
|
3671 | onComplete(response[ 's'], response[ 'd']);
|
3672 | }, Math.floor(0));
|
3673 | }
|
3674 | });
|
3675 | };
|
3676 | PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
|
3677 | this.putInternal('p', pathString, data, onComplete, hash);
|
3678 | };
|
3679 | PersistentConnection.prototype.merge = function (pathString, data, onComplete, hash) {
|
3680 | this.putInternal('m', pathString, data, onComplete, hash);
|
3681 | };
|
3682 | PersistentConnection.prototype.putInternal = function (action, pathString, data, onComplete, hash) {
|
3683 | this.initConnection_();
|
3684 | var request = {
|
3685 | p: pathString,
|
3686 | d: data
|
3687 | };
|
3688 | if (hash !== undefined) {
|
3689 | request[ 'h'] = hash;
|
3690 | }
|
3691 |
|
3692 | this.outstandingPuts_.push({
|
3693 | action: action,
|
3694 | request: request,
|
3695 | onComplete: onComplete
|
3696 | });
|
3697 | this.outstandingPutCount_++;
|
3698 | var index = this.outstandingPuts_.length - 1;
|
3699 | if (this.connected_) {
|
3700 | this.sendPut_(index);
|
3701 | }
|
3702 | else {
|
3703 | this.log_('Buffering put: ' + pathString);
|
3704 | }
|
3705 | };
|
3706 | PersistentConnection.prototype.sendPut_ = function (index) {
|
3707 | var _this = this;
|
3708 | var action = this.outstandingPuts_[index].action;
|
3709 | var request = this.outstandingPuts_[index].request;
|
3710 | var onComplete = this.outstandingPuts_[index].onComplete;
|
3711 | this.outstandingPuts_[index].queued = this.connected_;
|
3712 | this.sendRequest(action, request, function (message) {
|
3713 | _this.log_(action + ' response', message);
|
3714 | delete _this.outstandingPuts_[index];
|
3715 | _this.outstandingPutCount_--;
|
3716 |
|
3717 | if (_this.outstandingPutCount_ === 0) {
|
3718 | _this.outstandingPuts_ = [];
|
3719 | }
|
3720 | if (onComplete) {
|
3721 | onComplete(message[ 's'], message[ 'd']);
|
3722 | }
|
3723 | });
|
3724 | };
|
3725 | PersistentConnection.prototype.reportStats = function (stats) {
|
3726 | var _this = this;
|
3727 |
|
3728 | if (this.connected_) {
|
3729 | var request = { c: stats };
|
3730 | this.log_('reportStats', request);
|
3731 | this.sendRequest( 's', request, function (result) {
|
3732 | var status = result[ 's'];
|
3733 | if (status !== 'ok') {
|
3734 | var errorReason = result[ 'd'];
|
3735 | _this.log_('reportStats', 'Error sending stats: ' + errorReason);
|
3736 | }
|
3737 | });
|
3738 | }
|
3739 | };
|
3740 | PersistentConnection.prototype.onDataMessage_ = function (message) {
|
3741 | if ('r' in message) {
|
3742 |
|
3743 | this.log_('from server: ' + util.stringify(message));
|
3744 | var reqNum = message['r'];
|
3745 | var onResponse = this.requestCBHash_[reqNum];
|
3746 | if (onResponse) {
|
3747 | delete this.requestCBHash_[reqNum];
|
3748 | onResponse(message[ 'b']);
|
3749 | }
|
3750 | }
|
3751 | else if ('error' in message) {
|
3752 | throw 'A server-side error has occurred: ' + message['error'];
|
3753 | }
|
3754 | else if ('a' in message) {
|
3755 |
|
3756 | this.onDataPush_(message['a'], message['b']);
|
3757 | }
|
3758 | };
|
3759 | PersistentConnection.prototype.onDataPush_ = function (action, body) {
|
3760 | this.log_('handleServerMessage', action, body);
|
3761 | if (action === 'd') {
|
3762 | this.onDataUpdate_(body[ 'p'], body[ 'd'],
|
3763 | false, body['t']);
|
3764 | }
|
3765 | else if (action === 'm') {
|
3766 | this.onDataUpdate_(body[ 'p'], body[ 'd'],
|
3767 | true, body['t']);
|
3768 | }
|
3769 | else if (action === 'c') {
|
3770 | this.onListenRevoked_(body[ 'p'], body[ 'q']);
|
3771 | }
|
3772 | else if (action === 'ac') {
|
3773 | this.onAuthRevoked_(body[ 's'], body[ 'd']);
|
3774 | }
|
3775 | else if (action === 'apc') {
|
3776 | this.onAppCheckRevoked_(body[ 's'], body[ 'd']);
|
3777 | }
|
3778 | else if (action === 'sd') {
|
3779 | this.onSecurityDebugPacket_(body);
|
3780 | }
|
3781 | else {
|
3782 | error('Unrecognized action received from server: ' +
|
3783 | util.stringify(action) +
|
3784 | '\nAre you using the latest client?');
|
3785 | }
|
3786 | };
|
3787 | PersistentConnection.prototype.onReady_ = function (timestamp, sessionId) {
|
3788 | this.log_('connection ready');
|
3789 | this.connected_ = true;
|
3790 | this.lastConnectionEstablishedTime_ = new Date().getTime();
|
3791 | this.handleTimestamp_(timestamp);
|
3792 | this.lastSessionId = sessionId;
|
3793 | if (this.firstConnection_) {
|
3794 | this.sendConnectStats_();
|
3795 | }
|
3796 | this.restoreState_();
|
3797 | this.firstConnection_ = false;
|
3798 | this.onConnectStatus_(true);
|
3799 | };
|
3800 | PersistentConnection.prototype.scheduleConnect_ = function (timeout) {
|
3801 | var _this = this;
|
3802 | util.assert(!this.realtime_, "Scheduling a connect when we're already connected/ing?");
|
3803 | if (this.establishConnectionTimer_) {
|
3804 | clearTimeout(this.establishConnectionTimer_);
|
3805 | }
|
3806 |
|
3807 |
|
3808 | this.establishConnectionTimer_ = setTimeout(function () {
|
3809 | _this.establishConnectionTimer_ = null;
|
3810 | _this.establishConnection_();
|
3811 |
|
3812 | }, Math.floor(timeout));
|
3813 | };
|
3814 | PersistentConnection.prototype.initConnection_ = function () {
|
3815 | if (!this.realtime_ && this.firstConnection_) {
|
3816 | this.scheduleConnect_(0);
|
3817 | }
|
3818 | };
|
3819 | PersistentConnection.prototype.onVisible_ = function (visible) {
|
3820 |
|
3821 | if (visible &&
|
3822 | !this.visible_ &&
|
3823 | this.reconnectDelay_ === this.maxReconnectDelay_) {
|
3824 | this.log_('Window became visible. Reducing delay.');
|
3825 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3826 | if (!this.realtime_) {
|
3827 | this.scheduleConnect_(0);
|
3828 | }
|
3829 | }
|
3830 | this.visible_ = visible;
|
3831 | };
|
3832 | PersistentConnection.prototype.onOnline_ = function (online) {
|
3833 | if (online) {
|
3834 | this.log_('Browser went online.');
|
3835 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3836 | if (!this.realtime_) {
|
3837 | this.scheduleConnect_(0);
|
3838 | }
|
3839 | }
|
3840 | else {
|
3841 | this.log_('Browser went offline. Killing connection.');
|
3842 | if (this.realtime_) {
|
3843 | this.realtime_.close();
|
3844 | }
|
3845 | }
|
3846 | };
|
3847 | PersistentConnection.prototype.onRealtimeDisconnect_ = function () {
|
3848 | this.log_('data client disconnected');
|
3849 | this.connected_ = false;
|
3850 | this.realtime_ = null;
|
3851 |
|
3852 | this.cancelSentTransactions_();
|
3853 |
|
3854 | this.requestCBHash_ = {};
|
3855 | if (this.shouldReconnect_()) {
|
3856 | if (!this.visible_) {
|
3857 | this.log_("Window isn't visible. Delaying reconnect.");
|
3858 | this.reconnectDelay_ = this.maxReconnectDelay_;
|
3859 | this.lastConnectionAttemptTime_ = new Date().getTime();
|
3860 | }
|
3861 | else if (this.lastConnectionEstablishedTime_) {
|
3862 |
|
3863 | var timeSinceLastConnectSucceeded = new Date().getTime() - this.lastConnectionEstablishedTime_;
|
3864 | if (timeSinceLastConnectSucceeded > RECONNECT_DELAY_RESET_TIMEOUT) {
|
3865 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3866 | }
|
3867 | this.lastConnectionEstablishedTime_ = null;
|
3868 | }
|
3869 | var timeSinceLastConnectAttempt = new Date().getTime() - this.lastConnectionAttemptTime_;
|
3870 | var reconnectDelay = Math.max(0, this.reconnectDelay_ - timeSinceLastConnectAttempt);
|
3871 | reconnectDelay = Math.random() * reconnectDelay;
|
3872 | this.log_('Trying to reconnect in ' + reconnectDelay + 'ms');
|
3873 | this.scheduleConnect_(reconnectDelay);
|
3874 |
|
3875 | this.reconnectDelay_ = Math.min(this.maxReconnectDelay_, this.reconnectDelay_ * RECONNECT_DELAY_MULTIPLIER);
|
3876 | }
|
3877 | this.onConnectStatus_(false);
|
3878 | };
|
3879 | PersistentConnection.prototype.establishConnection_ = function () {
|
3880 | return tslib.__awaiter(this, void 0, void 0, function () {
|
3881 | var onDataMessage, onReady, onDisconnect_1, connId, lastSessionId, canceled_1, connection_1, closeFn, sendRequestFn, forceRefresh, _a, authToken, appCheckToken, error_1;
|
3882 | var _this = this;
|
3883 | return tslib.__generator(this, function (_b) {
|
3884 | switch (_b.label) {
|
3885 | case 0:
|
3886 | if (!this.shouldReconnect_()) return [3 , 4];
|
3887 | this.log_('Making a connection attempt');
|
3888 | this.lastConnectionAttemptTime_ = new Date().getTime();
|
3889 | this.lastConnectionEstablishedTime_ = null;
|
3890 | onDataMessage = this.onDataMessage_.bind(this);
|
3891 | onReady = this.onReady_.bind(this);
|
3892 | onDisconnect_1 = this.onRealtimeDisconnect_.bind(this);
|
3893 | connId = this.id + ':' + PersistentConnection.nextConnectionId_++;
|
3894 | lastSessionId = this.lastSessionId;
|
3895 | canceled_1 = false;
|
3896 | connection_1 = null;
|
3897 | closeFn = function () {
|
3898 | if (connection_1) {
|
3899 | connection_1.close();
|
3900 | }
|
3901 | else {
|
3902 | canceled_1 = true;
|
3903 | onDisconnect_1();
|
3904 | }
|
3905 | };
|
3906 | sendRequestFn = function (msg) {
|
3907 | util.assert(connection_1, "sendRequest call when we're not connected not allowed.");
|
3908 | connection_1.sendRequest(msg);
|
3909 | };
|
3910 | this.realtime_ = {
|
3911 | close: closeFn,
|
3912 | sendRequest: sendRequestFn
|
3913 | };
|
3914 | forceRefresh = this.forceTokenRefresh_;
|
3915 | this.forceTokenRefresh_ = false;
|
3916 | _b.label = 1;
|
3917 | case 1:
|
3918 | _b.trys.push([1, 3, , 4]);
|
3919 | return [4 , Promise.all([
|
3920 | this.authTokenProvider_.getToken(forceRefresh),
|
3921 | this.appCheckTokenProvider_.getToken(forceRefresh)
|
3922 | ])];
|
3923 | case 2:
|
3924 | _a = tslib.__read.apply(void 0, [_b.sent(), 2]), authToken = _a[0], appCheckToken = _a[1];
|
3925 | if (!canceled_1) {
|
3926 | log('getToken() completed. Creating connection.');
|
3927 | this.authToken_ = authToken && authToken.accessToken;
|
3928 | this.appCheckToken_ = appCheckToken && appCheckToken.token;
|
3929 | connection_1 = new Connection(connId, this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, onDataMessage, onReady, onDisconnect_1,
|
3930 | function (reason) {
|
3931 | warn(reason + ' (' + _this.repoInfo_.toString() + ')');
|
3932 | _this.interrupt(SERVER_KILL_INTERRUPT_REASON);
|
3933 | }, lastSessionId);
|
3934 | }
|
3935 | else {
|
3936 | log('getToken() completed but was canceled');
|
3937 | }
|
3938 | return [3 , 4];
|
3939 | case 3:
|
3940 | error_1 = _b.sent();
|
3941 | this.log_('Failed to get token: ' + error_1);
|
3942 | if (!canceled_1) {
|
3943 | if (this.repoInfo_.nodeAdmin) {
|
3944 |
|
3945 |
|
3946 |
|
3947 | warn(error_1);
|
3948 | }
|
3949 | closeFn();
|
3950 | }
|
3951 | return [3 , 4];
|
3952 | case 4: return [2 ];
|
3953 | }
|
3954 | });
|
3955 | });
|
3956 | };
|
3957 | PersistentConnection.prototype.interrupt = function (reason) {
|
3958 | log('Interrupting connection for reason: ' + reason);
|
3959 | this.interruptReasons_[reason] = true;
|
3960 | if (this.realtime_) {
|
3961 | this.realtime_.close();
|
3962 | }
|
3963 | else {
|
3964 | if (this.establishConnectionTimer_) {
|
3965 | clearTimeout(this.establishConnectionTimer_);
|
3966 | this.establishConnectionTimer_ = null;
|
3967 | }
|
3968 | if (this.connected_) {
|
3969 | this.onRealtimeDisconnect_();
|
3970 | }
|
3971 | }
|
3972 | };
|
3973 | PersistentConnection.prototype.resume = function (reason) {
|
3974 | log('Resuming connection for reason: ' + reason);
|
3975 | delete this.interruptReasons_[reason];
|
3976 | if (util.isEmpty(this.interruptReasons_)) {
|
3977 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3978 | if (!this.realtime_) {
|
3979 | this.scheduleConnect_(0);
|
3980 | }
|
3981 | }
|
3982 | };
|
3983 | PersistentConnection.prototype.handleTimestamp_ = function (timestamp) {
|
3984 | var delta = timestamp - new Date().getTime();
|
3985 | this.onServerInfoUpdate_({ serverTimeOffset: delta });
|
3986 | };
|
3987 | PersistentConnection.prototype.cancelSentTransactions_ = function () {
|
3988 | for (var i = 0; i < this.outstandingPuts_.length; i++) {
|
3989 | var put = this.outstandingPuts_[i];
|
3990 | if (put && 'h' in put.request && put.queued) {
|
3991 | if (put.onComplete) {
|
3992 | put.onComplete('disconnect');
|
3993 | }
|
3994 | delete this.outstandingPuts_[i];
|
3995 | this.outstandingPutCount_--;
|
3996 | }
|
3997 | }
|
3998 |
|
3999 | if (this.outstandingPutCount_ === 0) {
|
4000 | this.outstandingPuts_ = [];
|
4001 | }
|
4002 | };
|
4003 | PersistentConnection.prototype.onListenRevoked_ = function (pathString, query) {
|
4004 |
|
4005 | var queryId;
|
4006 | if (!query) {
|
4007 | queryId = 'default';
|
4008 | }
|
4009 | else {
|
4010 | queryId = query.map(function (q) { return ObjectToUniqueKey(q); }).join('$');
|
4011 | }
|
4012 | var listen = this.removeListen_(pathString, queryId);
|
4013 | if (listen && listen.onComplete) {
|
4014 | listen.onComplete('permission_denied');
|
4015 | }
|
4016 | };
|
4017 | PersistentConnection.prototype.removeListen_ = function (pathString, queryId) {
|
4018 | var normalizedPathString = new Path(pathString).toString();
|
4019 | var listen;
|
4020 | if (this.listens.has(normalizedPathString)) {
|
4021 | var map = this.listens.get(normalizedPathString);
|
4022 | listen = map.get(queryId);
|
4023 | map.delete(queryId);
|
4024 | if (map.size === 0) {
|
4025 | this.listens.delete(normalizedPathString);
|
4026 | }
|
4027 | }
|
4028 | else {
|
4029 |
|
4030 | listen = undefined;
|
4031 | }
|
4032 | return listen;
|
4033 | };
|
4034 | PersistentConnection.prototype.onAuthRevoked_ = function (statusCode, explanation) {
|
4035 | log('Auth token revoked: ' + statusCode + '/' + explanation);
|
4036 | this.authToken_ = null;
|
4037 | this.forceTokenRefresh_ = true;
|
4038 | this.realtime_.close();
|
4039 | if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
|
4040 |
|
4041 |
|
4042 |
|
4043 | this.invalidAuthTokenCount_++;
|
4044 | if (this.invalidAuthTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
|
4045 |
|
4046 | this.reconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
|
4047 |
|
4048 |
|
4049 | this.authTokenProvider_.notifyForInvalidToken();
|
4050 | }
|
4051 | }
|
4052 | };
|
4053 | PersistentConnection.prototype.onAppCheckRevoked_ = function (statusCode, explanation) {
|
4054 | log('App check token revoked: ' + statusCode + '/' + explanation);
|
4055 | this.appCheckToken_ = null;
|
4056 | this.forceTokenRefresh_ = true;
|
4057 |
|
4058 |
|
4059 | if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
|
4060 |
|
4061 |
|
4062 |
|
4063 | this.invalidAppCheckTokenCount_++;
|
4064 | if (this.invalidAppCheckTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
|
4065 | this.appCheckTokenProvider_.notifyForInvalidToken();
|
4066 | }
|
4067 | }
|
4068 | };
|
4069 | PersistentConnection.prototype.onSecurityDebugPacket_ = function (body) {
|
4070 | if (this.securityDebugCallback_) {
|
4071 | this.securityDebugCallback_(body);
|
4072 | }
|
4073 | else {
|
4074 | if ('msg' in body) {
|
4075 | console.log('FIREBASE: ' + body['msg'].replace('\n', '\nFIREBASE: '));
|
4076 | }
|
4077 | }
|
4078 | };
|
4079 | PersistentConnection.prototype.restoreState_ = function () {
|
4080 | var e_1, _a, e_2, _b;
|
4081 |
|
4082 | this.tryAuth();
|
4083 | this.tryAppCheck();
|
4084 | try {
|
4085 |
|
4086 |
|
4087 | for (var _c = tslib.__values(this.listens.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
|
4088 | var queries = _d.value;
|
4089 | try {
|
4090 | for (var _e = (e_2 = void 0, tslib.__values(queries.values())), _f = _e.next(); !_f.done; _f = _e.next()) {
|
4091 | var listenSpec = _f.value;
|
4092 | this.sendListen_(listenSpec);
|
4093 | }
|
4094 | }
|
4095 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
4096 | finally {
|
4097 | try {
|
4098 | if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
4099 | }
|
4100 | finally { if (e_2) throw e_2.error; }
|
4101 | }
|
4102 | }
|
4103 | }
|
4104 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
4105 | finally {
|
4106 | try {
|
4107 | if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
4108 | }
|
4109 | finally { if (e_1) throw e_1.error; }
|
4110 | }
|
4111 | for (var i = 0; i < this.outstandingPuts_.length; i++) {
|
4112 | if (this.outstandingPuts_[i]) {
|
4113 | this.sendPut_(i);
|
4114 | }
|
4115 | }
|
4116 | while (this.onDisconnectRequestQueue_.length) {
|
4117 | var request = this.onDisconnectRequestQueue_.shift();
|
4118 | this.sendOnDisconnect_(request.action, request.pathString, request.data, request.onComplete);
|
4119 | }
|
4120 | for (var i = 0; i < this.outstandingGets_.length; i++) {
|
4121 | if (this.outstandingGets_[i]) {
|
4122 | this.sendGet_(i);
|
4123 | }
|
4124 | }
|
4125 | };
|
4126 | |
4127 |
|
4128 |
|
4129 | PersistentConnection.prototype.sendConnectStats_ = function () {
|
4130 | var stats = {};
|
4131 | var clientName = 'js';
|
4132 | if (util.isNodeSdk()) {
|
4133 | if (this.repoInfo_.nodeAdmin) {
|
4134 | clientName = 'admin_node';
|
4135 | }
|
4136 | else {
|
4137 | clientName = 'node';
|
4138 | }
|
4139 | }
|
4140 | stats['sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-')] = 1;
|
4141 | if (util.isMobileCordova()) {
|
4142 | stats['framework.cordova'] = 1;
|
4143 | }
|
4144 | else if (util.isReactNative()) {
|
4145 | stats['framework.reactnative'] = 1;
|
4146 | }
|
4147 | this.reportStats(stats);
|
4148 | };
|
4149 | PersistentConnection.prototype.shouldReconnect_ = function () {
|
4150 | var online = OnlineMonitor.getInstance().currentlyOnline();
|
4151 | return util.isEmpty(this.interruptReasons_) && online;
|
4152 | };
|
4153 | PersistentConnection.nextPersistentConnectionId_ = 0;
|
4154 | |
4155 |
|
4156 |
|
4157 | PersistentConnection.nextConnectionId_ = 0;
|
4158 | return PersistentConnection;
|
4159 | }(ServerActions));
|
4160 |
|
4161 |
|
4162 |
|
4163 |
|
4164 |
|
4165 |
|
4166 |
|
4167 |
|
4168 |
|
4169 |
|
4170 |
|
4171 |
|
4172 |
|
4173 |
|
4174 |
|
4175 |
|
4176 |
|
4177 | var NamedNode = (function () {
|
4178 | function NamedNode(name, node) {
|
4179 | this.name = name;
|
4180 | this.node = node;
|
4181 | }
|
4182 | NamedNode.Wrap = function (name, node) {
|
4183 | return new NamedNode(name, node);
|
4184 | };
|
4185 | return NamedNode;
|
4186 | }());
|
4187 |
|
4188 |
|
4189 |
|
4190 |
|
4191 |
|
4192 |
|
4193 |
|
4194 |
|
4195 |
|
4196 |
|
4197 |
|
4198 |
|
4199 |
|
4200 |
|
4201 |
|
4202 |
|
4203 |
|
4204 | var Index = (function () {
|
4205 | function Index() {
|
4206 | }
|
4207 | |
4208 |
|
4209 |
|
4210 |
|
4211 | Index.prototype.getCompare = function () {
|
4212 | return this.compare.bind(this);
|
4213 | };
|
4214 | |
4215 |
|
4216 |
|
4217 |
|
4218 |
|
4219 |
|
4220 |
|
4221 | Index.prototype.indexedValueChanged = function (oldNode, newNode) {
|
4222 | var oldWrapped = new NamedNode(MIN_NAME, oldNode);
|
4223 | var newWrapped = new NamedNode(MIN_NAME, newNode);
|
4224 | return this.compare(oldWrapped, newWrapped) !== 0;
|
4225 | };
|
4226 | |
4227 |
|
4228 |
|
4229 |
|
4230 | Index.prototype.minPost = function () {
|
4231 |
|
4232 | return NamedNode.MIN;
|
4233 | };
|
4234 | return Index;
|
4235 | }());
|
4236 |
|
4237 |
|
4238 |
|
4239 |
|
4240 |
|
4241 |
|
4242 |
|
4243 |
|
4244 |
|
4245 |
|
4246 |
|
4247 |
|
4248 |
|
4249 |
|
4250 |
|
4251 |
|
4252 |
|
4253 | var __EMPTY_NODE;
|
4254 | var KeyIndex = (function (_super) {
|
4255 | tslib.__extends(KeyIndex, _super);
|
4256 | function KeyIndex() {
|
4257 | return _super !== null && _super.apply(this, arguments) || this;
|
4258 | }
|
4259 | Object.defineProperty(KeyIndex, "__EMPTY_NODE", {
|
4260 | get: function () {
|
4261 | return __EMPTY_NODE;
|
4262 | },
|
4263 | set: function (val) {
|
4264 | __EMPTY_NODE = val;
|
4265 | },
|
4266 | enumerable: false,
|
4267 | configurable: true
|
4268 | });
|
4269 | KeyIndex.prototype.compare = function (a, b) {
|
4270 | return nameCompare(a.name, b.name);
|
4271 | };
|
4272 | KeyIndex.prototype.isDefinedOn = function (node) {
|
4273 |
|
4274 |
|
4275 | throw util.assertionError('KeyIndex.isDefinedOn not expected to be called.');
|
4276 | };
|
4277 | KeyIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
|
4278 | return false;
|
4279 | };
|
4280 | KeyIndex.prototype.minPost = function () {
|
4281 |
|
4282 | return NamedNode.MIN;
|
4283 | };
|
4284 | KeyIndex.prototype.maxPost = function () {
|
4285 |
|
4286 |
|
4287 | return new NamedNode(MAX_NAME, __EMPTY_NODE);
|
4288 | };
|
4289 | KeyIndex.prototype.makePost = function (indexValue, name) {
|
4290 | util.assert(typeof indexValue === 'string', 'KeyIndex indexValue must always be a string.');
|
4291 |
|
4292 | return new NamedNode(indexValue, __EMPTY_NODE);
|
4293 | };
|
4294 | |
4295 |
|
4296 |
|
4297 | KeyIndex.prototype.toString = function () {
|
4298 | return '.key';
|
4299 | };
|
4300 | return KeyIndex;
|
4301 | }(Index));
|
4302 | var KEY_INDEX = new KeyIndex();
|
4303 |
|
4304 |
|
4305 |
|
4306 |
|
4307 |
|
4308 |
|
4309 |
|
4310 |
|
4311 |
|
4312 |
|
4313 |
|
4314 |
|
4315 |
|
4316 |
|
4317 |
|
4318 |
|
4319 |
|
4320 |
|
4321 |
|
4322 |
|
4323 | var SortedMapIterator = (function () {
|
4324 | |
4325 |
|
4326 |
|
4327 |
|
4328 | function SortedMapIterator(node, startKey, comparator, isReverse_, resultGenerator_) {
|
4329 | if (resultGenerator_ === void 0) { resultGenerator_ = null; }
|
4330 | this.isReverse_ = isReverse_;
|
4331 | this.resultGenerator_ = resultGenerator_;
|
4332 | this.nodeStack_ = [];
|
4333 | var cmp = 1;
|
4334 | while (!node.isEmpty()) {
|
4335 | node = node;
|
4336 | cmp = startKey ? comparator(node.key, startKey) : 1;
|
4337 |
|
4338 | if (isReverse_) {
|
4339 | cmp *= -1;
|
4340 | }
|
4341 | if (cmp < 0) {
|
4342 |
|
4343 | if (this.isReverse_) {
|
4344 | node = node.left;
|
4345 | }
|
4346 | else {
|
4347 | node = node.right;
|
4348 | }
|
4349 | }
|
4350 | else if (cmp === 0) {
|
4351 |
|
4352 | this.nodeStack_.push(node);
|
4353 | break;
|
4354 | }
|
4355 | else {
|
4356 |
|
4357 | this.nodeStack_.push(node);
|
4358 | if (this.isReverse_) {
|
4359 | node = node.right;
|
4360 | }
|
4361 | else {
|
4362 | node = node.left;
|
4363 | }
|
4364 | }
|
4365 | }
|
4366 | }
|
4367 | SortedMapIterator.prototype.getNext = function () {
|
4368 | if (this.nodeStack_.length === 0) {
|
4369 | return null;
|
4370 | }
|
4371 | var node = this.nodeStack_.pop();
|
4372 | var result;
|
4373 | if (this.resultGenerator_) {
|
4374 | result = this.resultGenerator_(node.key, node.value);
|
4375 | }
|
4376 | else {
|
4377 | result = { key: node.key, value: node.value };
|
4378 | }
|
4379 | if (this.isReverse_) {
|
4380 | node = node.left;
|
4381 | while (!node.isEmpty()) {
|
4382 | this.nodeStack_.push(node);
|
4383 | node = node.right;
|
4384 | }
|
4385 | }
|
4386 | else {
|
4387 | node = node.right;
|
4388 | while (!node.isEmpty()) {
|
4389 | this.nodeStack_.push(node);
|
4390 | node = node.left;
|
4391 | }
|
4392 | }
|
4393 | return result;
|
4394 | };
|
4395 | SortedMapIterator.prototype.hasNext = function () {
|
4396 | return this.nodeStack_.length > 0;
|
4397 | };
|
4398 | SortedMapIterator.prototype.peek = function () {
|
4399 | if (this.nodeStack_.length === 0) {
|
4400 | return null;
|
4401 | }
|
4402 | var node = this.nodeStack_[this.nodeStack_.length - 1];
|
4403 | if (this.resultGenerator_) {
|
4404 | return this.resultGenerator_(node.key, node.value);
|
4405 | }
|
4406 | else {
|
4407 | return { key: node.key, value: node.value };
|
4408 | }
|
4409 | };
|
4410 | return SortedMapIterator;
|
4411 | }());
|
4412 |
|
4413 |
|
4414 |
|
4415 | var LLRBNode = (function () {
|
4416 | |
4417 |
|
4418 |
|
4419 |
|
4420 |
|
4421 |
|
4422 |
|
4423 | function LLRBNode(key, value, color, left, right) {
|
4424 | this.key = key;
|
4425 | this.value = value;
|
4426 | this.color = color != null ? color : LLRBNode.RED;
|
4427 | this.left =
|
4428 | left != null ? left : SortedMap.EMPTY_NODE;
|
4429 | this.right =
|
4430 | right != null ? right : SortedMap.EMPTY_NODE;
|
4431 | }
|
4432 | |
4433 |
|
4434 |
|
4435 |
|
4436 |
|
4437 |
|
4438 |
|
4439 |
|
4440 |
|
4441 |
|
4442 | LLRBNode.prototype.copy = function (key, value, color, left, right) {
|
4443 | return new LLRBNode(key != null ? key : this.key, value != null ? value : this.value, color != null ? color : this.color, left != null ? left : this.left, right != null ? right : this.right);
|
4444 | };
|
4445 | |
4446 |
|
4447 |
|
4448 | LLRBNode.prototype.count = function () {
|
4449 | return this.left.count() + 1 + this.right.count();
|
4450 | };
|
4451 | |
4452 |
|
4453 |
|
4454 | LLRBNode.prototype.isEmpty = function () {
|
4455 | return false;
|
4456 | };
|
4457 | |
4458 |
|
4459 |
|
4460 |
|
4461 |
|
4462 |
|
4463 |
|
4464 |
|
4465 |
|
4466 | LLRBNode.prototype.inorderTraversal = function (action) {
|
4467 | return (this.left.inorderTraversal(action) ||
|
4468 | !!action(this.key, this.value) ||
|
4469 | this.right.inorderTraversal(action));
|
4470 | };
|
4471 | |
4472 |
|
4473 |
|
4474 |
|
4475 |
|
4476 |
|
4477 |
|
4478 |
|
4479 | LLRBNode.prototype.reverseTraversal = function (action) {
|
4480 | return (this.right.reverseTraversal(action) ||
|
4481 | action(this.key, this.value) ||
|
4482 | this.left.reverseTraversal(action));
|
4483 | };
|
4484 | |
4485 |
|
4486 |
|
4487 | LLRBNode.prototype.min_ = function () {
|
4488 | if (this.left.isEmpty()) {
|
4489 | return this;
|
4490 | }
|
4491 | else {
|
4492 | return this.left.min_();
|
4493 | }
|
4494 | };
|
4495 | |
4496 |
|
4497 |
|
4498 | LLRBNode.prototype.minKey = function () {
|
4499 | return this.min_().key;
|
4500 | };
|
4501 | |
4502 |
|
4503 |
|
4504 | LLRBNode.prototype.maxKey = function () {
|
4505 | if (this.right.isEmpty()) {
|
4506 | return this.key;
|
4507 | }
|
4508 | else {
|
4509 | return this.right.maxKey();
|
4510 | }
|
4511 | };
|
4512 | |
4513 |
|
4514 |
|
4515 |
|
4516 |
|
4517 |
|
4518 | LLRBNode.prototype.insert = function (key, value, comparator) {
|
4519 | var n = this;
|
4520 | var cmp = comparator(key, n.key);
|
4521 | if (cmp < 0) {
|
4522 | n = n.copy(null, null, null, n.left.insert(key, value, comparator), null);
|
4523 | }
|
4524 | else if (cmp === 0) {
|
4525 | n = n.copy(null, value, null, null, null);
|
4526 | }
|
4527 | else {
|
4528 | n = n.copy(null, null, null, null, n.right.insert(key, value, comparator));
|
4529 | }
|
4530 | return n.fixUp_();
|
4531 | };
|
4532 | |
4533 |
|
4534 |
|
4535 | LLRBNode.prototype.removeMin_ = function () {
|
4536 | if (this.left.isEmpty()) {
|
4537 | return SortedMap.EMPTY_NODE;
|
4538 | }
|
4539 | var n = this;
|
4540 | if (!n.left.isRed_() && !n.left.left.isRed_()) {
|
4541 | n = n.moveRedLeft_();
|
4542 | }
|
4543 | n = n.copy(null, null, null, n.left.removeMin_(), null);
|
4544 | return n.fixUp_();
|
4545 | };
|
4546 | |
4547 |
|
4548 |
|
4549 |
|
4550 |
|
4551 | LLRBNode.prototype.remove = function (key, comparator) {
|
4552 | var n, smallest;
|
4553 | n = this;
|
4554 | if (comparator(key, n.key) < 0) {
|
4555 | if (!n.left.isEmpty() && !n.left.isRed_() && !n.left.left.isRed_()) {
|
4556 | n = n.moveRedLeft_();
|
4557 | }
|
4558 | n = n.copy(null, null, null, n.left.remove(key, comparator), null);
|
4559 | }
|
4560 | else {
|
4561 | if (n.left.isRed_()) {
|
4562 | n = n.rotateRight_();
|
4563 | }
|
4564 | if (!n.right.isEmpty() && !n.right.isRed_() && !n.right.left.isRed_()) {
|
4565 | n = n.moveRedRight_();
|
4566 | }
|
4567 | if (comparator(key, n.key) === 0) {
|
4568 | if (n.right.isEmpty()) {
|
4569 | return SortedMap.EMPTY_NODE;
|
4570 | }
|
4571 | else {
|
4572 | smallest = n.right.min_();
|
4573 | n = n.copy(smallest.key, smallest.value, null, null, n.right.removeMin_());
|
4574 | }
|
4575 | }
|
4576 | n = n.copy(null, null, null, null, n.right.remove(key, comparator));
|
4577 | }
|
4578 | return n.fixUp_();
|
4579 | };
|
4580 | |
4581 |
|
4582 |
|
4583 | LLRBNode.prototype.isRed_ = function () {
|
4584 | return this.color;
|
4585 | };
|
4586 | |
4587 |
|
4588 |
|
4589 | LLRBNode.prototype.fixUp_ = function () {
|
4590 | var n = this;
|
4591 | if (n.right.isRed_() && !n.left.isRed_()) {
|
4592 | n = n.rotateLeft_();
|
4593 | }
|
4594 | if (n.left.isRed_() && n.left.left.isRed_()) {
|
4595 | n = n.rotateRight_();
|
4596 | }
|
4597 | if (n.left.isRed_() && n.right.isRed_()) {
|
4598 | n = n.colorFlip_();
|
4599 | }
|
4600 | return n;
|
4601 | };
|
4602 | |
4603 |
|
4604 |
|
4605 | LLRBNode.prototype.moveRedLeft_ = function () {
|
4606 | var n = this.colorFlip_();
|
4607 | if (n.right.left.isRed_()) {
|
4608 | n = n.copy(null, null, null, null, n.right.rotateRight_());
|
4609 | n = n.rotateLeft_();
|
4610 | n = n.colorFlip_();
|
4611 | }
|
4612 | return n;
|
4613 | };
|
4614 | |
4615 |
|
4616 |
|
4617 | LLRBNode.prototype.moveRedRight_ = function () {
|
4618 | var n = this.colorFlip_();
|
4619 | if (n.left.left.isRed_()) {
|
4620 | n = n.rotateRight_();
|
4621 | n = n.colorFlip_();
|
4622 | }
|
4623 | return n;
|
4624 | };
|
4625 | |
4626 |
|
4627 |
|
4628 | LLRBNode.prototype.rotateLeft_ = function () {
|
4629 | var nl = this.copy(null, null, LLRBNode.RED, null, this.right.left);
|
4630 | return this.right.copy(null, null, this.color, nl, null);
|
4631 | };
|
4632 | |
4633 |
|
4634 |
|
4635 | LLRBNode.prototype.rotateRight_ = function () {
|
4636 | var nr = this.copy(null, null, LLRBNode.RED, this.left.right, null);
|
4637 | return this.left.copy(null, null, this.color, null, nr);
|
4638 | };
|
4639 | |
4640 |
|
4641 |
|
4642 | LLRBNode.prototype.colorFlip_ = function () {
|
4643 | var left = this.left.copy(null, null, !this.left.color, null, null);
|
4644 | var right = this.right.copy(null, null, !this.right.color, null, null);
|
4645 | return this.copy(null, null, !this.color, left, right);
|
4646 | };
|
4647 | |
4648 |
|
4649 |
|
4650 |
|
4651 |
|
4652 | LLRBNode.prototype.checkMaxDepth_ = function () {
|
4653 | var blackDepth = this.check_();
|
4654 | return Math.pow(2.0, blackDepth) <= this.count() + 1;
|
4655 | };
|
4656 | LLRBNode.prototype.check_ = function () {
|
4657 | if (this.isRed_() && this.left.isRed_()) {
|
4658 | throw new Error('Red node has red child(' + this.key + ',' + this.value + ')');
|
4659 | }
|
4660 | if (this.right.isRed_()) {
|
4661 | throw new Error('Right child of (' + this.key + ',' + this.value + ') is red');
|
4662 | }
|
4663 | var blackDepth = this.left.check_();
|
4664 | if (blackDepth !== this.right.check_()) {
|
4665 | throw new Error('Black depths differ');
|
4666 | }
|
4667 | else {
|
4668 | return blackDepth + (this.isRed_() ? 0 : 1);
|
4669 | }
|
4670 | };
|
4671 | LLRBNode.RED = true;
|
4672 | LLRBNode.BLACK = false;
|
4673 | return LLRBNode;
|
4674 | }());
|
4675 |
|
4676 |
|
4677 |
|
4678 | var LLRBEmptyNode = (function () {
|
4679 | function LLRBEmptyNode() {
|
4680 | }
|
4681 | |
4682 |
|
4683 |
|
4684 |
|
4685 |
|
4686 | LLRBEmptyNode.prototype.copy = function (key, value, color, left, right) {
|
4687 | return this;
|
4688 | };
|
4689 | |
4690 |
|
4691 |
|
4692 |
|
4693 |
|
4694 |
|
4695 |
|
4696 |
|
4697 | LLRBEmptyNode.prototype.insert = function (key, value, comparator) {
|
4698 | return new LLRBNode(key, value, null);
|
4699 | };
|
4700 | |
4701 |
|
4702 |
|
4703 |
|
4704 |
|
4705 |
|
4706 |
|
4707 | LLRBEmptyNode.prototype.remove = function (key, comparator) {
|
4708 | return this;
|
4709 | };
|
4710 | |
4711 |
|
4712 |
|
4713 | LLRBEmptyNode.prototype.count = function () {
|
4714 | return 0;
|
4715 | };
|
4716 | |
4717 |
|
4718 |
|
4719 | LLRBEmptyNode.prototype.isEmpty = function () {
|
4720 | return true;
|
4721 | };
|
4722 | |
4723 |
|
4724 |
|
4725 |
|
4726 |
|
4727 |
|
4728 |
|
4729 |
|
4730 | LLRBEmptyNode.prototype.inorderTraversal = function (action) {
|
4731 | return false;
|
4732 | };
|
4733 | |
4734 |
|
4735 |
|
4736 |
|
4737 |
|
4738 |
|
4739 |
|
4740 |
|
4741 | LLRBEmptyNode.prototype.reverseTraversal = function (action) {
|
4742 | return false;
|
4743 | };
|
4744 | LLRBEmptyNode.prototype.minKey = function () {
|
4745 | return null;
|
4746 | };
|
4747 | LLRBEmptyNode.prototype.maxKey = function () {
|
4748 | return null;
|
4749 | };
|
4750 | LLRBEmptyNode.prototype.check_ = function () {
|
4751 | return 0;
|
4752 | };
|
4753 | |
4754 |
|
4755 |
|
4756 | LLRBEmptyNode.prototype.isRed_ = function () {
|
4757 | return false;
|
4758 | };
|
4759 | return LLRBEmptyNode;
|
4760 | }());
|
4761 |
|
4762 |
|
4763 |
|
4764 |
|
4765 | var SortedMap = (function () {
|
4766 | |
4767 |
|
4768 |
|
4769 |
|
4770 | function SortedMap(comparator_, root_) {
|
4771 | if (root_ === void 0) { root_ = SortedMap.EMPTY_NODE; }
|
4772 | this.comparator_ = comparator_;
|
4773 | this.root_ = root_;
|
4774 | }
|
4775 | |
4776 |
|
4777 |
|
4778 |
|
4779 |
|
4780 |
|
4781 |
|
4782 |
|
4783 | SortedMap.prototype.insert = function (key, value) {
|
4784 | return new SortedMap(this.comparator_, this.root_
|
4785 | .insert(key, value, this.comparator_)
|
4786 | .copy(null, null, LLRBNode.BLACK, null, null));
|
4787 | };
|
4788 | |
4789 |
|
4790 |
|
4791 |
|
4792 |
|
4793 |
|
4794 | SortedMap.prototype.remove = function (key) {
|
4795 | return new SortedMap(this.comparator_, this.root_
|
4796 | .remove(key, this.comparator_)
|
4797 | .copy(null, null, LLRBNode.BLACK, null, null));
|
4798 | };
|
4799 | |
4800 |
|
4801 |
|
4802 |
|
4803 |
|
4804 |
|
4805 |
|
4806 | SortedMap.prototype.get = function (key) {
|
4807 | var cmp;
|
4808 | var node = this.root_;
|
4809 | while (!node.isEmpty()) {
|
4810 | cmp = this.comparator_(key, node.key);
|
4811 | if (cmp === 0) {
|
4812 | return node.value;
|
4813 | }
|
4814 | else if (cmp < 0) {
|
4815 | node = node.left;
|
4816 | }
|
4817 | else if (cmp > 0) {
|
4818 | node = node.right;
|
4819 | }
|
4820 | }
|
4821 | return null;
|
4822 | };
|
4823 | |
4824 |
|
4825 |
|
4826 |
|
4827 |
|
4828 | SortedMap.prototype.getPredecessorKey = function (key) {
|
4829 | var cmp, node = this.root_, rightParent = null;
|
4830 | while (!node.isEmpty()) {
|
4831 | cmp = this.comparator_(key, node.key);
|
4832 | if (cmp === 0) {
|
4833 | if (!node.left.isEmpty()) {
|
4834 | node = node.left;
|
4835 | while (!node.right.isEmpty()) {
|
4836 | node = node.right;
|
4837 | }
|
4838 | return node.key;
|
4839 | }
|
4840 | else if (rightParent) {
|
4841 | return rightParent.key;
|
4842 | }
|
4843 | else {
|
4844 | return null;
|
4845 | }
|
4846 | }
|
4847 | else if (cmp < 0) {
|
4848 | node = node.left;
|
4849 | }
|
4850 | else if (cmp > 0) {
|
4851 | rightParent = node;
|
4852 | node = node.right;
|
4853 | }
|
4854 | }
|
4855 | throw new Error('Attempted to find predecessor key for a nonexistent key. What gives?');
|
4856 | };
|
4857 | |
4858 |
|
4859 |
|
4860 | SortedMap.prototype.isEmpty = function () {
|
4861 | return this.root_.isEmpty();
|
4862 | };
|
4863 | |
4864 |
|
4865 |
|
4866 | SortedMap.prototype.count = function () {
|
4867 | return this.root_.count();
|
4868 | };
|
4869 | |
4870 |
|
4871 |
|
4872 | SortedMap.prototype.minKey = function () {
|
4873 | return this.root_.minKey();
|
4874 | };
|
4875 | |
4876 |
|
4877 |
|
4878 | SortedMap.prototype.maxKey = function () {
|
4879 | return this.root_.maxKey();
|
4880 | };
|
4881 | |
4882 |
|
4883 |
|
4884 |
|
4885 |
|
4886 |
|
4887 |
|
4888 |
|
4889 |
|
4890 | SortedMap.prototype.inorderTraversal = function (action) {
|
4891 | return this.root_.inorderTraversal(action);
|
4892 | };
|
4893 | |
4894 |
|
4895 |
|
4896 |
|
4897 |
|
4898 |
|
4899 |
|
4900 |
|
4901 | SortedMap.prototype.reverseTraversal = function (action) {
|
4902 | return this.root_.reverseTraversal(action);
|
4903 | };
|
4904 | |
4905 |
|
4906 |
|
4907 |
|
4908 | SortedMap.prototype.getIterator = function (resultGenerator) {
|
4909 | return new SortedMapIterator(this.root_, null, this.comparator_, false, resultGenerator);
|
4910 | };
|
4911 | SortedMap.prototype.getIteratorFrom = function (key, resultGenerator) {
|
4912 | return new SortedMapIterator(this.root_, key, this.comparator_, false, resultGenerator);
|
4913 | };
|
4914 | SortedMap.prototype.getReverseIteratorFrom = function (key, resultGenerator) {
|
4915 | return new SortedMapIterator(this.root_, key, this.comparator_, true, resultGenerator);
|
4916 | };
|
4917 | SortedMap.prototype.getReverseIterator = function (resultGenerator) {
|
4918 | return new SortedMapIterator(this.root_, null, this.comparator_, true, resultGenerator);
|
4919 | };
|
4920 | |
4921 |
|
4922 |
|
4923 | SortedMap.EMPTY_NODE = new LLRBEmptyNode();
|
4924 | return SortedMap;
|
4925 | }());
|
4926 |
|
4927 |
|
4928 |
|
4929 |
|
4930 |
|
4931 |
|
4932 |
|
4933 |
|
4934 |
|
4935 |
|
4936 |
|
4937 |
|
4938 |
|
4939 |
|
4940 |
|
4941 |
|
4942 |
|
4943 | function NAME_ONLY_COMPARATOR(left, right) {
|
4944 | return nameCompare(left.name, right.name);
|
4945 | }
|
4946 | function NAME_COMPARATOR(left, right) {
|
4947 | return nameCompare(left, right);
|
4948 | }
|
4949 |
|
4950 |
|
4951 |
|
4952 |
|
4953 |
|
4954 |
|
4955 |
|
4956 |
|
4957 |
|
4958 |
|
4959 |
|
4960 |
|
4961 |
|
4962 |
|
4963 |
|
4964 |
|
4965 |
|
4966 | var MAX_NODE$2;
|
4967 | function setMaxNode$1(val) {
|
4968 | MAX_NODE$2 = val;
|
4969 | }
|
4970 | var priorityHashText = function (priority) {
|
4971 | if (typeof priority === 'number') {
|
4972 | return 'number:' + doubleToIEEE754String(priority);
|
4973 | }
|
4974 | else {
|
4975 | return 'string:' + priority;
|
4976 | }
|
4977 | };
|
4978 |
|
4979 |
|
4980 |
|
4981 | var validatePriorityNode = function (priorityNode) {
|
4982 | if (priorityNode.isLeafNode()) {
|
4983 | var val = priorityNode.val();
|
4984 | util.assert(typeof val === 'string' ||
|
4985 | typeof val === 'number' ||
|
4986 | (typeof val === 'object' && util.contains(val, '.sv')), 'Priority must be a string or number.');
|
4987 | }
|
4988 | else {
|
4989 | util.assert(priorityNode === MAX_NODE$2 || priorityNode.isEmpty(), 'priority of unexpected type.');
|
4990 | }
|
4991 |
|
4992 | util.assert(priorityNode === MAX_NODE$2 || priorityNode.getPriority().isEmpty(), "Priority nodes can't have a priority of their own.");
|
4993 | };
|
4994 |
|
4995 |
|
4996 |
|
4997 |
|
4998 |
|
4999 |
|
5000 |
|
5001 |
|
5002 |
|
5003 |
|
5004 |
|
5005 |
|
5006 |
|
5007 |
|
5008 |
|
5009 |
|
5010 |
|
5011 | var __childrenNodeConstructor;
|
5012 |
|
5013 |
|
5014 |
|
5015 |
|
5016 |
|
5017 | var LeafNode = (function () {
|
5018 | |
5019 |
|
5020 |
|
5021 |
|
5022 |
|
5023 | function LeafNode(value_, priorityNode_) {
|
5024 | if (priorityNode_ === void 0) { priorityNode_ = LeafNode.__childrenNodeConstructor.EMPTY_NODE; }
|
5025 | this.value_ = value_;
|
5026 | this.priorityNode_ = priorityNode_;
|
5027 | this.lazyHash_ = null;
|
5028 | util.assert(this.value_ !== undefined && this.value_ !== null, "LeafNode shouldn't be created with null/undefined value.");
|
5029 | validatePriorityNode(this.priorityNode_);
|
5030 | }
|
5031 | Object.defineProperty(LeafNode, "__childrenNodeConstructor", {
|
5032 | get: function () {
|
5033 | return __childrenNodeConstructor;
|
5034 | },
|
5035 | set: function (val) {
|
5036 | __childrenNodeConstructor = val;
|
5037 | },
|
5038 | enumerable: false,
|
5039 | configurable: true
|
5040 | });
|
5041 |
|
5042 | LeafNode.prototype.isLeafNode = function () {
|
5043 | return true;
|
5044 | };
|
5045 |
|
5046 | LeafNode.prototype.getPriority = function () {
|
5047 | return this.priorityNode_;
|
5048 | };
|
5049 |
|
5050 | LeafNode.prototype.updatePriority = function (newPriorityNode) {
|
5051 | return new LeafNode(this.value_, newPriorityNode);
|
5052 | };
|
5053 |
|
5054 | LeafNode.prototype.getImmediateChild = function (childName) {
|
5055 |
|
5056 | if (childName === '.priority') {
|
5057 | return this.priorityNode_;
|
5058 | }
|
5059 | else {
|
5060 | return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
|
5061 | }
|
5062 | };
|
5063 |
|
5064 | LeafNode.prototype.getChild = function (path) {
|
5065 | if (pathIsEmpty(path)) {
|
5066 | return this;
|
5067 | }
|
5068 | else if (pathGetFront(path) === '.priority') {
|
5069 | return this.priorityNode_;
|
5070 | }
|
5071 | else {
|
5072 | return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
|
5073 | }
|
5074 | };
|
5075 | LeafNode.prototype.hasChild = function () {
|
5076 | return false;
|
5077 | };
|
5078 |
|
5079 | LeafNode.prototype.getPredecessorChildName = function (childName, childNode) {
|
5080 | return null;
|
5081 | };
|
5082 |
|
5083 | LeafNode.prototype.updateImmediateChild = function (childName, newChildNode) {
|
5084 | if (childName === '.priority') {
|
5085 | return this.updatePriority(newChildNode);
|
5086 | }
|
5087 | else if (newChildNode.isEmpty() && childName !== '.priority') {
|
5088 | return this;
|
5089 | }
|
5090 | else {
|
5091 | return LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateImmediateChild(childName, newChildNode).updatePriority(this.priorityNode_);
|
5092 | }
|
5093 | };
|
5094 |
|
5095 | LeafNode.prototype.updateChild = function (path, newChildNode) {
|
5096 | var front = pathGetFront(path);
|
5097 | if (front === null) {
|
5098 | return newChildNode;
|
5099 | }
|
5100 | else if (newChildNode.isEmpty() && front !== '.priority') {
|
5101 | return this;
|
5102 | }
|
5103 | else {
|
5104 | util.assert(front !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
|
5105 | return this.updateImmediateChild(front, LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateChild(pathPopFront(path), newChildNode));
|
5106 | }
|
5107 | };
|
5108 |
|
5109 | LeafNode.prototype.isEmpty = function () {
|
5110 | return false;
|
5111 | };
|
5112 |
|
5113 | LeafNode.prototype.numChildren = function () {
|
5114 | return 0;
|
5115 | };
|
5116 |
|
5117 | LeafNode.prototype.forEachChild = function (index, action) {
|
5118 | return false;
|
5119 | };
|
5120 | LeafNode.prototype.val = function (exportFormat) {
|
5121 | if (exportFormat && !this.getPriority().isEmpty()) {
|
5122 | return {
|
5123 | '.value': this.getValue(),
|
5124 | '.priority': this.getPriority().val()
|
5125 | };
|
5126 | }
|
5127 | else {
|
5128 | return this.getValue();
|
5129 | }
|
5130 | };
|
5131 |
|
5132 | LeafNode.prototype.hash = function () {
|
5133 | if (this.lazyHash_ === null) {
|
5134 | var toHash = '';
|
5135 | if (!this.priorityNode_.isEmpty()) {
|
5136 | toHash +=
|
5137 | 'priority:' +
|
5138 | priorityHashText(this.priorityNode_.val()) +
|
5139 | ':';
|
5140 | }
|
5141 | var type = typeof this.value_;
|
5142 | toHash += type + ':';
|
5143 | if (type === 'number') {
|
5144 | toHash += doubleToIEEE754String(this.value_);
|
5145 | }
|
5146 | else {
|
5147 | toHash += this.value_;
|
5148 | }
|
5149 | this.lazyHash_ = sha1(toHash);
|
5150 | }
|
5151 | return this.lazyHash_;
|
5152 | };
|
5153 | |
5154 |
|
5155 |
|
5156 |
|
5157 | LeafNode.prototype.getValue = function () {
|
5158 | return this.value_;
|
5159 | };
|
5160 | LeafNode.prototype.compareTo = function (other) {
|
5161 | if (other === LeafNode.__childrenNodeConstructor.EMPTY_NODE) {
|
5162 | return 1;
|
5163 | }
|
5164 | else if (other instanceof LeafNode.__childrenNodeConstructor) {
|
5165 | return -1;
|
5166 | }
|
5167 | else {
|
5168 | util.assert(other.isLeafNode(), 'Unknown node type');
|
5169 | return this.compareToLeafNode_(other);
|
5170 | }
|
5171 | };
|
5172 | |
5173 |
|
5174 |
|
5175 | LeafNode.prototype.compareToLeafNode_ = function (otherLeaf) {
|
5176 | var otherLeafType = typeof otherLeaf.value_;
|
5177 | var thisLeafType = typeof this.value_;
|
5178 | var otherIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(otherLeafType);
|
5179 | var thisIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(thisLeafType);
|
5180 | util.assert(otherIndex >= 0, 'Unknown leaf type: ' + otherLeafType);
|
5181 | util.assert(thisIndex >= 0, 'Unknown leaf type: ' + thisLeafType);
|
5182 | if (otherIndex === thisIndex) {
|
5183 |
|
5184 | if (thisLeafType === 'object') {
|
5185 |
|
5186 | return 0;
|
5187 | }
|
5188 | else {
|
5189 |
|
5190 | if (this.value_ < otherLeaf.value_) {
|
5191 | return -1;
|
5192 | }
|
5193 | else if (this.value_ === otherLeaf.value_) {
|
5194 | return 0;
|
5195 | }
|
5196 | else {
|
5197 | return 1;
|
5198 | }
|
5199 | }
|
5200 | }
|
5201 | else {
|
5202 | return thisIndex - otherIndex;
|
5203 | }
|
5204 | };
|
5205 | LeafNode.prototype.withIndex = function () {
|
5206 | return this;
|
5207 | };
|
5208 | LeafNode.prototype.isIndexed = function () {
|
5209 | return true;
|
5210 | };
|
5211 | LeafNode.prototype.equals = function (other) {
|
5212 | if (other === this) {
|
5213 | return true;
|
5214 | }
|
5215 | else if (other.isLeafNode()) {
|
5216 | var otherLeaf = other;
|
5217 | return (this.value_ === otherLeaf.value_ &&
|
5218 | this.priorityNode_.equals(otherLeaf.priorityNode_));
|
5219 | }
|
5220 | else {
|
5221 | return false;
|
5222 | }
|
5223 | };
|
5224 | |
5225 |
|
5226 |
|
5227 |
|
5228 | LeafNode.VALUE_TYPE_ORDER = ['object', 'boolean', 'number', 'string'];
|
5229 | return LeafNode;
|
5230 | }());
|
5231 |
|
5232 |
|
5233 |
|
5234 |
|
5235 |
|
5236 |
|
5237 |
|
5238 |
|
5239 |
|
5240 |
|
5241 |
|
5242 |
|
5243 |
|
5244 |
|
5245 |
|
5246 |
|
5247 |
|
5248 | var nodeFromJSON$1;
|
5249 | var MAX_NODE$1;
|
5250 | function setNodeFromJSON(val) {
|
5251 | nodeFromJSON$1 = val;
|
5252 | }
|
5253 | function setMaxNode(val) {
|
5254 | MAX_NODE$1 = val;
|
5255 | }
|
5256 | var PriorityIndex = (function (_super) {
|
5257 | tslib.__extends(PriorityIndex, _super);
|
5258 | function PriorityIndex() {
|
5259 | return _super !== null && _super.apply(this, arguments) || this;
|
5260 | }
|
5261 | PriorityIndex.prototype.compare = function (a, b) {
|
5262 | var aPriority = a.node.getPriority();
|
5263 | var bPriority = b.node.getPriority();
|
5264 | var indexCmp = aPriority.compareTo(bPriority);
|
5265 | if (indexCmp === 0) {
|
5266 | return nameCompare(a.name, b.name);
|
5267 | }
|
5268 | else {
|
5269 | return indexCmp;
|
5270 | }
|
5271 | };
|
5272 | PriorityIndex.prototype.isDefinedOn = function (node) {
|
5273 | return !node.getPriority().isEmpty();
|
5274 | };
|
5275 | PriorityIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
|
5276 | return !oldNode.getPriority().equals(newNode.getPriority());
|
5277 | };
|
5278 | PriorityIndex.prototype.minPost = function () {
|
5279 |
|
5280 | return NamedNode.MIN;
|
5281 | };
|
5282 | PriorityIndex.prototype.maxPost = function () {
|
5283 | return new NamedNode(MAX_NAME, new LeafNode('[PRIORITY-POST]', MAX_NODE$1));
|
5284 | };
|
5285 | PriorityIndex.prototype.makePost = function (indexValue, name) {
|
5286 | var priorityNode = nodeFromJSON$1(indexValue);
|
5287 | return new NamedNode(name, new LeafNode('[PRIORITY-POST]', priorityNode));
|
5288 | };
|
5289 | |
5290 |
|
5291 |
|
5292 | PriorityIndex.prototype.toString = function () {
|
5293 | return '.priority';
|
5294 | };
|
5295 | return PriorityIndex;
|
5296 | }(Index));
|
5297 | var PRIORITY_INDEX = new PriorityIndex();
|
5298 |
|
5299 |
|
5300 |
|
5301 |
|
5302 |
|
5303 |
|
5304 |
|
5305 |
|
5306 |
|
5307 |
|
5308 |
|
5309 |
|
5310 |
|
5311 |
|
5312 |
|
5313 |
|
5314 |
|
5315 | var LOG_2 = Math.log(2);
|
5316 | var Base12Num = (function () {
|
5317 | function Base12Num(length) {
|
5318 | var logBase2 = function (num) {
|
5319 |
|
5320 | return parseInt((Math.log(num) / LOG_2), 10);
|
5321 | };
|
5322 | var bitMask = function (bits) { return parseInt(Array(bits + 1).join('1'), 2); };
|
5323 | this.count = logBase2(length + 1);
|
5324 | this.current_ = this.count - 1;
|
5325 | var mask = bitMask(this.count);
|
5326 | this.bits_ = (length + 1) & mask;
|
5327 | }
|
5328 | Base12Num.prototype.nextBitIsOne = function () {
|
5329 |
|
5330 | var result = !(this.bits_ & (0x1 << this.current_));
|
5331 | this.current_--;
|
5332 | return result;
|
5333 | };
|
5334 | return Base12Num;
|
5335 | }());
|
5336 |
|
5337 |
|
5338 |
|
5339 |
|
5340 |
|
5341 |
|
5342 |
|
5343 |
|
5344 |
|
5345 |
|
5346 |
|
5347 |
|
5348 |
|
5349 | var buildChildSet = function (childList, cmp, keyFn, mapSortFn) {
|
5350 | childList.sort(cmp);
|
5351 | var buildBalancedTree = function (low, high) {
|
5352 | var length = high - low;
|
5353 | var namedNode;
|
5354 | var key;
|
5355 | if (length === 0) {
|
5356 | return null;
|
5357 | }
|
5358 | else if (length === 1) {
|
5359 | namedNode = childList[low];
|
5360 | key = keyFn ? keyFn(namedNode) : namedNode;
|
5361 | return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, null, null);
|
5362 | }
|
5363 | else {
|
5364 |
|
5365 | var middle = parseInt((length / 2), 10) + low;
|
5366 | var left = buildBalancedTree(low, middle);
|
5367 | var right = buildBalancedTree(middle + 1, high);
|
5368 | namedNode = childList[middle];
|
5369 | key = keyFn ? keyFn(namedNode) : namedNode;
|
5370 | return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, left, right);
|
5371 | }
|
5372 | };
|
5373 | var buildFrom12Array = function (base12) {
|
5374 | var node = null;
|
5375 | var root = null;
|
5376 | var index = childList.length;
|
5377 | var buildPennant = function (chunkSize, color) {
|
5378 | var low = index - chunkSize;
|
5379 | var high = index;
|
5380 | index -= chunkSize;
|
5381 | var childTree = buildBalancedTree(low + 1, high);
|
5382 | var namedNode = childList[low];
|
5383 | var key = keyFn ? keyFn(namedNode) : namedNode;
|
5384 | attachPennant(new LLRBNode(key, namedNode.node, color, null, childTree));
|
5385 | };
|
5386 | var attachPennant = function (pennant) {
|
5387 | if (node) {
|
5388 | node.left = pennant;
|
5389 | node = pennant;
|
5390 | }
|
5391 | else {
|
5392 | root = pennant;
|
5393 | node = pennant;
|
5394 | }
|
5395 | };
|
5396 | for (var i = 0; i < base12.count; ++i) {
|
5397 | var isOne = base12.nextBitIsOne();
|
5398 |
|
5399 | var chunkSize = Math.pow(2, base12.count - (i + 1));
|
5400 | if (isOne) {
|
5401 | buildPennant(chunkSize, LLRBNode.BLACK);
|
5402 | }
|
5403 | else {
|
5404 |
|
5405 | buildPennant(chunkSize, LLRBNode.BLACK);
|
5406 | buildPennant(chunkSize, LLRBNode.RED);
|
5407 | }
|
5408 | }
|
5409 | return root;
|
5410 | };
|
5411 | var base12 = new Base12Num(childList.length);
|
5412 | var root = buildFrom12Array(base12);
|
5413 |
|
5414 | return new SortedMap(mapSortFn || cmp, root);
|
5415 | };
|
5416 |
|
5417 |
|
5418 |
|
5419 |
|
5420 |
|
5421 |
|
5422 |
|
5423 |
|
5424 |
|
5425 |
|
5426 |
|
5427 |
|
5428 |
|
5429 |
|
5430 |
|
5431 |
|
5432 |
|
5433 | var _defaultIndexMap;
|
5434 | var fallbackObject = {};
|
5435 | var IndexMap = (function () {
|
5436 | function IndexMap(indexes_, indexSet_) {
|
5437 | this.indexes_ = indexes_;
|
5438 | this.indexSet_ = indexSet_;
|
5439 | }
|
5440 | Object.defineProperty(IndexMap, "Default", {
|
5441 | |
5442 |
|
5443 |
|
5444 | get: function () {
|
5445 | util.assert(fallbackObject && PRIORITY_INDEX, 'ChildrenNode.ts has not been loaded');
|
5446 | _defaultIndexMap =
|
5447 | _defaultIndexMap ||
|
5448 | new IndexMap({ '.priority': fallbackObject }, { '.priority': PRIORITY_INDEX });
|
5449 | return _defaultIndexMap;
|
5450 | },
|
5451 | enumerable: false,
|
5452 | configurable: true
|
5453 | });
|
5454 | IndexMap.prototype.get = function (indexKey) {
|
5455 | var sortedMap = util.safeGet(this.indexes_, indexKey);
|
5456 | if (!sortedMap) {
|
5457 | throw new Error('No index defined for ' + indexKey);
|
5458 | }
|
5459 | if (sortedMap instanceof SortedMap) {
|
5460 | return sortedMap;
|
5461 | }
|
5462 | else {
|
5463 |
|
5464 |
|
5465 | return null;
|
5466 | }
|
5467 | };
|
5468 | IndexMap.prototype.hasIndex = function (indexDefinition) {
|
5469 | return util.contains(this.indexSet_, indexDefinition.toString());
|
5470 | };
|
5471 | IndexMap.prototype.addIndex = function (indexDefinition, existingChildren) {
|
5472 | util.assert(indexDefinition !== KEY_INDEX, "KeyIndex always exists and isn't meant to be added to the IndexMap.");
|
5473 | var childList = [];
|
5474 | var sawIndexedValue = false;
|
5475 | var iter = existingChildren.getIterator(NamedNode.Wrap);
|
5476 | var next = iter.getNext();
|
5477 | while (next) {
|
5478 | sawIndexedValue =
|
5479 | sawIndexedValue || indexDefinition.isDefinedOn(next.node);
|
5480 | childList.push(next);
|
5481 | next = iter.getNext();
|
5482 | }
|
5483 | var newIndex;
|
5484 | if (sawIndexedValue) {
|
5485 | newIndex = buildChildSet(childList, indexDefinition.getCompare());
|
5486 | }
|
5487 | else {
|
5488 | newIndex = fallbackObject;
|
5489 | }
|
5490 | var indexName = indexDefinition.toString();
|
5491 | var newIndexSet = tslib.__assign({}, this.indexSet_);
|
5492 | newIndexSet[indexName] = indexDefinition;
|
5493 | var newIndexes = tslib.__assign({}, this.indexes_);
|
5494 | newIndexes[indexName] = newIndex;
|
5495 | return new IndexMap(newIndexes, newIndexSet);
|
5496 | };
|
5497 | |
5498 |
|
5499 |
|
5500 | IndexMap.prototype.addToIndexes = function (namedNode, existingChildren) {
|
5501 | var _this = this;
|
5502 | var newIndexes = util.map(this.indexes_, function (indexedChildren, indexName) {
|
5503 | var index = util.safeGet(_this.indexSet_, indexName);
|
5504 | util.assert(index, 'Missing index implementation for ' + indexName);
|
5505 | if (indexedChildren === fallbackObject) {
|
5506 |
|
5507 | if (index.isDefinedOn(namedNode.node)) {
|
5508 |
|
5509 | var childList = [];
|
5510 | var iter = existingChildren.getIterator(NamedNode.Wrap);
|
5511 | var next = iter.getNext();
|
5512 | while (next) {
|
5513 | if (next.name !== namedNode.name) {
|
5514 | childList.push(next);
|
5515 | }
|
5516 | next = iter.getNext();
|
5517 | }
|
5518 | childList.push(namedNode);
|
5519 | return buildChildSet(childList, index.getCompare());
|
5520 | }
|
5521 | else {
|
5522 |
|
5523 | return fallbackObject;
|
5524 | }
|
5525 | }
|
5526 | else {
|
5527 | var existingSnap = existingChildren.get(namedNode.name);
|
5528 | var newChildren = indexedChildren;
|
5529 | if (existingSnap) {
|
5530 | newChildren = newChildren.remove(new NamedNode(namedNode.name, existingSnap));
|
5531 | }
|
5532 | return newChildren.insert(namedNode, namedNode.node);
|
5533 | }
|
5534 | });
|
5535 | return new IndexMap(newIndexes, this.indexSet_);
|
5536 | };
|
5537 | |
5538 |
|
5539 |
|
5540 | IndexMap.prototype.removeFromIndexes = function (namedNode, existingChildren) {
|
5541 | var newIndexes = util.map(this.indexes_, function (indexedChildren) {
|
5542 | if (indexedChildren === fallbackObject) {
|
5543 |
|
5544 | return indexedChildren;
|
5545 | }
|
5546 | else {
|
5547 | var existingSnap = existingChildren.get(namedNode.name);
|
5548 | if (existingSnap) {
|
5549 | return indexedChildren.remove(new NamedNode(namedNode.name, existingSnap));
|
5550 | }
|
5551 | else {
|
5552 |
|
5553 | return indexedChildren;
|
5554 | }
|
5555 | }
|
5556 | });
|
5557 | return new IndexMap(newIndexes, this.indexSet_);
|
5558 | };
|
5559 | return IndexMap;
|
5560 | }());
|
5561 |
|
5562 |
|
5563 |
|
5564 |
|
5565 |
|
5566 |
|
5567 |
|
5568 |
|
5569 |
|
5570 |
|
5571 |
|
5572 |
|
5573 |
|
5574 |
|
5575 |
|
5576 |
|
5577 |
|
5578 |
|
5579 | var EMPTY_NODE;
|
5580 |
|
5581 |
|
5582 |
|
5583 |
|
5584 |
|
5585 | var ChildrenNode = (function () {
|
5586 | |
5587 |
|
5588 |
|
5589 |
|
5590 | function ChildrenNode(children_, priorityNode_, indexMap_) {
|
5591 | this.children_ = children_;
|
5592 | this.priorityNode_ = priorityNode_;
|
5593 | this.indexMap_ = indexMap_;
|
5594 | this.lazyHash_ = null;
|
5595 | |
5596 |
|
5597 |
|
5598 |
|
5599 |
|
5600 | if (this.priorityNode_) {
|
5601 | validatePriorityNode(this.priorityNode_);
|
5602 | }
|
5603 | if (this.children_.isEmpty()) {
|
5604 | util.assert(!this.priorityNode_ || this.priorityNode_.isEmpty(), 'An empty node cannot have a priority');
|
5605 | }
|
5606 | }
|
5607 | Object.defineProperty(ChildrenNode, "EMPTY_NODE", {
|
5608 | get: function () {
|
5609 | return (EMPTY_NODE ||
|
5610 | (EMPTY_NODE = new ChildrenNode(new SortedMap(NAME_COMPARATOR), null, IndexMap.Default)));
|
5611 | },
|
5612 | enumerable: false,
|
5613 | configurable: true
|
5614 | });
|
5615 |
|
5616 | ChildrenNode.prototype.isLeafNode = function () {
|
5617 | return false;
|
5618 | };
|
5619 |
|
5620 | ChildrenNode.prototype.getPriority = function () {
|
5621 | return this.priorityNode_ || EMPTY_NODE;
|
5622 | };
|
5623 |
|
5624 | ChildrenNode.prototype.updatePriority = function (newPriorityNode) {
|
5625 | if (this.children_.isEmpty()) {
|
5626 |
|
5627 | return this;
|
5628 | }
|
5629 | else {
|
5630 | return new ChildrenNode(this.children_, newPriorityNode, this.indexMap_);
|
5631 | }
|
5632 | };
|
5633 |
|
5634 | ChildrenNode.prototype.getImmediateChild = function (childName) {
|
5635 |
|
5636 | if (childName === '.priority') {
|
5637 | return this.getPriority();
|
5638 | }
|
5639 | else {
|
5640 | var child = this.children_.get(childName);
|
5641 | return child === null ? EMPTY_NODE : child;
|
5642 | }
|
5643 | };
|
5644 |
|
5645 | ChildrenNode.prototype.getChild = function (path) {
|
5646 | var front = pathGetFront(path);
|
5647 | if (front === null) {
|
5648 | return this;
|
5649 | }
|
5650 | return this.getImmediateChild(front).getChild(pathPopFront(path));
|
5651 | };
|
5652 |
|
5653 | ChildrenNode.prototype.hasChild = function (childName) {
|
5654 | return this.children_.get(childName) !== null;
|
5655 | };
|
5656 |
|
5657 | ChildrenNode.prototype.updateImmediateChild = function (childName, newChildNode) {
|
5658 | util.assert(newChildNode, 'We should always be passing snapshot nodes');
|
5659 | if (childName === '.priority') {
|
5660 | return this.updatePriority(newChildNode);
|
5661 | }
|
5662 | else {
|
5663 | var namedNode = new NamedNode(childName, newChildNode);
|
5664 | var newChildren = void 0, newIndexMap = void 0;
|
5665 | if (newChildNode.isEmpty()) {
|
5666 | newChildren = this.children_.remove(childName);
|
5667 | newIndexMap = this.indexMap_.removeFromIndexes(namedNode, this.children_);
|
5668 | }
|
5669 | else {
|
5670 | newChildren = this.children_.insert(childName, newChildNode);
|
5671 | newIndexMap = this.indexMap_.addToIndexes(namedNode, this.children_);
|
5672 | }
|
5673 | var newPriority = newChildren.isEmpty()
|
5674 | ? EMPTY_NODE
|
5675 | : this.priorityNode_;
|
5676 | return new ChildrenNode(newChildren, newPriority, newIndexMap);
|
5677 | }
|
5678 | };
|
5679 |
|
5680 | ChildrenNode.prototype.updateChild = function (path, newChildNode) {
|
5681 | var front = pathGetFront(path);
|
5682 | if (front === null) {
|
5683 | return newChildNode;
|
5684 | }
|
5685 | else {
|
5686 | util.assert(pathGetFront(path) !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
|
5687 | var newImmediateChild = this.getImmediateChild(front).updateChild(pathPopFront(path), newChildNode);
|
5688 | return this.updateImmediateChild(front, newImmediateChild);
|
5689 | }
|
5690 | };
|
5691 |
|
5692 | ChildrenNode.prototype.isEmpty = function () {
|
5693 | return this.children_.isEmpty();
|
5694 | };
|
5695 |
|
5696 | ChildrenNode.prototype.numChildren = function () {
|
5697 | return this.children_.count();
|
5698 | };
|
5699 |
|
5700 | ChildrenNode.prototype.val = function (exportFormat) {
|
5701 | if (this.isEmpty()) {
|
5702 | return null;
|
5703 | }
|
5704 | var obj = {};
|
5705 | var numKeys = 0, maxKey = 0, allIntegerKeys = true;
|
5706 | this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
5707 | obj[key] = childNode.val(exportFormat);
|
5708 | numKeys++;
|
5709 | if (allIntegerKeys && ChildrenNode.INTEGER_REGEXP_.test(key)) {
|
5710 | maxKey = Math.max(maxKey, Number(key));
|
5711 | }
|
5712 | else {
|
5713 | allIntegerKeys = false;
|
5714 | }
|
5715 | });
|
5716 | if (!exportFormat && allIntegerKeys && maxKey < 2 * numKeys) {
|
5717 |
|
5718 | var array = [];
|
5719 |
|
5720 | for (var key in obj) {
|
5721 | array[key] = obj[key];
|
5722 | }
|
5723 | return array;
|
5724 | }
|
5725 | else {
|
5726 | if (exportFormat && !this.getPriority().isEmpty()) {
|
5727 | obj['.priority'] = this.getPriority().val();
|
5728 | }
|
5729 | return obj;
|
5730 | }
|
5731 | };
|
5732 |
|
5733 | ChildrenNode.prototype.hash = function () {
|
5734 | if (this.lazyHash_ === null) {
|
5735 | var toHash_1 = '';
|
5736 | if (!this.getPriority().isEmpty()) {
|
5737 | toHash_1 +=
|
5738 | 'priority:' +
|
5739 | priorityHashText(this.getPriority().val()) +
|
5740 | ':';
|
5741 | }
|
5742 | this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
5743 | var childHash = childNode.hash();
|
5744 | if (childHash !== '') {
|
5745 | toHash_1 += ':' + key + ':' + childHash;
|
5746 | }
|
5747 | });
|
5748 | this.lazyHash_ = toHash_1 === '' ? '' : sha1(toHash_1);
|
5749 | }
|
5750 | return this.lazyHash_;
|
5751 | };
|
5752 |
|
5753 | ChildrenNode.prototype.getPredecessorChildName = function (childName, childNode, index) {
|
5754 | var idx = this.resolveIndex_(index);
|
5755 | if (idx) {
|
5756 | var predecessor = idx.getPredecessorKey(new NamedNode(childName, childNode));
|
5757 | return predecessor ? predecessor.name : null;
|
5758 | }
|
5759 | else {
|
5760 | return this.children_.getPredecessorKey(childName);
|
5761 | }
|
5762 | };
|
5763 | ChildrenNode.prototype.getFirstChildName = function (indexDefinition) {
|
5764 | var idx = this.resolveIndex_(indexDefinition);
|
5765 | if (idx) {
|
5766 | var minKey = idx.minKey();
|
5767 | return minKey && minKey.name;
|
5768 | }
|
5769 | else {
|
5770 | return this.children_.minKey();
|
5771 | }
|
5772 | };
|
5773 | ChildrenNode.prototype.getFirstChild = function (indexDefinition) {
|
5774 | var minKey = this.getFirstChildName(indexDefinition);
|
5775 | if (minKey) {
|
5776 | return new NamedNode(minKey, this.children_.get(minKey));
|
5777 | }
|
5778 | else {
|
5779 | return null;
|
5780 | }
|
5781 | };
|
5782 | |
5783 |
|
5784 |
|
5785 | ChildrenNode.prototype.getLastChildName = function (indexDefinition) {
|
5786 | var idx = this.resolveIndex_(indexDefinition);
|
5787 | if (idx) {
|
5788 | var maxKey = idx.maxKey();
|
5789 | return maxKey && maxKey.name;
|
5790 | }
|
5791 | else {
|
5792 | return this.children_.maxKey();
|
5793 | }
|
5794 | };
|
5795 | ChildrenNode.prototype.getLastChild = function (indexDefinition) {
|
5796 | var maxKey = this.getLastChildName(indexDefinition);
|
5797 | if (maxKey) {
|
5798 | return new NamedNode(maxKey, this.children_.get(maxKey));
|
5799 | }
|
5800 | else {
|
5801 | return null;
|
5802 | }
|
5803 | };
|
5804 | ChildrenNode.prototype.forEachChild = function (index, action) {
|
5805 | var idx = this.resolveIndex_(index);
|
5806 | if (idx) {
|
5807 | return idx.inorderTraversal(function (wrappedNode) {
|
5808 | return action(wrappedNode.name, wrappedNode.node);
|
5809 | });
|
5810 | }
|
5811 | else {
|
5812 | return this.children_.inorderTraversal(action);
|
5813 | }
|
5814 | };
|
5815 | ChildrenNode.prototype.getIterator = function (indexDefinition) {
|
5816 | return this.getIteratorFrom(indexDefinition.minPost(), indexDefinition);
|
5817 | };
|
5818 | ChildrenNode.prototype.getIteratorFrom = function (startPost, indexDefinition) {
|
5819 | var idx = this.resolveIndex_(indexDefinition);
|
5820 | if (idx) {
|
5821 | return idx.getIteratorFrom(startPost, function (key) { return key; });
|
5822 | }
|
5823 | else {
|
5824 | var iterator = this.children_.getIteratorFrom(startPost.name, NamedNode.Wrap);
|
5825 | var next = iterator.peek();
|
5826 | while (next != null && indexDefinition.compare(next, startPost) < 0) {
|
5827 | iterator.getNext();
|
5828 | next = iterator.peek();
|
5829 | }
|
5830 | return iterator;
|
5831 | }
|
5832 | };
|
5833 | ChildrenNode.prototype.getReverseIterator = function (indexDefinition) {
|
5834 | return this.getReverseIteratorFrom(indexDefinition.maxPost(), indexDefinition);
|
5835 | };
|
5836 | ChildrenNode.prototype.getReverseIteratorFrom = function (endPost, indexDefinition) {
|
5837 | var idx = this.resolveIndex_(indexDefinition);
|
5838 | if (idx) {
|
5839 | return idx.getReverseIteratorFrom(endPost, function (key) {
|
5840 | return key;
|
5841 | });
|
5842 | }
|
5843 | else {
|
5844 | var iterator = this.children_.getReverseIteratorFrom(endPost.name, NamedNode.Wrap);
|
5845 | var next = iterator.peek();
|
5846 | while (next != null && indexDefinition.compare(next, endPost) > 0) {
|
5847 | iterator.getNext();
|
5848 | next = iterator.peek();
|
5849 | }
|
5850 | return iterator;
|
5851 | }
|
5852 | };
|
5853 | ChildrenNode.prototype.compareTo = function (other) {
|
5854 | if (this.isEmpty()) {
|
5855 | if (other.isEmpty()) {
|
5856 | return 0;
|
5857 | }
|
5858 | else {
|
5859 | return -1;
|
5860 | }
|
5861 | }
|
5862 | else if (other.isLeafNode() || other.isEmpty()) {
|
5863 | return 1;
|
5864 | }
|
5865 | else if (other === MAX_NODE) {
|
5866 | return -1;
|
5867 | }
|
5868 | else {
|
5869 |
|
5870 | return 0;
|
5871 | }
|
5872 | };
|
5873 | ChildrenNode.prototype.withIndex = function (indexDefinition) {
|
5874 | if (indexDefinition === KEY_INDEX ||
|
5875 | this.indexMap_.hasIndex(indexDefinition)) {
|
5876 | return this;
|
5877 | }
|
5878 | else {
|
5879 | var newIndexMap = this.indexMap_.addIndex(indexDefinition, this.children_);
|
5880 | return new ChildrenNode(this.children_, this.priorityNode_, newIndexMap);
|
5881 | }
|
5882 | };
|
5883 | ChildrenNode.prototype.isIndexed = function (index) {
|
5884 | return index === KEY_INDEX || this.indexMap_.hasIndex(index);
|
5885 | };
|
5886 | ChildrenNode.prototype.equals = function (other) {
|
5887 | if (other === this) {
|
5888 | return true;
|
5889 | }
|
5890 | else if (other.isLeafNode()) {
|
5891 | return false;
|
5892 | }
|
5893 | else {
|
5894 | var otherChildrenNode = other;
|
5895 | if (!this.getPriority().equals(otherChildrenNode.getPriority())) {
|
5896 | return false;
|
5897 | }
|
5898 | else if (this.children_.count() === otherChildrenNode.children_.count()) {
|
5899 | var thisIter = this.getIterator(PRIORITY_INDEX);
|
5900 | var otherIter = otherChildrenNode.getIterator(PRIORITY_INDEX);
|
5901 | var thisCurrent = thisIter.getNext();
|
5902 | var otherCurrent = otherIter.getNext();
|
5903 | while (thisCurrent && otherCurrent) {
|
5904 | if (thisCurrent.name !== otherCurrent.name ||
|
5905 | !thisCurrent.node.equals(otherCurrent.node)) {
|
5906 | return false;
|
5907 | }
|
5908 | thisCurrent = thisIter.getNext();
|
5909 | otherCurrent = otherIter.getNext();
|
5910 | }
|
5911 | return thisCurrent === null && otherCurrent === null;
|
5912 | }
|
5913 | else {
|
5914 | return false;
|
5915 | }
|
5916 | }
|
5917 | };
|
5918 | |
5919 |
|
5920 |
|
5921 |
|
5922 |
|
5923 | ChildrenNode.prototype.resolveIndex_ = function (indexDefinition) {
|
5924 | if (indexDefinition === KEY_INDEX) {
|
5925 | return null;
|
5926 | }
|
5927 | else {
|
5928 | return this.indexMap_.get(indexDefinition.toString());
|
5929 | }
|
5930 | };
|
5931 | ChildrenNode.INTEGER_REGEXP_ = /^(0|[1-9]\d*)$/;
|
5932 | return ChildrenNode;
|
5933 | }());
|
5934 | var MaxNode = (function (_super) {
|
5935 | tslib.__extends(MaxNode, _super);
|
5936 | function MaxNode() {
|
5937 | return _super.call(this, new SortedMap(NAME_COMPARATOR), ChildrenNode.EMPTY_NODE, IndexMap.Default) || this;
|
5938 | }
|
5939 | MaxNode.prototype.compareTo = function (other) {
|
5940 | if (other === this) {
|
5941 | return 0;
|
5942 | }
|
5943 | else {
|
5944 | return 1;
|
5945 | }
|
5946 | };
|
5947 | MaxNode.prototype.equals = function (other) {
|
5948 |
|
5949 | return other === this;
|
5950 | };
|
5951 | MaxNode.prototype.getPriority = function () {
|
5952 | return this;
|
5953 | };
|
5954 | MaxNode.prototype.getImmediateChild = function (childName) {
|
5955 | return ChildrenNode.EMPTY_NODE;
|
5956 | };
|
5957 | MaxNode.prototype.isEmpty = function () {
|
5958 | return false;
|
5959 | };
|
5960 | return MaxNode;
|
5961 | }(ChildrenNode));
|
5962 |
|
5963 |
|
5964 |
|
5965 | var MAX_NODE = new MaxNode();
|
5966 | Object.defineProperties(NamedNode, {
|
5967 | MIN: {
|
5968 | value: new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE)
|
5969 | },
|
5970 | MAX: {
|
5971 | value: new NamedNode(MAX_NAME, MAX_NODE)
|
5972 | }
|
5973 | });
|
5974 |
|
5975 |
|
5976 |
|
5977 | KeyIndex.__EMPTY_NODE = ChildrenNode.EMPTY_NODE;
|
5978 | LeafNode.__childrenNodeConstructor = ChildrenNode;
|
5979 | setMaxNode$1(MAX_NODE);
|
5980 | setMaxNode(MAX_NODE);
|
5981 |
|
5982 |
|
5983 |
|
5984 |
|
5985 |
|
5986 |
|
5987 |
|
5988 |
|
5989 |
|
5990 |
|
5991 |
|
5992 |
|
5993 |
|
5994 |
|
5995 |
|
5996 |
|
5997 |
|
5998 | var USE_HINZE = true;
|
5999 |
|
6000 |
|
6001 |
|
6002 |
|
6003 |
|
6004 |
|
6005 | function nodeFromJSON(json, priority) {
|
6006 | if (priority === void 0) { priority = null; }
|
6007 | if (json === null) {
|
6008 | return ChildrenNode.EMPTY_NODE;
|
6009 | }
|
6010 | if (typeof json === 'object' && '.priority' in json) {
|
6011 | priority = json['.priority'];
|
6012 | }
|
6013 | util.assert(priority === null ||
|
6014 | typeof priority === 'string' ||
|
6015 | typeof priority === 'number' ||
|
6016 | (typeof priority === 'object' && '.sv' in priority), 'Invalid priority type found: ' + typeof priority);
|
6017 | if (typeof json === 'object' && '.value' in json && json['.value'] !== null) {
|
6018 | json = json['.value'];
|
6019 | }
|
6020 |
|
6021 | if (typeof json !== 'object' || '.sv' in json) {
|
6022 | var jsonLeaf = json;
|
6023 | return new LeafNode(jsonLeaf, nodeFromJSON(priority));
|
6024 | }
|
6025 | if (!(json instanceof Array) && USE_HINZE) {
|
6026 | var children_1 = [];
|
6027 | var childrenHavePriority_1 = false;
|
6028 | var hinzeJsonObj = json;
|
6029 | each(hinzeJsonObj, function (key, child) {
|
6030 | if (key.substring(0, 1) !== '.') {
|
6031 |
|
6032 | var childNode = nodeFromJSON(child);
|
6033 | if (!childNode.isEmpty()) {
|
6034 | childrenHavePriority_1 =
|
6035 | childrenHavePriority_1 || !childNode.getPriority().isEmpty();
|
6036 | children_1.push(new NamedNode(key, childNode));
|
6037 | }
|
6038 | }
|
6039 | });
|
6040 | if (children_1.length === 0) {
|
6041 | return ChildrenNode.EMPTY_NODE;
|
6042 | }
|
6043 | var childSet = buildChildSet(children_1, NAME_ONLY_COMPARATOR, function (namedNode) { return namedNode.name; }, NAME_COMPARATOR);
|
6044 | if (childrenHavePriority_1) {
|
6045 | var sortedChildSet = buildChildSet(children_1, PRIORITY_INDEX.getCompare());
|
6046 | return new ChildrenNode(childSet, nodeFromJSON(priority), new IndexMap({ '.priority': sortedChildSet }, { '.priority': PRIORITY_INDEX }));
|
6047 | }
|
6048 | else {
|
6049 | return new ChildrenNode(childSet, nodeFromJSON(priority), IndexMap.Default);
|
6050 | }
|
6051 | }
|
6052 | else {
|
6053 | var node_1 = ChildrenNode.EMPTY_NODE;
|
6054 | each(json, function (key, childData) {
|
6055 | if (util.contains(json, key)) {
|
6056 | if (key.substring(0, 1) !== '.') {
|
6057 |
|
6058 | var childNode = nodeFromJSON(childData);
|
6059 | if (childNode.isLeafNode() || !childNode.isEmpty()) {
|
6060 | node_1 = node_1.updateImmediateChild(key, childNode);
|
6061 | }
|
6062 | }
|
6063 | }
|
6064 | });
|
6065 | return node_1.updatePriority(nodeFromJSON(priority));
|
6066 | }
|
6067 | }
|
6068 | setNodeFromJSON(nodeFromJSON);
|
6069 |
|
6070 |
|
6071 |
|
6072 |
|
6073 |
|
6074 |
|
6075 |
|
6076 |
|
6077 |
|
6078 |
|
6079 |
|
6080 |
|
6081 |
|
6082 |
|
6083 |
|
6084 |
|
6085 |
|
6086 | var PathIndex = (function (_super) {
|
6087 | tslib.__extends(PathIndex, _super);
|
6088 | function PathIndex(indexPath_) {
|
6089 | var _this = _super.call(this) || this;
|
6090 | _this.indexPath_ = indexPath_;
|
6091 | util.assert(!pathIsEmpty(indexPath_) && pathGetFront(indexPath_) !== '.priority', "Can't create PathIndex with empty path or .priority key");
|
6092 | return _this;
|
6093 | }
|
6094 | PathIndex.prototype.extractChild = function (snap) {
|
6095 | return snap.getChild(this.indexPath_);
|
6096 | };
|
6097 | PathIndex.prototype.isDefinedOn = function (node) {
|
6098 | return !node.getChild(this.indexPath_).isEmpty();
|
6099 | };
|
6100 | PathIndex.prototype.compare = function (a, b) {
|
6101 | var aChild = this.extractChild(a.node);
|
6102 | var bChild = this.extractChild(b.node);
|
6103 | var indexCmp = aChild.compareTo(bChild);
|
6104 | if (indexCmp === 0) {
|
6105 | return nameCompare(a.name, b.name);
|
6106 | }
|
6107 | else {
|
6108 | return indexCmp;
|
6109 | }
|
6110 | };
|
6111 | PathIndex.prototype.makePost = function (indexValue, name) {
|
6112 | var valueNode = nodeFromJSON(indexValue);
|
6113 | var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, valueNode);
|
6114 | return new NamedNode(name, node);
|
6115 | };
|
6116 | PathIndex.prototype.maxPost = function () {
|
6117 | var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, MAX_NODE);
|
6118 | return new NamedNode(MAX_NAME, node);
|
6119 | };
|
6120 | PathIndex.prototype.toString = function () {
|
6121 | return pathSlice(this.indexPath_, 0).join('/');
|
6122 | };
|
6123 | return PathIndex;
|
6124 | }(Index));
|
6125 |
|
6126 |
|
6127 |
|
6128 |
|
6129 |
|
6130 |
|
6131 |
|
6132 |
|
6133 |
|
6134 |
|
6135 |
|
6136 |
|
6137 |
|
6138 |
|
6139 |
|
6140 |
|
6141 |
|
6142 | var ValueIndex = (function (_super) {
|
6143 | tslib.__extends(ValueIndex, _super);
|
6144 | function ValueIndex() {
|
6145 | return _super !== null && _super.apply(this, arguments) || this;
|
6146 | }
|
6147 | ValueIndex.prototype.compare = function (a, b) {
|
6148 | var indexCmp = a.node.compareTo(b.node);
|
6149 | if (indexCmp === 0) {
|
6150 | return nameCompare(a.name, b.name);
|
6151 | }
|
6152 | else {
|
6153 | return indexCmp;
|
6154 | }
|
6155 | };
|
6156 | ValueIndex.prototype.isDefinedOn = function (node) {
|
6157 | return true;
|
6158 | };
|
6159 | ValueIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
|
6160 | return !oldNode.equals(newNode);
|
6161 | };
|
6162 | ValueIndex.prototype.minPost = function () {
|
6163 |
|
6164 | return NamedNode.MIN;
|
6165 | };
|
6166 | ValueIndex.prototype.maxPost = function () {
|
6167 |
|
6168 | return NamedNode.MAX;
|
6169 | };
|
6170 | ValueIndex.prototype.makePost = function (indexValue, name) {
|
6171 | var valueNode = nodeFromJSON(indexValue);
|
6172 | return new NamedNode(name, valueNode);
|
6173 | };
|
6174 | |
6175 |
|
6176 |
|
6177 | ValueIndex.prototype.toString = function () {
|
6178 | return '.value';
|
6179 | };
|
6180 | return ValueIndex;
|
6181 | }(Index));
|
6182 | var VALUE_INDEX = new ValueIndex();
|
6183 |
|
6184 |
|
6185 |
|
6186 |
|
6187 |
|
6188 |
|
6189 |
|
6190 |
|
6191 |
|
6192 |
|
6193 |
|
6194 |
|
6195 |
|
6196 |
|
6197 |
|
6198 |
|
6199 |
|
6200 |
|
6201 | var PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
|
6202 | var MIN_PUSH_CHAR = '-';
|
6203 | var MAX_PUSH_CHAR = 'z';
|
6204 | var MAX_KEY_LEN = 786;
|
6205 |
|
6206 |
|
6207 |
|
6208 |
|
6209 |
|
6210 |
|
6211 |
|
6212 |
|
6213 |
|
6214 |
|
6215 |
|
6216 |
|
6217 |
|
6218 |
|
6219 | var nextPushId = (function () {
|
6220 |
|
6221 |
|
6222 | var lastPushTime = 0;
|
6223 |
|
6224 |
|
6225 |
|
6226 |
|
6227 | var lastRandChars = [];
|
6228 | return function (now) {
|
6229 | var duplicateTime = now === lastPushTime;
|
6230 | lastPushTime = now;
|
6231 | var i;
|
6232 | var timeStampChars = new Array(8);
|
6233 | for (i = 7; i >= 0; i--) {
|
6234 | timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
|
6235 |
|
6236 |
|
6237 | now = Math.floor(now / 64);
|
6238 | }
|
6239 | util.assert(now === 0, 'Cannot push at time == 0');
|
6240 | var id = timeStampChars.join('');
|
6241 | if (!duplicateTime) {
|
6242 | for (i = 0; i < 12; i++) {
|
6243 | lastRandChars[i] = Math.floor(Math.random() * 64);
|
6244 | }
|
6245 | }
|
6246 | else {
|
6247 |
|
6248 |
|
6249 | for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
|
6250 | lastRandChars[i] = 0;
|
6251 | }
|
6252 | lastRandChars[i]++;
|
6253 | }
|
6254 | for (i = 0; i < 12; i++) {
|
6255 | id += PUSH_CHARS.charAt(lastRandChars[i]);
|
6256 | }
|
6257 | util.assert(id.length === 20, 'nextPushId: Length should be 20.');
|
6258 | return id;
|
6259 | };
|
6260 | })();
|
6261 | var successor = function (key) {
|
6262 | if (key === '' + INTEGER_32_MAX) {
|
6263 |
|
6264 | return MIN_PUSH_CHAR;
|
6265 | }
|
6266 | var keyAsInt = tryParseInt(key);
|
6267 | if (keyAsInt != null) {
|
6268 | return '' + (keyAsInt + 1);
|
6269 | }
|
6270 | var next = new Array(key.length);
|
6271 | for (var i_1 = 0; i_1 < next.length; i_1++) {
|
6272 | next[i_1] = key.charAt(i_1);
|
6273 | }
|
6274 | if (next.length < MAX_KEY_LEN) {
|
6275 | next.push(MIN_PUSH_CHAR);
|
6276 | return next.join('');
|
6277 | }
|
6278 | var i = next.length - 1;
|
6279 | while (i >= 0 && next[i] === MAX_PUSH_CHAR) {
|
6280 | i--;
|
6281 | }
|
6282 |
|
6283 |
|
6284 | if (i === -1) {
|
6285 | return MAX_NAME;
|
6286 | }
|
6287 | var source = next[i];
|
6288 | var sourcePlusOne = PUSH_CHARS.charAt(PUSH_CHARS.indexOf(source) + 1);
|
6289 | next[i] = sourcePlusOne;
|
6290 | return next.slice(0, i + 1).join('');
|
6291 | };
|
6292 |
|
6293 | var predecessor = function (key) {
|
6294 | if (key === '' + INTEGER_32_MIN) {
|
6295 | return MIN_NAME;
|
6296 | }
|
6297 | var keyAsInt = tryParseInt(key);
|
6298 | if (keyAsInt != null) {
|
6299 | return '' + (keyAsInt - 1);
|
6300 | }
|
6301 | var next = new Array(key.length);
|
6302 | for (var i = 0; i < next.length; i++) {
|
6303 | next[i] = key.charAt(i);
|
6304 | }
|
6305 |
|
6306 |
|
6307 |
|
6308 |
|
6309 |
|
6310 |
|
6311 |
|
6312 |
|
6313 |
|
6314 |
|
6315 |
|
6316 | if (next[next.length - 1] === MIN_PUSH_CHAR) {
|
6317 | if (next.length === 1) {
|
6318 |
|
6319 | return '' + INTEGER_32_MAX;
|
6320 | }
|
6321 | delete next[next.length - 1];
|
6322 | return next.join('');
|
6323 | }
|
6324 |
|
6325 |
|
6326 |
|
6327 | next[next.length - 1] = PUSH_CHARS.charAt(PUSH_CHARS.indexOf(next[next.length - 1]) - 1);
|
6328 | return next.join('') + MAX_PUSH_CHAR.repeat(MAX_KEY_LEN - next.length);
|
6329 | };
|
6330 |
|
6331 |
|
6332 |
|
6333 |
|
6334 |
|
6335 |
|
6336 |
|
6337 |
|
6338 |
|
6339 |
|
6340 |
|
6341 |
|
6342 |
|
6343 |
|
6344 |
|
6345 |
|
6346 |
|
6347 | function changeValue(snapshotNode) {
|
6348 | return { type: "value" , snapshotNode: snapshotNode };
|
6349 | }
|
6350 | function changeChildAdded(childName, snapshotNode) {
|
6351 | return { type: "child_added" , snapshotNode: snapshotNode, childName: childName };
|
6352 | }
|
6353 | function changeChildRemoved(childName, snapshotNode) {
|
6354 | return { type: "child_removed" , snapshotNode: snapshotNode, childName: childName };
|
6355 | }
|
6356 | function changeChildChanged(childName, snapshotNode, oldSnap) {
|
6357 | return {
|
6358 | type: "child_changed" ,
|
6359 | snapshotNode: snapshotNode,
|
6360 | childName: childName,
|
6361 | oldSnap: oldSnap
|
6362 | };
|
6363 | }
|
6364 | function changeChildMoved(childName, snapshotNode) {
|
6365 | return { type: "child_moved" , snapshotNode: snapshotNode, childName: childName };
|
6366 | }
|
6367 |
|
6368 |
|
6369 |
|
6370 |
|
6371 |
|
6372 |
|
6373 |
|
6374 |
|
6375 |
|
6376 |
|
6377 |
|
6378 |
|
6379 |
|
6380 |
|
6381 |
|
6382 |
|
6383 |
|
6384 |
|
6385 |
|
6386 |
|
6387 | var IndexedFilter = (function () {
|
6388 | function IndexedFilter(index_) {
|
6389 | this.index_ = index_;
|
6390 | }
|
6391 | IndexedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
|
6392 | util.assert(snap.isIndexed(this.index_), 'A node must be indexed if only a child is updated');
|
6393 | var oldChild = snap.getImmediateChild(key);
|
6394 |
|
6395 | if (oldChild.getChild(affectedPath).equals(newChild.getChild(affectedPath))) {
|
6396 |
|
6397 |
|
6398 |
|
6399 | if (oldChild.isEmpty() === newChild.isEmpty()) {
|
6400 |
|
6401 |
|
6402 |
|
6403 | return snap;
|
6404 | }
|
6405 | }
|
6406 | if (optChangeAccumulator != null) {
|
6407 | if (newChild.isEmpty()) {
|
6408 | if (snap.hasChild(key)) {
|
6409 | optChangeAccumulator.trackChildChange(changeChildRemoved(key, oldChild));
|
6410 | }
|
6411 | else {
|
6412 | util.assert(snap.isLeafNode(), 'A child remove without an old child only makes sense on a leaf node');
|
6413 | }
|
6414 | }
|
6415 | else if (oldChild.isEmpty()) {
|
6416 | optChangeAccumulator.trackChildChange(changeChildAdded(key, newChild));
|
6417 | }
|
6418 | else {
|
6419 | optChangeAccumulator.trackChildChange(changeChildChanged(key, newChild, oldChild));
|
6420 | }
|
6421 | }
|
6422 | if (snap.isLeafNode() && newChild.isEmpty()) {
|
6423 | return snap;
|
6424 | }
|
6425 | else {
|
6426 |
|
6427 | return snap.updateImmediateChild(key, newChild).withIndex(this.index_);
|
6428 | }
|
6429 | };
|
6430 | IndexedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
|
6431 | if (optChangeAccumulator != null) {
|
6432 | if (!oldSnap.isLeafNode()) {
|
6433 | oldSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
6434 | if (!newSnap.hasChild(key)) {
|
6435 | optChangeAccumulator.trackChildChange(changeChildRemoved(key, childNode));
|
6436 | }
|
6437 | });
|
6438 | }
|
6439 | if (!newSnap.isLeafNode()) {
|
6440 | newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
6441 | if (oldSnap.hasChild(key)) {
|
6442 | var oldChild = oldSnap.getImmediateChild(key);
|
6443 | if (!oldChild.equals(childNode)) {
|
6444 | optChangeAccumulator.trackChildChange(changeChildChanged(key, childNode, oldChild));
|
6445 | }
|
6446 | }
|
6447 | else {
|
6448 | optChangeAccumulator.trackChildChange(changeChildAdded(key, childNode));
|
6449 | }
|
6450 | });
|
6451 | }
|
6452 | }
|
6453 | return newSnap.withIndex(this.index_);
|
6454 | };
|
6455 | IndexedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
|
6456 | if (oldSnap.isEmpty()) {
|
6457 | return ChildrenNode.EMPTY_NODE;
|
6458 | }
|
6459 | else {
|
6460 | return oldSnap.updatePriority(newPriority);
|
6461 | }
|
6462 | };
|
6463 | IndexedFilter.prototype.filtersNodes = function () {
|
6464 | return false;
|
6465 | };
|
6466 | IndexedFilter.prototype.getIndexedFilter = function () {
|
6467 | return this;
|
6468 | };
|
6469 | IndexedFilter.prototype.getIndex = function () {
|
6470 | return this.index_;
|
6471 | };
|
6472 | return IndexedFilter;
|
6473 | }());
|
6474 |
|
6475 |
|
6476 |
|
6477 |
|
6478 |
|
6479 |
|
6480 |
|
6481 |
|
6482 |
|
6483 |
|
6484 |
|
6485 |
|
6486 |
|
6487 |
|
6488 |
|
6489 |
|
6490 |
|
6491 |
|
6492 |
|
6493 |
|
6494 | var RangedFilter = (function () {
|
6495 | function RangedFilter(params) {
|
6496 | this.indexedFilter_ = new IndexedFilter(params.getIndex());
|
6497 | this.index_ = params.getIndex();
|
6498 | this.startPost_ = RangedFilter.getStartPost_(params);
|
6499 | this.endPost_ = RangedFilter.getEndPost_(params);
|
6500 | }
|
6501 | RangedFilter.prototype.getStartPost = function () {
|
6502 | return this.startPost_;
|
6503 | };
|
6504 | RangedFilter.prototype.getEndPost = function () {
|
6505 | return this.endPost_;
|
6506 | };
|
6507 | RangedFilter.prototype.matches = function (node) {
|
6508 | return (this.index_.compare(this.getStartPost(), node) <= 0 &&
|
6509 | this.index_.compare(node, this.getEndPost()) <= 0);
|
6510 | };
|
6511 | RangedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
|
6512 | if (!this.matches(new NamedNode(key, newChild))) {
|
6513 | newChild = ChildrenNode.EMPTY_NODE;
|
6514 | }
|
6515 | return this.indexedFilter_.updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
|
6516 | };
|
6517 | RangedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
|
6518 | if (newSnap.isLeafNode()) {
|
6519 |
|
6520 | newSnap = ChildrenNode.EMPTY_NODE;
|
6521 | }
|
6522 | var filtered = newSnap.withIndex(this.index_);
|
6523 |
|
6524 | filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
|
6525 | var self = this;
|
6526 | newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
6527 | if (!self.matches(new NamedNode(key, childNode))) {
|
6528 | filtered = filtered.updateImmediateChild(key, ChildrenNode.EMPTY_NODE);
|
6529 | }
|
6530 | });
|
6531 | return this.indexedFilter_.updateFullNode(oldSnap, filtered, optChangeAccumulator);
|
6532 | };
|
6533 | RangedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
|
6534 |
|
6535 | return oldSnap;
|
6536 | };
|
6537 | RangedFilter.prototype.filtersNodes = function () {
|
6538 | return true;
|
6539 | };
|
6540 | RangedFilter.prototype.getIndexedFilter = function () {
|
6541 | return this.indexedFilter_;
|
6542 | };
|
6543 | RangedFilter.prototype.getIndex = function () {
|
6544 | return this.index_;
|
6545 | };
|
6546 | RangedFilter.getStartPost_ = function (params) {
|
6547 | if (params.hasStart()) {
|
6548 | var startName = params.getIndexStartName();
|
6549 | return params.getIndex().makePost(params.getIndexStartValue(), startName);
|
6550 | }
|
6551 | else {
|
6552 | return params.getIndex().minPost();
|
6553 | }
|
6554 | };
|
6555 | RangedFilter.getEndPost_ = function (params) {
|
6556 | if (params.hasEnd()) {
|
6557 | var endName = params.getIndexEndName();
|
6558 | return params.getIndex().makePost(params.getIndexEndValue(), endName);
|
6559 | }
|
6560 | else {
|
6561 | return params.getIndex().maxPost();
|
6562 | }
|
6563 | };
|
6564 | return RangedFilter;
|
6565 | }());
|
6566 |
|
6567 |
|
6568 |
|
6569 |
|
6570 |
|
6571 |
|
6572 |
|
6573 |
|
6574 |
|
6575 |
|
6576 |
|
6577 |
|
6578 |
|
6579 |
|
6580 |
|
6581 |
|
6582 |
|
6583 |
|
6584 |
|
6585 |
|
6586 | var LimitedFilter = (function () {
|
6587 | function LimitedFilter(params) {
|
6588 | this.rangedFilter_ = new RangedFilter(params);
|
6589 | this.index_ = params.getIndex();
|
6590 | this.limit_ = params.getLimit();
|
6591 | this.reverse_ = !params.isViewFromLeft();
|
6592 | }
|
6593 | LimitedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
|
6594 | if (!this.rangedFilter_.matches(new NamedNode(key, newChild))) {
|
6595 | newChild = ChildrenNode.EMPTY_NODE;
|
6596 | }
|
6597 | if (snap.getImmediateChild(key).equals(newChild)) {
|
6598 |
|
6599 | return snap;
|
6600 | }
|
6601 | else if (snap.numChildren() < this.limit_) {
|
6602 | return this.rangedFilter_
|
6603 | .getIndexedFilter()
|
6604 | .updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
|
6605 | }
|
6606 | else {
|
6607 | return this.fullLimitUpdateChild_(snap, key, newChild, source, optChangeAccumulator);
|
6608 | }
|
6609 | };
|
6610 | LimitedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
|
6611 | var filtered;
|
6612 | if (newSnap.isLeafNode() || newSnap.isEmpty()) {
|
6613 |
|
6614 | filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
|
6615 | }
|
6616 | else {
|
6617 | if (this.limit_ * 2 < newSnap.numChildren() &&
|
6618 | newSnap.isIndexed(this.index_)) {
|
6619 |
|
6620 | filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
|
6621 |
|
6622 | var iterator = void 0;
|
6623 | if (this.reverse_) {
|
6624 | iterator = newSnap.getReverseIteratorFrom(this.rangedFilter_.getEndPost(), this.index_);
|
6625 | }
|
6626 | else {
|
6627 | iterator = newSnap.getIteratorFrom(this.rangedFilter_.getStartPost(), this.index_);
|
6628 | }
|
6629 | var count = 0;
|
6630 | while (iterator.hasNext() && count < this.limit_) {
|
6631 | var next = iterator.getNext();
|
6632 | var inRange = void 0;
|
6633 | if (this.reverse_) {
|
6634 | inRange =
|
6635 | this.index_.compare(this.rangedFilter_.getStartPost(), next) <= 0;
|
6636 | }
|
6637 | else {
|
6638 | inRange =
|
6639 | this.index_.compare(next, this.rangedFilter_.getEndPost()) <= 0;
|
6640 | }
|
6641 | if (inRange) {
|
6642 | filtered = filtered.updateImmediateChild(next.name, next.node);
|
6643 | count++;
|
6644 | }
|
6645 | else {
|
6646 |
|
6647 | break;
|
6648 | }
|
6649 | }
|
6650 | }
|
6651 | else {
|
6652 |
|
6653 | filtered = newSnap.withIndex(this.index_);
|
6654 |
|
6655 | filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
|
6656 | var startPost = void 0;
|
6657 | var endPost = void 0;
|
6658 | var cmp = void 0;
|
6659 | var iterator = void 0;
|
6660 | if (this.reverse_) {
|
6661 | iterator = filtered.getReverseIterator(this.index_);
|
6662 | startPost = this.rangedFilter_.getEndPost();
|
6663 | endPost = this.rangedFilter_.getStartPost();
|
6664 | var indexCompare_1 = this.index_.getCompare();
|
6665 | cmp = function (a, b) { return indexCompare_1(b, a); };
|
6666 | }
|
6667 | else {
|
6668 | iterator = filtered.getIterator(this.index_);
|
6669 | startPost = this.rangedFilter_.getStartPost();
|
6670 | endPost = this.rangedFilter_.getEndPost();
|
6671 | cmp = this.index_.getCompare();
|
6672 | }
|
6673 | var count = 0;
|
6674 | var foundStartPost = false;
|
6675 | while (iterator.hasNext()) {
|
6676 | var next = iterator.getNext();
|
6677 | if (!foundStartPost && cmp(startPost, next) <= 0) {
|
6678 |
|
6679 | foundStartPost = true;
|
6680 | }
|
6681 | var inRange = foundStartPost && count < this.limit_ && cmp(next, endPost) <= 0;
|
6682 | if (inRange) {
|
6683 | count++;
|
6684 | }
|
6685 | else {
|
6686 | filtered = filtered.updateImmediateChild(next.name, ChildrenNode.EMPTY_NODE);
|
6687 | }
|
6688 | }
|
6689 | }
|
6690 | }
|
6691 | return this.rangedFilter_
|
6692 | .getIndexedFilter()
|
6693 | .updateFullNode(oldSnap, filtered, optChangeAccumulator);
|
6694 | };
|
6695 | LimitedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
|
6696 |
|
6697 | return oldSnap;
|
6698 | };
|
6699 | LimitedFilter.prototype.filtersNodes = function () {
|
6700 | return true;
|
6701 | };
|
6702 | LimitedFilter.prototype.getIndexedFilter = function () {
|
6703 | return this.rangedFilter_.getIndexedFilter();
|
6704 | };
|
6705 | LimitedFilter.prototype.getIndex = function () {
|
6706 | return this.index_;
|
6707 | };
|
6708 | LimitedFilter.prototype.fullLimitUpdateChild_ = function (snap, childKey, childSnap, source, changeAccumulator) {
|
6709 |
|
6710 | var cmp;
|
6711 | if (this.reverse_) {
|
6712 | var indexCmp_1 = this.index_.getCompare();
|
6713 | cmp = function (a, b) { return indexCmp_1(b, a); };
|
6714 | }
|
6715 | else {
|
6716 | cmp = this.index_.getCompare();
|
6717 | }
|
6718 | var oldEventCache = snap;
|
6719 | util.assert(oldEventCache.numChildren() === this.limit_, '');
|
6720 | var newChildNamedNode = new NamedNode(childKey, childSnap);
|
6721 | var windowBoundary = this.reverse_
|
6722 | ? oldEventCache.getFirstChild(this.index_)
|
6723 | : oldEventCache.getLastChild(this.index_);
|
6724 | var inRange = this.rangedFilter_.matches(newChildNamedNode);
|
6725 | if (oldEventCache.hasChild(childKey)) {
|
6726 | var oldChildSnap = oldEventCache.getImmediateChild(childKey);
|
6727 | var nextChild = source.getChildAfterChild(this.index_, windowBoundary, this.reverse_);
|
6728 | while (nextChild != null &&
|
6729 | (nextChild.name === childKey || oldEventCache.hasChild(nextChild.name))) {
|
6730 |
|
6731 |
|
6732 |
|
6733 | nextChild = source.getChildAfterChild(this.index_, nextChild, this.reverse_);
|
6734 | }
|
6735 | var compareNext = nextChild == null ? 1 : cmp(nextChild, newChildNamedNode);
|
6736 | var remainsInWindow = inRange && !childSnap.isEmpty() && compareNext >= 0;
|
6737 | if (remainsInWindow) {
|
6738 | if (changeAccumulator != null) {
|
6739 | changeAccumulator.trackChildChange(changeChildChanged(childKey, childSnap, oldChildSnap));
|
6740 | }
|
6741 | return oldEventCache.updateImmediateChild(childKey, childSnap);
|
6742 | }
|
6743 | else {
|
6744 | if (changeAccumulator != null) {
|
6745 | changeAccumulator.trackChildChange(changeChildRemoved(childKey, oldChildSnap));
|
6746 | }
|
6747 | var newEventCache = oldEventCache.updateImmediateChild(childKey, ChildrenNode.EMPTY_NODE);
|
6748 | var nextChildInRange = nextChild != null && this.rangedFilter_.matches(nextChild);
|
6749 | if (nextChildInRange) {
|
6750 | if (changeAccumulator != null) {
|
6751 | changeAccumulator.trackChildChange(changeChildAdded(nextChild.name, nextChild.node));
|
6752 | }
|
6753 | return newEventCache.updateImmediateChild(nextChild.name, nextChild.node);
|
6754 | }
|
6755 | else {
|
6756 | return newEventCache;
|
6757 | }
|
6758 | }
|
6759 | }
|
6760 | else if (childSnap.isEmpty()) {
|
6761 |
|
6762 | return snap;
|
6763 | }
|
6764 | else if (inRange) {
|
6765 | if (cmp(windowBoundary, newChildNamedNode) >= 0) {
|
6766 | if (changeAccumulator != null) {
|
6767 | changeAccumulator.trackChildChange(changeChildRemoved(windowBoundary.name, windowBoundary.node));
|
6768 | changeAccumulator.trackChildChange(changeChildAdded(childKey, childSnap));
|
6769 | }
|
6770 | return oldEventCache
|
6771 | .updateImmediateChild(childKey, childSnap)
|
6772 | .updateImmediateChild(windowBoundary.name, ChildrenNode.EMPTY_NODE);
|
6773 | }
|
6774 | else {
|
6775 | return snap;
|
6776 | }
|
6777 | }
|
6778 | else {
|
6779 | return snap;
|
6780 | }
|
6781 | };
|
6782 | return LimitedFilter;
|
6783 | }());
|
6784 |
|
6785 |
|
6786 |
|
6787 |
|
6788 |
|
6789 |
|
6790 |
|
6791 |
|
6792 |
|
6793 |
|
6794 |
|
6795 |
|
6796 |
|
6797 |
|
6798 |
|
6799 |
|
6800 |
|
6801 |
|
6802 |
|
6803 |
|
6804 |
|
6805 |
|
6806 |
|
6807 |
|
6808 | var QueryParams = (function () {
|
6809 | function QueryParams() {
|
6810 | this.limitSet_ = false;
|
6811 | this.startSet_ = false;
|
6812 | this.startNameSet_ = false;
|
6813 | this.startAfterSet_ = false;
|
6814 | this.endSet_ = false;
|
6815 | this.endNameSet_ = false;
|
6816 | this.endBeforeSet_ = false;
|
6817 | this.limit_ = 0;
|
6818 | this.viewFrom_ = '';
|
6819 | this.indexStartValue_ = null;
|
6820 | this.indexStartName_ = '';
|
6821 | this.indexEndValue_ = null;
|
6822 | this.indexEndName_ = '';
|
6823 | this.index_ = PRIORITY_INDEX;
|
6824 | }
|
6825 | QueryParams.prototype.hasStart = function () {
|
6826 | return this.startSet_;
|
6827 | };
|
6828 | QueryParams.prototype.hasStartAfter = function () {
|
6829 | return this.startAfterSet_;
|
6830 | };
|
6831 | QueryParams.prototype.hasEndBefore = function () {
|
6832 | return this.endBeforeSet_;
|
6833 | };
|
6834 | |
6835 |
|
6836 |
|
6837 | QueryParams.prototype.isViewFromLeft = function () {
|
6838 | if (this.viewFrom_ === '') {
|
6839 |
|
6840 |
|
6841 |
|
6842 |
|
6843 | return this.startSet_;
|
6844 | }
|
6845 | else {
|
6846 | return this.viewFrom_ === "l" ;
|
6847 | }
|
6848 | };
|
6849 | |
6850 |
|
6851 |
|
6852 | QueryParams.prototype.getIndexStartValue = function () {
|
6853 | util.assert(this.startSet_, 'Only valid if start has been set');
|
6854 | return this.indexStartValue_;
|
6855 | };
|
6856 | |
6857 |
|
6858 |
|
6859 |
|
6860 | QueryParams.prototype.getIndexStartName = function () {
|
6861 | util.assert(this.startSet_, 'Only valid if start has been set');
|
6862 | if (this.startNameSet_) {
|
6863 | return this.indexStartName_;
|
6864 | }
|
6865 | else {
|
6866 | return MIN_NAME;
|
6867 | }
|
6868 | };
|
6869 | QueryParams.prototype.hasEnd = function () {
|
6870 | return this.endSet_;
|
6871 | };
|
6872 | |
6873 |
|
6874 |
|
6875 | QueryParams.prototype.getIndexEndValue = function () {
|
6876 | util.assert(this.endSet_, 'Only valid if end has been set');
|
6877 | return this.indexEndValue_;
|
6878 | };
|
6879 | |
6880 |
|
6881 |
|
6882 |
|
6883 | QueryParams.prototype.getIndexEndName = function () {
|
6884 | util.assert(this.endSet_, 'Only valid if end has been set');
|
6885 | if (this.endNameSet_) {
|
6886 | return this.indexEndName_;
|
6887 | }
|
6888 | else {
|
6889 | return MAX_NAME;
|
6890 | }
|
6891 | };
|
6892 | QueryParams.prototype.hasLimit = function () {
|
6893 | return this.limitSet_;
|
6894 | };
|
6895 | |
6896 |
|
6897 |
|
6898 | QueryParams.prototype.hasAnchoredLimit = function () {
|
6899 | return this.limitSet_ && this.viewFrom_ !== '';
|
6900 | };
|
6901 | |
6902 |
|
6903 |
|
6904 | QueryParams.prototype.getLimit = function () {
|
6905 | util.assert(this.limitSet_, 'Only valid if limit has been set');
|
6906 | return this.limit_;
|
6907 | };
|
6908 | QueryParams.prototype.getIndex = function () {
|
6909 | return this.index_;
|
6910 | };
|
6911 | QueryParams.prototype.loadsAllData = function () {
|
6912 | return !(this.startSet_ || this.endSet_ || this.limitSet_);
|
6913 | };
|
6914 | QueryParams.prototype.isDefault = function () {
|
6915 | return this.loadsAllData() && this.index_ === PRIORITY_INDEX;
|
6916 | };
|
6917 | QueryParams.prototype.copy = function () {
|
6918 | var copy = new QueryParams();
|
6919 | copy.limitSet_ = this.limitSet_;
|
6920 | copy.limit_ = this.limit_;
|
6921 | copy.startSet_ = this.startSet_;
|
6922 | copy.indexStartValue_ = this.indexStartValue_;
|
6923 | copy.startNameSet_ = this.startNameSet_;
|
6924 | copy.indexStartName_ = this.indexStartName_;
|
6925 | copy.endSet_ = this.endSet_;
|
6926 | copy.indexEndValue_ = this.indexEndValue_;
|
6927 | copy.endNameSet_ = this.endNameSet_;
|
6928 | copy.indexEndName_ = this.indexEndName_;
|
6929 | copy.index_ = this.index_;
|
6930 | copy.viewFrom_ = this.viewFrom_;
|
6931 | return copy;
|
6932 | };
|
6933 | return QueryParams;
|
6934 | }());
|
6935 | function queryParamsGetNodeFilter(queryParams) {
|
6936 | if (queryParams.loadsAllData()) {
|
6937 | return new IndexedFilter(queryParams.getIndex());
|
6938 | }
|
6939 | else if (queryParams.hasLimit()) {
|
6940 | return new LimitedFilter(queryParams);
|
6941 | }
|
6942 | else {
|
6943 | return new RangedFilter(queryParams);
|
6944 | }
|
6945 | }
|
6946 | function queryParamsLimitToFirst(queryParams, newLimit) {
|
6947 | var newParams = queryParams.copy();
|
6948 | newParams.limitSet_ = true;
|
6949 | newParams.limit_ = newLimit;
|
6950 | newParams.viewFrom_ = "l" ;
|
6951 | return newParams;
|
6952 | }
|
6953 | function queryParamsLimitToLast(queryParams, newLimit) {
|
6954 | var newParams = queryParams.copy();
|
6955 | newParams.limitSet_ = true;
|
6956 | newParams.limit_ = newLimit;
|
6957 | newParams.viewFrom_ = "r" ;
|
6958 | return newParams;
|
6959 | }
|
6960 | function queryParamsStartAt(queryParams, indexValue, key) {
|
6961 | var newParams = queryParams.copy();
|
6962 | newParams.startSet_ = true;
|
6963 | if (indexValue === undefined) {
|
6964 | indexValue = null;
|
6965 | }
|
6966 | newParams.indexStartValue_ = indexValue;
|
6967 | if (key != null) {
|
6968 | newParams.startNameSet_ = true;
|
6969 | newParams.indexStartName_ = key;
|
6970 | }
|
6971 | else {
|
6972 | newParams.startNameSet_ = false;
|
6973 | newParams.indexStartName_ = '';
|
6974 | }
|
6975 | return newParams;
|
6976 | }
|
6977 | function queryParamsStartAfter(queryParams, indexValue, key) {
|
6978 | var params;
|
6979 | if (queryParams.index_ === KEY_INDEX) {
|
6980 | if (typeof indexValue === 'string') {
|
6981 | indexValue = successor(indexValue);
|
6982 | }
|
6983 | params = queryParamsStartAt(queryParams, indexValue, key);
|
6984 | }
|
6985 | else {
|
6986 | var childKey = void 0;
|
6987 | if (key == null) {
|
6988 | childKey = MAX_NAME;
|
6989 | }
|
6990 | else {
|
6991 | childKey = successor(key);
|
6992 | }
|
6993 | params = queryParamsStartAt(queryParams, indexValue, childKey);
|
6994 | }
|
6995 | params.startAfterSet_ = true;
|
6996 | return params;
|
6997 | }
|
6998 | function queryParamsEndAt(queryParams, indexValue, key) {
|
6999 | var newParams = queryParams.copy();
|
7000 | newParams.endSet_ = true;
|
7001 | if (indexValue === undefined) {
|
7002 | indexValue = null;
|
7003 | }
|
7004 | newParams.indexEndValue_ = indexValue;
|
7005 | if (key !== undefined) {
|
7006 | newParams.endNameSet_ = true;
|
7007 | newParams.indexEndName_ = key;
|
7008 | }
|
7009 | else {
|
7010 | newParams.endNameSet_ = false;
|
7011 | newParams.indexEndName_ = '';
|
7012 | }
|
7013 | return newParams;
|
7014 | }
|
7015 | function queryParamsEndBefore(queryParams, indexValue, key) {
|
7016 | var childKey;
|
7017 | var params;
|
7018 | if (queryParams.index_ === KEY_INDEX) {
|
7019 | if (typeof indexValue === 'string') {
|
7020 | indexValue = predecessor(indexValue);
|
7021 | }
|
7022 | params = queryParamsEndAt(queryParams, indexValue, key);
|
7023 | }
|
7024 | else {
|
7025 | if (key == null) {
|
7026 | childKey = MIN_NAME;
|
7027 | }
|
7028 | else {
|
7029 | childKey = predecessor(key);
|
7030 | }
|
7031 | params = queryParamsEndAt(queryParams, indexValue, childKey);
|
7032 | }
|
7033 | params.endBeforeSet_ = true;
|
7034 | return params;
|
7035 | }
|
7036 | function queryParamsOrderBy(queryParams, index) {
|
7037 | var newParams = queryParams.copy();
|
7038 | newParams.index_ = index;
|
7039 | return newParams;
|
7040 | }
|
7041 |
|
7042 |
|
7043 |
|
7044 |
|
7045 |
|
7046 | function queryParamsToRestQueryStringParameters(queryParams) {
|
7047 | var qs = {};
|
7048 | if (queryParams.isDefault()) {
|
7049 | return qs;
|
7050 | }
|
7051 | var orderBy;
|
7052 | if (queryParams.index_ === PRIORITY_INDEX) {
|
7053 | orderBy = "$priority" ;
|
7054 | }
|
7055 | else if (queryParams.index_ === VALUE_INDEX) {
|
7056 | orderBy = "$value" ;
|
7057 | }
|
7058 | else if (queryParams.index_ === KEY_INDEX) {
|
7059 | orderBy = "$key" ;
|
7060 | }
|
7061 | else {
|
7062 | util.assert(queryParams.index_ instanceof PathIndex, 'Unrecognized index type!');
|
7063 | orderBy = queryParams.index_.toString();
|
7064 | }
|
7065 | qs["orderBy" ] = util.stringify(orderBy);
|
7066 | if (queryParams.startSet_) {
|
7067 | qs["startAt" ] = util.stringify(queryParams.indexStartValue_);
|
7068 | if (queryParams.startNameSet_) {
|
7069 | qs["startAt" ] +=
|
7070 | ',' + util.stringify(queryParams.indexStartName_);
|
7071 | }
|
7072 | }
|
7073 | if (queryParams.endSet_) {
|
7074 | qs["endAt" ] = util.stringify(queryParams.indexEndValue_);
|
7075 | if (queryParams.endNameSet_) {
|
7076 | qs["endAt" ] +=
|
7077 | ',' + util.stringify(queryParams.indexEndName_);
|
7078 | }
|
7079 | }
|
7080 | if (queryParams.limitSet_) {
|
7081 | if (queryParams.isViewFromLeft()) {
|
7082 | qs["limitToFirst" ] = queryParams.limit_;
|
7083 | }
|
7084 | else {
|
7085 | qs["limitToLast" ] = queryParams.limit_;
|
7086 | }
|
7087 | }
|
7088 | return qs;
|
7089 | }
|
7090 | function queryParamsGetQueryObject(queryParams) {
|
7091 | var obj = {};
|
7092 | if (queryParams.startSet_) {
|
7093 | obj["sp" ] =
|
7094 | queryParams.indexStartValue_;
|
7095 | if (queryParams.startNameSet_) {
|
7096 | obj["sn" ] =
|
7097 | queryParams.indexStartName_;
|
7098 | }
|
7099 | }
|
7100 | if (queryParams.endSet_) {
|
7101 | obj["ep" ] = queryParams.indexEndValue_;
|
7102 | if (queryParams.endNameSet_) {
|
7103 | obj["en" ] = queryParams.indexEndName_;
|
7104 | }
|
7105 | }
|
7106 | if (queryParams.limitSet_) {
|
7107 | obj["l" ] = queryParams.limit_;
|
7108 | var viewFrom = queryParams.viewFrom_;
|
7109 | if (viewFrom === '') {
|
7110 | if (queryParams.isViewFromLeft()) {
|
7111 | viewFrom = "l" ;
|
7112 | }
|
7113 | else {
|
7114 | viewFrom = "r" ;
|
7115 | }
|
7116 | }
|
7117 | obj["vf" ] = viewFrom;
|
7118 | }
|
7119 |
|
7120 | if (queryParams.index_ !== PRIORITY_INDEX) {
|
7121 | obj["i" ] = queryParams.index_.toString();
|
7122 | }
|
7123 | return obj;
|
7124 | }
|
7125 |
|
7126 |
|
7127 |
|
7128 |
|
7129 |
|
7130 |
|
7131 |
|
7132 |
|
7133 |
|
7134 |
|
7135 |
|
7136 |
|
7137 |
|
7138 |
|
7139 |
|
7140 |
|
7141 |
|
7142 |
|
7143 |
|
7144 |
|
7145 |
|
7146 |
|
7147 | var ReadonlyRestClient = (function (_super) {
|
7148 | tslib.__extends(ReadonlyRestClient, _super);
|
7149 | |
7150 |
|
7151 |
|
7152 |
|
7153 | function ReadonlyRestClient(repoInfo_, onDataUpdate_, authTokenProvider_, appCheckTokenProvider_) {
|
7154 | var _this = _super.call(this) || this;
|
7155 | _this.repoInfo_ = repoInfo_;
|
7156 | _this.onDataUpdate_ = onDataUpdate_;
|
7157 | _this.authTokenProvider_ = authTokenProvider_;
|
7158 | _this.appCheckTokenProvider_ = appCheckTokenProvider_;
|
7159 |
|
7160 | _this.log_ = logWrapper('p:rest:');
|
7161 | |
7162 |
|
7163 |
|
7164 |
|
7165 | _this.listens_ = {};
|
7166 | return _this;
|
7167 | }
|
7168 | ReadonlyRestClient.prototype.reportStats = function (stats) {
|
7169 | throw new Error('Method not implemented.');
|
7170 | };
|
7171 | ReadonlyRestClient.getListenId_ = function (query, tag) {
|
7172 | if (tag !== undefined) {
|
7173 | return 'tag$' + tag;
|
7174 | }
|
7175 | else {
|
7176 | util.assert(query._queryParams.isDefault(), "should have a tag if it's not a default query.");
|
7177 | return query._path.toString();
|
7178 | }
|
7179 | };
|
7180 |
|
7181 | ReadonlyRestClient.prototype.listen = function (query, currentHashFn, tag, onComplete) {
|
7182 | var _this = this;
|
7183 | var pathString = query._path.toString();
|
7184 | this.log_('Listen called for ' + pathString + ' ' + query._queryIdentifier);
|
7185 |
|
7186 | var listenId = ReadonlyRestClient.getListenId_(query, tag);
|
7187 | var thisListen = {};
|
7188 | this.listens_[listenId] = thisListen;
|
7189 | var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
|
7190 | this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
|
7191 | var data = result;
|
7192 | if (error === 404) {
|
7193 | data = null;
|
7194 | error = null;
|
7195 | }
|
7196 | if (error === null) {
|
7197 | _this.onDataUpdate_(pathString, data, false, tag);
|
7198 | }
|
7199 | if (util.safeGet(_this.listens_, listenId) === thisListen) {
|
7200 | var status_1;
|
7201 | if (!error) {
|
7202 | status_1 = 'ok';
|
7203 | }
|
7204 | else if (error === 401) {
|
7205 | status_1 = 'permission_denied';
|
7206 | }
|
7207 | else {
|
7208 | status_1 = 'rest_error:' + error;
|
7209 | }
|
7210 | onComplete(status_1, null);
|
7211 | }
|
7212 | });
|
7213 | };
|
7214 |
|
7215 | ReadonlyRestClient.prototype.unlisten = function (query, tag) {
|
7216 | var listenId = ReadonlyRestClient.getListenId_(query, tag);
|
7217 | delete this.listens_[listenId];
|
7218 | };
|
7219 | ReadonlyRestClient.prototype.get = function (query) {
|
7220 | var _this = this;
|
7221 | var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
|
7222 | var pathString = query._path.toString();
|
7223 | var deferred = new util.Deferred();
|
7224 | this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
|
7225 | var data = result;
|
7226 | if (error === 404) {
|
7227 | data = null;
|
7228 | error = null;
|
7229 | }
|
7230 | if (error === null) {
|
7231 | _this.onDataUpdate_(pathString, data,
|
7232 | false,
|
7233 | null);
|
7234 | deferred.resolve(data);
|
7235 | }
|
7236 | else {
|
7237 | deferred.reject(new Error(data));
|
7238 | }
|
7239 | });
|
7240 | return deferred.promise;
|
7241 | };
|
7242 |
|
7243 | ReadonlyRestClient.prototype.refreshAuthToken = function (token) {
|
7244 |
|
7245 | };
|
7246 | |
7247 |
|
7248 |
|
7249 |
|
7250 | ReadonlyRestClient.prototype.restRequest_ = function (pathString, queryStringParameters, callback) {
|
7251 | var _this = this;
|
7252 | if (queryStringParameters === void 0) { queryStringParameters = {}; }
|
7253 | queryStringParameters['format'] = 'export';
|
7254 | return Promise.all([
|
7255 | this.authTokenProvider_.getToken( false),
|
7256 | this.appCheckTokenProvider_.getToken( false)
|
7257 | ]).then(function (_a) {
|
7258 | var _b = tslib.__read(_a, 2), authToken = _b[0], appCheckToken = _b[1];
|
7259 | if (authToken && authToken.accessToken) {
|
7260 | queryStringParameters['auth'] = authToken.accessToken;
|
7261 | }
|
7262 | if (appCheckToken && appCheckToken.token) {
|
7263 | queryStringParameters['ac'] = appCheckToken.token;
|
7264 | }
|
7265 | var url = (_this.repoInfo_.secure ? 'https://' : 'http://') +
|
7266 | _this.repoInfo_.host +
|
7267 | pathString +
|
7268 | '?' +
|
7269 | 'ns=' +
|
7270 | _this.repoInfo_.namespace +
|
7271 | util.querystring(queryStringParameters);
|
7272 | _this.log_('Sending REST request for ' + url);
|
7273 | var xhr = new XMLHttpRequest();
|
7274 | xhr.onreadystatechange = function () {
|
7275 | if (callback && xhr.readyState === 4) {
|
7276 | _this.log_('REST Response for ' + url + ' received. status:', xhr.status, 'response:', xhr.responseText);
|
7277 | var res = null;
|
7278 | if (xhr.status >= 200 && xhr.status < 300) {
|
7279 | try {
|
7280 | res = util.jsonEval(xhr.responseText);
|
7281 | }
|
7282 | catch (e) {
|
7283 | warn('Failed to parse JSON response for ' +
|
7284 | url +
|
7285 | ': ' +
|
7286 | xhr.responseText);
|
7287 | }
|
7288 | callback(null, res);
|
7289 | }
|
7290 | else {
|
7291 |
|
7292 | if (xhr.status !== 401 && xhr.status !== 404) {
|
7293 | warn('Got unsuccessful REST response for ' +
|
7294 | url +
|
7295 | ' Status: ' +
|
7296 | xhr.status);
|
7297 | }
|
7298 | callback(xhr.status);
|
7299 | }
|
7300 | callback = null;
|
7301 | }
|
7302 | };
|
7303 | xhr.open('GET', url, true);
|
7304 | xhr.send();
|
7305 | });
|
7306 | };
|
7307 | return ReadonlyRestClient;
|
7308 | }(ServerActions));
|
7309 |
|
7310 |
|
7311 |
|
7312 |
|
7313 |
|
7314 |
|
7315 |
|
7316 |
|
7317 |
|
7318 |
|
7319 |
|
7320 |
|
7321 |
|
7322 |
|
7323 |
|
7324 |
|
7325 |
|
7326 |
|
7327 |
|
7328 |
|
7329 | var SnapshotHolder = (function () {
|
7330 | function SnapshotHolder() {
|
7331 | this.rootNode_ = ChildrenNode.EMPTY_NODE;
|
7332 | }
|
7333 | SnapshotHolder.prototype.getNode = function (path) {
|
7334 | return this.rootNode_.getChild(path);
|
7335 | };
|
7336 | SnapshotHolder.prototype.updateSnapshot = function (path, newSnapshotNode) {
|
7337 | this.rootNode_ = this.rootNode_.updateChild(path, newSnapshotNode);
|
7338 | };
|
7339 | return SnapshotHolder;
|
7340 | }());
|
7341 |
|
7342 |
|
7343 |
|
7344 |
|
7345 |
|
7346 |
|
7347 |
|
7348 |
|
7349 |
|
7350 |
|
7351 |
|
7352 |
|
7353 |
|
7354 |
|
7355 |
|
7356 |
|
7357 |
|
7358 | function newSparseSnapshotTree() {
|
7359 | return {
|
7360 | value: null,
|
7361 | children: new Map()
|
7362 | };
|
7363 | }
|
7364 |
|
7365 |
|
7366 |
|
7367 |
|
7368 |
|
7369 |
|
7370 |
|
7371 | function sparseSnapshotTreeRemember(sparseSnapshotTree, path, data) {
|
7372 | if (pathIsEmpty(path)) {
|
7373 | sparseSnapshotTree.value = data;
|
7374 | sparseSnapshotTree.children.clear();
|
7375 | }
|
7376 | else if (sparseSnapshotTree.value !== null) {
|
7377 | sparseSnapshotTree.value = sparseSnapshotTree.value.updateChild(path, data);
|
7378 | }
|
7379 | else {
|
7380 | var childKey = pathGetFront(path);
|
7381 | if (!sparseSnapshotTree.children.has(childKey)) {
|
7382 | sparseSnapshotTree.children.set(childKey, newSparseSnapshotTree());
|
7383 | }
|
7384 | var child = sparseSnapshotTree.children.get(childKey);
|
7385 | path = pathPopFront(path);
|
7386 | sparseSnapshotTreeRemember(child, path, data);
|
7387 | }
|
7388 | }
|
7389 |
|
7390 |
|
7391 |
|
7392 |
|
7393 |
|
7394 |
|
7395 | function sparseSnapshotTreeForget(sparseSnapshotTree, path) {
|
7396 | if (pathIsEmpty(path)) {
|
7397 | sparseSnapshotTree.value = null;
|
7398 | sparseSnapshotTree.children.clear();
|
7399 | return true;
|
7400 | }
|
7401 | else {
|
7402 | if (sparseSnapshotTree.value !== null) {
|
7403 | if (sparseSnapshotTree.value.isLeafNode()) {
|
7404 |
|
7405 | return false;
|
7406 | }
|
7407 | else {
|
7408 | var value = sparseSnapshotTree.value;
|
7409 | sparseSnapshotTree.value = null;
|
7410 | value.forEachChild(PRIORITY_INDEX, function (key, tree) {
|
7411 | sparseSnapshotTreeRemember(sparseSnapshotTree, new Path(key), tree);
|
7412 | });
|
7413 | return sparseSnapshotTreeForget(sparseSnapshotTree, path);
|
7414 | }
|
7415 | }
|
7416 | else if (sparseSnapshotTree.children.size > 0) {
|
7417 | var childKey = pathGetFront(path);
|
7418 | path = pathPopFront(path);
|
7419 | if (sparseSnapshotTree.children.has(childKey)) {
|
7420 | var safeToRemove = sparseSnapshotTreeForget(sparseSnapshotTree.children.get(childKey), path);
|
7421 | if (safeToRemove) {
|
7422 | sparseSnapshotTree.children.delete(childKey);
|
7423 | }
|
7424 | }
|
7425 | return sparseSnapshotTree.children.size === 0;
|
7426 | }
|
7427 | else {
|
7428 | return true;
|
7429 | }
|
7430 | }
|
7431 | }
|
7432 |
|
7433 |
|
7434 |
|
7435 |
|
7436 |
|
7437 |
|
7438 |
|
7439 | function sparseSnapshotTreeForEachTree(sparseSnapshotTree, prefixPath, func) {
|
7440 | if (sparseSnapshotTree.value !== null) {
|
7441 | func(prefixPath, sparseSnapshotTree.value);
|
7442 | }
|
7443 | else {
|
7444 | sparseSnapshotTreeForEachChild(sparseSnapshotTree, function (key, tree) {
|
7445 | var path = new Path(prefixPath.toString() + '/' + key);
|
7446 | sparseSnapshotTreeForEachTree(tree, path, func);
|
7447 | });
|
7448 | }
|
7449 | }
|
7450 |
|
7451 |
|
7452 |
|
7453 |
|
7454 |
|
7455 |
|
7456 | function sparseSnapshotTreeForEachChild(sparseSnapshotTree, func) {
|
7457 | sparseSnapshotTree.children.forEach(function (tree, key) {
|
7458 | func(key, tree);
|
7459 | });
|
7460 | }
|
7461 |
|
7462 |
|
7463 |
|
7464 |
|
7465 |
|
7466 |
|
7467 |
|
7468 |
|
7469 |
|
7470 |
|
7471 |
|
7472 |
|
7473 |
|
7474 |
|
7475 |
|
7476 |
|
7477 |
|
7478 |
|
7479 |
|
7480 |
|
7481 |
|
7482 |
|
7483 | var StatsListener = (function () {
|
7484 | function StatsListener(collection_) {
|
7485 | this.collection_ = collection_;
|
7486 | this.last_ = null;
|
7487 | }
|
7488 | StatsListener.prototype.get = function () {
|
7489 | var newStats = this.collection_.get();
|
7490 | var delta = tslib.__assign({}, newStats);
|
7491 | if (this.last_) {
|
7492 | each(this.last_, function (stat, value) {
|
7493 | delta[stat] = delta[stat] - value;
|
7494 | });
|
7495 | }
|
7496 | this.last_ = newStats;
|
7497 | return delta;
|
7498 | };
|
7499 | return StatsListener;
|
7500 | }());
|
7501 |
|
7502 |
|
7503 |
|
7504 |
|
7505 |
|
7506 |
|
7507 |
|
7508 |
|
7509 |
|
7510 |
|
7511 |
|
7512 |
|
7513 |
|
7514 |
|
7515 |
|
7516 |
|
7517 |
|
7518 |
|
7519 |
|
7520 |
|
7521 | var FIRST_STATS_MIN_TIME = 10 * 1000;
|
7522 | var FIRST_STATS_MAX_TIME = 30 * 1000;
|
7523 |
|
7524 | var REPORT_STATS_INTERVAL = 5 * 60 * 1000;
|
7525 | var StatsReporter = (function () {
|
7526 | function StatsReporter(collection, server_) {
|
7527 | this.server_ = server_;
|
7528 | this.statsToReport_ = {};
|
7529 | this.statsListener_ = new StatsListener(collection);
|
7530 | var timeout = FIRST_STATS_MIN_TIME +
|
7531 | (FIRST_STATS_MAX_TIME - FIRST_STATS_MIN_TIME) * Math.random();
|
7532 | setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(timeout));
|
7533 | }
|
7534 | StatsReporter.prototype.reportStats_ = function () {
|
7535 | var _this = this;
|
7536 | var stats = this.statsListener_.get();
|
7537 | var reportedStats = {};
|
7538 | var haveStatsToReport = false;
|
7539 | each(stats, function (stat, value) {
|
7540 | if (value > 0 && util.contains(_this.statsToReport_, stat)) {
|
7541 | reportedStats[stat] = value;
|
7542 | haveStatsToReport = true;
|
7543 | }
|
7544 | });
|
7545 | if (haveStatsToReport) {
|
7546 | this.server_.reportStats(reportedStats);
|
7547 | }
|
7548 |
|
7549 | setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(Math.random() * 2 * REPORT_STATS_INTERVAL));
|
7550 | };
|
7551 | return StatsReporter;
|
7552 | }());
|
7553 |
|
7554 |
|
7555 |
|
7556 |
|
7557 |
|
7558 |
|
7559 |
|
7560 |
|
7561 |
|
7562 |
|
7563 |
|
7564 |
|
7565 |
|
7566 |
|
7567 |
|
7568 |
|
7569 |
|
7570 |
|
7571 |
|
7572 |
|
7573 |
|
7574 | var OperationType;
|
7575 | (function (OperationType) {
|
7576 | OperationType[OperationType["OVERWRITE"] = 0] = "OVERWRITE";
|
7577 | OperationType[OperationType["MERGE"] = 1] = "MERGE";
|
7578 | OperationType[OperationType["ACK_USER_WRITE"] = 2] = "ACK_USER_WRITE";
|
7579 | OperationType[OperationType["LISTEN_COMPLETE"] = 3] = "LISTEN_COMPLETE";
|
7580 | })(OperationType || (OperationType = {}));
|
7581 | function newOperationSourceUser() {
|
7582 | return {
|
7583 | fromUser: true,
|
7584 | fromServer: false,
|
7585 | queryId: null,
|
7586 | tagged: false
|
7587 | };
|
7588 | }
|
7589 | function newOperationSourceServer() {
|
7590 | return {
|
7591 | fromUser: false,
|
7592 | fromServer: true,
|
7593 | queryId: null,
|
7594 | tagged: false
|
7595 | };
|
7596 | }
|
7597 | function newOperationSourceServerTaggedQuery(queryId) {
|
7598 | return {
|
7599 | fromUser: false,
|
7600 | fromServer: true,
|
7601 | queryId: queryId,
|
7602 | tagged: true
|
7603 | };
|
7604 | }
|
7605 |
|
7606 |
|
7607 |
|
7608 |
|
7609 |
|
7610 |
|
7611 |
|
7612 |
|
7613 |
|
7614 |
|
7615 |
|
7616 |
|
7617 |
|
7618 |
|
7619 |
|
7620 |
|
7621 |
|
7622 | var AckUserWrite = (function () {
|
7623 | |
7624 |
|
7625 |
|
7626 | function AckUserWrite(
|
7627 | /** @inheritDoc */ path,
|
7628 | /** @inheritDoc */ affectedTree,
|
7629 | /** @inheritDoc */ revert) {
|
7630 | this.path = path;
|
7631 | this.affectedTree = affectedTree;
|
7632 | this.revert = revert;
|
7633 |
|
7634 | this.type = OperationType.ACK_USER_WRITE;
|
7635 |
|
7636 | this.source = newOperationSourceUser();
|
7637 | }
|
7638 | AckUserWrite.prototype.operationForChild = function (childName) {
|
7639 | if (!pathIsEmpty(this.path)) {
|
7640 | util.assert(pathGetFront(this.path) === childName, 'operationForChild called for unrelated child.');
|
7641 | return new AckUserWrite(pathPopFront(this.path), this.affectedTree, this.revert);
|
7642 | }
|
7643 | else if (this.affectedTree.value != null) {
|
7644 | util.assert(this.affectedTree.children.isEmpty(), 'affectedTree should not have overlapping affected paths.');
|
7645 |
|
7646 | return this;
|
7647 | }
|
7648 | else {
|
7649 | var childTree = this.affectedTree.subtree(new Path(childName));
|
7650 | return new AckUserWrite(newEmptyPath(), childTree, this.revert);
|
7651 | }
|
7652 | };
|
7653 | return AckUserWrite;
|
7654 | }());
|
7655 |
|
7656 |
|
7657 |
|
7658 |
|
7659 |
|
7660 |
|
7661 |
|
7662 |
|
7663 |
|
7664 |
|
7665 |
|
7666 |
|
7667 |
|
7668 |
|
7669 |
|
7670 |
|
7671 |
|
7672 | var ListenComplete = (function () {
|
7673 | function ListenComplete(source, path) {
|
7674 | this.source = source;
|
7675 | this.path = path;
|
7676 |
|
7677 | this.type = OperationType.LISTEN_COMPLETE;
|
7678 | }
|
7679 | ListenComplete.prototype.operationForChild = function (childName) {
|
7680 | if (pathIsEmpty(this.path)) {
|
7681 | return new ListenComplete(this.source, newEmptyPath());
|
7682 | }
|
7683 | else {
|
7684 | return new ListenComplete(this.source, pathPopFront(this.path));
|
7685 | }
|
7686 | };
|
7687 | return ListenComplete;
|
7688 | }());
|
7689 |
|
7690 |
|
7691 |
|
7692 |
|
7693 |
|
7694 |
|
7695 |
|
7696 |
|
7697 |
|
7698 |
|
7699 |
|
7700 |
|
7701 |
|
7702 |
|
7703 |
|
7704 |
|
7705 |
|
7706 | var Overwrite = (function () {
|
7707 | function Overwrite(source, path, snap) {
|
7708 | this.source = source;
|
7709 | this.path = path;
|
7710 | this.snap = snap;
|
7711 |
|
7712 | this.type = OperationType.OVERWRITE;
|
7713 | }
|
7714 | Overwrite.prototype.operationForChild = function (childName) {
|
7715 | if (pathIsEmpty(this.path)) {
|
7716 | return new Overwrite(this.source, newEmptyPath(), this.snap.getImmediateChild(childName));
|
7717 | }
|
7718 | else {
|
7719 | return new Overwrite(this.source, pathPopFront(this.path), this.snap);
|
7720 | }
|
7721 | };
|
7722 | return Overwrite;
|
7723 | }());
|
7724 |
|
7725 |
|
7726 |
|
7727 |
|
7728 |
|
7729 |
|
7730 |
|
7731 |
|
7732 |
|
7733 |
|
7734 |
|
7735 |
|
7736 |
|
7737 |
|
7738 |
|
7739 |
|
7740 |
|
7741 | var Merge = (function () {
|
7742 | function Merge(
|
7743 | /** @inheritDoc */ source,
|
7744 | /** @inheritDoc */ path,
|
7745 | /** @inheritDoc */ children) {
|
7746 | this.source = source;
|
7747 | this.path = path;
|
7748 | this.children = children;
|
7749 |
|
7750 | this.type = OperationType.MERGE;
|
7751 | }
|
7752 | Merge.prototype.operationForChild = function (childName) {
|
7753 | if (pathIsEmpty(this.path)) {
|
7754 | var childTree = this.children.subtree(new Path(childName));
|
7755 | if (childTree.isEmpty()) {
|
7756 |
|
7757 | return null;
|
7758 | }
|
7759 | else if (childTree.value) {
|
7760 |
|
7761 | return new Overwrite(this.source, newEmptyPath(), childTree.value);
|
7762 | }
|
7763 | else {
|
7764 |
|
7765 | return new Merge(this.source, newEmptyPath(), childTree);
|
7766 | }
|
7767 | }
|
7768 | else {
|
7769 | util.assert(pathGetFront(this.path) === childName, "Can't get a merge for a child not on the path of the operation");
|
7770 | return new Merge(this.source, pathPopFront(this.path), this.children);
|
7771 | }
|
7772 | };
|
7773 | Merge.prototype.toString = function () {
|
7774 | return ('Operation(' +
|
7775 | this.path +
|
7776 | ': ' +
|
7777 | this.source.toString() +
|
7778 | ' merge: ' +
|
7779 | this.children.toString() +
|
7780 | ')');
|
7781 | };
|
7782 | return Merge;
|
7783 | }());
|
7784 |
|
7785 |
|
7786 |
|
7787 |
|
7788 |
|
7789 |
|
7790 |
|
7791 |
|
7792 |
|
7793 |
|
7794 |
|
7795 |
|
7796 |
|
7797 |
|
7798 |
|
7799 |
|
7800 |
|
7801 |
|
7802 |
|
7803 |
|
7804 |
|
7805 |
|
7806 |
|
7807 | var CacheNode = (function () {
|
7808 | function CacheNode(node_, fullyInitialized_, filtered_) {
|
7809 | this.node_ = node_;
|
7810 | this.fullyInitialized_ = fullyInitialized_;
|
7811 | this.filtered_ = filtered_;
|
7812 | }
|
7813 | |
7814 |
|
7815 |
|
7816 | CacheNode.prototype.isFullyInitialized = function () {
|
7817 | return this.fullyInitialized_;
|
7818 | };
|
7819 | |
7820 |
|
7821 |
|
7822 | CacheNode.prototype.isFiltered = function () {
|
7823 | return this.filtered_;
|
7824 | };
|
7825 | CacheNode.prototype.isCompleteForPath = function (path) {
|
7826 | if (pathIsEmpty(path)) {
|
7827 | return this.isFullyInitialized() && !this.filtered_;
|
7828 | }
|
7829 | var childKey = pathGetFront(path);
|
7830 | return this.isCompleteForChild(childKey);
|
7831 | };
|
7832 | CacheNode.prototype.isCompleteForChild = function (key) {
|
7833 | return ((this.isFullyInitialized() && !this.filtered_) || this.node_.hasChild(key));
|
7834 | };
|
7835 | CacheNode.prototype.getNode = function () {
|
7836 | return this.node_;
|
7837 | };
|
7838 | return CacheNode;
|
7839 | }());
|
7840 |
|
7841 |
|
7842 |
|
7843 |
|
7844 |
|
7845 |
|
7846 |
|
7847 |
|
7848 |
|
7849 |
|
7850 |
|
7851 |
|
7852 |
|
7853 |
|
7854 |
|
7855 |
|
7856 |
|
7857 |
|
7858 |
|
7859 |
|
7860 |
|
7861 |
|
7862 |
|
7863 | var EventGenerator = (function () {
|
7864 | function EventGenerator(query_) {
|
7865 | this.query_ = query_;
|
7866 | this.index_ = this.query_._queryParams.getIndex();
|
7867 | }
|
7868 | return EventGenerator;
|
7869 | }());
|
7870 |
|
7871 |
|
7872 |
|
7873 |
|
7874 |
|
7875 |
|
7876 |
|
7877 |
|
7878 |
|
7879 | function eventGeneratorGenerateEventsForChanges(eventGenerator, changes, eventCache, eventRegistrations) {
|
7880 | var events = [];
|
7881 | var moves = [];
|
7882 | changes.forEach(function (change) {
|
7883 | if (change.type === "child_changed" &&
|
7884 | eventGenerator.index_.indexedValueChanged(change.oldSnap, change.snapshotNode)) {
|
7885 | moves.push(changeChildMoved(change.childName, change.snapshotNode));
|
7886 | }
|
7887 | });
|
7888 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_removed" , changes, eventRegistrations, eventCache);
|
7889 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_added" , changes, eventRegistrations, eventCache);
|
7890 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_moved" , moves, eventRegistrations, eventCache);
|
7891 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_changed" , changes, eventRegistrations, eventCache);
|
7892 | eventGeneratorGenerateEventsForType(eventGenerator, events, "value" , changes, eventRegistrations, eventCache);
|
7893 | return events;
|
7894 | }
|
7895 |
|
7896 |
|
7897 |
|
7898 | function eventGeneratorGenerateEventsForType(eventGenerator, events, eventType, changes, registrations, eventCache) {
|
7899 | var filteredChanges = changes.filter(function (change) { return change.type === eventType; });
|
7900 | filteredChanges.sort(function (a, b) {
|
7901 | return eventGeneratorCompareChanges(eventGenerator, a, b);
|
7902 | });
|
7903 | filteredChanges.forEach(function (change) {
|
7904 | var materializedChange = eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache);
|
7905 | registrations.forEach(function (registration) {
|
7906 | if (registration.respondsTo(change.type)) {
|
7907 | events.push(registration.createEvent(materializedChange, eventGenerator.query_));
|
7908 | }
|
7909 | });
|
7910 | });
|
7911 | }
|
7912 | function eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache) {
|
7913 | if (change.type === 'value' || change.type === 'child_removed') {
|
7914 | return change;
|
7915 | }
|
7916 | else {
|
7917 | change.prevName = eventCache.getPredecessorChildName(change.childName, change.snapshotNode, eventGenerator.index_);
|
7918 | return change;
|
7919 | }
|
7920 | }
|
7921 | function eventGeneratorCompareChanges(eventGenerator, a, b) {
|
7922 | if (a.childName == null || b.childName == null) {
|
7923 | throw util.assertionError('Should only compare child_ events.');
|
7924 | }
|
7925 | var aWrapped = new NamedNode(a.childName, a.snapshotNode);
|
7926 | var bWrapped = new NamedNode(b.childName, b.snapshotNode);
|
7927 | return eventGenerator.index_.compare(aWrapped, bWrapped);
|
7928 | }
|
7929 |
|
7930 |
|
7931 |
|
7932 |
|
7933 |
|
7934 |
|
7935 |
|
7936 |
|
7937 |
|
7938 |
|
7939 |
|
7940 |
|
7941 |
|
7942 |
|
7943 |
|
7944 |
|
7945 |
|
7946 | function newViewCache(eventCache, serverCache) {
|
7947 | return { eventCache: eventCache, serverCache: serverCache };
|
7948 | }
|
7949 | function viewCacheUpdateEventSnap(viewCache, eventSnap, complete, filtered) {
|
7950 | return newViewCache(new CacheNode(eventSnap, complete, filtered), viewCache.serverCache);
|
7951 | }
|
7952 | function viewCacheUpdateServerSnap(viewCache, serverSnap, complete, filtered) {
|
7953 | return newViewCache(viewCache.eventCache, new CacheNode(serverSnap, complete, filtered));
|
7954 | }
|
7955 | function viewCacheGetCompleteEventSnap(viewCache) {
|
7956 | return viewCache.eventCache.isFullyInitialized()
|
7957 | ? viewCache.eventCache.getNode()
|
7958 | : null;
|
7959 | }
|
7960 | function viewCacheGetCompleteServerSnap(viewCache) {
|
7961 | return viewCache.serverCache.isFullyInitialized()
|
7962 | ? viewCache.serverCache.getNode()
|
7963 | : null;
|
7964 | }
|
7965 |
|
7966 |
|
7967 |
|
7968 |
|
7969 |
|
7970 |
|
7971 |
|
7972 |
|
7973 |
|
7974 |
|
7975 |
|
7976 |
|
7977 |
|
7978 |
|
7979 |
|
7980 |
|
7981 |
|
7982 | var emptyChildrenSingleton;
|
7983 |
|
7984 |
|
7985 |
|
7986 |
|
7987 | var EmptyChildren = function () {
|
7988 | if (!emptyChildrenSingleton) {
|
7989 | emptyChildrenSingleton = new SortedMap(stringCompare);
|
7990 | }
|
7991 | return emptyChildrenSingleton;
|
7992 | };
|
7993 |
|
7994 |
|
7995 |
|
7996 | var ImmutableTree = (function () {
|
7997 | function ImmutableTree(value, children) {
|
7998 | if (children === void 0) { children = EmptyChildren(); }
|
7999 | this.value = value;
|
8000 | this.children = children;
|
8001 | }
|
8002 | ImmutableTree.fromObject = function (obj) {
|
8003 | var tree = new ImmutableTree(null);
|
8004 | each(obj, function (childPath, childSnap) {
|
8005 | tree = tree.set(new Path(childPath), childSnap);
|
8006 | });
|
8007 | return tree;
|
8008 | };
|
8009 | |
8010 |
|
8011 |
|
8012 | ImmutableTree.prototype.isEmpty = function () {
|
8013 | return this.value === null && this.children.isEmpty();
|
8014 | };
|
8015 | |
8016 |
|
8017 |
|
8018 |
|
8019 |
|
8020 |
|
8021 |
|
8022 |
|
8023 |
|
8024 |
|
8025 | ImmutableTree.prototype.findRootMostMatchingPathAndValue = function (relativePath, predicate) {
|
8026 | if (this.value != null && predicate(this.value)) {
|
8027 | return { path: newEmptyPath(), value: this.value };
|
8028 | }
|
8029 | else {
|
8030 | if (pathIsEmpty(relativePath)) {
|
8031 | return null;
|
8032 | }
|
8033 | else {
|
8034 | var front = pathGetFront(relativePath);
|
8035 | var child = this.children.get(front);
|
8036 | if (child !== null) {
|
8037 | var childExistingPathAndValue = child.findRootMostMatchingPathAndValue(pathPopFront(relativePath), predicate);
|
8038 | if (childExistingPathAndValue != null) {
|
8039 | var fullPath = pathChild(new Path(front), childExistingPathAndValue.path);
|
8040 | return { path: fullPath, value: childExistingPathAndValue.value };
|
8041 | }
|
8042 | else {
|
8043 | return null;
|
8044 | }
|
8045 | }
|
8046 | else {
|
8047 | return null;
|
8048 | }
|
8049 | }
|
8050 | }
|
8051 | };
|
8052 | |
8053 |
|
8054 |
|
8055 |
|
8056 | ImmutableTree.prototype.findRootMostValueAndPath = function (relativePath) {
|
8057 | return this.findRootMostMatchingPathAndValue(relativePath, function () { return true; });
|
8058 | };
|
8059 | |
8060 |
|
8061 |
|
8062 | ImmutableTree.prototype.subtree = function (relativePath) {
|
8063 | if (pathIsEmpty(relativePath)) {
|
8064 | return this;
|
8065 | }
|
8066 | else {
|
8067 | var front = pathGetFront(relativePath);
|
8068 | var childTree = this.children.get(front);
|
8069 | if (childTree !== null) {
|
8070 | return childTree.subtree(pathPopFront(relativePath));
|
8071 | }
|
8072 | else {
|
8073 | return new ImmutableTree(null);
|
8074 | }
|
8075 | }
|
8076 | };
|
8077 | |
8078 |
|
8079 |
|
8080 |
|
8081 |
|
8082 |
|
8083 |
|
8084 | ImmutableTree.prototype.set = function (relativePath, toSet) {
|
8085 | if (pathIsEmpty(relativePath)) {
|
8086 | return new ImmutableTree(toSet, this.children);
|
8087 | }
|
8088 | else {
|
8089 | var front = pathGetFront(relativePath);
|
8090 | var child = this.children.get(front) || new ImmutableTree(null);
|
8091 | var newChild = child.set(pathPopFront(relativePath), toSet);
|
8092 | var newChildren = this.children.insert(front, newChild);
|
8093 | return new ImmutableTree(this.value, newChildren);
|
8094 | }
|
8095 | };
|
8096 | |
8097 |
|
8098 |
|
8099 |
|
8100 |
|
8101 |
|
8102 | ImmutableTree.prototype.remove = function (relativePath) {
|
8103 | if (pathIsEmpty(relativePath)) {
|
8104 | if (this.children.isEmpty()) {
|
8105 | return new ImmutableTree(null);
|
8106 | }
|
8107 | else {
|
8108 | return new ImmutableTree(null, this.children);
|
8109 | }
|
8110 | }
|
8111 | else {
|
8112 | var front = pathGetFront(relativePath);
|
8113 | var child = this.children.get(front);
|
8114 | if (child) {
|
8115 | var newChild = child.remove(pathPopFront(relativePath));
|
8116 | var newChildren = void 0;
|
8117 | if (newChild.isEmpty()) {
|
8118 | newChildren = this.children.remove(front);
|
8119 | }
|
8120 | else {
|
8121 | newChildren = this.children.insert(front, newChild);
|
8122 | }
|
8123 | if (this.value === null && newChildren.isEmpty()) {
|
8124 | return new ImmutableTree(null);
|
8125 | }
|
8126 | else {
|
8127 | return new ImmutableTree(this.value, newChildren);
|
8128 | }
|
8129 | }
|
8130 | else {
|
8131 | return this;
|
8132 | }
|
8133 | }
|
8134 | };
|
8135 | |
8136 |
|
8137 |
|
8138 |
|
8139 |
|
8140 |
|
8141 | ImmutableTree.prototype.get = function (relativePath) {
|
8142 | if (pathIsEmpty(relativePath)) {
|
8143 | return this.value;
|
8144 | }
|
8145 | else {
|
8146 | var front = pathGetFront(relativePath);
|
8147 | var child = this.children.get(front);
|
8148 | if (child) {
|
8149 | return child.get(pathPopFront(relativePath));
|
8150 | }
|
8151 | else {
|
8152 | return null;
|
8153 | }
|
8154 | }
|
8155 | };
|
8156 | |
8157 |
|
8158 |
|
8159 |
|
8160 |
|
8161 |
|
8162 |
|
8163 | ImmutableTree.prototype.setTree = function (relativePath, newTree) {
|
8164 | if (pathIsEmpty(relativePath)) {
|
8165 | return newTree;
|
8166 | }
|
8167 | else {
|
8168 | var front = pathGetFront(relativePath);
|
8169 | var child = this.children.get(front) || new ImmutableTree(null);
|
8170 | var newChild = child.setTree(pathPopFront(relativePath), newTree);
|
8171 | var newChildren = void 0;
|
8172 | if (newChild.isEmpty()) {
|
8173 | newChildren = this.children.remove(front);
|
8174 | }
|
8175 | else {
|
8176 | newChildren = this.children.insert(front, newChild);
|
8177 | }
|
8178 | return new ImmutableTree(this.value, newChildren);
|
8179 | }
|
8180 | };
|
8181 | |
8182 |
|
8183 |
|
8184 |
|
8185 |
|
8186 | ImmutableTree.prototype.fold = function (fn) {
|
8187 | return this.fold_(newEmptyPath(), fn);
|
8188 | };
|
8189 | |
8190 |
|
8191 |
|
8192 | ImmutableTree.prototype.fold_ = function (pathSoFar, fn) {
|
8193 | var accum = {};
|
8194 | this.children.inorderTraversal(function (childKey, childTree) {
|
8195 | accum[childKey] = childTree.fold_(pathChild(pathSoFar, childKey), fn);
|
8196 | });
|
8197 | return fn(pathSoFar, this.value, accum);
|
8198 | };
|
8199 | |
8200 |
|
8201 |
|
8202 | ImmutableTree.prototype.findOnPath = function (path, f) {
|
8203 | return this.findOnPath_(path, newEmptyPath(), f);
|
8204 | };
|
8205 | ImmutableTree.prototype.findOnPath_ = function (pathToFollow, pathSoFar, f) {
|
8206 | var result = this.value ? f(pathSoFar, this.value) : false;
|
8207 | if (result) {
|
8208 | return result;
|
8209 | }
|
8210 | else {
|
8211 | if (pathIsEmpty(pathToFollow)) {
|
8212 | return null;
|
8213 | }
|
8214 | else {
|
8215 | var front = pathGetFront(pathToFollow);
|
8216 | var nextChild = this.children.get(front);
|
8217 | if (nextChild) {
|
8218 | return nextChild.findOnPath_(pathPopFront(pathToFollow), pathChild(pathSoFar, front), f);
|
8219 | }
|
8220 | else {
|
8221 | return null;
|
8222 | }
|
8223 | }
|
8224 | }
|
8225 | };
|
8226 | ImmutableTree.prototype.foreachOnPath = function (path, f) {
|
8227 | return this.foreachOnPath_(path, newEmptyPath(), f);
|
8228 | };
|
8229 | ImmutableTree.prototype.foreachOnPath_ = function (pathToFollow, currentRelativePath, f) {
|
8230 | if (pathIsEmpty(pathToFollow)) {
|
8231 | return this;
|
8232 | }
|
8233 | else {
|
8234 | if (this.value) {
|
8235 | f(currentRelativePath, this.value);
|
8236 | }
|
8237 | var front = pathGetFront(pathToFollow);
|
8238 | var nextChild = this.children.get(front);
|
8239 | if (nextChild) {
|
8240 | return nextChild.foreachOnPath_(pathPopFront(pathToFollow), pathChild(currentRelativePath, front), f);
|
8241 | }
|
8242 | else {
|
8243 | return new ImmutableTree(null);
|
8244 | }
|
8245 | }
|
8246 | };
|
8247 | |
8248 |
|
8249 |
|
8250 |
|
8251 |
|
8252 |
|
8253 | ImmutableTree.prototype.foreach = function (f) {
|
8254 | this.foreach_(newEmptyPath(), f);
|
8255 | };
|
8256 | ImmutableTree.prototype.foreach_ = function (currentRelativePath, f) {
|
8257 | this.children.inorderTraversal(function (childName, childTree) {
|
8258 | childTree.foreach_(pathChild(currentRelativePath, childName), f);
|
8259 | });
|
8260 | if (this.value) {
|
8261 | f(currentRelativePath, this.value);
|
8262 | }
|
8263 | };
|
8264 | ImmutableTree.prototype.foreachChild = function (f) {
|
8265 | this.children.inorderTraversal(function (childName, childTree) {
|
8266 | if (childTree.value) {
|
8267 | f(childName, childTree.value);
|
8268 | }
|
8269 | });
|
8270 | };
|
8271 | return ImmutableTree;
|
8272 | }());
|
8273 |
|
8274 |
|
8275 |
|
8276 |
|
8277 |
|
8278 |
|
8279 |
|
8280 |
|
8281 |
|
8282 |
|
8283 |
|
8284 |
|
8285 |
|
8286 |
|
8287 |
|
8288 |
|
8289 |
|
8290 |
|
8291 |
|
8292 |
|
8293 |
|
8294 |
|
8295 |
|
8296 | var CompoundWrite = (function () {
|
8297 | function CompoundWrite(writeTree_) {
|
8298 | this.writeTree_ = writeTree_;
|
8299 | }
|
8300 | CompoundWrite.empty = function () {
|
8301 | return new CompoundWrite(new ImmutableTree(null));
|
8302 | };
|
8303 | return CompoundWrite;
|
8304 | }());
|
8305 | function compoundWriteAddWrite(compoundWrite, path, node) {
|
8306 | if (pathIsEmpty(path)) {
|
8307 | return new CompoundWrite(new ImmutableTree(node));
|
8308 | }
|
8309 | else {
|
8310 | var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
|
8311 | if (rootmost != null) {
|
8312 | var rootMostPath = rootmost.path;
|
8313 | var value = rootmost.value;
|
8314 | var relativePath = newRelativePath(rootMostPath, path);
|
8315 | value = value.updateChild(relativePath, node);
|
8316 | return new CompoundWrite(compoundWrite.writeTree_.set(rootMostPath, value));
|
8317 | }
|
8318 | else {
|
8319 | var subtree = new ImmutableTree(node);
|
8320 | var newWriteTree = compoundWrite.writeTree_.setTree(path, subtree);
|
8321 | return new CompoundWrite(newWriteTree);
|
8322 | }
|
8323 | }
|
8324 | }
|
8325 | function compoundWriteAddWrites(compoundWrite, path, updates) {
|
8326 | var newWrite = compoundWrite;
|
8327 | each(updates, function (childKey, node) {
|
8328 | newWrite = compoundWriteAddWrite(newWrite, pathChild(path, childKey), node);
|
8329 | });
|
8330 | return newWrite;
|
8331 | }
|
8332 |
|
8333 |
|
8334 |
|
8335 |
|
8336 |
|
8337 |
|
8338 |
|
8339 |
|
8340 | function compoundWriteRemoveWrite(compoundWrite, path) {
|
8341 | if (pathIsEmpty(path)) {
|
8342 | return CompoundWrite.empty();
|
8343 | }
|
8344 | else {
|
8345 | var newWriteTree = compoundWrite.writeTree_.setTree(path, new ImmutableTree(null));
|
8346 | return new CompoundWrite(newWriteTree);
|
8347 | }
|
8348 | }
|
8349 |
|
8350 |
|
8351 |
|
8352 |
|
8353 |
|
8354 |
|
8355 |
|
8356 |
|
8357 | function compoundWriteHasCompleteWrite(compoundWrite, path) {
|
8358 | return compoundWriteGetCompleteNode(compoundWrite, path) != null;
|
8359 | }
|
8360 |
|
8361 |
|
8362 |
|
8363 |
|
8364 |
|
8365 |
|
8366 |
|
8367 |
|
8368 | function compoundWriteGetCompleteNode(compoundWrite, path) {
|
8369 | var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
|
8370 | if (rootmost != null) {
|
8371 | return compoundWrite.writeTree_
|
8372 | .get(rootmost.path)
|
8373 | .getChild(newRelativePath(rootmost.path, path));
|
8374 | }
|
8375 | else {
|
8376 | return null;
|
8377 | }
|
8378 | }
|
8379 |
|
8380 |
|
8381 |
|
8382 |
|
8383 |
|
8384 |
|
8385 | function compoundWriteGetCompleteChildren(compoundWrite) {
|
8386 | var children = [];
|
8387 | var node = compoundWrite.writeTree_.value;
|
8388 | if (node != null) {
|
8389 |
|
8390 | if (!node.isLeafNode()) {
|
8391 | node.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
|
8392 | children.push(new NamedNode(childName, childNode));
|
8393 | });
|
8394 | }
|
8395 | }
|
8396 | else {
|
8397 | compoundWrite.writeTree_.children.inorderTraversal(function (childName, childTree) {
|
8398 | if (childTree.value != null) {
|
8399 | children.push(new NamedNode(childName, childTree.value));
|
8400 | }
|
8401 | });
|
8402 | }
|
8403 | return children;
|
8404 | }
|
8405 | function compoundWriteChildCompoundWrite(compoundWrite, path) {
|
8406 | if (pathIsEmpty(path)) {
|
8407 | return compoundWrite;
|
8408 | }
|
8409 | else {
|
8410 | var shadowingNode = compoundWriteGetCompleteNode(compoundWrite, path);
|
8411 | if (shadowingNode != null) {
|
8412 | return new CompoundWrite(new ImmutableTree(shadowingNode));
|
8413 | }
|
8414 | else {
|
8415 | return new CompoundWrite(compoundWrite.writeTree_.subtree(path));
|
8416 | }
|
8417 | }
|
8418 | }
|
8419 |
|
8420 |
|
8421 |
|
8422 |
|
8423 | function compoundWriteIsEmpty(compoundWrite) {
|
8424 | return compoundWrite.writeTree_.isEmpty();
|
8425 | }
|
8426 |
|
8427 |
|
8428 |
|
8429 |
|
8430 |
|
8431 |
|
8432 | function compoundWriteApply(compoundWrite, node) {
|
8433 | return applySubtreeWrite(newEmptyPath(), compoundWrite.writeTree_, node);
|
8434 | }
|
8435 | function applySubtreeWrite(relativePath, writeTree, node) {
|
8436 | if (writeTree.value != null) {
|
8437 |
|
8438 | return node.updateChild(relativePath, writeTree.value);
|
8439 | }
|
8440 | else {
|
8441 | var priorityWrite_1 = null;
|
8442 | writeTree.children.inorderTraversal(function (childKey, childTree) {
|
8443 | if (childKey === '.priority') {
|
8444 |
|
8445 |
|
8446 | util.assert(childTree.value !== null, 'Priority writes must always be leaf nodes');
|
8447 | priorityWrite_1 = childTree.value;
|
8448 | }
|
8449 | else {
|
8450 | node = applySubtreeWrite(pathChild(relativePath, childKey), childTree, node);
|
8451 | }
|
8452 | });
|
8453 |
|
8454 | if (!node.getChild(relativePath).isEmpty() && priorityWrite_1 !== null) {
|
8455 | node = node.updateChild(pathChild(relativePath, '.priority'), priorityWrite_1);
|
8456 | }
|
8457 | return node;
|
8458 | }
|
8459 | }
|
8460 |
|
8461 |
|
8462 |
|
8463 |
|
8464 |
|
8465 |
|
8466 |
|
8467 |
|
8468 |
|
8469 |
|
8470 |
|
8471 |
|
8472 |
|
8473 |
|
8474 |
|
8475 |
|
8476 |
|
8477 |
|
8478 |
|
8479 |
|
8480 |
|
8481 | function writeTreeChildWrites(writeTree, path) {
|
8482 | return newWriteTreeRef(path, writeTree);
|
8483 | }
|
8484 |
|
8485 |
|
8486 |
|
8487 |
|
8488 |
|
8489 | function writeTreeAddOverwrite(writeTree, path, snap, writeId, visible) {
|
8490 | util.assert(writeId > writeTree.lastWriteId, 'Stacking an older write on top of newer ones');
|
8491 | if (visible === undefined) {
|
8492 | visible = true;
|
8493 | }
|
8494 | writeTree.allWrites.push({
|
8495 | path: path,
|
8496 | snap: snap,
|
8497 | writeId: writeId,
|
8498 | visible: visible
|
8499 | });
|
8500 | if (visible) {
|
8501 | writeTree.visibleWrites = compoundWriteAddWrite(writeTree.visibleWrites, path, snap);
|
8502 | }
|
8503 | writeTree.lastWriteId = writeId;
|
8504 | }
|
8505 |
|
8506 |
|
8507 |
|
8508 | function writeTreeAddMerge(writeTree, path, changedChildren, writeId) {
|
8509 | util.assert(writeId > writeTree.lastWriteId, 'Stacking an older merge on top of newer ones');
|
8510 | writeTree.allWrites.push({
|
8511 | path: path,
|
8512 | children: changedChildren,
|
8513 | writeId: writeId,
|
8514 | visible: true
|
8515 | });
|
8516 | writeTree.visibleWrites = compoundWriteAddWrites(writeTree.visibleWrites, path, changedChildren);
|
8517 | writeTree.lastWriteId = writeId;
|
8518 | }
|
8519 | function writeTreeGetWrite(writeTree, writeId) {
|
8520 | for (var i = 0; i < writeTree.allWrites.length; i++) {
|
8521 | var record = writeTree.allWrites[i];
|
8522 | if (record.writeId === writeId) {
|
8523 | return record;
|
8524 | }
|
8525 | }
|
8526 | return null;
|
8527 | }
|
8528 |
|
8529 |
|
8530 |
|
8531 |
|
8532 |
|
8533 |
|
8534 |
|
8535 | function writeTreeRemoveWrite(writeTree, writeId) {
|
8536 |
|
8537 |
|
8538 |
|
8539 |
|
8540 | var idx = writeTree.allWrites.findIndex(function (s) {
|
8541 | return s.writeId === writeId;
|
8542 | });
|
8543 | util.assert(idx >= 0, 'removeWrite called with nonexistent writeId.');
|
8544 | var writeToRemove = writeTree.allWrites[idx];
|
8545 | writeTree.allWrites.splice(idx, 1);
|
8546 | var removedWriteWasVisible = writeToRemove.visible;
|
8547 | var removedWriteOverlapsWithOtherWrites = false;
|
8548 | var i = writeTree.allWrites.length - 1;
|
8549 | while (removedWriteWasVisible && i >= 0) {
|
8550 | var currentWrite = writeTree.allWrites[i];
|
8551 | if (currentWrite.visible) {
|
8552 | if (i >= idx &&
|
8553 | writeTreeRecordContainsPath_(currentWrite, writeToRemove.path)) {
|
8554 |
|
8555 | removedWriteWasVisible = false;
|
8556 | }
|
8557 | else if (pathContains(writeToRemove.path, currentWrite.path)) {
|
8558 |
|
8559 | removedWriteOverlapsWithOtherWrites = true;
|
8560 | }
|
8561 | }
|
8562 | i--;
|
8563 | }
|
8564 | if (!removedWriteWasVisible) {
|
8565 | return false;
|
8566 | }
|
8567 | else if (removedWriteOverlapsWithOtherWrites) {
|
8568 |
|
8569 | writeTreeResetTree_(writeTree);
|
8570 | return true;
|
8571 | }
|
8572 | else {
|
8573 |
|
8574 | if (writeToRemove.snap) {
|
8575 | writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, writeToRemove.path);
|
8576 | }
|
8577 | else {
|
8578 | var children = writeToRemove.children;
|
8579 | each(children, function (childName) {
|
8580 | writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, pathChild(writeToRemove.path, childName));
|
8581 | });
|
8582 | }
|
8583 | return true;
|
8584 | }
|
8585 | }
|
8586 | function writeTreeRecordContainsPath_(writeRecord, path) {
|
8587 | if (writeRecord.snap) {
|
8588 | return pathContains(writeRecord.path, path);
|
8589 | }
|
8590 | else {
|
8591 | for (var childName in writeRecord.children) {
|
8592 | if (writeRecord.children.hasOwnProperty(childName) &&
|
8593 | pathContains(pathChild(writeRecord.path, childName), path)) {
|
8594 | return true;
|
8595 | }
|
8596 | }
|
8597 | return false;
|
8598 | }
|
8599 | }
|
8600 |
|
8601 |
|
8602 |
|
8603 | function writeTreeResetTree_(writeTree) {
|
8604 | writeTree.visibleWrites = writeTreeLayerTree_(writeTree.allWrites, writeTreeDefaultFilter_, newEmptyPath());
|
8605 | if (writeTree.allWrites.length > 0) {
|
8606 | writeTree.lastWriteId =
|
8607 | writeTree.allWrites[writeTree.allWrites.length - 1].writeId;
|
8608 | }
|
8609 | else {
|
8610 | writeTree.lastWriteId = -1;
|
8611 | }
|
8612 | }
|
8613 |
|
8614 |
|
8615 |
|
8616 | function writeTreeDefaultFilter_(write) {
|
8617 | return write.visible;
|
8618 | }
|
8619 |
|
8620 |
|
8621 |
|
8622 |
|
8623 | function writeTreeLayerTree_(writes, filter, treeRoot) {
|
8624 | var compoundWrite = CompoundWrite.empty();
|
8625 | for (var i = 0; i < writes.length; ++i) {
|
8626 | var write = writes[i];
|
8627 |
|
8628 |
|
8629 |
|
8630 | if (filter(write)) {
|
8631 | var writePath = write.path;
|
8632 | var relativePath = void 0;
|
8633 | if (write.snap) {
|
8634 | if (pathContains(treeRoot, writePath)) {
|
8635 | relativePath = newRelativePath(treeRoot, writePath);
|
8636 | compoundWrite = compoundWriteAddWrite(compoundWrite, relativePath, write.snap);
|
8637 | }
|
8638 | else if (pathContains(writePath, treeRoot)) {
|
8639 | relativePath = newRelativePath(writePath, treeRoot);
|
8640 | compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), write.snap.getChild(relativePath));
|
8641 | }
|
8642 | else ;
|
8643 | }
|
8644 | else if (write.children) {
|
8645 | if (pathContains(treeRoot, writePath)) {
|
8646 | relativePath = newRelativePath(treeRoot, writePath);
|
8647 | compoundWrite = compoundWriteAddWrites(compoundWrite, relativePath, write.children);
|
8648 | }
|
8649 | else if (pathContains(writePath, treeRoot)) {
|
8650 | relativePath = newRelativePath(writePath, treeRoot);
|
8651 | if (pathIsEmpty(relativePath)) {
|
8652 | compoundWrite = compoundWriteAddWrites(compoundWrite, newEmptyPath(), write.children);
|
8653 | }
|
8654 | else {
|
8655 | var child = util.safeGet(write.children, pathGetFront(relativePath));
|
8656 | if (child) {
|
8657 |
|
8658 | var deepNode = child.getChild(pathPopFront(relativePath));
|
8659 | compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), deepNode);
|
8660 | }
|
8661 | }
|
8662 | }
|
8663 | else ;
|
8664 | }
|
8665 | else {
|
8666 | throw util.assertionError('WriteRecord should have .snap or .children');
|
8667 | }
|
8668 | }
|
8669 | }
|
8670 | return compoundWrite;
|
8671 | }
|
8672 |
|
8673 |
|
8674 |
|
8675 |
|
8676 |
|
8677 |
|
8678 |
|
8679 | function writeTreeCalcCompleteEventCache(writeTree, treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
|
8680 | if (!writeIdsToExclude && !includeHiddenWrites) {
|
8681 | var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
|
8682 | if (shadowingNode != null) {
|
8683 | return shadowingNode;
|
8684 | }
|
8685 | else {
|
8686 | var subMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8687 | if (compoundWriteIsEmpty(subMerge)) {
|
8688 | return completeServerCache;
|
8689 | }
|
8690 | else if (completeServerCache == null &&
|
8691 | !compoundWriteHasCompleteWrite(subMerge, newEmptyPath())) {
|
8692 |
|
8693 | return null;
|
8694 | }
|
8695 | else {
|
8696 | var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
|
8697 | return compoundWriteApply(subMerge, layeredCache);
|
8698 | }
|
8699 | }
|
8700 | }
|
8701 | else {
|
8702 | var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8703 | if (!includeHiddenWrites && compoundWriteIsEmpty(merge)) {
|
8704 | return completeServerCache;
|
8705 | }
|
8706 | else {
|
8707 |
|
8708 | if (!includeHiddenWrites &&
|
8709 | completeServerCache == null &&
|
8710 | !compoundWriteHasCompleteWrite(merge, newEmptyPath())) {
|
8711 | return null;
|
8712 | }
|
8713 | else {
|
8714 | var filter = function (write) {
|
8715 | return ((write.visible || includeHiddenWrites) &&
|
8716 | (!writeIdsToExclude ||
|
8717 | !~writeIdsToExclude.indexOf(write.writeId)) &&
|
8718 | (pathContains(write.path, treePath) ||
|
8719 | pathContains(treePath, write.path)));
|
8720 | };
|
8721 | var mergeAtPath = writeTreeLayerTree_(writeTree.allWrites, filter, treePath);
|
8722 | var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
|
8723 | return compoundWriteApply(mergeAtPath, layeredCache);
|
8724 | }
|
8725 | }
|
8726 | }
|
8727 | }
|
8728 |
|
8729 |
|
8730 |
|
8731 |
|
8732 | function writeTreeCalcCompleteEventChildren(writeTree, treePath, completeServerChildren) {
|
8733 | var completeChildren = ChildrenNode.EMPTY_NODE;
|
8734 | var topLevelSet = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
|
8735 | if (topLevelSet) {
|
8736 | if (!topLevelSet.isLeafNode()) {
|
8737 |
|
8738 | topLevelSet.forEachChild(PRIORITY_INDEX, function (childName, childSnap) {
|
8739 | completeChildren = completeChildren.updateImmediateChild(childName, childSnap);
|
8740 | });
|
8741 | }
|
8742 | return completeChildren;
|
8743 | }
|
8744 | else if (completeServerChildren) {
|
8745 |
|
8746 |
|
8747 | var merge_1 = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8748 | completeServerChildren.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
|
8749 | var node = compoundWriteApply(compoundWriteChildCompoundWrite(merge_1, new Path(childName)), childNode);
|
8750 | completeChildren = completeChildren.updateImmediateChild(childName, node);
|
8751 | });
|
8752 |
|
8753 | compoundWriteGetCompleteChildren(merge_1).forEach(function (namedNode) {
|
8754 | completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
|
8755 | });
|
8756 | return completeChildren;
|
8757 | }
|
8758 | else {
|
8759 |
|
8760 |
|
8761 | var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8762 | compoundWriteGetCompleteChildren(merge).forEach(function (namedNode) {
|
8763 | completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
|
8764 | });
|
8765 | return completeChildren;
|
8766 | }
|
8767 | }
|
8768 |
|
8769 |
|
8770 |
|
8771 |
|
8772 |
|
8773 |
|
8774 |
|
8775 |
|
8776 |
|
8777 |
|
8778 |
|
8779 |
|
8780 |
|
8781 |
|
8782 | function writeTreeCalcEventCacheAfterServerOverwrite(writeTree, treePath, childPath, existingEventSnap, existingServerSnap) {
|
8783 | util.assert(existingEventSnap || existingServerSnap, 'Either existingEventSnap or existingServerSnap must exist');
|
8784 | var path = pathChild(treePath, childPath);
|
8785 | if (compoundWriteHasCompleteWrite(writeTree.visibleWrites, path)) {
|
8786 |
|
8787 |
|
8788 | return null;
|
8789 | }
|
8790 | else {
|
8791 |
|
8792 | var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
|
8793 | if (compoundWriteIsEmpty(childMerge)) {
|
8794 |
|
8795 | return existingServerSnap.getChild(childPath);
|
8796 | }
|
8797 | else {
|
8798 |
|
8799 |
|
8800 |
|
8801 |
|
8802 |
|
8803 |
|
8804 | return compoundWriteApply(childMerge, existingServerSnap.getChild(childPath));
|
8805 | }
|
8806 | }
|
8807 | }
|
8808 |
|
8809 |
|
8810 |
|
8811 |
|
8812 | function writeTreeCalcCompleteChild(writeTree, treePath, childKey, existingServerSnap) {
|
8813 | var path = pathChild(treePath, childKey);
|
8814 | var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
|
8815 | if (shadowingNode != null) {
|
8816 | return shadowingNode;
|
8817 | }
|
8818 | else {
|
8819 | if (existingServerSnap.isCompleteForChild(childKey)) {
|
8820 | var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
|
8821 | return compoundWriteApply(childMerge, existingServerSnap.getNode().getImmediateChild(childKey));
|
8822 | }
|
8823 | else {
|
8824 | return null;
|
8825 | }
|
8826 | }
|
8827 | }
|
8828 |
|
8829 |
|
8830 |
|
8831 |
|
8832 |
|
8833 | function writeTreeShadowingWrite(writeTree, path) {
|
8834 | return compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
|
8835 | }
|
8836 |
|
8837 |
|
8838 |
|
8839 |
|
8840 | function writeTreeCalcIndexedSlice(writeTree, treePath, completeServerData, startPost, count, reverse, index) {
|
8841 | var toIterate;
|
8842 | var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8843 | var shadowingNode = compoundWriteGetCompleteNode(merge, newEmptyPath());
|
8844 | if (shadowingNode != null) {
|
8845 | toIterate = shadowingNode;
|
8846 | }
|
8847 | else if (completeServerData != null) {
|
8848 | toIterate = compoundWriteApply(merge, completeServerData);
|
8849 | }
|
8850 | else {
|
8851 |
|
8852 | return [];
|
8853 | }
|
8854 | toIterate = toIterate.withIndex(index);
|
8855 | if (!toIterate.isEmpty() && !toIterate.isLeafNode()) {
|
8856 | var nodes = [];
|
8857 | var cmp = index.getCompare();
|
8858 | var iter = reverse
|
8859 | ? toIterate.getReverseIteratorFrom(startPost, index)
|
8860 | : toIterate.getIteratorFrom(startPost, index);
|
8861 | var next = iter.getNext();
|
8862 | while (next && nodes.length < count) {
|
8863 | if (cmp(next, startPost) !== 0) {
|
8864 | nodes.push(next);
|
8865 | }
|
8866 | next = iter.getNext();
|
8867 | }
|
8868 | return nodes;
|
8869 | }
|
8870 | else {
|
8871 | return [];
|
8872 | }
|
8873 | }
|
8874 | function newWriteTree() {
|
8875 | return {
|
8876 | visibleWrites: CompoundWrite.empty(),
|
8877 | allWrites: [],
|
8878 | lastWriteId: -1
|
8879 | };
|
8880 | }
|
8881 |
|
8882 |
|
8883 |
|
8884 |
|
8885 |
|
8886 |
|
8887 |
|
8888 |
|
8889 | function writeTreeRefCalcCompleteEventCache(writeTreeRef, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
|
8890 | return writeTreeCalcCompleteEventCache(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites);
|
8891 | }
|
8892 |
|
8893 |
|
8894 |
|
8895 |
|
8896 |
|
8897 | function writeTreeRefCalcCompleteEventChildren(writeTreeRef, completeServerChildren) {
|
8898 | return writeTreeCalcCompleteEventChildren(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerChildren);
|
8899 | }
|
8900 |
|
8901 |
|
8902 |
|
8903 |
|
8904 |
|
8905 |
|
8906 |
|
8907 |
|
8908 |
|
8909 |
|
8910 |
|
8911 |
|
8912 |
|
8913 |
|
8914 |
|
8915 |
|
8916 | function writeTreeRefCalcEventCacheAfterServerOverwrite(writeTreeRef, path, existingEventSnap, existingServerSnap) {
|
8917 | return writeTreeCalcEventCacheAfterServerOverwrite(writeTreeRef.writeTree, writeTreeRef.treePath, path, existingEventSnap, existingServerSnap);
|
8918 | }
|
8919 |
|
8920 |
|
8921 |
|
8922 |
|
8923 |
|
8924 |
|
8925 | function writeTreeRefShadowingWrite(writeTreeRef, path) {
|
8926 | return writeTreeShadowingWrite(writeTreeRef.writeTree, pathChild(writeTreeRef.treePath, path));
|
8927 | }
|
8928 |
|
8929 |
|
8930 |
|
8931 |
|
8932 | function writeTreeRefCalcIndexedSlice(writeTreeRef, completeServerData, startPost, count, reverse, index) {
|
8933 | return writeTreeCalcIndexedSlice(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerData, startPost, count, reverse, index);
|
8934 | }
|
8935 |
|
8936 |
|
8937 |
|
8938 |
|
8939 | function writeTreeRefCalcCompleteChild(writeTreeRef, childKey, existingServerCache) {
|
8940 | return writeTreeCalcCompleteChild(writeTreeRef.writeTree, writeTreeRef.treePath, childKey, existingServerCache);
|
8941 | }
|
8942 |
|
8943 |
|
8944 |
|
8945 | function writeTreeRefChild(writeTreeRef, childName) {
|
8946 | return newWriteTreeRef(pathChild(writeTreeRef.treePath, childName), writeTreeRef.writeTree);
|
8947 | }
|
8948 | function newWriteTreeRef(path, writeTree) {
|
8949 | return {
|
8950 | treePath: path,
|
8951 | writeTree: writeTree
|
8952 | };
|
8953 | }
|
8954 |
|
8955 |
|
8956 |
|
8957 |
|
8958 |
|
8959 |
|
8960 |
|
8961 |
|
8962 |
|
8963 |
|
8964 |
|
8965 |
|
8966 |
|
8967 |
|
8968 |
|
8969 |
|
8970 |
|
8971 | var ChildChangeAccumulator = (function () {
|
8972 | function ChildChangeAccumulator() {
|
8973 | this.changeMap = new Map();
|
8974 | }
|
8975 | ChildChangeAccumulator.prototype.trackChildChange = function (change) {
|
8976 | var type = change.type;
|
8977 | var childKey = change.childName;
|
8978 | util.assert(type === "child_added" ||
|
8979 | type === "child_changed" ||
|
8980 | type === "child_removed" , 'Only child changes supported for tracking');
|
8981 | util.assert(childKey !== '.priority', 'Only non-priority child changes can be tracked.');
|
8982 | var oldChange = this.changeMap.get(childKey);
|
8983 | if (oldChange) {
|
8984 | var oldType = oldChange.type;
|
8985 | if (type === "child_added" &&
|
8986 | oldType === "child_removed" ) {
|
8987 | this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.snapshotNode));
|
8988 | }
|
8989 | else if (type === "child_removed" &&
|
8990 | oldType === "child_added" ) {
|
8991 | this.changeMap.delete(childKey);
|
8992 | }
|
8993 | else if (type === "child_removed" &&
|
8994 | oldType === "child_changed" ) {
|
8995 | this.changeMap.set(childKey, changeChildRemoved(childKey, oldChange.oldSnap));
|
8996 | }
|
8997 | else if (type === "child_changed" &&
|
8998 | oldType === "child_added" ) {
|
8999 | this.changeMap.set(childKey, changeChildAdded(childKey, change.snapshotNode));
|
9000 | }
|
9001 | else if (type === "child_changed" &&
|
9002 | oldType === "child_changed" ) {
|
9003 | this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.oldSnap));
|
9004 | }
|
9005 | else {
|
9006 | throw util.assertionError('Illegal combination of changes: ' +
|
9007 | change +
|
9008 | ' occurred after ' +
|
9009 | oldChange);
|
9010 | }
|
9011 | }
|
9012 | else {
|
9013 | this.changeMap.set(childKey, change);
|
9014 | }
|
9015 | };
|
9016 | ChildChangeAccumulator.prototype.getChanges = function () {
|
9017 | return Array.from(this.changeMap.values());
|
9018 | };
|
9019 | return ChildChangeAccumulator;
|
9020 | }());
|
9021 |
|
9022 |
|
9023 |
|
9024 |
|
9025 |
|
9026 |
|
9027 |
|
9028 |
|
9029 |
|
9030 |
|
9031 |
|
9032 |
|
9033 |
|
9034 |
|
9035 |
|
9036 |
|
9037 |
|
9038 |
|
9039 |
|
9040 |
|
9041 |
|
9042 | var NoCompleteChildSource_ = (function () {
|
9043 | function NoCompleteChildSource_() {
|
9044 | }
|
9045 | NoCompleteChildSource_.prototype.getCompleteChild = function (childKey) {
|
9046 | return null;
|
9047 | };
|
9048 | NoCompleteChildSource_.prototype.getChildAfterChild = function (index, child, reverse) {
|
9049 | return null;
|
9050 | };
|
9051 | return NoCompleteChildSource_;
|
9052 | }());
|
9053 |
|
9054 |
|
9055 |
|
9056 | var NO_COMPLETE_CHILD_SOURCE = new NoCompleteChildSource_();
|
9057 |
|
9058 |
|
9059 |
|
9060 |
|
9061 | var WriteTreeCompleteChildSource = (function () {
|
9062 | function WriteTreeCompleteChildSource(writes_, viewCache_, optCompleteServerCache_) {
|
9063 | if (optCompleteServerCache_ === void 0) { optCompleteServerCache_ = null; }
|
9064 | this.writes_ = writes_;
|
9065 | this.viewCache_ = viewCache_;
|
9066 | this.optCompleteServerCache_ = optCompleteServerCache_;
|
9067 | }
|
9068 | WriteTreeCompleteChildSource.prototype.getCompleteChild = function (childKey) {
|
9069 | var node = this.viewCache_.eventCache;
|
9070 | if (node.isCompleteForChild(childKey)) {
|
9071 | return node.getNode().getImmediateChild(childKey);
|
9072 | }
|
9073 | else {
|
9074 | var serverNode = this.optCompleteServerCache_ != null
|
9075 | ? new CacheNode(this.optCompleteServerCache_, true, false)
|
9076 | : this.viewCache_.serverCache;
|
9077 | return writeTreeRefCalcCompleteChild(this.writes_, childKey, serverNode);
|
9078 | }
|
9079 | };
|
9080 | WriteTreeCompleteChildSource.prototype.getChildAfterChild = function (index, child, reverse) {
|
9081 | var completeServerData = this.optCompleteServerCache_ != null
|
9082 | ? this.optCompleteServerCache_
|
9083 | : viewCacheGetCompleteServerSnap(this.viewCache_);
|
9084 | var nodes = writeTreeRefCalcIndexedSlice(this.writes_, completeServerData, child, 1, reverse, index);
|
9085 | if (nodes.length === 0) {
|
9086 | return null;
|
9087 | }
|
9088 | else {
|
9089 | return nodes[0];
|
9090 | }
|
9091 | };
|
9092 | return WriteTreeCompleteChildSource;
|
9093 | }());
|
9094 |
|
9095 |
|
9096 |
|
9097 |
|
9098 |
|
9099 |
|
9100 |
|
9101 |
|
9102 |
|
9103 |
|
9104 |
|
9105 |
|
9106 |
|
9107 |
|
9108 |
|
9109 |
|
9110 |
|
9111 | function newViewProcessor(filter) {
|
9112 | return { filter: filter };
|
9113 | }
|
9114 | function viewProcessorAssertIndexed(viewProcessor, viewCache) {
|
9115 | util.assert(viewCache.eventCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Event snap not indexed');
|
9116 | util.assert(viewCache.serverCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Server snap not indexed');
|
9117 | }
|
9118 | function viewProcessorApplyOperation(viewProcessor, oldViewCache, operation, writesCache, completeCache) {
|
9119 | var accumulator = new ChildChangeAccumulator();
|
9120 | var newViewCache, filterServerNode;
|
9121 | if (operation.type === OperationType.OVERWRITE) {
|
9122 | var overwrite = operation;
|
9123 | if (overwrite.source.fromUser) {
|
9124 | newViewCache = viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, accumulator);
|
9125 | }
|
9126 | else {
|
9127 | util.assert(overwrite.source.fromServer, 'Unknown source.');
|
9128 |
|
9129 |
|
9130 |
|
9131 | filterServerNode =
|
9132 | overwrite.source.tagged ||
|
9133 | (oldViewCache.serverCache.isFiltered() && !pathIsEmpty(overwrite.path));
|
9134 | newViewCache = viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, filterServerNode, accumulator);
|
9135 | }
|
9136 | }
|
9137 | else if (operation.type === OperationType.MERGE) {
|
9138 | var merge = operation;
|
9139 | if (merge.source.fromUser) {
|
9140 | newViewCache = viewProcessorApplyUserMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, accumulator);
|
9141 | }
|
9142 | else {
|
9143 | util.assert(merge.source.fromServer, 'Unknown source.');
|
9144 |
|
9145 | filterServerNode =
|
9146 | merge.source.tagged || oldViewCache.serverCache.isFiltered();
|
9147 | newViewCache = viewProcessorApplyServerMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, filterServerNode, accumulator);
|
9148 | }
|
9149 | }
|
9150 | else if (operation.type === OperationType.ACK_USER_WRITE) {
|
9151 | var ackUserWrite = operation;
|
9152 | if (!ackUserWrite.revert) {
|
9153 | newViewCache = viewProcessorAckUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, ackUserWrite.affectedTree, writesCache, completeCache, accumulator);
|
9154 | }
|
9155 | else {
|
9156 | newViewCache = viewProcessorRevertUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, writesCache, completeCache, accumulator);
|
9157 | }
|
9158 | }
|
9159 | else if (operation.type === OperationType.LISTEN_COMPLETE) {
|
9160 | newViewCache = viewProcessorListenComplete(viewProcessor, oldViewCache, operation.path, writesCache, accumulator);
|
9161 | }
|
9162 | else {
|
9163 | throw util.assertionError('Unknown operation type: ' + operation.type);
|
9164 | }
|
9165 | var changes = accumulator.getChanges();
|
9166 | viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, changes);
|
9167 | return { viewCache: newViewCache, changes: changes };
|
9168 | }
|
9169 | function viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, accumulator) {
|
9170 | var eventSnap = newViewCache.eventCache;
|
9171 | if (eventSnap.isFullyInitialized()) {
|
9172 | var isLeafOrEmpty = eventSnap.getNode().isLeafNode() || eventSnap.getNode().isEmpty();
|
9173 | var oldCompleteSnap = viewCacheGetCompleteEventSnap(oldViewCache);
|
9174 | if (accumulator.length > 0 ||
|
9175 | !oldViewCache.eventCache.isFullyInitialized() ||
|
9176 | (isLeafOrEmpty && !eventSnap.getNode().equals(oldCompleteSnap)) ||
|
9177 | !eventSnap.getNode().getPriority().equals(oldCompleteSnap.getPriority())) {
|
9178 | accumulator.push(changeValue(viewCacheGetCompleteEventSnap(newViewCache)));
|
9179 | }
|
9180 | }
|
9181 | }
|
9182 | function viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, viewCache, changePath, writesCache, source, accumulator) {
|
9183 | var oldEventSnap = viewCache.eventCache;
|
9184 | if (writeTreeRefShadowingWrite(writesCache, changePath) != null) {
|
9185 |
|
9186 | return viewCache;
|
9187 | }
|
9188 | else {
|
9189 | var newEventCache = void 0, serverNode = void 0;
|
9190 | if (pathIsEmpty(changePath)) {
|
9191 |
|
9192 | util.assert(viewCache.serverCache.isFullyInitialized(), 'If change path is empty, we must have complete server data');
|
9193 | if (viewCache.serverCache.isFiltered()) {
|
9194 |
|
9195 |
|
9196 |
|
9197 | var serverCache = viewCacheGetCompleteServerSnap(viewCache);
|
9198 | var completeChildren = serverCache instanceof ChildrenNode
|
9199 | ? serverCache
|
9200 | : ChildrenNode.EMPTY_NODE;
|
9201 | var completeEventChildren = writeTreeRefCalcCompleteEventChildren(writesCache, completeChildren);
|
9202 | newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeEventChildren, accumulator);
|
9203 | }
|
9204 | else {
|
9205 | var completeNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
|
9206 | newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeNode, accumulator);
|
9207 | }
|
9208 | }
|
9209 | else {
|
9210 | var childKey = pathGetFront(changePath);
|
9211 | if (childKey === '.priority') {
|
9212 | util.assert(pathGetLength(changePath) === 1, "Can't have a priority with additional path components");
|
9213 | var oldEventNode = oldEventSnap.getNode();
|
9214 | serverNode = viewCache.serverCache.getNode();
|
9215 |
|
9216 | var updatedPriority = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventNode, serverNode);
|
9217 | if (updatedPriority != null) {
|
9218 | newEventCache = viewProcessor.filter.updatePriority(oldEventNode, updatedPriority);
|
9219 | }
|
9220 | else {
|
9221 |
|
9222 | newEventCache = oldEventSnap.getNode();
|
9223 | }
|
9224 | }
|
9225 | else {
|
9226 | var childChangePath = pathPopFront(changePath);
|
9227 |
|
9228 | var newEventChild = void 0;
|
9229 | if (oldEventSnap.isCompleteForChild(childKey)) {
|
9230 | serverNode = viewCache.serverCache.getNode();
|
9231 | var eventChildUpdate = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventSnap.getNode(), serverNode);
|
9232 | if (eventChildUpdate != null) {
|
9233 | newEventChild = oldEventSnap
|
9234 | .getNode()
|
9235 | .getImmediateChild(childKey)
|
9236 | .updateChild(childChangePath, eventChildUpdate);
|
9237 | }
|
9238 | else {
|
9239 |
|
9240 | newEventChild = oldEventSnap.getNode().getImmediateChild(childKey);
|
9241 | }
|
9242 | }
|
9243 | else {
|
9244 | newEventChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
|
9245 | }
|
9246 | if (newEventChild != null) {
|
9247 | newEventCache = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newEventChild, childChangePath, source, accumulator);
|
9248 | }
|
9249 | else {
|
9250 |
|
9251 | newEventCache = oldEventSnap.getNode();
|
9252 | }
|
9253 | }
|
9254 | }
|
9255 | return viewCacheUpdateEventSnap(viewCache, newEventCache, oldEventSnap.isFullyInitialized() || pathIsEmpty(changePath), viewProcessor.filter.filtersNodes());
|
9256 | }
|
9257 | }
|
9258 | function viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, filterServerNode, accumulator) {
|
9259 | var oldServerSnap = oldViewCache.serverCache;
|
9260 | var newServerCache;
|
9261 | var serverFilter = filterServerNode
|
9262 | ? viewProcessor.filter
|
9263 | : viewProcessor.filter.getIndexedFilter();
|
9264 | if (pathIsEmpty(changePath)) {
|
9265 | newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), changedSnap, null);
|
9266 | }
|
9267 | else if (serverFilter.filtersNodes() && !oldServerSnap.isFiltered()) {
|
9268 |
|
9269 | var newServerNode = oldServerSnap
|
9270 | .getNode()
|
9271 | .updateChild(changePath, changedSnap);
|
9272 | newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), newServerNode, null);
|
9273 | }
|
9274 | else {
|
9275 | var childKey = pathGetFront(changePath);
|
9276 | if (!oldServerSnap.isCompleteForPath(changePath) &&
|
9277 | pathGetLength(changePath) > 1) {
|
9278 |
|
9279 | return oldViewCache;
|
9280 | }
|
9281 | var childChangePath = pathPopFront(changePath);
|
9282 | var childNode = oldServerSnap.getNode().getImmediateChild(childKey);
|
9283 | var newChildNode = childNode.updateChild(childChangePath, changedSnap);
|
9284 | if (childKey === '.priority') {
|
9285 | newServerCache = serverFilter.updatePriority(oldServerSnap.getNode(), newChildNode);
|
9286 | }
|
9287 | else {
|
9288 | newServerCache = serverFilter.updateChild(oldServerSnap.getNode(), childKey, newChildNode, childChangePath, NO_COMPLETE_CHILD_SOURCE, null);
|
9289 | }
|
9290 | }
|
9291 | var newViewCache = viewCacheUpdateServerSnap(oldViewCache, newServerCache, oldServerSnap.isFullyInitialized() || pathIsEmpty(changePath), serverFilter.filtersNodes());
|
9292 | var source = new WriteTreeCompleteChildSource(writesCache, newViewCache, completeCache);
|
9293 | return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, changePath, writesCache, source, accumulator);
|
9294 | }
|
9295 | function viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, accumulator) {
|
9296 | var oldEventSnap = oldViewCache.eventCache;
|
9297 | var newViewCache, newEventCache;
|
9298 | var source = new WriteTreeCompleteChildSource(writesCache, oldViewCache, completeCache);
|
9299 | if (pathIsEmpty(changePath)) {
|
9300 | newEventCache = viewProcessor.filter.updateFullNode(oldViewCache.eventCache.getNode(), changedSnap, accumulator);
|
9301 | newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, true, viewProcessor.filter.filtersNodes());
|
9302 | }
|
9303 | else {
|
9304 | var childKey = pathGetFront(changePath);
|
9305 | if (childKey === '.priority') {
|
9306 | newEventCache = viewProcessor.filter.updatePriority(oldViewCache.eventCache.getNode(), changedSnap);
|
9307 | newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, oldEventSnap.isFullyInitialized(), oldEventSnap.isFiltered());
|
9308 | }
|
9309 | else {
|
9310 | var childChangePath = pathPopFront(changePath);
|
9311 | var oldChild = oldEventSnap.getNode().getImmediateChild(childKey);
|
9312 | var newChild = void 0;
|
9313 | if (pathIsEmpty(childChangePath)) {
|
9314 |
|
9315 | newChild = changedSnap;
|
9316 | }
|
9317 | else {
|
9318 | var childNode = source.getCompleteChild(childKey);
|
9319 | if (childNode != null) {
|
9320 | if (pathGetBack(childChangePath) === '.priority' &&
|
9321 | childNode.getChild(pathParent(childChangePath)).isEmpty()) {
|
9322 |
|
9323 |
|
9324 | newChild = childNode;
|
9325 | }
|
9326 | else {
|
9327 | newChild = childNode.updateChild(childChangePath, changedSnap);
|
9328 | }
|
9329 | }
|
9330 | else {
|
9331 |
|
9332 | newChild = ChildrenNode.EMPTY_NODE;
|
9333 | }
|
9334 | }
|
9335 | if (!oldChild.equals(newChild)) {
|
9336 | var newEventSnap = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newChild, childChangePath, source, accumulator);
|
9337 | newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventSnap, oldEventSnap.isFullyInitialized(), viewProcessor.filter.filtersNodes());
|
9338 | }
|
9339 | else {
|
9340 | newViewCache = oldViewCache;
|
9341 | }
|
9342 | }
|
9343 | }
|
9344 | return newViewCache;
|
9345 | }
|
9346 | function viewProcessorCacheHasChild(viewCache, childKey) {
|
9347 | return viewCache.eventCache.isCompleteForChild(childKey);
|
9348 | }
|
9349 | function viewProcessorApplyUserMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, accumulator) {
|
9350 |
|
9351 |
|
9352 |
|
9353 |
|
9354 |
|
9355 |
|
9356 | var curViewCache = viewCache;
|
9357 | changedChildren.foreach(function (relativePath, childNode) {
|
9358 | var writePath = pathChild(path, relativePath);
|
9359 | if (viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
|
9360 | curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
|
9361 | }
|
9362 | });
|
9363 | changedChildren.foreach(function (relativePath, childNode) {
|
9364 | var writePath = pathChild(path, relativePath);
|
9365 | if (!viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
|
9366 | curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
|
9367 | }
|
9368 | });
|
9369 | return curViewCache;
|
9370 | }
|
9371 | function viewProcessorApplyMerge(viewProcessor, node, merge) {
|
9372 | merge.foreach(function (relativePath, childNode) {
|
9373 | node = node.updateChild(relativePath, childNode);
|
9374 | });
|
9375 | return node;
|
9376 | }
|
9377 | function viewProcessorApplyServerMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, filterServerNode, accumulator) {
|
9378 |
|
9379 |
|
9380 | if (viewCache.serverCache.getNode().isEmpty() &&
|
9381 | !viewCache.serverCache.isFullyInitialized()) {
|
9382 | return viewCache;
|
9383 | }
|
9384 |
|
9385 |
|
9386 |
|
9387 |
|
9388 |
|
9389 |
|
9390 | var curViewCache = viewCache;
|
9391 | var viewMergeTree;
|
9392 | if (pathIsEmpty(path)) {
|
9393 | viewMergeTree = changedChildren;
|
9394 | }
|
9395 | else {
|
9396 | viewMergeTree = new ImmutableTree(null).setTree(path, changedChildren);
|
9397 | }
|
9398 | var serverNode = viewCache.serverCache.getNode();
|
9399 | viewMergeTree.children.inorderTraversal(function (childKey, childTree) {
|
9400 | if (serverNode.hasChild(childKey)) {
|
9401 | var serverChild = viewCache.serverCache
|
9402 | .getNode()
|
9403 | .getImmediateChild(childKey);
|
9404 | var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childTree);
|
9405 | curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
|
9406 | }
|
9407 | });
|
9408 | viewMergeTree.children.inorderTraversal(function (childKey, childMergeTree) {
|
9409 | var isUnknownDeepMerge = !viewCache.serverCache.isCompleteForChild(childKey) &&
|
9410 | childMergeTree.value === undefined;
|
9411 | if (!serverNode.hasChild(childKey) && !isUnknownDeepMerge) {
|
9412 | var serverChild = viewCache.serverCache
|
9413 | .getNode()
|
9414 | .getImmediateChild(childKey);
|
9415 | var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childMergeTree);
|
9416 | curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
|
9417 | }
|
9418 | });
|
9419 | return curViewCache;
|
9420 | }
|
9421 | function viewProcessorAckUserWrite(viewProcessor, viewCache, ackPath, affectedTree, writesCache, completeCache, accumulator) {
|
9422 | if (writeTreeRefShadowingWrite(writesCache, ackPath) != null) {
|
9423 | return viewCache;
|
9424 | }
|
9425 |
|
9426 | var filterServerNode = viewCache.serverCache.isFiltered();
|
9427 |
|
9428 |
|
9429 | var serverCache = viewCache.serverCache;
|
9430 | if (affectedTree.value != null) {
|
9431 |
|
9432 | if ((pathIsEmpty(ackPath) && serverCache.isFullyInitialized()) ||
|
9433 | serverCache.isCompleteForPath(ackPath)) {
|
9434 | return viewProcessorApplyServerOverwrite(viewProcessor, viewCache, ackPath, serverCache.getNode().getChild(ackPath), writesCache, completeCache, filterServerNode, accumulator);
|
9435 | }
|
9436 | else if (pathIsEmpty(ackPath)) {
|
9437 |
|
9438 |
|
9439 | var changedChildren_1 = new ImmutableTree(null);
|
9440 | serverCache.getNode().forEachChild(KEY_INDEX, function (name, node) {
|
9441 | changedChildren_1 = changedChildren_1.set(new Path(name), node);
|
9442 | });
|
9443 | return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_1, writesCache, completeCache, filterServerNode, accumulator);
|
9444 | }
|
9445 | else {
|
9446 | return viewCache;
|
9447 | }
|
9448 | }
|
9449 | else {
|
9450 |
|
9451 | var changedChildren_2 = new ImmutableTree(null);
|
9452 | affectedTree.foreach(function (mergePath, value) {
|
9453 | var serverCachePath = pathChild(ackPath, mergePath);
|
9454 | if (serverCache.isCompleteForPath(serverCachePath)) {
|
9455 | changedChildren_2 = changedChildren_2.set(mergePath, serverCache.getNode().getChild(serverCachePath));
|
9456 | }
|
9457 | });
|
9458 | return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_2, writesCache, completeCache, filterServerNode, accumulator);
|
9459 | }
|
9460 | }
|
9461 | function viewProcessorListenComplete(viewProcessor, viewCache, path, writesCache, accumulator) {
|
9462 | var oldServerNode = viewCache.serverCache;
|
9463 | var newViewCache = viewCacheUpdateServerSnap(viewCache, oldServerNode.getNode(), oldServerNode.isFullyInitialized() || pathIsEmpty(path), oldServerNode.isFiltered());
|
9464 | return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, path, writesCache, NO_COMPLETE_CHILD_SOURCE, accumulator);
|
9465 | }
|
9466 | function viewProcessorRevertUserWrite(viewProcessor, viewCache, path, writesCache, completeServerCache, accumulator) {
|
9467 | var complete;
|
9468 | if (writeTreeRefShadowingWrite(writesCache, path) != null) {
|
9469 | return viewCache;
|
9470 | }
|
9471 | else {
|
9472 | var source = new WriteTreeCompleteChildSource(writesCache, viewCache, completeServerCache);
|
9473 | var oldEventCache = viewCache.eventCache.getNode();
|
9474 | var newEventCache = void 0;
|
9475 | if (pathIsEmpty(path) || pathGetFront(path) === '.priority') {
|
9476 | var newNode = void 0;
|
9477 | if (viewCache.serverCache.isFullyInitialized()) {
|
9478 | newNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
|
9479 | }
|
9480 | else {
|
9481 | var serverChildren = viewCache.serverCache.getNode();
|
9482 | util.assert(serverChildren instanceof ChildrenNode, 'serverChildren would be complete if leaf node');
|
9483 | newNode = writeTreeRefCalcCompleteEventChildren(writesCache, serverChildren);
|
9484 | }
|
9485 | newNode = newNode;
|
9486 | newEventCache = viewProcessor.filter.updateFullNode(oldEventCache, newNode, accumulator);
|
9487 | }
|
9488 | else {
|
9489 | var childKey = pathGetFront(path);
|
9490 | var newChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
|
9491 | if (newChild == null &&
|
9492 | viewCache.serverCache.isCompleteForChild(childKey)) {
|
9493 | newChild = oldEventCache.getImmediateChild(childKey);
|
9494 | }
|
9495 | if (newChild != null) {
|
9496 | newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, newChild, pathPopFront(path), source, accumulator);
|
9497 | }
|
9498 | else if (viewCache.eventCache.getNode().hasChild(childKey)) {
|
9499 |
|
9500 | newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, ChildrenNode.EMPTY_NODE, pathPopFront(path), source, accumulator);
|
9501 | }
|
9502 | else {
|
9503 | newEventCache = oldEventCache;
|
9504 | }
|
9505 | if (newEventCache.isEmpty() &&
|
9506 | viewCache.serverCache.isFullyInitialized()) {
|
9507 |
|
9508 | complete = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
|
9509 | if (complete.isLeafNode()) {
|
9510 | newEventCache = viewProcessor.filter.updateFullNode(newEventCache, complete, accumulator);
|
9511 | }
|
9512 | }
|
9513 | }
|
9514 | complete =
|
9515 | viewCache.serverCache.isFullyInitialized() ||
|
9516 | writeTreeRefShadowingWrite(writesCache, newEmptyPath()) != null;
|
9517 | return viewCacheUpdateEventSnap(viewCache, newEventCache, complete, viewProcessor.filter.filtersNodes());
|
9518 | }
|
9519 | }
|
9520 |
|
9521 |
|
9522 |
|
9523 |
|
9524 |
|
9525 |
|
9526 |
|
9527 |
|
9528 |
|
9529 |
|
9530 |
|
9531 |
|
9532 |
|
9533 |
|
9534 |
|
9535 |
|
9536 |
|
9537 |
|
9538 |
|
9539 |
|
9540 |
|
9541 |
|
9542 |
|
9543 |
|
9544 |
|
9545 |
|
9546 | var View = (function () {
|
9547 | function View(query_, initialViewCache) {
|
9548 | this.query_ = query_;
|
9549 | this.eventRegistrations_ = [];
|
9550 | var params = this.query_._queryParams;
|
9551 | var indexFilter = new IndexedFilter(params.getIndex());
|
9552 | var filter = queryParamsGetNodeFilter(params);
|
9553 | this.processor_ = newViewProcessor(filter);
|
9554 | var initialServerCache = initialViewCache.serverCache;
|
9555 | var initialEventCache = initialViewCache.eventCache;
|
9556 |
|
9557 | var serverSnap = indexFilter.updateFullNode(ChildrenNode.EMPTY_NODE, initialServerCache.getNode(), null);
|
9558 | var eventSnap = filter.updateFullNode(ChildrenNode.EMPTY_NODE, initialEventCache.getNode(), null);
|
9559 | var newServerCache = new CacheNode(serverSnap, initialServerCache.isFullyInitialized(), indexFilter.filtersNodes());
|
9560 | var newEventCache = new CacheNode(eventSnap, initialEventCache.isFullyInitialized(), filter.filtersNodes());
|
9561 | this.viewCache_ = newViewCache(newEventCache, newServerCache);
|
9562 | this.eventGenerator_ = new EventGenerator(this.query_);
|
9563 | }
|
9564 | Object.defineProperty(View.prototype, "query", {
|
9565 | get: function () {
|
9566 | return this.query_;
|
9567 | },
|
9568 | enumerable: false,
|
9569 | configurable: true
|
9570 | });
|
9571 | return View;
|
9572 | }());
|
9573 | function viewGetServerCache(view) {
|
9574 | return view.viewCache_.serverCache.getNode();
|
9575 | }
|
9576 | function viewGetCompleteNode(view) {
|
9577 | return viewCacheGetCompleteEventSnap(view.viewCache_);
|
9578 | }
|
9579 | function viewGetCompleteServerCache(view, path) {
|
9580 | var cache = viewCacheGetCompleteServerSnap(view.viewCache_);
|
9581 | if (cache) {
|
9582 |
|
9583 |
|
9584 | if (view.query._queryParams.loadsAllData() ||
|
9585 | (!pathIsEmpty(path) &&
|
9586 | !cache.getImmediateChild(pathGetFront(path)).isEmpty())) {
|
9587 | return cache.getChild(path);
|
9588 | }
|
9589 | }
|
9590 | return null;
|
9591 | }
|
9592 | function viewIsEmpty(view) {
|
9593 | return view.eventRegistrations_.length === 0;
|
9594 | }
|
9595 | function viewAddEventRegistration(view, eventRegistration) {
|
9596 | view.eventRegistrations_.push(eventRegistration);
|
9597 | }
|
9598 |
|
9599 |
|
9600 |
|
9601 |
|
9602 |
|
9603 | function viewRemoveEventRegistration(view, eventRegistration, cancelError) {
|
9604 | var cancelEvents = [];
|
9605 | if (cancelError) {
|
9606 | util.assert(eventRegistration == null, 'A cancel should cancel all event registrations.');
|
9607 | var path_1 = view.query._path;
|
9608 | view.eventRegistrations_.forEach(function (registration) {
|
9609 | var maybeEvent = registration.createCancelEvent(cancelError, path_1);
|
9610 | if (maybeEvent) {
|
9611 | cancelEvents.push(maybeEvent);
|
9612 | }
|
9613 | });
|
9614 | }
|
9615 | if (eventRegistration) {
|
9616 | var remaining = [];
|
9617 | for (var i = 0; i < view.eventRegistrations_.length; ++i) {
|
9618 | var existing = view.eventRegistrations_[i];
|
9619 | if (!existing.matches(eventRegistration)) {
|
9620 | remaining.push(existing);
|
9621 | }
|
9622 | else if (eventRegistration.hasAnyCallback()) {
|
9623 |
|
9624 | remaining = remaining.concat(view.eventRegistrations_.slice(i + 1));
|
9625 | break;
|
9626 | }
|
9627 | }
|
9628 | view.eventRegistrations_ = remaining;
|
9629 | }
|
9630 | else {
|
9631 | view.eventRegistrations_ = [];
|
9632 | }
|
9633 | return cancelEvents;
|
9634 | }
|
9635 |
|
9636 |
|
9637 |
|
9638 | function viewApplyOperation(view, operation, writesCache, completeServerCache) {
|
9639 | if (operation.type === OperationType.MERGE &&
|
9640 | operation.source.queryId !== null) {
|
9641 | util.assert(viewCacheGetCompleteServerSnap(view.viewCache_), 'We should always have a full cache before handling merges');
|
9642 | util.assert(viewCacheGetCompleteEventSnap(view.viewCache_), 'Missing event cache, even though we have a server cache');
|
9643 | }
|
9644 | var oldViewCache = view.viewCache_;
|
9645 | var result = viewProcessorApplyOperation(view.processor_, oldViewCache, operation, writesCache, completeServerCache);
|
9646 | viewProcessorAssertIndexed(view.processor_, result.viewCache);
|
9647 | util.assert(result.viewCache.serverCache.isFullyInitialized() ||
|
9648 | !oldViewCache.serverCache.isFullyInitialized(), 'Once a server snap is complete, it should never go back');
|
9649 | view.viewCache_ = result.viewCache;
|
9650 | return viewGenerateEventsForChanges_(view, result.changes, result.viewCache.eventCache.getNode(), null);
|
9651 | }
|
9652 | function viewGetInitialEvents(view, registration) {
|
9653 | var eventSnap = view.viewCache_.eventCache;
|
9654 | var initialChanges = [];
|
9655 | if (!eventSnap.getNode().isLeafNode()) {
|
9656 | var eventNode = eventSnap.getNode();
|
9657 | eventNode.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
9658 | initialChanges.push(changeChildAdded(key, childNode));
|
9659 | });
|
9660 | }
|
9661 | if (eventSnap.isFullyInitialized()) {
|
9662 | initialChanges.push(changeValue(eventSnap.getNode()));
|
9663 | }
|
9664 | return viewGenerateEventsForChanges_(view, initialChanges, eventSnap.getNode(), registration);
|
9665 | }
|
9666 | function viewGenerateEventsForChanges_(view, changes, eventCache, eventRegistration) {
|
9667 | var registrations = eventRegistration
|
9668 | ? [eventRegistration]
|
9669 | : view.eventRegistrations_;
|
9670 | return eventGeneratorGenerateEventsForChanges(view.eventGenerator_, changes, eventCache, registrations);
|
9671 | }
|
9672 |
|
9673 |
|
9674 |
|
9675 |
|
9676 |
|
9677 |
|
9678 |
|
9679 |
|
9680 |
|
9681 |
|
9682 |
|
9683 |
|
9684 |
|
9685 |
|
9686 |
|
9687 |
|
9688 |
|
9689 | var referenceConstructor$1;
|
9690 |
|
9691 |
|
9692 |
|
9693 |
|
9694 |
|
9695 |
|
9696 |
|
9697 |
|
9698 |
|
9699 |
|
9700 | var SyncPoint = (function () {
|
9701 | function SyncPoint() {
|
9702 | |
9703 |
|
9704 |
|
9705 |
|
9706 |
|
9707 |
|
9708 | this.views = new Map();
|
9709 | }
|
9710 | return SyncPoint;
|
9711 | }());
|
9712 | function syncPointSetReferenceConstructor(val) {
|
9713 | util.assert(!referenceConstructor$1, '__referenceConstructor has already been defined');
|
9714 | referenceConstructor$1 = val;
|
9715 | }
|
9716 | function syncPointGetReferenceConstructor() {
|
9717 | util.assert(referenceConstructor$1, 'Reference.ts has not been loaded');
|
9718 | return referenceConstructor$1;
|
9719 | }
|
9720 | function syncPointIsEmpty(syncPoint) {
|
9721 | return syncPoint.views.size === 0;
|
9722 | }
|
9723 | function syncPointApplyOperation(syncPoint, operation, writesCache, optCompleteServerCache) {
|
9724 | var e_1, _a;
|
9725 | var queryId = operation.source.queryId;
|
9726 | if (queryId !== null) {
|
9727 | var view = syncPoint.views.get(queryId);
|
9728 | util.assert(view != null, 'SyncTree gave us an op for an invalid query.');
|
9729 | return viewApplyOperation(view, operation, writesCache, optCompleteServerCache);
|
9730 | }
|
9731 | else {
|
9732 | var events = [];
|
9733 | try {
|
9734 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9735 | var view = _c.value;
|
9736 | events = events.concat(viewApplyOperation(view, operation, writesCache, optCompleteServerCache));
|
9737 | }
|
9738 | }
|
9739 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
9740 | finally {
|
9741 | try {
|
9742 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9743 | }
|
9744 | finally { if (e_1) throw e_1.error; }
|
9745 | }
|
9746 | return events;
|
9747 | }
|
9748 | }
|
9749 |
|
9750 |
|
9751 |
|
9752 |
|
9753 |
|
9754 |
|
9755 |
|
9756 |
|
9757 |
|
9758 | function syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete) {
|
9759 | var queryId = query._queryIdentifier;
|
9760 | var view = syncPoint.views.get(queryId);
|
9761 | if (!view) {
|
9762 |
|
9763 | var eventCache = writeTreeRefCalcCompleteEventCache(writesCache, serverCacheComplete ? serverCache : null);
|
9764 | var eventCacheComplete = false;
|
9765 | if (eventCache) {
|
9766 | eventCacheComplete = true;
|
9767 | }
|
9768 | else if (serverCache instanceof ChildrenNode) {
|
9769 | eventCache = writeTreeRefCalcCompleteEventChildren(writesCache, serverCache);
|
9770 | eventCacheComplete = false;
|
9771 | }
|
9772 | else {
|
9773 | eventCache = ChildrenNode.EMPTY_NODE;
|
9774 | eventCacheComplete = false;
|
9775 | }
|
9776 | var viewCache = newViewCache(new CacheNode(eventCache, eventCacheComplete, false), new CacheNode(serverCache, serverCacheComplete, false));
|
9777 | return new View(query, viewCache);
|
9778 | }
|
9779 | return view;
|
9780 | }
|
9781 |
|
9782 |
|
9783 |
|
9784 |
|
9785 |
|
9786 |
|
9787 |
|
9788 |
|
9789 |
|
9790 |
|
9791 | function syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete) {
|
9792 | var view = syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete);
|
9793 | if (!syncPoint.views.has(query._queryIdentifier)) {
|
9794 | syncPoint.views.set(query._queryIdentifier, view);
|
9795 | }
|
9796 |
|
9797 | viewAddEventRegistration(view, eventRegistration);
|
9798 | return viewGetInitialEvents(view, eventRegistration);
|
9799 | }
|
9800 |
|
9801 |
|
9802 |
|
9803 |
|
9804 |
|
9805 |
|
9806 |
|
9807 |
|
9808 |
|
9809 |
|
9810 | function syncPointRemoveEventRegistration(syncPoint, query, eventRegistration, cancelError) {
|
9811 | var e_2, _a;
|
9812 | var queryId = query._queryIdentifier;
|
9813 | var removed = [];
|
9814 | var cancelEvents = [];
|
9815 | var hadCompleteView = syncPointHasCompleteView(syncPoint);
|
9816 | if (queryId === 'default') {
|
9817 | try {
|
9818 |
|
9819 | for (var _b = tslib.__values(syncPoint.views.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9820 | var _d = tslib.__read(_c.value, 2), viewQueryId = _d[0], view = _d[1];
|
9821 | cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
|
9822 | if (viewIsEmpty(view)) {
|
9823 | syncPoint.views.delete(viewQueryId);
|
9824 |
|
9825 | if (!view.query._queryParams.loadsAllData()) {
|
9826 | removed.push(view.query);
|
9827 | }
|
9828 | }
|
9829 | }
|
9830 | }
|
9831 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
9832 | finally {
|
9833 | try {
|
9834 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9835 | }
|
9836 | finally { if (e_2) throw e_2.error; }
|
9837 | }
|
9838 | }
|
9839 | else {
|
9840 |
|
9841 | var view = syncPoint.views.get(queryId);
|
9842 | if (view) {
|
9843 | cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
|
9844 | if (viewIsEmpty(view)) {
|
9845 | syncPoint.views.delete(queryId);
|
9846 |
|
9847 | if (!view.query._queryParams.loadsAllData()) {
|
9848 | removed.push(view.query);
|
9849 | }
|
9850 | }
|
9851 | }
|
9852 | }
|
9853 | if (hadCompleteView && !syncPointHasCompleteView(syncPoint)) {
|
9854 |
|
9855 | removed.push(new (syncPointGetReferenceConstructor())(query._repo, query._path));
|
9856 | }
|
9857 | return { removed: removed, events: cancelEvents };
|
9858 | }
|
9859 | function syncPointGetQueryViews(syncPoint) {
|
9860 | var e_3, _a;
|
9861 | var result = [];
|
9862 | try {
|
9863 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9864 | var view = _c.value;
|
9865 | if (!view.query._queryParams.loadsAllData()) {
|
9866 | result.push(view);
|
9867 | }
|
9868 | }
|
9869 | }
|
9870 | catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
9871 | finally {
|
9872 | try {
|
9873 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9874 | }
|
9875 | finally { if (e_3) throw e_3.error; }
|
9876 | }
|
9877 | return result;
|
9878 | }
|
9879 |
|
9880 |
|
9881 |
|
9882 |
|
9883 | function syncPointGetCompleteServerCache(syncPoint, path) {
|
9884 | var e_4, _a;
|
9885 | var serverCache = null;
|
9886 | try {
|
9887 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9888 | var view = _c.value;
|
9889 | serverCache = serverCache || viewGetCompleteServerCache(view, path);
|
9890 | }
|
9891 | }
|
9892 | catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
9893 | finally {
|
9894 | try {
|
9895 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9896 | }
|
9897 | finally { if (e_4) throw e_4.error; }
|
9898 | }
|
9899 | return serverCache;
|
9900 | }
|
9901 | function syncPointViewForQuery(syncPoint, query) {
|
9902 | var params = query._queryParams;
|
9903 | if (params.loadsAllData()) {
|
9904 | return syncPointGetCompleteView(syncPoint);
|
9905 | }
|
9906 | else {
|
9907 | var queryId = query._queryIdentifier;
|
9908 | return syncPoint.views.get(queryId);
|
9909 | }
|
9910 | }
|
9911 | function syncPointViewExistsForQuery(syncPoint, query) {
|
9912 | return syncPointViewForQuery(syncPoint, query) != null;
|
9913 | }
|
9914 | function syncPointHasCompleteView(syncPoint) {
|
9915 | return syncPointGetCompleteView(syncPoint) != null;
|
9916 | }
|
9917 | function syncPointGetCompleteView(syncPoint) {
|
9918 | var e_5, _a;
|
9919 | try {
|
9920 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9921 | var view = _c.value;
|
9922 | if (view.query._queryParams.loadsAllData()) {
|
9923 | return view;
|
9924 | }
|
9925 | }
|
9926 | }
|
9927 | catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
9928 | finally {
|
9929 | try {
|
9930 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9931 | }
|
9932 | finally { if (e_5) throw e_5.error; }
|
9933 | }
|
9934 | return null;
|
9935 | }
|
9936 |
|
9937 |
|
9938 |
|
9939 |
|
9940 |
|
9941 |
|
9942 |
|
9943 |
|
9944 |
|
9945 |
|
9946 |
|
9947 |
|
9948 |
|
9949 |
|
9950 |
|
9951 |
|
9952 |
|
9953 | var referenceConstructor;
|
9954 | function syncTreeSetReferenceConstructor(val) {
|
9955 | util.assert(!referenceConstructor, '__referenceConstructor has already been defined');
|
9956 | referenceConstructor = val;
|
9957 | }
|
9958 | function syncTreeGetReferenceConstructor() {
|
9959 | util.assert(referenceConstructor, 'Reference.ts has not been loaded');
|
9960 | return referenceConstructor;
|
9961 | }
|
9962 |
|
9963 |
|
9964 |
|
9965 | var syncTreeNextQueryTag_ = 1;
|
9966 |
|
9967 |
|
9968 |
|
9969 |
|
9970 |
|
9971 |
|
9972 |
|
9973 |
|
9974 |
|
9975 |
|
9976 |
|
9977 |
|
9978 |
|
9979 |
|
9980 |
|
9981 |
|
9982 |
|
9983 |
|
9984 |
|
9985 |
|
9986 |
|
9987 | var SyncTree = (function () {
|
9988 | |
9989 |
|
9990 |
|
9991 |
|
9992 | function SyncTree(listenProvider_) {
|
9993 | this.listenProvider_ = listenProvider_;
|
9994 | |
9995 |
|
9996 |
|
9997 | this.syncPointTree_ = new ImmutableTree(null);
|
9998 | |
9999 |
|
10000 |
|
10001 | this.pendingWriteTree_ = newWriteTree();
|
10002 | this.tagToQueryMap = new Map();
|
10003 | this.queryToTagMap = new Map();
|
10004 | }
|
10005 | return SyncTree;
|
10006 | }());
|
10007 |
|
10008 |
|
10009 |
|
10010 |
|
10011 |
|
10012 | function syncTreeApplyUserOverwrite(syncTree, path, newData, writeId, visible) {
|
10013 |
|
10014 | writeTreeAddOverwrite(syncTree.pendingWriteTree_, path, newData, writeId, visible);
|
10015 | if (!visible) {
|
10016 | return [];
|
10017 | }
|
10018 | else {
|
10019 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceUser(), path, newData));
|
10020 | }
|
10021 | }
|
10022 |
|
10023 |
|
10024 |
|
10025 |
|
10026 |
|
10027 | function syncTreeApplyUserMerge(syncTree, path, changedChildren, writeId) {
|
10028 |
|
10029 | writeTreeAddMerge(syncTree.pendingWriteTree_, path, changedChildren, writeId);
|
10030 | var changeTree = ImmutableTree.fromObject(changedChildren);
|
10031 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceUser(), path, changeTree));
|
10032 | }
|
10033 |
|
10034 |
|
10035 |
|
10036 |
|
10037 |
|
10038 |
|
10039 | function syncTreeAckUserWrite(syncTree, writeId, revert) {
|
10040 | if (revert === void 0) { revert = false; }
|
10041 | var write = writeTreeGetWrite(syncTree.pendingWriteTree_, writeId);
|
10042 | var needToReevaluate = writeTreeRemoveWrite(syncTree.pendingWriteTree_, writeId);
|
10043 | if (!needToReevaluate) {
|
10044 | return [];
|
10045 | }
|
10046 | else {
|
10047 | var affectedTree_1 = new ImmutableTree(null);
|
10048 | if (write.snap != null) {
|
10049 |
|
10050 | affectedTree_1 = affectedTree_1.set(newEmptyPath(), true);
|
10051 | }
|
10052 | else {
|
10053 | each(write.children, function (pathString) {
|
10054 | affectedTree_1 = affectedTree_1.set(new Path(pathString), true);
|
10055 | });
|
10056 | }
|
10057 | return syncTreeApplyOperationToSyncPoints_(syncTree, new AckUserWrite(write.path, affectedTree_1, revert));
|
10058 | }
|
10059 | }
|
10060 |
|
10061 |
|
10062 |
|
10063 |
|
10064 |
|
10065 | function syncTreeApplyServerOverwrite(syncTree, path, newData) {
|
10066 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceServer(), path, newData));
|
10067 | }
|
10068 |
|
10069 |
|
10070 |
|
10071 |
|
10072 |
|
10073 | function syncTreeApplyServerMerge(syncTree, path, changedChildren) {
|
10074 | var changeTree = ImmutableTree.fromObject(changedChildren);
|
10075 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceServer(), path, changeTree));
|
10076 | }
|
10077 |
|
10078 |
|
10079 |
|
10080 |
|
10081 |
|
10082 | function syncTreeApplyListenComplete(syncTree, path) {
|
10083 | return syncTreeApplyOperationToSyncPoints_(syncTree, new ListenComplete(newOperationSourceServer(), path));
|
10084 | }
|
10085 |
|
10086 |
|
10087 |
|
10088 |
|
10089 |
|
10090 | function syncTreeApplyTaggedListenComplete(syncTree, path, tag) {
|
10091 | var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
|
10092 | if (queryKey) {
|
10093 | var r = syncTreeParseQueryKey_(queryKey);
|
10094 | var queryPath = r.path, queryId = r.queryId;
|
10095 | var relativePath = newRelativePath(queryPath, path);
|
10096 | var op = new ListenComplete(newOperationSourceServerTaggedQuery(queryId), relativePath);
|
10097 | return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
|
10098 | }
|
10099 | else {
|
10100 |
|
10101 | return [];
|
10102 | }
|
10103 | }
|
10104 |
|
10105 |
|
10106 |
|
10107 |
|
10108 |
|
10109 |
|
10110 |
|
10111 |
|
10112 |
|
10113 |
|
10114 | function syncTreeRemoveEventRegistration(syncTree, query, eventRegistration, cancelError) {
|
10115 |
|
10116 | var path = query._path;
|
10117 | var maybeSyncPoint = syncTree.syncPointTree_.get(path);
|
10118 | var cancelEvents = [];
|
10119 |
|
10120 |
|
10121 |
|
10122 | if (maybeSyncPoint &&
|
10123 | (query._queryIdentifier === 'default' ||
|
10124 | syncPointViewExistsForQuery(maybeSyncPoint, query))) {
|
10125 | var removedAndEvents = syncPointRemoveEventRegistration(maybeSyncPoint, query, eventRegistration, cancelError);
|
10126 | if (syncPointIsEmpty(maybeSyncPoint)) {
|
10127 | syncTree.syncPointTree_ = syncTree.syncPointTree_.remove(path);
|
10128 | }
|
10129 | var removed = removedAndEvents.removed;
|
10130 | cancelEvents = removedAndEvents.events;
|
10131 |
|
10132 |
|
10133 |
|
10134 |
|
10135 |
|
10136 |
|
10137 | var removingDefault = -1 !==
|
10138 | removed.findIndex(function (query) {
|
10139 | return query._queryParams.loadsAllData();
|
10140 | });
|
10141 | var covered = syncTree.syncPointTree_.findOnPath(path, function (relativePath, parentSyncPoint) {
|
10142 | return syncPointHasCompleteView(parentSyncPoint);
|
10143 | });
|
10144 | if (removingDefault && !covered) {
|
10145 | var subtree = syncTree.syncPointTree_.subtree(path);
|
10146 |
|
10147 |
|
10148 | if (!subtree.isEmpty()) {
|
10149 |
|
10150 | var newViews = syncTreeCollectDistinctViewsForSubTree_(subtree);
|
10151 |
|
10152 | for (var i = 0; i < newViews.length; ++i) {
|
10153 | var view = newViews[i], newQuery = view.query;
|
10154 | var listener = syncTreeCreateListenerForView_(syncTree, view);
|
10155 | syncTree.listenProvider_.startListening(syncTreeQueryForListening_(newQuery), syncTreeTagForQuery_(syncTree, newQuery), listener.hashFn, listener.onComplete);
|
10156 | }
|
10157 | }
|
10158 | }
|
10159 |
|
10160 |
|
10161 |
|
10162 | if (!covered && removed.length > 0 && !cancelError) {
|
10163 |
|
10164 |
|
10165 | if (removingDefault) {
|
10166 |
|
10167 | var defaultTag = null;
|
10168 | syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(query), defaultTag);
|
10169 | }
|
10170 | else {
|
10171 | removed.forEach(function (queryToRemove) {
|
10172 | var tagToRemove = syncTree.queryToTagMap.get(syncTreeMakeQueryKey_(queryToRemove));
|
10173 | syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToRemove), tagToRemove);
|
10174 | });
|
10175 | }
|
10176 | }
|
10177 |
|
10178 | syncTreeRemoveTags_(syncTree, removed);
|
10179 | }
|
10180 | return cancelEvents;
|
10181 | }
|
10182 |
|
10183 |
|
10184 |
|
10185 |
|
10186 |
|
10187 |
|
10188 |
|
10189 |
|
10190 |
|
10191 |
|
10192 |
|
10193 |
|
10194 |
|
10195 | function syncTreeRegisterQuery(syncTree, query) {
|
10196 | var _a = syncTreeRegisterSyncPoint(query, syncTree), syncPoint = _a.syncPoint, serverCache = _a.serverCache, writesCache = _a.writesCache, serverCacheComplete = _a.serverCacheComplete;
|
10197 | var view = syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete);
|
10198 | if (!syncPoint.views.has(query._queryIdentifier)) {
|
10199 | syncPoint.views.set(query._queryIdentifier, view);
|
10200 | }
|
10201 | if (!query._queryParams.loadsAllData()) {
|
10202 | return syncTreeTagForQuery_(syncTree, query);
|
10203 | }
|
10204 | return null;
|
10205 | }
|
10206 |
|
10207 |
|
10208 |
|
10209 |
|
10210 |
|
10211 | function syncTreeApplyTaggedQueryOverwrite(syncTree, path, snap, tag) {
|
10212 | var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
|
10213 | if (queryKey != null) {
|
10214 | var r = syncTreeParseQueryKey_(queryKey);
|
10215 | var queryPath = r.path, queryId = r.queryId;
|
10216 | var relativePath = newRelativePath(queryPath, path);
|
10217 | var op = new Overwrite(newOperationSourceServerTaggedQuery(queryId), relativePath, snap);
|
10218 | return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
|
10219 | }
|
10220 | else {
|
10221 |
|
10222 | return [];
|
10223 | }
|
10224 | }
|
10225 |
|
10226 |
|
10227 |
|
10228 |
|
10229 |
|
10230 | function syncTreeApplyTaggedQueryMerge(syncTree, path, changedChildren, tag) {
|
10231 | var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
|
10232 | if (queryKey) {
|
10233 | var r = syncTreeParseQueryKey_(queryKey);
|
10234 | var queryPath = r.path, queryId = r.queryId;
|
10235 | var relativePath = newRelativePath(queryPath, path);
|
10236 | var changeTree = ImmutableTree.fromObject(changedChildren);
|
10237 | var op = new Merge(newOperationSourceServerTaggedQuery(queryId), relativePath, changeTree);
|
10238 | return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
|
10239 | }
|
10240 | else {
|
10241 |
|
10242 | return [];
|
10243 | }
|
10244 | }
|
10245 |
|
10246 |
|
10247 |
|
10248 |
|
10249 |
|
10250 | function syncTreeRegisterSyncPoint(query, syncTree) {
|
10251 | var path = query._path;
|
10252 | var serverCache = null;
|
10253 | var foundAncestorDefaultView = false;
|
10254 |
|
10255 |
|
10256 | syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
|
10257 | var relativePath = newRelativePath(pathToSyncPoint, path);
|
10258 | serverCache =
|
10259 | serverCache || syncPointGetCompleteServerCache(sp, relativePath);
|
10260 | foundAncestorDefaultView =
|
10261 | foundAncestorDefaultView || syncPointHasCompleteView(sp);
|
10262 | });
|
10263 | var syncPoint = syncTree.syncPointTree_.get(path);
|
10264 | if (!syncPoint) {
|
10265 | syncPoint = new SyncPoint();
|
10266 | syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
|
10267 | }
|
10268 | else {
|
10269 | foundAncestorDefaultView =
|
10270 | foundAncestorDefaultView || syncPointHasCompleteView(syncPoint);
|
10271 | serverCache =
|
10272 | serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10273 | }
|
10274 | var serverCacheComplete;
|
10275 | if (serverCache != null) {
|
10276 | serverCacheComplete = true;
|
10277 | }
|
10278 | else {
|
10279 | serverCacheComplete = false;
|
10280 | serverCache = ChildrenNode.EMPTY_NODE;
|
10281 | var subtree = syncTree.syncPointTree_.subtree(path);
|
10282 | subtree.foreachChild(function (childName, childSyncPoint) {
|
10283 | var completeCache = syncPointGetCompleteServerCache(childSyncPoint, newEmptyPath());
|
10284 | if (completeCache) {
|
10285 | serverCache = serverCache.updateImmediateChild(childName, completeCache);
|
10286 | }
|
10287 | });
|
10288 | }
|
10289 | var viewAlreadyExists = syncPointViewExistsForQuery(syncPoint, query);
|
10290 | if (!viewAlreadyExists && !query._queryParams.loadsAllData()) {
|
10291 |
|
10292 | var queryKey = syncTreeMakeQueryKey_(query);
|
10293 | util.assert(!syncTree.queryToTagMap.has(queryKey), 'View does not exist, but we have a tag');
|
10294 | var tag = syncTreeGetNextQueryTag_();
|
10295 | syncTree.queryToTagMap.set(queryKey, tag);
|
10296 | syncTree.tagToQueryMap.set(tag, queryKey);
|
10297 | }
|
10298 | var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
|
10299 | return {
|
10300 | syncPoint: syncPoint,
|
10301 | writesCache: writesCache,
|
10302 | serverCache: serverCache,
|
10303 | serverCacheComplete: serverCacheComplete,
|
10304 | foundAncestorDefaultView: foundAncestorDefaultView,
|
10305 | viewAlreadyExists: viewAlreadyExists
|
10306 | };
|
10307 | }
|
10308 |
|
10309 |
|
10310 |
|
10311 |
|
10312 |
|
10313 | function syncTreeAddEventRegistration(syncTree, query, eventRegistration) {
|
10314 | var _a = syncTreeRegisterSyncPoint(query, syncTree), syncPoint = _a.syncPoint, serverCache = _a.serverCache, writesCache = _a.writesCache, serverCacheComplete = _a.serverCacheComplete, viewAlreadyExists = _a.viewAlreadyExists, foundAncestorDefaultView = _a.foundAncestorDefaultView;
|
10315 | var events = syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete);
|
10316 | if (!viewAlreadyExists && !foundAncestorDefaultView) {
|
10317 | var view = syncPointViewForQuery(syncPoint, query);
|
10318 | events = events.concat(syncTreeSetupListener_(syncTree, query, view));
|
10319 | }
|
10320 | return events;
|
10321 | }
|
10322 |
|
10323 |
|
10324 |
|
10325 |
|
10326 |
|
10327 |
|
10328 |
|
10329 |
|
10330 |
|
10331 |
|
10332 |
|
10333 | function syncTreeCalcCompleteEventCache(syncTree, path, writeIdsToExclude) {
|
10334 | var includeHiddenSets = true;
|
10335 | var writeTree = syncTree.pendingWriteTree_;
|
10336 | var serverCache = syncTree.syncPointTree_.findOnPath(path, function (pathSoFar, syncPoint) {
|
10337 | var relativePath = newRelativePath(pathSoFar, path);
|
10338 | var serverCache = syncPointGetCompleteServerCache(syncPoint, relativePath);
|
10339 | if (serverCache) {
|
10340 | return serverCache;
|
10341 | }
|
10342 | });
|
10343 | return writeTreeCalcCompleteEventCache(writeTree, path, serverCache, writeIdsToExclude, includeHiddenSets);
|
10344 | }
|
10345 | function syncTreeGetServerValue(syncTree, query) {
|
10346 | var path = query._path;
|
10347 | var serverCache = null;
|
10348 |
|
10349 |
|
10350 | syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
|
10351 | var relativePath = newRelativePath(pathToSyncPoint, path);
|
10352 | serverCache =
|
10353 | serverCache || syncPointGetCompleteServerCache(sp, relativePath);
|
10354 | });
|
10355 | var syncPoint = syncTree.syncPointTree_.get(path);
|
10356 | if (!syncPoint) {
|
10357 | syncPoint = new SyncPoint();
|
10358 | syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
|
10359 | }
|
10360 | else {
|
10361 | serverCache =
|
10362 | serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10363 | }
|
10364 | var serverCacheComplete = serverCache != null;
|
10365 | var serverCacheNode = serverCacheComplete
|
10366 | ? new CacheNode(serverCache, true, false)
|
10367 | : null;
|
10368 | var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, query._path);
|
10369 | var view = syncPointGetView(syncPoint, query, writesCache, serverCacheComplete ? serverCacheNode.getNode() : ChildrenNode.EMPTY_NODE, serverCacheComplete);
|
10370 | return viewGetCompleteNode(view);
|
10371 | }
|
10372 |
|
10373 |
|
10374 |
|
10375 |
|
10376 |
|
10377 |
|
10378 |
|
10379 |
|
10380 |
|
10381 |
|
10382 |
|
10383 |
|
10384 |
|
10385 | function syncTreeApplyOperationToSyncPoints_(syncTree, operation) {
|
10386 | return syncTreeApplyOperationHelper_(operation, syncTree.syncPointTree_,
|
10387 | null, writeTreeChildWrites(syncTree.pendingWriteTree_, newEmptyPath()));
|
10388 | }
|
10389 |
|
10390 |
|
10391 |
|
10392 | function syncTreeApplyOperationHelper_(operation, syncPointTree, serverCache, writesCache) {
|
10393 | if (pathIsEmpty(operation.path)) {
|
10394 | return syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache);
|
10395 | }
|
10396 | else {
|
10397 | var syncPoint = syncPointTree.get(newEmptyPath());
|
10398 |
|
10399 | if (serverCache == null && syncPoint != null) {
|
10400 | serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10401 | }
|
10402 | var events = [];
|
10403 | var childName = pathGetFront(operation.path);
|
10404 | var childOperation = operation.operationForChild(childName);
|
10405 | var childTree = syncPointTree.children.get(childName);
|
10406 | if (childTree && childOperation) {
|
10407 | var childServerCache = serverCache
|
10408 | ? serverCache.getImmediateChild(childName)
|
10409 | : null;
|
10410 | var childWritesCache = writeTreeRefChild(writesCache, childName);
|
10411 | events = events.concat(syncTreeApplyOperationHelper_(childOperation, childTree, childServerCache, childWritesCache));
|
10412 | }
|
10413 | if (syncPoint) {
|
10414 | events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
|
10415 | }
|
10416 | return events;
|
10417 | }
|
10418 | }
|
10419 |
|
10420 |
|
10421 |
|
10422 | function syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache) {
|
10423 | var syncPoint = syncPointTree.get(newEmptyPath());
|
10424 |
|
10425 | if (serverCache == null && syncPoint != null) {
|
10426 | serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10427 | }
|
10428 | var events = [];
|
10429 | syncPointTree.children.inorderTraversal(function (childName, childTree) {
|
10430 | var childServerCache = serverCache
|
10431 | ? serverCache.getImmediateChild(childName)
|
10432 | : null;
|
10433 | var childWritesCache = writeTreeRefChild(writesCache, childName);
|
10434 | var childOperation = operation.operationForChild(childName);
|
10435 | if (childOperation) {
|
10436 | events = events.concat(syncTreeApplyOperationDescendantsHelper_(childOperation, childTree, childServerCache, childWritesCache));
|
10437 | }
|
10438 | });
|
10439 | if (syncPoint) {
|
10440 | events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
|
10441 | }
|
10442 | return events;
|
10443 | }
|
10444 | function syncTreeCreateListenerForView_(syncTree, view) {
|
10445 | var query = view.query;
|
10446 | var tag = syncTreeTagForQuery_(syncTree, query);
|
10447 | return {
|
10448 | hashFn: function () {
|
10449 | var cache = viewGetServerCache(view) || ChildrenNode.EMPTY_NODE;
|
10450 | return cache.hash();
|
10451 | },
|
10452 | onComplete: function (status) {
|
10453 | if (status === 'ok') {
|
10454 | if (tag) {
|
10455 | return syncTreeApplyTaggedListenComplete(syncTree, query._path, tag);
|
10456 | }
|
10457 | else {
|
10458 | return syncTreeApplyListenComplete(syncTree, query._path);
|
10459 | }
|
10460 | }
|
10461 | else {
|
10462 |
|
10463 |
|
10464 | var error = errorForServerCode(status, query);
|
10465 | return syncTreeRemoveEventRegistration(syncTree, query,
|
10466 | null, error);
|
10467 | }
|
10468 | }
|
10469 | };
|
10470 | }
|
10471 |
|
10472 |
|
10473 |
|
10474 | function syncTreeTagForQuery_(syncTree, query) {
|
10475 | var queryKey = syncTreeMakeQueryKey_(query);
|
10476 | return syncTree.queryToTagMap.get(queryKey);
|
10477 | }
|
10478 |
|
10479 |
|
10480 |
|
10481 | function syncTreeMakeQueryKey_(query) {
|
10482 | return query._path.toString() + '$' + query._queryIdentifier;
|
10483 | }
|
10484 |
|
10485 |
|
10486 |
|
10487 | function syncTreeQueryKeyForTag_(syncTree, tag) {
|
10488 | return syncTree.tagToQueryMap.get(tag);
|
10489 | }
|
10490 |
|
10491 |
|
10492 |
|
10493 | function syncTreeParseQueryKey_(queryKey) {
|
10494 | var splitIndex = queryKey.indexOf('$');
|
10495 | util.assert(splitIndex !== -1 && splitIndex < queryKey.length - 1, 'Bad queryKey.');
|
10496 | return {
|
10497 | queryId: queryKey.substr(splitIndex + 1),
|
10498 | path: new Path(queryKey.substr(0, splitIndex))
|
10499 | };
|
10500 | }
|
10501 |
|
10502 |
|
10503 |
|
10504 | function syncTreeApplyTaggedOperation_(syncTree, queryPath, operation) {
|
10505 | var syncPoint = syncTree.syncPointTree_.get(queryPath);
|
10506 | util.assert(syncPoint, "Missing sync point for query tag that we're tracking");
|
10507 | var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, queryPath);
|
10508 | return syncPointApplyOperation(syncPoint, operation, writesCache, null);
|
10509 | }
|
10510 |
|
10511 |
|
10512 |
|
10513 |
|
10514 | function syncTreeCollectDistinctViewsForSubTree_(subtree) {
|
10515 | return subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
|
10516 | if (maybeChildSyncPoint && syncPointHasCompleteView(maybeChildSyncPoint)) {
|
10517 | var completeView = syncPointGetCompleteView(maybeChildSyncPoint);
|
10518 | return [completeView];
|
10519 | }
|
10520 | else {
|
10521 |
|
10522 | var views_1 = [];
|
10523 | if (maybeChildSyncPoint) {
|
10524 | views_1 = syncPointGetQueryViews(maybeChildSyncPoint);
|
10525 | }
|
10526 | each(childMap, function (_key, childViews) {
|
10527 | views_1 = views_1.concat(childViews);
|
10528 | });
|
10529 | return views_1;
|
10530 | }
|
10531 | });
|
10532 | }
|
10533 |
|
10534 |
|
10535 |
|
10536 |
|
10537 |
|
10538 | function syncTreeQueryForListening_(query) {
|
10539 | if (query._queryParams.loadsAllData() && !query._queryParams.isDefault()) {
|
10540 |
|
10541 |
|
10542 |
|
10543 | return new (syncTreeGetReferenceConstructor())(query._repo, query._path);
|
10544 | }
|
10545 | else {
|
10546 | return query;
|
10547 | }
|
10548 | }
|
10549 | function syncTreeRemoveTags_(syncTree, queries) {
|
10550 | for (var j = 0; j < queries.length; ++j) {
|
10551 | var removedQuery = queries[j];
|
10552 | if (!removedQuery._queryParams.loadsAllData()) {
|
10553 |
|
10554 | var removedQueryKey = syncTreeMakeQueryKey_(removedQuery);
|
10555 | var removedQueryTag = syncTree.queryToTagMap.get(removedQueryKey);
|
10556 | syncTree.queryToTagMap.delete(removedQueryKey);
|
10557 | syncTree.tagToQueryMap.delete(removedQueryTag);
|
10558 | }
|
10559 | }
|
10560 | }
|
10561 |
|
10562 |
|
10563 |
|
10564 | function syncTreeGetNextQueryTag_() {
|
10565 | return syncTreeNextQueryTag_++;
|
10566 | }
|
10567 |
|
10568 |
|
10569 |
|
10570 |
|
10571 |
|
10572 | function syncTreeSetupListener_(syncTree, query, view) {
|
10573 | var path = query._path;
|
10574 | var tag = syncTreeTagForQuery_(syncTree, query);
|
10575 | var listener = syncTreeCreateListenerForView_(syncTree, view);
|
10576 | var events = syncTree.listenProvider_.startListening(syncTreeQueryForListening_(query), tag, listener.hashFn, listener.onComplete);
|
10577 | var subtree = syncTree.syncPointTree_.subtree(path);
|
10578 |
|
10579 |
|
10580 | if (tag) {
|
10581 | util.assert(!syncPointHasCompleteView(subtree.value), "If we're adding a query, it shouldn't be shadowed");
|
10582 | }
|
10583 | else {
|
10584 |
|
10585 | var queriesToStop = subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
|
10586 | if (!pathIsEmpty(relativePath) &&
|
10587 | maybeChildSyncPoint &&
|
10588 | syncPointHasCompleteView(maybeChildSyncPoint)) {
|
10589 | return [syncPointGetCompleteView(maybeChildSyncPoint).query];
|
10590 | }
|
10591 | else {
|
10592 |
|
10593 | var queries_1 = [];
|
10594 | if (maybeChildSyncPoint) {
|
10595 | queries_1 = queries_1.concat(syncPointGetQueryViews(maybeChildSyncPoint).map(function (view) { return view.query; }));
|
10596 | }
|
10597 | each(childMap, function (_key, childQueries) {
|
10598 | queries_1 = queries_1.concat(childQueries);
|
10599 | });
|
10600 | return queries_1;
|
10601 | }
|
10602 | });
|
10603 | for (var i = 0; i < queriesToStop.length; ++i) {
|
10604 | var queryToStop = queriesToStop[i];
|
10605 | syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToStop), syncTreeTagForQuery_(syncTree, queryToStop));
|
10606 | }
|
10607 | }
|
10608 | return events;
|
10609 | }
|
10610 |
|
10611 |
|
10612 |
|
10613 |
|
10614 |
|
10615 |
|
10616 |
|
10617 |
|
10618 |
|
10619 |
|
10620 |
|
10621 |
|
10622 |
|
10623 |
|
10624 |
|
10625 |
|
10626 |
|
10627 | var ExistingValueProvider = (function () {
|
10628 | function ExistingValueProvider(node_) {
|
10629 | this.node_ = node_;
|
10630 | }
|
10631 | ExistingValueProvider.prototype.getImmediateChild = function (childName) {
|
10632 | var child = this.node_.getImmediateChild(childName);
|
10633 | return new ExistingValueProvider(child);
|
10634 | };
|
10635 | ExistingValueProvider.prototype.node = function () {
|
10636 | return this.node_;
|
10637 | };
|
10638 | return ExistingValueProvider;
|
10639 | }());
|
10640 | var DeferredValueProvider = (function () {
|
10641 | function DeferredValueProvider(syncTree, path) {
|
10642 | this.syncTree_ = syncTree;
|
10643 | this.path_ = path;
|
10644 | }
|
10645 | DeferredValueProvider.prototype.getImmediateChild = function (childName) {
|
10646 | var childPath = pathChild(this.path_, childName);
|
10647 | return new DeferredValueProvider(this.syncTree_, childPath);
|
10648 | };
|
10649 | DeferredValueProvider.prototype.node = function () {
|
10650 | return syncTreeCalcCompleteEventCache(this.syncTree_, this.path_);
|
10651 | };
|
10652 | return DeferredValueProvider;
|
10653 | }());
|
10654 |
|
10655 |
|
10656 |
|
10657 | var generateWithValues = function (values) {
|
10658 | values = values || {};
|
10659 | values['timestamp'] = values['timestamp'] || new Date().getTime();
|
10660 | return values;
|
10661 | };
|
10662 |
|
10663 |
|
10664 |
|
10665 |
|
10666 | var resolveDeferredLeafValue = function (value, existingVal, serverValues) {
|
10667 | if (!value || typeof value !== 'object') {
|
10668 | return value;
|
10669 | }
|
10670 | util.assert('.sv' in value, 'Unexpected leaf node or priority contents');
|
10671 | if (typeof value['.sv'] === 'string') {
|
10672 | return resolveScalarDeferredValue(value['.sv'], existingVal, serverValues);
|
10673 | }
|
10674 | else if (typeof value['.sv'] === 'object') {
|
10675 | return resolveComplexDeferredValue(value['.sv'], existingVal);
|
10676 | }
|
10677 | else {
|
10678 | util.assert(false, 'Unexpected server value: ' + JSON.stringify(value, null, 2));
|
10679 | }
|
10680 | };
|
10681 | var resolveScalarDeferredValue = function (op, existing, serverValues) {
|
10682 | switch (op) {
|
10683 | case 'timestamp':
|
10684 | return serverValues['timestamp'];
|
10685 | default:
|
10686 | util.assert(false, 'Unexpected server value: ' + op);
|
10687 | }
|
10688 | };
|
10689 | var resolveComplexDeferredValue = function (op, existing, unused) {
|
10690 | if (!op.hasOwnProperty('increment')) {
|
10691 | util.assert(false, 'Unexpected server value: ' + JSON.stringify(op, null, 2));
|
10692 | }
|
10693 | var delta = op['increment'];
|
10694 | if (typeof delta !== 'number') {
|
10695 | util.assert(false, 'Unexpected increment value: ' + delta);
|
10696 | }
|
10697 | var existingNode = existing.node();
|
10698 | util.assert(existingNode !== null && typeof existingNode !== 'undefined', 'Expected ChildrenNode.EMPTY_NODE for nulls');
|
10699 |
|
10700 | if (!existingNode.isLeafNode()) {
|
10701 | return delta;
|
10702 | }
|
10703 | var leaf = existingNode;
|
10704 | var existingVal = leaf.getValue();
|
10705 | if (typeof existingVal !== 'number') {
|
10706 | return delta;
|
10707 | }
|
10708 |
|
10709 | return existingVal + delta;
|
10710 | };
|
10711 |
|
10712 |
|
10713 |
|
10714 |
|
10715 |
|
10716 |
|
10717 |
|
10718 | var resolveDeferredValueTree = function (path, node, syncTree, serverValues) {
|
10719 | return resolveDeferredValue(node, new DeferredValueProvider(syncTree, path), serverValues);
|
10720 | };
|
10721 |
|
10722 |
|
10723 |
|
10724 |
|
10725 |
|
10726 | var resolveDeferredValueSnapshot = function (node, existing, serverValues) {
|
10727 | return resolveDeferredValue(node, new ExistingValueProvider(existing), serverValues);
|
10728 | };
|
10729 | function resolveDeferredValue(node, existingVal, serverValues) {
|
10730 | var rawPri = node.getPriority().val();
|
10731 | var priority = resolveDeferredLeafValue(rawPri, existingVal.getImmediateChild('.priority'), serverValues);
|
10732 | var newNode;
|
10733 | if (node.isLeafNode()) {
|
10734 | var leafNode = node;
|
10735 | var value = resolveDeferredLeafValue(leafNode.getValue(), existingVal, serverValues);
|
10736 | if (value !== leafNode.getValue() ||
|
10737 | priority !== leafNode.getPriority().val()) {
|
10738 | return new LeafNode(value, nodeFromJSON(priority));
|
10739 | }
|
10740 | else {
|
10741 | return node;
|
10742 | }
|
10743 | }
|
10744 | else {
|
10745 | var childrenNode = node;
|
10746 | newNode = childrenNode;
|
10747 | if (priority !== childrenNode.getPriority().val()) {
|
10748 | newNode = newNode.updatePriority(new LeafNode(priority));
|
10749 | }
|
10750 | childrenNode.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
|
10751 | var newChildNode = resolveDeferredValue(childNode, existingVal.getImmediateChild(childName), serverValues);
|
10752 | if (newChildNode !== childNode) {
|
10753 | newNode = newNode.updateImmediateChild(childName, newChildNode);
|
10754 | }
|
10755 | });
|
10756 | return newNode;
|
10757 | }
|
10758 | }
|
10759 |
|
10760 |
|
10761 |
|
10762 |
|
10763 |
|
10764 |
|
10765 |
|
10766 |
|
10767 |
|
10768 |
|
10769 |
|
10770 |
|
10771 |
|
10772 |
|
10773 |
|
10774 |
|
10775 |
|
10776 |
|
10777 |
|
10778 |
|
10779 |
|
10780 |
|
10781 | var Tree = (function () {
|
10782 | |
10783 |
|
10784 |
|
10785 |
|
10786 |
|
10787 | function Tree(name, parent, node) {
|
10788 | if (name === void 0) { name = ''; }
|
10789 | if (parent === void 0) { parent = null; }
|
10790 | if (node === void 0) { node = { children: {}, childCount: 0 }; }
|
10791 | this.name = name;
|
10792 | this.parent = parent;
|
10793 | this.node = node;
|
10794 | }
|
10795 | return Tree;
|
10796 | }());
|
10797 |
|
10798 |
|
10799 |
|
10800 |
|
10801 |
|
10802 |
|
10803 | function treeSubTree(tree, pathObj) {
|
10804 |
|
10805 | var path = pathObj instanceof Path ? pathObj : new Path(pathObj);
|
10806 | var child = tree, next = pathGetFront(path);
|
10807 | while (next !== null) {
|
10808 | var childNode = util.safeGet(child.node.children, next) || {
|
10809 | children: {},
|
10810 | childCount: 0
|
10811 | };
|
10812 | child = new Tree(next, child, childNode);
|
10813 | path = pathPopFront(path);
|
10814 | next = pathGetFront(path);
|
10815 | }
|
10816 | return child;
|
10817 | }
|
10818 |
|
10819 |
|
10820 |
|
10821 |
|
10822 |
|
10823 | function treeGetValue(tree) {
|
10824 | return tree.node.value;
|
10825 | }
|
10826 |
|
10827 |
|
10828 |
|
10829 |
|
10830 |
|
10831 | function treeSetValue(tree, value) {
|
10832 | tree.node.value = value;
|
10833 | treeUpdateParents(tree);
|
10834 | }
|
10835 |
|
10836 |
|
10837 |
|
10838 | function treeHasChildren(tree) {
|
10839 | return tree.node.childCount > 0;
|
10840 | }
|
10841 |
|
10842 |
|
10843 |
|
10844 | function treeIsEmpty(tree) {
|
10845 | return treeGetValue(tree) === undefined && !treeHasChildren(tree);
|
10846 | }
|
10847 |
|
10848 |
|
10849 |
|
10850 |
|
10851 |
|
10852 | function treeForEachChild(tree, action) {
|
10853 | each(tree.node.children, function (child, childTree) {
|
10854 | action(new Tree(child, tree, childTree));
|
10855 | });
|
10856 | }
|
10857 |
|
10858 |
|
10859 |
|
10860 |
|
10861 |
|
10862 |
|
10863 |
|
10864 |
|
10865 |
|
10866 | function treeForEachDescendant(tree, action, includeSelf, childrenFirst) {
|
10867 | if (includeSelf && !childrenFirst) {
|
10868 | action(tree);
|
10869 | }
|
10870 | treeForEachChild(tree, function (child) {
|
10871 | treeForEachDescendant(child, action, true, childrenFirst);
|
10872 | });
|
10873 | if (includeSelf && childrenFirst) {
|
10874 | action(tree);
|
10875 | }
|
10876 | }
|
10877 |
|
10878 |
|
10879 |
|
10880 |
|
10881 |
|
10882 |
|
10883 |
|
10884 |
|
10885 | function treeForEachAncestor(tree, action, includeSelf) {
|
10886 | var node = includeSelf ? tree : tree.parent;
|
10887 | while (node !== null) {
|
10888 | if (action(node)) {
|
10889 | return true;
|
10890 | }
|
10891 | node = node.parent;
|
10892 | }
|
10893 | return false;
|
10894 | }
|
10895 |
|
10896 |
|
10897 |
|
10898 | function treeGetPath(tree) {
|
10899 | return new Path(tree.parent === null
|
10900 | ? tree.name
|
10901 | : treeGetPath(tree.parent) + '/' + tree.name);
|
10902 | }
|
10903 |
|
10904 |
|
10905 |
|
10906 | function treeUpdateParents(tree) {
|
10907 | if (tree.parent !== null) {
|
10908 | treeUpdateChild(tree.parent, tree.name, tree);
|
10909 | }
|
10910 | }
|
10911 |
|
10912 |
|
10913 |
|
10914 |
|
10915 |
|
10916 |
|
10917 | function treeUpdateChild(tree, childName, child) {
|
10918 | var childEmpty = treeIsEmpty(child);
|
10919 | var childExists = util.contains(tree.node.children, childName);
|
10920 | if (childEmpty && childExists) {
|
10921 | delete tree.node.children[childName];
|
10922 | tree.node.childCount--;
|
10923 | treeUpdateParents(tree);
|
10924 | }
|
10925 | else if (!childEmpty && !childExists) {
|
10926 | tree.node.children[childName] = child.node;
|
10927 | tree.node.childCount++;
|
10928 | treeUpdateParents(tree);
|
10929 | }
|
10930 | }
|
10931 |
|
10932 |
|
10933 |
|
10934 |
|
10935 |
|
10936 |
|
10937 |
|
10938 |
|
10939 |
|
10940 |
|
10941 |
|
10942 |
|
10943 |
|
10944 |
|
10945 |
|
10946 |
|
10947 |
|
10948 |
|
10949 |
|
10950 |
|
10951 | var INVALID_KEY_REGEX_ = /[\[\].#$\/\u0000-\u001F\u007F]/;
|
10952 |
|
10953 |
|
10954 |
|
10955 |
|
10956 | var INVALID_PATH_REGEX_ = /[\[\].#$\u0000-\u001F\u007F]/;
|
10957 |
|
10958 |
|
10959 |
|
10960 | var MAX_LEAF_SIZE_ = 10 * 1024 * 1024;
|
10961 | var isValidKey = function (key) {
|
10962 | return (typeof key === 'string' && key.length !== 0 && !INVALID_KEY_REGEX_.test(key));
|
10963 | };
|
10964 | var isValidPathString = function (pathString) {
|
10965 | return (typeof pathString === 'string' &&
|
10966 | pathString.length !== 0 &&
|
10967 | !INVALID_PATH_REGEX_.test(pathString));
|
10968 | };
|
10969 | var isValidRootPathString = function (pathString) {
|
10970 | if (pathString) {
|
10971 |
|
10972 | pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
|
10973 | }
|
10974 | return isValidPathString(pathString);
|
10975 | };
|
10976 | var isValidPriority = function (priority) {
|
10977 | return (priority === null ||
|
10978 | typeof priority === 'string' ||
|
10979 | (typeof priority === 'number' && !isInvalidJSONNumber(priority)) ||
|
10980 | (priority &&
|
10981 | typeof priority === 'object' &&
|
10982 |
|
10983 | util.contains(priority, '.sv')));
|
10984 | };
|
10985 |
|
10986 |
|
10987 |
|
10988 | var validateFirebaseDataArg = function (fnName, value, path, optional) {
|
10989 | if (optional && value === undefined) {
|
10990 | return;
|
10991 | }
|
10992 | validateFirebaseData(util.errorPrefix(fnName, 'value'), value, path);
|
10993 | };
|
10994 |
|
10995 |
|
10996 |
|
10997 | var validateFirebaseData = function (errorPrefix, data, path_) {
|
10998 | var path = path_ instanceof Path ? new ValidationPath(path_, errorPrefix) : path_;
|
10999 | if (data === undefined) {
|
11000 | throw new Error(errorPrefix + 'contains undefined ' + validationPathToErrorString(path));
|
11001 | }
|
11002 | if (typeof data === 'function') {
|
11003 | throw new Error(errorPrefix +
|
11004 | 'contains a function ' +
|
11005 | validationPathToErrorString(path) +
|
11006 | ' with contents = ' +
|
11007 | data.toString());
|
11008 | }
|
11009 | if (isInvalidJSONNumber(data)) {
|
11010 | throw new Error(errorPrefix +
|
11011 | 'contains ' +
|
11012 | data.toString() +
|
11013 | ' ' +
|
11014 | validationPathToErrorString(path));
|
11015 | }
|
11016 |
|
11017 | if (typeof data === 'string' &&
|
11018 | data.length > MAX_LEAF_SIZE_ / 3 &&
|
11019 | util.stringLength(data) > MAX_LEAF_SIZE_) {
|
11020 | throw new Error(errorPrefix +
|
11021 | 'contains a string greater than ' +
|
11022 | MAX_LEAF_SIZE_ +
|
11023 | ' utf8 bytes ' +
|
11024 | validationPathToErrorString(path) +
|
11025 | " ('" +
|
11026 | data.substring(0, 50) +
|
11027 | "...')");
|
11028 | }
|
11029 |
|
11030 |
|
11031 | if (data && typeof data === 'object') {
|
11032 | var hasDotValue_1 = false;
|
11033 | var hasActualChild_1 = false;
|
11034 | each(data, function (key, value) {
|
11035 | if (key === '.value') {
|
11036 | hasDotValue_1 = true;
|
11037 | }
|
11038 | else if (key !== '.priority' && key !== '.sv') {
|
11039 | hasActualChild_1 = true;
|
11040 | if (!isValidKey(key)) {
|
11041 | throw new Error(errorPrefix +
|
11042 | ' contains an invalid key (' +
|
11043 | key +
|
11044 | ') ' +
|
11045 | validationPathToErrorString(path) +
|
11046 | '. Keys must be non-empty strings ' +
|
11047 | 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
|
11048 | }
|
11049 | }
|
11050 | validationPathPush(path, key);
|
11051 | validateFirebaseData(errorPrefix, value, path);
|
11052 | validationPathPop(path);
|
11053 | });
|
11054 | if (hasDotValue_1 && hasActualChild_1) {
|
11055 | throw new Error(errorPrefix +
|
11056 | ' contains ".value" child ' +
|
11057 | validationPathToErrorString(path) +
|
11058 | ' in addition to actual children.');
|
11059 | }
|
11060 | }
|
11061 | };
|
11062 |
|
11063 |
|
11064 |
|
11065 | var validateFirebaseMergePaths = function (errorPrefix, mergePaths) {
|
11066 | var i, curPath;
|
11067 | for (i = 0; i < mergePaths.length; i++) {
|
11068 | curPath = mergePaths[i];
|
11069 | var keys = pathSlice(curPath);
|
11070 | for (var j = 0; j < keys.length; j++) {
|
11071 | if (keys[j] === '.priority' && j === keys.length - 1) ;
|
11072 | else if (!isValidKey(keys[j])) {
|
11073 | throw new Error(errorPrefix +
|
11074 | 'contains an invalid key (' +
|
11075 | keys[j] +
|
11076 | ') in path ' +
|
11077 | curPath.toString() +
|
11078 | '. Keys must be non-empty strings ' +
|
11079 | 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
|
11080 | }
|
11081 | }
|
11082 | }
|
11083 |
|
11084 |
|
11085 |
|
11086 | mergePaths.sort(pathCompare);
|
11087 | var prevPath = null;
|
11088 | for (i = 0; i < mergePaths.length; i++) {
|
11089 | curPath = mergePaths[i];
|
11090 | if (prevPath !== null && pathContains(prevPath, curPath)) {
|
11091 | throw new Error(errorPrefix +
|
11092 | 'contains a path ' +
|
11093 | prevPath.toString() +
|
11094 | ' that is ancestor of another path ' +
|
11095 | curPath.toString());
|
11096 | }
|
11097 | prevPath = curPath;
|
11098 | }
|
11099 | };
|
11100 |
|
11101 |
|
11102 |
|
11103 |
|
11104 | var validateFirebaseMergeDataArg = function (fnName, data, path, optional) {
|
11105 | if (optional && data === undefined) {
|
11106 | return;
|
11107 | }
|
11108 | var errorPrefix = util.errorPrefix(fnName, 'values');
|
11109 | if (!(data && typeof data === 'object') || Array.isArray(data)) {
|
11110 | throw new Error(errorPrefix + ' must be an object containing the children to replace.');
|
11111 | }
|
11112 | var mergePaths = [];
|
11113 | each(data, function (key, value) {
|
11114 | var curPath = new Path(key);
|
11115 | validateFirebaseData(errorPrefix, value, pathChild(path, curPath));
|
11116 | if (pathGetBack(curPath) === '.priority') {
|
11117 | if (!isValidPriority(value)) {
|
11118 | throw new Error(errorPrefix +
|
11119 | "contains an invalid value for '" +
|
11120 | curPath.toString() +
|
11121 | "', which must be a valid " +
|
11122 | 'Firebase priority (a string, finite number, server value, or null).');
|
11123 | }
|
11124 | }
|
11125 | mergePaths.push(curPath);
|
11126 | });
|
11127 | validateFirebaseMergePaths(errorPrefix, mergePaths);
|
11128 | };
|
11129 | var validatePriority = function (fnName, priority, optional) {
|
11130 | if (optional && priority === undefined) {
|
11131 | return;
|
11132 | }
|
11133 | if (isInvalidJSONNumber(priority)) {
|
11134 | throw new Error(util.errorPrefix(fnName, 'priority') +
|
11135 | 'is ' +
|
11136 | priority.toString() +
|
11137 | ', but must be a valid Firebase priority (a string, finite number, ' +
|
11138 | 'server value, or null).');
|
11139 | }
|
11140 |
|
11141 | if (!isValidPriority(priority)) {
|
11142 | throw new Error(util.errorPrefix(fnName, 'priority') +
|
11143 | 'must be a valid Firebase priority ' +
|
11144 | '(a string, finite number, server value, or null).');
|
11145 | }
|
11146 | };
|
11147 | var validateKey = function (fnName, argumentName, key, optional) {
|
11148 | if (optional && key === undefined) {
|
11149 | return;
|
11150 | }
|
11151 | if (!isValidKey(key)) {
|
11152 | throw new Error(util.errorPrefix(fnName, argumentName) +
|
11153 | 'was an invalid key = "' +
|
11154 | key +
|
11155 | '". Firebase keys must be non-empty strings and ' +
|
11156 | 'can\'t contain ".", "#", "$", "/", "[", or "]").');
|
11157 | }
|
11158 | };
|
11159 |
|
11160 |
|
11161 |
|
11162 | var validatePathString = function (fnName, argumentName, pathString, optional) {
|
11163 | if (optional && pathString === undefined) {
|
11164 | return;
|
11165 | }
|
11166 | if (!isValidPathString(pathString)) {
|
11167 | throw new Error(util.errorPrefix(fnName, argumentName) +
|
11168 | 'was an invalid path = "' +
|
11169 | pathString +
|
11170 | '". Paths must be non-empty strings and ' +
|
11171 | 'can\'t contain ".", "#", "$", "[", or "]"');
|
11172 | }
|
11173 | };
|
11174 | var validateRootPathString = function (fnName, argumentName, pathString, optional) {
|
11175 | if (pathString) {
|
11176 |
|
11177 | pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
|
11178 | }
|
11179 | validatePathString(fnName, argumentName, pathString, optional);
|
11180 | };
|
11181 |
|
11182 |
|
11183 |
|
11184 | var validateWritablePath = function (fnName, path) {
|
11185 | if (pathGetFront(path) === '.info') {
|
11186 | throw new Error(fnName + " failed = Can't modify data under /.info/");
|
11187 | }
|
11188 | };
|
11189 | var validateUrl = function (fnName, parsedUrl) {
|
11190 |
|
11191 | var pathString = parsedUrl.path.toString();
|
11192 | if (!(typeof parsedUrl.repoInfo.host === 'string') ||
|
11193 | parsedUrl.repoInfo.host.length === 0 ||
|
11194 | (!isValidKey(parsedUrl.repoInfo.namespace) &&
|
11195 | parsedUrl.repoInfo.host.split(':')[0] !== 'localhost') ||
|
11196 | (pathString.length !== 0 && !isValidRootPathString(pathString))) {
|
11197 | throw new Error(util.errorPrefix(fnName, 'url') +
|
11198 | 'must be a valid firebase URL and ' +
|
11199 | 'the path can\'t contain ".", "#", "$", "[", or "]".');
|
11200 | }
|
11201 | };
|
11202 |
|
11203 |
|
11204 |
|
11205 |
|
11206 |
|
11207 |
|
11208 |
|
11209 |
|
11210 |
|
11211 |
|
11212 |
|
11213 |
|
11214 |
|
11215 |
|
11216 |
|
11217 |
|
11218 |
|
11219 |
|
11220 |
|
11221 |
|
11222 |
|
11223 |
|
11224 |
|
11225 |
|
11226 |
|
11227 |
|
11228 |
|
11229 |
|
11230 |
|
11231 |
|
11232 | var EventQueue = (function () {
|
11233 | function EventQueue() {
|
11234 | this.eventLists_ = [];
|
11235 | |
11236 |
|
11237 |
|
11238 | this.recursionDepth_ = 0;
|
11239 | }
|
11240 | return EventQueue;
|
11241 | }());
|
11242 |
|
11243 |
|
11244 |
|
11245 | function eventQueueQueueEvents(eventQueue, eventDataList) {
|
11246 |
|
11247 | var currList = null;
|
11248 | for (var i = 0; i < eventDataList.length; i++) {
|
11249 | var data = eventDataList[i];
|
11250 | var path = data.getPath();
|
11251 | if (currList !== null && !pathEquals(path, currList.path)) {
|
11252 | eventQueue.eventLists_.push(currList);
|
11253 | currList = null;
|
11254 | }
|
11255 | if (currList === null) {
|
11256 | currList = { events: [], path: path };
|
11257 | }
|
11258 | currList.events.push(data);
|
11259 | }
|
11260 | if (currList) {
|
11261 | eventQueue.eventLists_.push(currList);
|
11262 | }
|
11263 | }
|
11264 |
|
11265 |
|
11266 |
|
11267 |
|
11268 |
|
11269 |
|
11270 |
|
11271 |
|
11272 |
|
11273 | function eventQueueRaiseEventsAtPath(eventQueue, path, eventDataList) {
|
11274 | eventQueueQueueEvents(eventQueue, eventDataList);
|
11275 | eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
|
11276 | return pathEquals(eventPath, path);
|
11277 | });
|
11278 | }
|
11279 |
|
11280 |
|
11281 |
|
11282 |
|
11283 |
|
11284 |
|
11285 |
|
11286 |
|
11287 |
|
11288 | function eventQueueRaiseEventsForChangedPath(eventQueue, changedPath, eventDataList) {
|
11289 | eventQueueQueueEvents(eventQueue, eventDataList);
|
11290 | eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
|
11291 | return pathContains(eventPath, changedPath) ||
|
11292 | pathContains(changedPath, eventPath);
|
11293 | });
|
11294 | }
|
11295 | function eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, predicate) {
|
11296 | eventQueue.recursionDepth_++;
|
11297 | var sentAll = true;
|
11298 | for (var i = 0; i < eventQueue.eventLists_.length; i++) {
|
11299 | var eventList = eventQueue.eventLists_[i];
|
11300 | if (eventList) {
|
11301 | var eventPath = eventList.path;
|
11302 | if (predicate(eventPath)) {
|
11303 | eventListRaise(eventQueue.eventLists_[i]);
|
11304 | eventQueue.eventLists_[i] = null;
|
11305 | }
|
11306 | else {
|
11307 | sentAll = false;
|
11308 | }
|
11309 | }
|
11310 | }
|
11311 | if (sentAll) {
|
11312 | eventQueue.eventLists_ = [];
|
11313 | }
|
11314 | eventQueue.recursionDepth_--;
|
11315 | }
|
11316 |
|
11317 |
|
11318 |
|
11319 | function eventListRaise(eventList) {
|
11320 | for (var i = 0; i < eventList.events.length; i++) {
|
11321 | var eventData = eventList.events[i];
|
11322 | if (eventData !== null) {
|
11323 | eventList.events[i] = null;
|
11324 | var eventFn = eventData.getEventRunner();
|
11325 | if (logger) {
|
11326 | log('event: ' + eventData.toString());
|
11327 | }
|
11328 | exceptionGuard(eventFn);
|
11329 | }
|
11330 | }
|
11331 | }
|
11332 |
|
11333 |
|
11334 |
|
11335 |
|
11336 |
|
11337 |
|
11338 |
|
11339 |
|
11340 |
|
11341 |
|
11342 |
|
11343 |
|
11344 |
|
11345 |
|
11346 |
|
11347 |
|
11348 |
|
11349 | var INTERRUPT_REASON = 'repo_interrupt';
|
11350 |
|
11351 |
|
11352 |
|
11353 |
|
11354 |
|
11355 | var MAX_TRANSACTION_RETRIES = 25;
|
11356 |
|
11357 |
|
11358 |
|
11359 | var Repo = (function () {
|
11360 | function Repo(repoInfo_, forceRestClient_, authTokenProvider_, appCheckProvider_) {
|
11361 | this.repoInfo_ = repoInfo_;
|
11362 | this.forceRestClient_ = forceRestClient_;
|
11363 | this.authTokenProvider_ = authTokenProvider_;
|
11364 | this.appCheckProvider_ = appCheckProvider_;
|
11365 | this.dataUpdateCount = 0;
|
11366 | this.statsListener_ = null;
|
11367 | this.eventQueue_ = new EventQueue();
|
11368 | this.nextWriteId_ = 1;
|
11369 | this.interceptServerDataCallback_ = null;
|
11370 |
|
11371 | this.onDisconnect_ = newSparseSnapshotTree();
|
11372 |
|
11373 | this.transactionQueueTree_ = new Tree();
|
11374 |
|
11375 | this.persistentConnection_ = null;
|
11376 |
|
11377 | this.key = this.repoInfo_.toURLString();
|
11378 | }
|
11379 | |
11380 |
|
11381 |
|
11382 | Repo.prototype.toString = function () {
|
11383 | return ((this.repoInfo_.secure ? 'https://' : 'http://') + this.repoInfo_.host);
|
11384 | };
|
11385 | return Repo;
|
11386 | }());
|
11387 | function repoStart(repo, appId, authOverride) {
|
11388 | repo.stats_ = statsManagerGetCollection(repo.repoInfo_);
|
11389 | if (repo.forceRestClient_ || beingCrawled()) {
|
11390 | repo.server_ = new ReadonlyRestClient(repo.repoInfo_, function (pathString, data, isMerge, tag) {
|
11391 | repoOnDataUpdate(repo, pathString, data, isMerge, tag);
|
11392 | }, repo.authTokenProvider_, repo.appCheckProvider_);
|
11393 |
|
11394 | setTimeout(function () { return repoOnConnectStatus(repo, true); }, 0);
|
11395 | }
|
11396 | else {
|
11397 |
|
11398 | if (typeof authOverride !== 'undefined' && authOverride !== null) {
|
11399 | if (typeof authOverride !== 'object') {
|
11400 | throw new Error('Only objects are supported for option databaseAuthVariableOverride');
|
11401 | }
|
11402 | try {
|
11403 | util.stringify(authOverride);
|
11404 | }
|
11405 | catch (e) {
|
11406 | throw new Error('Invalid authOverride provided: ' + e);
|
11407 | }
|
11408 | }
|
11409 | repo.persistentConnection_ = new PersistentConnection(repo.repoInfo_, appId, function (pathString, data, isMerge, tag) {
|
11410 | repoOnDataUpdate(repo, pathString, data, isMerge, tag);
|
11411 | }, function (connectStatus) {
|
11412 | repoOnConnectStatus(repo, connectStatus);
|
11413 | }, function (updates) {
|
11414 | repoOnServerInfoUpdate(repo, updates);
|
11415 | }, repo.authTokenProvider_, repo.appCheckProvider_, authOverride);
|
11416 | repo.server_ = repo.persistentConnection_;
|
11417 | }
|
11418 | repo.authTokenProvider_.addTokenChangeListener(function (token) {
|
11419 | repo.server_.refreshAuthToken(token);
|
11420 | });
|
11421 | repo.appCheckProvider_.addTokenChangeListener(function (result) {
|
11422 | repo.server_.refreshAppCheckToken(result.token);
|
11423 | });
|
11424 |
|
11425 |
|
11426 | repo.statsReporter_ = statsManagerGetOrCreateReporter(repo.repoInfo_, function () { return new StatsReporter(repo.stats_, repo.server_); });
|
11427 |
|
11428 | repo.infoData_ = new SnapshotHolder();
|
11429 | repo.infoSyncTree_ = new SyncTree({
|
11430 | startListening: function (query, tag, currentHashFn, onComplete) {
|
11431 | var infoEvents = [];
|
11432 | var node = repo.infoData_.getNode(query._path);
|
11433 |
|
11434 |
|
11435 | if (!node.isEmpty()) {
|
11436 | infoEvents = syncTreeApplyServerOverwrite(repo.infoSyncTree_, query._path, node);
|
11437 | setTimeout(function () {
|
11438 | onComplete('ok');
|
11439 | }, 0);
|
11440 | }
|
11441 | return infoEvents;
|
11442 | },
|
11443 | stopListening: function () { }
|
11444 | });
|
11445 | repoUpdateInfo(repo, 'connected', false);
|
11446 | repo.serverSyncTree_ = new SyncTree({
|
11447 | startListening: function (query, tag, currentHashFn, onComplete) {
|
11448 | repo.server_.listen(query, currentHashFn, tag, function (status, data) {
|
11449 | var events = onComplete(status, data);
|
11450 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, query._path, events);
|
11451 | });
|
11452 |
|
11453 | return [];
|
11454 | },
|
11455 | stopListening: function (query, tag) {
|
11456 | repo.server_.unlisten(query, tag);
|
11457 | }
|
11458 | });
|
11459 | }
|
11460 |
|
11461 |
|
11462 |
|
11463 | function repoServerTime(repo) {
|
11464 | var offsetNode = repo.infoData_.getNode(new Path('.info/serverTimeOffset'));
|
11465 | var offset = offsetNode.val() || 0;
|
11466 | return new Date().getTime() + offset;
|
11467 | }
|
11468 |
|
11469 |
|
11470 |
|
11471 | function repoGenerateServerValues(repo) {
|
11472 | return generateWithValues({
|
11473 | timestamp: repoServerTime(repo)
|
11474 | });
|
11475 | }
|
11476 |
|
11477 |
|
11478 |
|
11479 | function repoOnDataUpdate(repo, pathString, data, isMerge, tag) {
|
11480 |
|
11481 | repo.dataUpdateCount++;
|
11482 | var path = new Path(pathString);
|
11483 | data = repo.interceptServerDataCallback_
|
11484 | ? repo.interceptServerDataCallback_(pathString, data)
|
11485 | : data;
|
11486 | var events = [];
|
11487 | if (tag) {
|
11488 | if (isMerge) {
|
11489 | var taggedChildren = util.map(data, function (raw) { return nodeFromJSON(raw); });
|
11490 | events = syncTreeApplyTaggedQueryMerge(repo.serverSyncTree_, path, taggedChildren, tag);
|
11491 | }
|
11492 | else {
|
11493 | var taggedSnap = nodeFromJSON(data);
|
11494 | events = syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, path, taggedSnap, tag);
|
11495 | }
|
11496 | }
|
11497 | else if (isMerge) {
|
11498 | var changedChildren = util.map(data, function (raw) { return nodeFromJSON(raw); });
|
11499 | events = syncTreeApplyServerMerge(repo.serverSyncTree_, path, changedChildren);
|
11500 | }
|
11501 | else {
|
11502 | var snap = nodeFromJSON(data);
|
11503 | events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap);
|
11504 | }
|
11505 | var affectedPath = path;
|
11506 | if (events.length > 0) {
|
11507 |
|
11508 |
|
11509 | affectedPath = repoRerunTransactions(repo, path);
|
11510 | }
|
11511 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, events);
|
11512 | }
|
11513 | function repoOnConnectStatus(repo, connectStatus) {
|
11514 | repoUpdateInfo(repo, 'connected', connectStatus);
|
11515 | if (connectStatus === false) {
|
11516 | repoRunOnDisconnectEvents(repo);
|
11517 | }
|
11518 | }
|
11519 | function repoOnServerInfoUpdate(repo, updates) {
|
11520 | each(updates, function (key, value) {
|
11521 | repoUpdateInfo(repo, key, value);
|
11522 | });
|
11523 | }
|
11524 | function repoUpdateInfo(repo, pathString, value) {
|
11525 | var path = new Path('/.info/' + pathString);
|
11526 | var newNode = nodeFromJSON(value);
|
11527 | repo.infoData_.updateSnapshot(path, newNode);
|
11528 | var events = syncTreeApplyServerOverwrite(repo.infoSyncTree_, path, newNode);
|
11529 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
11530 | }
|
11531 | function repoGetNextWriteId(repo) {
|
11532 | return repo.nextWriteId_++;
|
11533 | }
|
11534 |
|
11535 |
|
11536 |
|
11537 |
|
11538 |
|
11539 |
|
11540 |
|
11541 |
|
11542 |
|
11543 |
|
11544 |
|
11545 |
|
11546 |
|
11547 |
|
11548 |
|
11549 | function repoGetValue(repo, query) {
|
11550 |
|
11551 | var cached = syncTreeGetServerValue(repo.serverSyncTree_, query);
|
11552 | if (cached != null) {
|
11553 | return Promise.resolve(cached);
|
11554 | }
|
11555 | return repo.server_.get(query).then(function (payload) {
|
11556 | var node = nodeFromJSON(payload).withIndex(query._queryParams.getIndex());
|
11557 |
|
11558 | if (query._queryParams.loadsAllData()) {
|
11559 | syncTreeApplyServerOverwrite(repo.serverSyncTree_, query._path, node);
|
11560 | }
|
11561 | else {
|
11562 |
|
11563 |
|
11564 |
|
11565 |
|
11566 | var tag = syncTreeRegisterQuery(repo.serverSyncTree_, query);
|
11567 | syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, query._path, node, tag);
|
11568 |
|
11569 |
|
11570 | }
|
11571 | var cancels = syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, null);
|
11572 | if (cancels.length > 0) {
|
11573 | repoLog(repo, 'unexpected cancel events in repoGetValue');
|
11574 | }
|
11575 | return node;
|
11576 | }, function (err) {
|
11577 | repoLog(repo, 'get for query ' + util.stringify(query) + ' failed: ' + err);
|
11578 | return Promise.reject(new Error(err));
|
11579 | });
|
11580 | }
|
11581 | function repoSetWithPriority(repo, path, newVal, newPriority, onComplete) {
|
11582 | repoLog(repo, 'set', {
|
11583 | path: path.toString(),
|
11584 | value: newVal,
|
11585 | priority: newPriority
|
11586 | });
|
11587 |
|
11588 |
|
11589 | var serverValues = repoGenerateServerValues(repo);
|
11590 | var newNodeUnresolved = nodeFromJSON(newVal, newPriority);
|
11591 | var existing = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path);
|
11592 | var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, existing, serverValues);
|
11593 | var writeId = repoGetNextWriteId(repo);
|
11594 | var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, writeId, true);
|
11595 | eventQueueQueueEvents(repo.eventQueue_, events);
|
11596 | repo.server_.put(path.toString(), newNodeUnresolved.val( true), function (status, errorReason) {
|
11597 | var success = status === 'ok';
|
11598 | if (!success) {
|
11599 | warn('set at ' + path + ' failed: ' + status);
|
11600 | }
|
11601 | var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId, !success);
|
11602 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, clearEvents);
|
11603 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11604 | });
|
11605 | var affectedPath = repoAbortTransactions(repo, path);
|
11606 | repoRerunTransactions(repo, affectedPath);
|
11607 |
|
11608 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, []);
|
11609 | }
|
11610 | function repoUpdate(repo, path, childrenToMerge, onComplete) {
|
11611 | repoLog(repo, 'update', { path: path.toString(), value: childrenToMerge });
|
11612 |
|
11613 | var empty = true;
|
11614 | var serverValues = repoGenerateServerValues(repo);
|
11615 | var changedChildren = {};
|
11616 | each(childrenToMerge, function (changedKey, changedValue) {
|
11617 | empty = false;
|
11618 | changedChildren[changedKey] = resolveDeferredValueTree(pathChild(path, changedKey), nodeFromJSON(changedValue), repo.serverSyncTree_, serverValues);
|
11619 | });
|
11620 | if (!empty) {
|
11621 | var writeId_1 = repoGetNextWriteId(repo);
|
11622 | var events = syncTreeApplyUserMerge(repo.serverSyncTree_, path, changedChildren, writeId_1);
|
11623 | eventQueueQueueEvents(repo.eventQueue_, events);
|
11624 | repo.server_.merge(path.toString(), childrenToMerge, function (status, errorReason) {
|
11625 | var success = status === 'ok';
|
11626 | if (!success) {
|
11627 | warn('update at ' + path + ' failed: ' + status);
|
11628 | }
|
11629 | var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId_1, !success);
|
11630 | var affectedPath = clearEvents.length > 0 ? repoRerunTransactions(repo, path) : path;
|
11631 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, clearEvents);
|
11632 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11633 | });
|
11634 | each(childrenToMerge, function (changedPath) {
|
11635 | var affectedPath = repoAbortTransactions(repo, pathChild(path, changedPath));
|
11636 | repoRerunTransactions(repo, affectedPath);
|
11637 | });
|
11638 |
|
11639 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, []);
|
11640 | }
|
11641 | else {
|
11642 | log("update() called with empty data. Don't do anything.");
|
11643 | repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
|
11644 | }
|
11645 | }
|
11646 |
|
11647 |
|
11648 |
|
11649 | function repoRunOnDisconnectEvents(repo) {
|
11650 | repoLog(repo, 'onDisconnectEvents');
|
11651 | var serverValues = repoGenerateServerValues(repo);
|
11652 | var resolvedOnDisconnectTree = newSparseSnapshotTree();
|
11653 | sparseSnapshotTreeForEachTree(repo.onDisconnect_, newEmptyPath(), function (path, node) {
|
11654 | var resolved = resolveDeferredValueTree(path, node, repo.serverSyncTree_, serverValues);
|
11655 | sparseSnapshotTreeRemember(resolvedOnDisconnectTree, path, resolved);
|
11656 | });
|
11657 | var events = [];
|
11658 | sparseSnapshotTreeForEachTree(resolvedOnDisconnectTree, newEmptyPath(), function (path, snap) {
|
11659 | events = events.concat(syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap));
|
11660 | var affectedPath = repoAbortTransactions(repo, path);
|
11661 | repoRerunTransactions(repo, affectedPath);
|
11662 | });
|
11663 | repo.onDisconnect_ = newSparseSnapshotTree();
|
11664 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, newEmptyPath(), events);
|
11665 | }
|
11666 | function repoOnDisconnectCancel(repo, path, onComplete) {
|
11667 | repo.server_.onDisconnectCancel(path.toString(), function (status, errorReason) {
|
11668 | if (status === 'ok') {
|
11669 | sparseSnapshotTreeForget(repo.onDisconnect_, path);
|
11670 | }
|
11671 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11672 | });
|
11673 | }
|
11674 | function repoOnDisconnectSet(repo, path, value, onComplete) {
|
11675 | var newNode = nodeFromJSON(value);
|
11676 | repo.server_.onDisconnectPut(path.toString(), newNode.val( true), function (status, errorReason) {
|
11677 | if (status === 'ok') {
|
11678 | sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
|
11679 | }
|
11680 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11681 | });
|
11682 | }
|
11683 | function repoOnDisconnectSetWithPriority(repo, path, value, priority, onComplete) {
|
11684 | var newNode = nodeFromJSON(value, priority);
|
11685 | repo.server_.onDisconnectPut(path.toString(), newNode.val( true), function (status, errorReason) {
|
11686 | if (status === 'ok') {
|
11687 | sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
|
11688 | }
|
11689 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11690 | });
|
11691 | }
|
11692 | function repoOnDisconnectUpdate(repo, path, childrenToMerge, onComplete) {
|
11693 | if (util.isEmpty(childrenToMerge)) {
|
11694 | log("onDisconnect().update() called with empty data. Don't do anything.");
|
11695 | repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
|
11696 | return;
|
11697 | }
|
11698 | repo.server_.onDisconnectMerge(path.toString(), childrenToMerge, function (status, errorReason) {
|
11699 | if (status === 'ok') {
|
11700 | each(childrenToMerge, function (childName, childNode) {
|
11701 | var newChildNode = nodeFromJSON(childNode);
|
11702 | sparseSnapshotTreeRemember(repo.onDisconnect_, pathChild(path, childName), newChildNode);
|
11703 | });
|
11704 | }
|
11705 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11706 | });
|
11707 | }
|
11708 | function repoAddEventCallbackForQuery(repo, query, eventRegistration) {
|
11709 | var events;
|
11710 | if (pathGetFront(query._path) === '.info') {
|
11711 | events = syncTreeAddEventRegistration(repo.infoSyncTree_, query, eventRegistration);
|
11712 | }
|
11713 | else {
|
11714 | events = syncTreeAddEventRegistration(repo.serverSyncTree_, query, eventRegistration);
|
11715 | }
|
11716 | eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
|
11717 | }
|
11718 | function repoRemoveEventCallbackForQuery(repo, query, eventRegistration) {
|
11719 |
|
11720 |
|
11721 | var events;
|
11722 | if (pathGetFront(query._path) === '.info') {
|
11723 | events = syncTreeRemoveEventRegistration(repo.infoSyncTree_, query, eventRegistration);
|
11724 | }
|
11725 | else {
|
11726 | events = syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, eventRegistration);
|
11727 | }
|
11728 | eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
|
11729 | }
|
11730 | function repoInterrupt(repo) {
|
11731 | if (repo.persistentConnection_) {
|
11732 | repo.persistentConnection_.interrupt(INTERRUPT_REASON);
|
11733 | }
|
11734 | }
|
11735 | function repoResume(repo) {
|
11736 | if (repo.persistentConnection_) {
|
11737 | repo.persistentConnection_.resume(INTERRUPT_REASON);
|
11738 | }
|
11739 | }
|
11740 | function repoLog(repo) {
|
11741 | var varArgs = [];
|
11742 | for (var _i = 1; _i < arguments.length; _i++) {
|
11743 | varArgs[_i - 1] = arguments[_i];
|
11744 | }
|
11745 | var prefix = '';
|
11746 | if (repo.persistentConnection_) {
|
11747 | prefix = repo.persistentConnection_.id + ':';
|
11748 | }
|
11749 | log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs)));
|
11750 | }
|
11751 | function repoCallOnCompleteCallback(repo, callback, status, errorReason) {
|
11752 | if (callback) {
|
11753 | exceptionGuard(function () {
|
11754 | if (status === 'ok') {
|
11755 | callback(null);
|
11756 | }
|
11757 | else {
|
11758 | var code = (status || 'error').toUpperCase();
|
11759 | var message = code;
|
11760 | if (errorReason) {
|
11761 | message += ': ' + errorReason;
|
11762 | }
|
11763 | var error = new Error(message);
|
11764 |
|
11765 | error.code = code;
|
11766 | callback(error);
|
11767 | }
|
11768 | });
|
11769 | }
|
11770 | }
|
11771 |
|
11772 |
|
11773 |
|
11774 |
|
11775 |
|
11776 |
|
11777 |
|
11778 |
|
11779 |
|
11780 |
|
11781 |
|
11782 | function repoStartTransaction(repo, path, transactionUpdate, onComplete, unwatcher, applyLocally) {
|
11783 | repoLog(repo, 'transaction on ' + path);
|
11784 |
|
11785 | var transaction = {
|
11786 | path: path,
|
11787 | update: transactionUpdate,
|
11788 | onComplete: onComplete,
|
11789 |
|
11790 | status: null,
|
11791 |
|
11792 |
|
11793 | order: LUIDGenerator(),
|
11794 |
|
11795 | applyLocally: applyLocally,
|
11796 |
|
11797 | retryCount: 0,
|
11798 |
|
11799 | unwatcher: unwatcher,
|
11800 |
|
11801 | abortReason: null,
|
11802 | currentWriteId: null,
|
11803 | currentInputSnapshot: null,
|
11804 | currentOutputSnapshotRaw: null,
|
11805 | currentOutputSnapshotResolved: null
|
11806 | };
|
11807 |
|
11808 | var currentState = repoGetLatestState(repo, path, undefined);
|
11809 | transaction.currentInputSnapshot = currentState;
|
11810 | var newVal = transaction.update(currentState.val());
|
11811 | if (newVal === undefined) {
|
11812 |
|
11813 | transaction.unwatcher();
|
11814 | transaction.currentOutputSnapshotRaw = null;
|
11815 | transaction.currentOutputSnapshotResolved = null;
|
11816 | if (transaction.onComplete) {
|
11817 | transaction.onComplete(null, false, transaction.currentInputSnapshot);
|
11818 | }
|
11819 | }
|
11820 | else {
|
11821 | validateFirebaseData('transaction failed: Data returned ', newVal, transaction.path);
|
11822 |
|
11823 | transaction.status = 0 ;
|
11824 | var queueNode = treeSubTree(repo.transactionQueueTree_, path);
|
11825 | var nodeQueue = treeGetValue(queueNode) || [];
|
11826 | nodeQueue.push(transaction);
|
11827 | treeSetValue(queueNode, nodeQueue);
|
11828 |
|
11829 |
|
11830 |
|
11831 |
|
11832 | var priorityForNode = void 0;
|
11833 | if (typeof newVal === 'object' &&
|
11834 | newVal !== null &&
|
11835 | util.contains(newVal, '.priority')) {
|
11836 |
|
11837 | priorityForNode = util.safeGet(newVal, '.priority');
|
11838 | util.assert(isValidPriority(priorityForNode), 'Invalid priority returned by transaction. ' +
|
11839 | 'Priority must be a valid string, finite number, server value, or null.');
|
11840 | }
|
11841 | else {
|
11842 | var currentNode = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path) ||
|
11843 | ChildrenNode.EMPTY_NODE;
|
11844 | priorityForNode = currentNode.getPriority().val();
|
11845 | }
|
11846 | var serverValues = repoGenerateServerValues(repo);
|
11847 | var newNodeUnresolved = nodeFromJSON(newVal, priorityForNode);
|
11848 | var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, currentState, serverValues);
|
11849 | transaction.currentOutputSnapshotRaw = newNodeUnresolved;
|
11850 | transaction.currentOutputSnapshotResolved = newNode;
|
11851 | transaction.currentWriteId = repoGetNextWriteId(repo);
|
11852 | var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, transaction.currentWriteId, transaction.applyLocally);
|
11853 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
11854 | repoSendReadyTransactions(repo, repo.transactionQueueTree_);
|
11855 | }
|
11856 | }
|
11857 |
|
11858 |
|
11859 |
|
11860 | function repoGetLatestState(repo, path, excludeSets) {
|
11861 | return (syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path, excludeSets) ||
|
11862 | ChildrenNode.EMPTY_NODE);
|
11863 | }
|
11864 |
|
11865 |
|
11866 |
|
11867 |
|
11868 |
|
11869 |
|
11870 |
|
11871 |
|
11872 |
|
11873 | function repoSendReadyTransactions(repo, node) {
|
11874 | if (node === void 0) { node = repo.transactionQueueTree_; }
|
11875 |
|
11876 | if (!node) {
|
11877 | repoPruneCompletedTransactionsBelowNode(repo, node);
|
11878 | }
|
11879 | if (treeGetValue(node)) {
|
11880 | var queue = repoBuildTransactionQueue(repo, node);
|
11881 | util.assert(queue.length > 0, 'Sending zero length transaction queue');
|
11882 | var allRun = queue.every(function (transaction) { return transaction.status === 0 ; });
|
11883 |
|
11884 | if (allRun) {
|
11885 | repoSendTransactionQueue(repo, treeGetPath(node), queue);
|
11886 | }
|
11887 | }
|
11888 | else if (treeHasChildren(node)) {
|
11889 | treeForEachChild(node, function (childNode) {
|
11890 | repoSendReadyTransactions(repo, childNode);
|
11891 | });
|
11892 | }
|
11893 | }
|
11894 |
|
11895 |
|
11896 |
|
11897 |
|
11898 |
|
11899 |
|
11900 |
|
11901 | function repoSendTransactionQueue(repo, path, queue) {
|
11902 |
|
11903 | var setsToIgnore = queue.map(function (txn) {
|
11904 | return txn.currentWriteId;
|
11905 | });
|
11906 | var latestState = repoGetLatestState(repo, path, setsToIgnore);
|
11907 | var snapToSend = latestState;
|
11908 | var latestHash = latestState.hash();
|
11909 | for (var i = 0; i < queue.length; i++) {
|
11910 | var txn = queue[i];
|
11911 | util.assert(txn.status === 0 , 'tryToSendTransactionQueue_: items in queue should all be run.');
|
11912 | txn.status = 1 ;
|
11913 | txn.retryCount++;
|
11914 | var relativePath = newRelativePath(path, txn.path);
|
11915 |
|
11916 | snapToSend = snapToSend.updateChild(relativePath , txn.currentOutputSnapshotRaw);
|
11917 | }
|
11918 | var dataToSend = snapToSend.val(true);
|
11919 | var pathToSend = path;
|
11920 |
|
11921 | repo.server_.put(pathToSend.toString(), dataToSend, function (status) {
|
11922 | repoLog(repo, 'transaction put response', {
|
11923 | path: pathToSend.toString(),
|
11924 | status: status
|
11925 | });
|
11926 | var events = [];
|
11927 | if (status === 'ok') {
|
11928 |
|
11929 |
|
11930 |
|
11931 | var callbacks = [];
|
11932 | var _loop_1 = function (i) {
|
11933 | queue[i].status = 2 ;
|
11934 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId));
|
11935 | if (queue[i].onComplete) {
|
11936 |
|
11937 |
|
11938 | callbacks.push(function () {
|
11939 | return queue[i].onComplete(null, true, queue[i].currentOutputSnapshotResolved);
|
11940 | });
|
11941 | }
|
11942 | queue[i].unwatcher();
|
11943 | };
|
11944 | for (var i = 0; i < queue.length; i++) {
|
11945 | _loop_1(i);
|
11946 | }
|
11947 |
|
11948 | repoPruneCompletedTransactionsBelowNode(repo, treeSubTree(repo.transactionQueueTree_, path));
|
11949 |
|
11950 | repoSendReadyTransactions(repo, repo.transactionQueueTree_);
|
11951 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
11952 |
|
11953 | for (var i = 0; i < callbacks.length; i++) {
|
11954 | exceptionGuard(callbacks[i]);
|
11955 | }
|
11956 | }
|
11957 | else {
|
11958 |
|
11959 | if (status === 'datastale') {
|
11960 | for (var i = 0; i < queue.length; i++) {
|
11961 | if (queue[i].status === 3 ) {
|
11962 | queue[i].status = 4 ;
|
11963 | }
|
11964 | else {
|
11965 | queue[i].status = 0 ;
|
11966 | }
|
11967 | }
|
11968 | }
|
11969 | else {
|
11970 | warn('transaction at ' + pathToSend.toString() + ' failed: ' + status);
|
11971 | for (var i = 0; i < queue.length; i++) {
|
11972 | queue[i].status = 4 ;
|
11973 | queue[i].abortReason = status;
|
11974 | }
|
11975 | }
|
11976 | repoRerunTransactions(repo, path);
|
11977 | }
|
11978 | }, latestHash);
|
11979 | }
|
11980 |
|
11981 |
|
11982 |
|
11983 |
|
11984 |
|
11985 |
|
11986 |
|
11987 |
|
11988 |
|
11989 |
|
11990 |
|
11991 | function repoRerunTransactions(repo, changedPath) {
|
11992 | var rootMostTransactionNode = repoGetAncestorTransactionNode(repo, changedPath);
|
11993 | var path = treeGetPath(rootMostTransactionNode);
|
11994 | var queue = repoBuildTransactionQueue(repo, rootMostTransactionNode);
|
11995 | repoRerunTransactionQueue(repo, queue, path);
|
11996 | return path;
|
11997 | }
|
11998 |
|
11999 |
|
12000 |
|
12001 |
|
12002 |
|
12003 |
|
12004 |
|
12005 | function repoRerunTransactionQueue(repo, queue, path) {
|
12006 | if (queue.length === 0) {
|
12007 | return;
|
12008 | }
|
12009 |
|
12010 |
|
12011 |
|
12012 | var callbacks = [];
|
12013 | var events = [];
|
12014 |
|
12015 | var txnsToRerun = queue.filter(function (q) {
|
12016 | return q.status === 0 ;
|
12017 | });
|
12018 | var setsToIgnore = txnsToRerun.map(function (q) {
|
12019 | return q.currentWriteId;
|
12020 | });
|
12021 | var _loop_2 = function (i) {
|
12022 | var transaction = queue[i];
|
12023 | var relativePath = newRelativePath(path, transaction.path);
|
12024 | var abortTransaction = false, abortReason;
|
12025 | util.assert(relativePath !== null, 'rerunTransactionsUnderNode_: relativePath should not be null.');
|
12026 | if (transaction.status === 4 ) {
|
12027 | abortTransaction = true;
|
12028 | abortReason = transaction.abortReason;
|
12029 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
|
12030 | }
|
12031 | else if (transaction.status === 0 ) {
|
12032 | if (transaction.retryCount >= MAX_TRANSACTION_RETRIES) {
|
12033 | abortTransaction = true;
|
12034 | abortReason = 'maxretry';
|
12035 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
|
12036 | }
|
12037 | else {
|
12038 |
|
12039 | var currentNode = repoGetLatestState(repo, transaction.path, setsToIgnore);
|
12040 | transaction.currentInputSnapshot = currentNode;
|
12041 | var newData = queue[i].update(currentNode.val());
|
12042 | if (newData !== undefined) {
|
12043 | validateFirebaseData('transaction failed: Data returned ', newData, transaction.path);
|
12044 | var newDataNode = nodeFromJSON(newData);
|
12045 | var hasExplicitPriority = typeof newData === 'object' &&
|
12046 | newData != null &&
|
12047 | util.contains(newData, '.priority');
|
12048 | if (!hasExplicitPriority) {
|
12049 |
|
12050 | newDataNode = newDataNode.updatePriority(currentNode.getPriority());
|
12051 | }
|
12052 | var oldWriteId = transaction.currentWriteId;
|
12053 | var serverValues = repoGenerateServerValues(repo);
|
12054 | var newNodeResolved = resolveDeferredValueSnapshot(newDataNode, currentNode, serverValues);
|
12055 | transaction.currentOutputSnapshotRaw = newDataNode;
|
12056 | transaction.currentOutputSnapshotResolved = newNodeResolved;
|
12057 | transaction.currentWriteId = repoGetNextWriteId(repo);
|
12058 |
|
12059 | setsToIgnore.splice(setsToIgnore.indexOf(oldWriteId), 1);
|
12060 | events = events.concat(syncTreeApplyUserOverwrite(repo.serverSyncTree_, transaction.path, newNodeResolved, transaction.currentWriteId, transaction.applyLocally));
|
12061 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, oldWriteId, true));
|
12062 | }
|
12063 | else {
|
12064 | abortTransaction = true;
|
12065 | abortReason = 'nodata';
|
12066 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
|
12067 | }
|
12068 | }
|
12069 | }
|
12070 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
12071 | events = [];
|
12072 | if (abortTransaction) {
|
12073 |
|
12074 | queue[i].status = 2 ;
|
12075 |
|
12076 |
|
12077 |
|
12078 | (function (unwatcher) {
|
12079 | setTimeout(unwatcher, Math.floor(0));
|
12080 | })(queue[i].unwatcher);
|
12081 | if (queue[i].onComplete) {
|
12082 | if (abortReason === 'nodata') {
|
12083 | callbacks.push(function () {
|
12084 | return queue[i].onComplete(null, false, queue[i].currentInputSnapshot);
|
12085 | });
|
12086 | }
|
12087 | else {
|
12088 | callbacks.push(function () {
|
12089 | return queue[i].onComplete(new Error(abortReason), false, null);
|
12090 | });
|
12091 | }
|
12092 | }
|
12093 | }
|
12094 | };
|
12095 | for (var i = 0; i < queue.length; i++) {
|
12096 | _loop_2(i);
|
12097 | }
|
12098 |
|
12099 | repoPruneCompletedTransactionsBelowNode(repo, repo.transactionQueueTree_);
|
12100 |
|
12101 | for (var i = 0; i < callbacks.length; i++) {
|
12102 | exceptionGuard(callbacks[i]);
|
12103 | }
|
12104 |
|
12105 | repoSendReadyTransactions(repo, repo.transactionQueueTree_);
|
12106 | }
|
12107 |
|
12108 |
|
12109 |
|
12110 |
|
12111 |
|
12112 |
|
12113 |
|
12114 |
|
12115 | function repoGetAncestorTransactionNode(repo, path) {
|
12116 | var front;
|
12117 |
|
12118 |
|
12119 | var transactionNode = repo.transactionQueueTree_;
|
12120 | front = pathGetFront(path);
|
12121 | while (front !== null && treeGetValue(transactionNode) === undefined) {
|
12122 | transactionNode = treeSubTree(transactionNode, front);
|
12123 | path = pathPopFront(path);
|
12124 | front = pathGetFront(path);
|
12125 | }
|
12126 | return transactionNode;
|
12127 | }
|
12128 |
|
12129 |
|
12130 |
|
12131 |
|
12132 |
|
12133 |
|
12134 |
|
12135 | function repoBuildTransactionQueue(repo, transactionNode) {
|
12136 |
|
12137 | var transactionQueue = [];
|
12138 | repoAggregateTransactionQueuesForNode(repo, transactionNode, transactionQueue);
|
12139 |
|
12140 | transactionQueue.sort(function (a, b) { return a.order - b.order; });
|
12141 | return transactionQueue;
|
12142 | }
|
12143 | function repoAggregateTransactionQueuesForNode(repo, node, queue) {
|
12144 | var nodeQueue = treeGetValue(node);
|
12145 | if (nodeQueue) {
|
12146 | for (var i = 0; i < nodeQueue.length; i++) {
|
12147 | queue.push(nodeQueue[i]);
|
12148 | }
|
12149 | }
|
12150 | treeForEachChild(node, function (child) {
|
12151 | repoAggregateTransactionQueuesForNode(repo, child, queue);
|
12152 | });
|
12153 | }
|
12154 |
|
12155 |
|
12156 |
|
12157 | function repoPruneCompletedTransactionsBelowNode(repo, node) {
|
12158 | var queue = treeGetValue(node);
|
12159 | if (queue) {
|
12160 | var to = 0;
|
12161 | for (var from = 0; from < queue.length; from++) {
|
12162 | if (queue[from].status !== 2 ) {
|
12163 | queue[to] = queue[from];
|
12164 | to++;
|
12165 | }
|
12166 | }
|
12167 | queue.length = to;
|
12168 | treeSetValue(node, queue.length > 0 ? queue : undefined);
|
12169 | }
|
12170 | treeForEachChild(node, function (childNode) {
|
12171 | repoPruneCompletedTransactionsBelowNode(repo, childNode);
|
12172 | });
|
12173 | }
|
12174 |
|
12175 |
|
12176 |
|
12177 |
|
12178 |
|
12179 |
|
12180 |
|
12181 | function repoAbortTransactions(repo, path) {
|
12182 | var affectedPath = treeGetPath(repoGetAncestorTransactionNode(repo, path));
|
12183 | var transactionNode = treeSubTree(repo.transactionQueueTree_, path);
|
12184 | treeForEachAncestor(transactionNode, function (node) {
|
12185 | repoAbortTransactionsOnNode(repo, node);
|
12186 | });
|
12187 | repoAbortTransactionsOnNode(repo, transactionNode);
|
12188 | treeForEachDescendant(transactionNode, function (node) {
|
12189 | repoAbortTransactionsOnNode(repo, node);
|
12190 | });
|
12191 | return affectedPath;
|
12192 | }
|
12193 |
|
12194 |
|
12195 |
|
12196 |
|
12197 |
|
12198 | function repoAbortTransactionsOnNode(repo, node) {
|
12199 | var queue = treeGetValue(node);
|
12200 | if (queue) {
|
12201 |
|
12202 |
|
12203 |
|
12204 | var callbacks = [];
|
12205 |
|
12206 |
|
12207 | var events = [];
|
12208 | var lastSent = -1;
|
12209 | for (var i = 0; i < queue.length; i++) {
|
12210 | if (queue[i].status === 3 ) ;
|
12211 | else if (queue[i].status === 1 ) {
|
12212 | util.assert(lastSent === i - 1, 'All SENT items should be at beginning of queue.');
|
12213 | lastSent = i;
|
12214 |
|
12215 | queue[i].status = 3 ;
|
12216 | queue[i].abortReason = 'set';
|
12217 | }
|
12218 | else {
|
12219 | util.assert(queue[i].status === 0 , 'Unexpected transaction status in abort');
|
12220 |
|
12221 | queue[i].unwatcher();
|
12222 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId, true));
|
12223 | if (queue[i].onComplete) {
|
12224 | callbacks.push(queue[i].onComplete.bind(null, new Error('set'), false, null));
|
12225 | }
|
12226 | }
|
12227 | }
|
12228 | if (lastSent === -1) {
|
12229 |
|
12230 | treeSetValue(node, undefined);
|
12231 | }
|
12232 | else {
|
12233 |
|
12234 | queue.length = lastSent + 1;
|
12235 | }
|
12236 |
|
12237 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, treeGetPath(node), events);
|
12238 | for (var i = 0; i < callbacks.length; i++) {
|
12239 | exceptionGuard(callbacks[i]);
|
12240 | }
|
12241 | }
|
12242 | }
|
12243 |
|
12244 |
|
12245 |
|
12246 |
|
12247 |
|
12248 |
|
12249 |
|
12250 |
|
12251 |
|
12252 |
|
12253 |
|
12254 |
|
12255 |
|
12256 |
|
12257 |
|
12258 |
|
12259 |
|
12260 | function decodePath(pathString) {
|
12261 | var pathStringDecoded = '';
|
12262 | var pieces = pathString.split('/');
|
12263 | for (var i = 0; i < pieces.length; i++) {
|
12264 | if (pieces[i].length > 0) {
|
12265 | var piece = pieces[i];
|
12266 | try {
|
12267 | piece = decodeURIComponent(piece.replace(/\+/g, ' '));
|
12268 | }
|
12269 | catch (e) { }
|
12270 | pathStringDecoded += '/' + piece;
|
12271 | }
|
12272 | }
|
12273 | return pathStringDecoded;
|
12274 | }
|
12275 |
|
12276 |
|
12277 |
|
12278 | function decodeQuery(queryString) {
|
12279 | var e_1, _a;
|
12280 | var results = {};
|
12281 | if (queryString.charAt(0) === '?') {
|
12282 | queryString = queryString.substring(1);
|
12283 | }
|
12284 | try {
|
12285 | for (var _b = tslib.__values(queryString.split('&')), _c = _b.next(); !_c.done; _c = _b.next()) {
|
12286 | var segment = _c.value;
|
12287 | if (segment.length === 0) {
|
12288 | continue;
|
12289 | }
|
12290 | var kv = segment.split('=');
|
12291 | if (kv.length === 2) {
|
12292 | results[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
|
12293 | }
|
12294 | else {
|
12295 | warn("Invalid query segment '" + segment + "' in query '" + queryString + "'");
|
12296 | }
|
12297 | }
|
12298 | }
|
12299 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
12300 | finally {
|
12301 | try {
|
12302 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
12303 | }
|
12304 | finally { if (e_1) throw e_1.error; }
|
12305 | }
|
12306 | return results;
|
12307 | }
|
12308 | var parseRepoInfo = function (dataURL, nodeAdmin) {
|
12309 | var parsedUrl = parseDatabaseURL(dataURL), namespace = parsedUrl.namespace;
|
12310 | if (parsedUrl.domain === 'firebase.com') {
|
12311 | fatal(parsedUrl.host +
|
12312 | ' is no longer supported. ' +
|
12313 | 'Please use <YOUR FIREBASE>.firebaseio.com instead');
|
12314 | }
|
12315 |
|
12316 | if ((!namespace || namespace === 'undefined') &&
|
12317 | parsedUrl.domain !== 'localhost') {
|
12318 | fatal('Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com');
|
12319 | }
|
12320 | if (!parsedUrl.secure) {
|
12321 | warnIfPageIsSecure();
|
12322 | }
|
12323 | var webSocketOnly = parsedUrl.scheme === 'ws' || parsedUrl.scheme === 'wss';
|
12324 | return {
|
12325 | repoInfo: new RepoInfo(parsedUrl.host, parsedUrl.secure, namespace, webSocketOnly, nodeAdmin,
|
12326 | '',
|
12327 | namespace !== parsedUrl.subdomain),
|
12328 | path: new Path(parsedUrl.pathString)
|
12329 | };
|
12330 | };
|
12331 | var parseDatabaseURL = function (dataURL) {
|
12332 |
|
12333 | var host = '', domain = '', subdomain = '', pathString = '', namespace = '';
|
12334 |
|
12335 | var secure = true, scheme = 'https', port = 443;
|
12336 |
|
12337 | if (typeof dataURL === 'string') {
|
12338 |
|
12339 | var colonInd = dataURL.indexOf('//');
|
12340 | if (colonInd >= 0) {
|
12341 | scheme = dataURL.substring(0, colonInd - 1);
|
12342 | dataURL = dataURL.substring(colonInd + 2);
|
12343 | }
|
12344 |
|
12345 | var slashInd = dataURL.indexOf('/');
|
12346 | if (slashInd === -1) {
|
12347 | slashInd = dataURL.length;
|
12348 | }
|
12349 | var questionMarkInd = dataURL.indexOf('?');
|
12350 | if (questionMarkInd === -1) {
|
12351 | questionMarkInd = dataURL.length;
|
12352 | }
|
12353 | host = dataURL.substring(0, Math.min(slashInd, questionMarkInd));
|
12354 | if (slashInd < questionMarkInd) {
|
12355 |
|
12356 | pathString = decodePath(dataURL.substring(slashInd, questionMarkInd));
|
12357 | }
|
12358 | var queryParams = decodeQuery(dataURL.substring(Math.min(dataURL.length, questionMarkInd)));
|
12359 |
|
12360 | colonInd = host.indexOf(':');
|
12361 | if (colonInd >= 0) {
|
12362 | secure = scheme === 'https' || scheme === 'wss';
|
12363 | port = parseInt(host.substring(colonInd + 1), 10);
|
12364 | }
|
12365 | else {
|
12366 | colonInd = host.length;
|
12367 | }
|
12368 | var hostWithoutPort = host.slice(0, colonInd);
|
12369 | if (hostWithoutPort.toLowerCase() === 'localhost') {
|
12370 | domain = 'localhost';
|
12371 | }
|
12372 | else if (hostWithoutPort.split('.').length <= 2) {
|
12373 | domain = hostWithoutPort;
|
12374 | }
|
12375 | else {
|
12376 |
|
12377 | var dotInd = host.indexOf('.');
|
12378 | subdomain = host.substring(0, dotInd).toLowerCase();
|
12379 | domain = host.substring(dotInd + 1);
|
12380 |
|
12381 | namespace = subdomain;
|
12382 | }
|
12383 |
|
12384 | if ('ns' in queryParams) {
|
12385 | namespace = queryParams['ns'];
|
12386 | }
|
12387 | }
|
12388 | return {
|
12389 | host: host,
|
12390 | port: port,
|
12391 | domain: domain,
|
12392 | subdomain: subdomain,
|
12393 | secure: secure,
|
12394 | scheme: scheme,
|
12395 | pathString: pathString,
|
12396 | namespace: namespace
|
12397 | };
|
12398 | };
|
12399 |
|
12400 |
|
12401 |
|
12402 |
|
12403 |
|
12404 |
|
12405 |
|
12406 |
|
12407 |
|
12408 |
|
12409 |
|
12410 |
|
12411 |
|
12412 |
|
12413 |
|
12414 |
|
12415 |
|
12416 |
|
12417 |
|
12418 |
|
12419 | var DataEvent = (function () {
|
12420 | |
12421 |
|
12422 |
|
12423 |
|
12424 |
|
12425 |
|
12426 | function DataEvent(eventType, eventRegistration, snapshot, prevName) {
|
12427 | this.eventType = eventType;
|
12428 | this.eventRegistration = eventRegistration;
|
12429 | this.snapshot = snapshot;
|
12430 | this.prevName = prevName;
|
12431 | }
|
12432 | DataEvent.prototype.getPath = function () {
|
12433 | var ref = this.snapshot.ref;
|
12434 | if (this.eventType === 'value') {
|
12435 | return ref._path;
|
12436 | }
|
12437 | else {
|
12438 | return ref.parent._path;
|
12439 | }
|
12440 | };
|
12441 | DataEvent.prototype.getEventType = function () {
|
12442 | return this.eventType;
|
12443 | };
|
12444 | DataEvent.prototype.getEventRunner = function () {
|
12445 | return this.eventRegistration.getEventRunner(this);
|
12446 | };
|
12447 | DataEvent.prototype.toString = function () {
|
12448 | return (this.getPath().toString() +
|
12449 | ':' +
|
12450 | this.eventType +
|
12451 | ':' +
|
12452 | util.stringify(this.snapshot.exportVal()));
|
12453 | };
|
12454 | return DataEvent;
|
12455 | }());
|
12456 | var CancelEvent = (function () {
|
12457 | function CancelEvent(eventRegistration, error, path) {
|
12458 | this.eventRegistration = eventRegistration;
|
12459 | this.error = error;
|
12460 | this.path = path;
|
12461 | }
|
12462 | CancelEvent.prototype.getPath = function () {
|
12463 | return this.path;
|
12464 | };
|
12465 | CancelEvent.prototype.getEventType = function () {
|
12466 | return 'cancel';
|
12467 | };
|
12468 | CancelEvent.prototype.getEventRunner = function () {
|
12469 | return this.eventRegistration.getEventRunner(this);
|
12470 | };
|
12471 | CancelEvent.prototype.toString = function () {
|
12472 | return this.path.toString() + ':cancel';
|
12473 | };
|
12474 | return CancelEvent;
|
12475 | }());
|
12476 |
|
12477 |
|
12478 |
|
12479 |
|
12480 |
|
12481 |
|
12482 |
|
12483 |
|
12484 |
|
12485 |
|
12486 |
|
12487 |
|
12488 |
|
12489 |
|
12490 |
|
12491 |
|
12492 |
|
12493 |
|
12494 |
|
12495 |
|
12496 |
|
12497 |
|
12498 |
|
12499 | var CallbackContext = (function () {
|
12500 | function CallbackContext(snapshotCallback, cancelCallback) {
|
12501 | this.snapshotCallback = snapshotCallback;
|
12502 | this.cancelCallback = cancelCallback;
|
12503 | }
|
12504 | CallbackContext.prototype.onValue = function (expDataSnapshot, previousChildName) {
|
12505 | this.snapshotCallback.call(null, expDataSnapshot, previousChildName);
|
12506 | };
|
12507 | CallbackContext.prototype.onCancel = function (error) {
|
12508 | util.assert(this.hasCancelCallback, 'Raising a cancel event on a listener with no cancel callback');
|
12509 | return this.cancelCallback.call(null, error);
|
12510 | };
|
12511 | Object.defineProperty(CallbackContext.prototype, "hasCancelCallback", {
|
12512 | get: function () {
|
12513 | return !!this.cancelCallback;
|
12514 | },
|
12515 | enumerable: false,
|
12516 | configurable: true
|
12517 | });
|
12518 | CallbackContext.prototype.matches = function (other) {
|
12519 | return (this.snapshotCallback === other.snapshotCallback ||
|
12520 | (this.snapshotCallback.userCallback !== undefined &&
|
12521 | this.snapshotCallback.userCallback ===
|
12522 | other.snapshotCallback.userCallback &&
|
12523 | this.snapshotCallback.context === other.snapshotCallback.context));
|
12524 | };
|
12525 | return CallbackContext;
|
12526 | }());
|
12527 |
|
12528 |
|
12529 |
|
12530 |
|
12531 |
|
12532 |
|
12533 |
|
12534 |
|
12535 |
|
12536 |
|
12537 |
|
12538 |
|
12539 |
|
12540 |
|
12541 |
|
12542 |
|
12543 |
|
12544 |
|
12545 |
|
12546 |
|
12547 |
|
12548 |
|
12549 |
|
12550 |
|
12551 |
|
12552 |
|
12553 |
|
12554 |
|
12555 |
|
12556 |
|
12557 |
|
12558 |
|
12559 |
|
12560 |
|
12561 |
|
12562 |
|
12563 |
|
12564 | var OnDisconnect = (function () {
|
12565 |
|
12566 | function OnDisconnect(_repo, _path) {
|
12567 | this._repo = _repo;
|
12568 | this._path = _path;
|
12569 | }
|
12570 | |
12571 |
|
12572 |
|
12573 |
|
12574 |
|
12575 |
|
12576 |
|
12577 |
|
12578 |
|
12579 |
|
12580 | OnDisconnect.prototype.cancel = function () {
|
12581 | var deferred = new util.Deferred();
|
12582 | repoOnDisconnectCancel(this._repo, this._path, deferred.wrapCallback(function () { }));
|
12583 | return deferred.promise;
|
12584 | };
|
12585 | |
12586 |
|
12587 |
|
12588 |
|
12589 |
|
12590 |
|
12591 | OnDisconnect.prototype.remove = function () {
|
12592 | validateWritablePath('OnDisconnect.remove', this._path);
|
12593 | var deferred = new util.Deferred();
|
12594 | repoOnDisconnectSet(this._repo, this._path, null, deferred.wrapCallback(function () { }));
|
12595 | return deferred.promise;
|
12596 | };
|
12597 | |
12598 |
|
12599 |
|
12600 |
|
12601 |
|
12602 |
|
12603 |
|
12604 |
|
12605 |
|
12606 |
|
12607 |
|
12608 |
|
12609 |
|
12610 |
|
12611 |
|
12612 |
|
12613 |
|
12614 |
|
12615 |
|
12616 | OnDisconnect.prototype.set = function (value) {
|
12617 | validateWritablePath('OnDisconnect.set', this._path);
|
12618 | validateFirebaseDataArg('OnDisconnect.set', value, this._path, false);
|
12619 | var deferred = new util.Deferred();
|
12620 | repoOnDisconnectSet(this._repo, this._path, value, deferred.wrapCallback(function () { }));
|
12621 | return deferred.promise;
|
12622 | };
|
12623 | |
12624 |
|
12625 |
|
12626 |
|
12627 |
|
12628 |
|
12629 |
|
12630 |
|
12631 |
|
12632 |
|
12633 | OnDisconnect.prototype.setWithPriority = function (value, priority) {
|
12634 | validateWritablePath('OnDisconnect.setWithPriority', this._path);
|
12635 | validateFirebaseDataArg('OnDisconnect.setWithPriority', value, this._path, false);
|
12636 | validatePriority('OnDisconnect.setWithPriority', priority, false);
|
12637 | var deferred = new util.Deferred();
|
12638 | repoOnDisconnectSetWithPriority(this._repo, this._path, value, priority, deferred.wrapCallback(function () { }));
|
12639 | return deferred.promise;
|
12640 | };
|
12641 | |
12642 |
|
12643 |
|
12644 |
|
12645 |
|
12646 |
|
12647 |
|
12648 |
|
12649 |
|
12650 |
|
12651 |
|
12652 |
|
12653 |
|
12654 |
|
12655 |
|
12656 |
|
12657 | OnDisconnect.prototype.update = function (values) {
|
12658 | validateWritablePath('OnDisconnect.update', this._path);
|
12659 | validateFirebaseMergeDataArg('OnDisconnect.update', values, this._path, false);
|
12660 | var deferred = new util.Deferred();
|
12661 | repoOnDisconnectUpdate(this._repo, this._path, values, deferred.wrapCallback(function () { }));
|
12662 | return deferred.promise;
|
12663 | };
|
12664 | return OnDisconnect;
|
12665 | }());
|
12666 |
|
12667 |
|
12668 |
|
12669 |
|
12670 |
|
12671 |
|
12672 |
|
12673 |
|
12674 |
|
12675 |
|
12676 |
|
12677 |
|
12678 |
|
12679 |
|
12680 |
|
12681 |
|
12682 |
|
12683 |
|
12684 |
|
12685 |
|
12686 | var QueryImpl = (function () {
|
12687 | |
12688 |
|
12689 |
|
12690 | function QueryImpl(_repo, _path, _queryParams, _orderByCalled) {
|
12691 | this._repo = _repo;
|
12692 | this._path = _path;
|
12693 | this._queryParams = _queryParams;
|
12694 | this._orderByCalled = _orderByCalled;
|
12695 | }
|
12696 | Object.defineProperty(QueryImpl.prototype, "key", {
|
12697 | get: function () {
|
12698 | if (pathIsEmpty(this._path)) {
|
12699 | return null;
|
12700 | }
|
12701 | else {
|
12702 | return pathGetBack(this._path);
|
12703 | }
|
12704 | },
|
12705 | enumerable: false,
|
12706 | configurable: true
|
12707 | });
|
12708 | Object.defineProperty(QueryImpl.prototype, "ref", {
|
12709 | get: function () {
|
12710 | return new ReferenceImpl(this._repo, this._path);
|
12711 | },
|
12712 | enumerable: false,
|
12713 | configurable: true
|
12714 | });
|
12715 | Object.defineProperty(QueryImpl.prototype, "_queryIdentifier", {
|
12716 | get: function () {
|
12717 | var obj = queryParamsGetQueryObject(this._queryParams);
|
12718 | var id = ObjectToUniqueKey(obj);
|
12719 | return id === '{}' ? 'default' : id;
|
12720 | },
|
12721 | enumerable: false,
|
12722 | configurable: true
|
12723 | });
|
12724 | Object.defineProperty(QueryImpl.prototype, "_queryObject", {
|
12725 | |
12726 |
|
12727 |
|
12728 | get: function () {
|
12729 | return queryParamsGetQueryObject(this._queryParams);
|
12730 | },
|
12731 | enumerable: false,
|
12732 | configurable: true
|
12733 | });
|
12734 | QueryImpl.prototype.isEqual = function (other) {
|
12735 | other = util.getModularInstance(other);
|
12736 | if (!(other instanceof QueryImpl)) {
|
12737 | return false;
|
12738 | }
|
12739 | var sameRepo = this._repo === other._repo;
|
12740 | var samePath = pathEquals(this._path, other._path);
|
12741 | var sameQueryIdentifier = this._queryIdentifier === other._queryIdentifier;
|
12742 | return sameRepo && samePath && sameQueryIdentifier;
|
12743 | };
|
12744 | QueryImpl.prototype.toJSON = function () {
|
12745 | return this.toString();
|
12746 | };
|
12747 | QueryImpl.prototype.toString = function () {
|
12748 | return this._repo.toString() + pathToUrlEncodedString(this._path);
|
12749 | };
|
12750 | return QueryImpl;
|
12751 | }());
|
12752 |
|
12753 |
|
12754 |
|
12755 | function validateNoPreviousOrderByCall(query, fnName) {
|
12756 | if (query._orderByCalled === true) {
|
12757 | throw new Error(fnName + ": You can't combine multiple orderBy calls.");
|
12758 | }
|
12759 | }
|
12760 |
|
12761 |
|
12762 |
|
12763 | function validateQueryEndpoints(params) {
|
12764 | var startNode = null;
|
12765 | var endNode = null;
|
12766 | if (params.hasStart()) {
|
12767 | startNode = params.getIndexStartValue();
|
12768 | }
|
12769 | if (params.hasEnd()) {
|
12770 | endNode = params.getIndexEndValue();
|
12771 | }
|
12772 | if (params.getIndex() === KEY_INDEX) {
|
12773 | var tooManyArgsError = 'Query: When ordering by key, you may only pass one argument to ' +
|
12774 | 'startAt(), endAt(), or equalTo().';
|
12775 | var wrongArgTypeError = 'Query: When ordering by key, the argument passed to startAt(), startAfter(), ' +
|
12776 | 'endAt(), endBefore(), or equalTo() must be a string.';
|
12777 | if (params.hasStart()) {
|
12778 | var startName = params.getIndexStartName();
|
12779 | if (startName !== MIN_NAME) {
|
12780 | throw new Error(tooManyArgsError);
|
12781 | }
|
12782 | else if (typeof startNode !== 'string') {
|
12783 | throw new Error(wrongArgTypeError);
|
12784 | }
|
12785 | }
|
12786 | if (params.hasEnd()) {
|
12787 | var endName = params.getIndexEndName();
|
12788 | if (endName !== MAX_NAME) {
|
12789 | throw new Error(tooManyArgsError);
|
12790 | }
|
12791 | else if (typeof endNode !== 'string') {
|
12792 | throw new Error(wrongArgTypeError);
|
12793 | }
|
12794 | }
|
12795 | }
|
12796 | else if (params.getIndex() === PRIORITY_INDEX) {
|
12797 | if ((startNode != null && !isValidPriority(startNode)) ||
|
12798 | (endNode != null && !isValidPriority(endNode))) {
|
12799 | throw new Error('Query: When ordering by priority, the first argument passed to startAt(), ' +
|
12800 | 'startAfter() endAt(), endBefore(), or equalTo() must be a valid priority value ' +
|
12801 | '(null, a number, or a string).');
|
12802 | }
|
12803 | }
|
12804 | else {
|
12805 | util.assert(params.getIndex() instanceof PathIndex ||
|
12806 | params.getIndex() === VALUE_INDEX, 'unknown index type.');
|
12807 | if ((startNode != null && typeof startNode === 'object') ||
|
12808 | (endNode != null && typeof endNode === 'object')) {
|
12809 | throw new Error('Query: First argument passed to startAt(), startAfter(), endAt(), endBefore(), or ' +
|
12810 | 'equalTo() cannot be an object.');
|
12811 | }
|
12812 | }
|
12813 | }
|
12814 |
|
12815 |
|
12816 |
|
12817 | function validateLimit(params) {
|
12818 | if (params.hasStart() &&
|
12819 | params.hasEnd() &&
|
12820 | params.hasLimit() &&
|
12821 | !params.hasAnchoredLimit()) {
|
12822 | throw new Error("Query: Can't combine startAt(), startAfter(), endAt(), endBefore(), and limit(). Use " +
|
12823 | 'limitToFirst() or limitToLast() instead.');
|
12824 | }
|
12825 | }
|
12826 |
|
12827 |
|
12828 |
|
12829 | var ReferenceImpl = (function (_super) {
|
12830 | tslib.__extends(ReferenceImpl, _super);
|
12831 |
|
12832 | function ReferenceImpl(repo, path) {
|
12833 | return _super.call(this, repo, path, new QueryParams(), false) || this;
|
12834 | }
|
12835 | Object.defineProperty(ReferenceImpl.prototype, "parent", {
|
12836 | get: function () {
|
12837 | var parentPath = pathParent(this._path);
|
12838 | return parentPath === null
|
12839 | ? null
|
12840 | : new ReferenceImpl(this._repo, parentPath);
|
12841 | },
|
12842 | enumerable: false,
|
12843 | configurable: true
|
12844 | });
|
12845 | Object.defineProperty(ReferenceImpl.prototype, "root", {
|
12846 | get: function () {
|
12847 | var ref = this;
|
12848 | while (ref.parent !== null) {
|
12849 | ref = ref.parent;
|
12850 | }
|
12851 | return ref;
|
12852 | },
|
12853 | enumerable: false,
|
12854 | configurable: true
|
12855 | });
|
12856 | return ReferenceImpl;
|
12857 | }(QueryImpl));
|
12858 |
|
12859 |
|
12860 |
|
12861 |
|
12862 |
|
12863 |
|
12864 |
|
12865 |
|
12866 |
|
12867 |
|
12868 |
|
12869 |
|
12870 |
|
12871 |
|
12872 | var DataSnapshot = (function () {
|
12873 | |
12874 |
|
12875 |
|
12876 |
|
12877 |
|
12878 |
|
12879 | function DataSnapshot(_node,
|
12880 | /**
|
12881 | * The location of this DataSnapshot.
|
12882 | */
|
12883 | ref, _index) {
|
12884 | this._node = _node;
|
12885 | this.ref = ref;
|
12886 | this._index = _index;
|
12887 | }
|
12888 | Object.defineProperty(DataSnapshot.prototype, "priority", {
|
12889 | |
12890 |
|
12891 |
|
12892 |
|
12893 |
|
12894 |
|
12895 |
|
12896 |
|
12897 | get: function () {
|
12898 |
|
12899 | return this._node.getPriority().val();
|
12900 | },
|
12901 | enumerable: false,
|
12902 | configurable: true
|
12903 | });
|
12904 | Object.defineProperty(DataSnapshot.prototype, "key", {
|
12905 | |
12906 |
|
12907 |
|
12908 |
|
12909 |
|
12910 |
|
12911 |
|
12912 |
|
12913 |
|
12914 | get: function () {
|
12915 | return this.ref.key;
|
12916 | },
|
12917 | enumerable: false,
|
12918 | configurable: true
|
12919 | });
|
12920 | Object.defineProperty(DataSnapshot.prototype, "size", {
|
12921 |
|
12922 | get: function () {
|
12923 | return this._node.numChildren();
|
12924 | },
|
12925 | enumerable: false,
|
12926 | configurable: true
|
12927 | });
|
12928 | |
12929 |
|
12930 |
|
12931 |
|
12932 |
|
12933 |
|
12934 |
|
12935 |
|
12936 |
|
12937 |
|
12938 |
|
12939 |
|
12940 | DataSnapshot.prototype.child = function (path) {
|
12941 | var childPath = new Path(path);
|
12942 | var childRef = child(this.ref, path);
|
12943 | return new DataSnapshot(this._node.getChild(childPath), childRef, PRIORITY_INDEX);
|
12944 | };
|
12945 | |
12946 |
|
12947 |
|
12948 |
|
12949 | DataSnapshot.prototype.exists = function () {
|
12950 | return !this._node.isEmpty();
|
12951 | };
|
12952 | |
12953 |
|
12954 |
|
12955 |
|
12956 |
|
12957 |
|
12958 |
|
12959 |
|
12960 |
|
12961 |
|
12962 | DataSnapshot.prototype.exportVal = function () {
|
12963 | return this._node.val(true);
|
12964 | };
|
12965 | |
12966 |
|
12967 |
|
12968 |
|
12969 |
|
12970 |
|
12971 |
|
12972 |
|
12973 |
|
12974 |
|
12975 |
|
12976 |
|
12977 |
|
12978 |
|
12979 |
|
12980 |
|
12981 |
|
12982 |
|
12983 | DataSnapshot.prototype.forEach = function (action) {
|
12984 | var _this = this;
|
12985 | if (this._node.isLeafNode()) {
|
12986 | return false;
|
12987 | }
|
12988 | var childrenNode = this._node;
|
12989 |
|
12990 | return !!childrenNode.forEachChild(this._index, function (key, node) {
|
12991 | return action(new DataSnapshot(node, child(_this.ref, key), PRIORITY_INDEX));
|
12992 | });
|
12993 | };
|
12994 | |
12995 |
|
12996 |
|
12997 |
|
12998 |
|
12999 |
|
13000 |
|
13001 | DataSnapshot.prototype.hasChild = function (path) {
|
13002 | var childPath = new Path(path);
|
13003 | return !this._node.getChild(childPath).isEmpty();
|
13004 | };
|
13005 | |
13006 |
|
13007 |
|
13008 |
|
13009 |
|
13010 |
|
13011 |
|
13012 |
|
13013 |
|
13014 |
|
13015 |
|
13016 |
|
13017 | DataSnapshot.prototype.hasChildren = function () {
|
13018 | if (this._node.isLeafNode()) {
|
13019 | return false;
|
13020 | }
|
13021 | else {
|
13022 | return !this._node.isEmpty();
|
13023 | }
|
13024 | };
|
13025 | |
13026 |
|
13027 |
|
13028 | DataSnapshot.prototype.toJSON = function () {
|
13029 | return this.exportVal();
|
13030 | };
|
13031 | |
13032 |
|
13033 |
|
13034 |
|
13035 |
|
13036 |
|
13037 |
|
13038 |
|
13039 |
|
13040 |
|
13041 |
|
13042 |
|
13043 | DataSnapshot.prototype.val = function () {
|
13044 | return this._node.val();
|
13045 | };
|
13046 | return DataSnapshot;
|
13047 | }());
|
13048 |
|
13049 |
|
13050 |
|
13051 |
|
13052 |
|
13053 |
|
13054 |
|
13055 |
|
13056 |
|
13057 |
|
13058 |
|
13059 |
|
13060 |
|
13061 |
|
13062 | function ref(db, path) {
|
13063 | db = util.getModularInstance(db);
|
13064 | db._checkNotDeleted('ref');
|
13065 | return path !== undefined ? child(db._root, path) : db._root;
|
13066 | }
|
13067 |
|
13068 |
|
13069 |
|
13070 |
|
13071 |
|
13072 |
|
13073 |
|
13074 |
|
13075 |
|
13076 |
|
13077 |
|
13078 |
|
13079 |
|
13080 |
|
13081 |
|
13082 |
|
13083 | function refFromURL(db, url) {
|
13084 | db = util.getModularInstance(db);
|
13085 | db._checkNotDeleted('refFromURL');
|
13086 | var parsedURL = parseRepoInfo(url, db._repo.repoInfo_.nodeAdmin);
|
13087 | validateUrl('refFromURL', parsedURL);
|
13088 | var repoInfo = parsedURL.repoInfo;
|
13089 | if (!db._repo.repoInfo_.isCustomHost() &&
|
13090 | repoInfo.host !== db._repo.repoInfo_.host) {
|
13091 | fatal('refFromURL' +
|
13092 | ': Host name does not match the current database: ' +
|
13093 | '(found ' +
|
13094 | repoInfo.host +
|
13095 | ' but expected ' +
|
13096 | db._repo.repoInfo_.host +
|
13097 | ')');
|
13098 | }
|
13099 | return ref(db, parsedURL.path.toString());
|
13100 | }
|
13101 |
|
13102 |
|
13103 |
|
13104 |
|
13105 |
|
13106 |
|
13107 |
|
13108 |
|
13109 |
|
13110 |
|
13111 |
|
13112 | function child(parent, path) {
|
13113 | parent = util.getModularInstance(parent);
|
13114 | if (pathGetFront(parent._path) === null) {
|
13115 | validateRootPathString('child', 'path', path, false);
|
13116 | }
|
13117 | else {
|
13118 | validatePathString('child', 'path', path, false);
|
13119 | }
|
13120 | return new ReferenceImpl(parent._repo, pathChild(parent._path, path));
|
13121 | }
|
13122 |
|
13123 |
|
13124 |
|
13125 |
|
13126 |
|
13127 |
|
13128 |
|
13129 | function onDisconnect(ref) {
|
13130 | ref = util.getModularInstance(ref);
|
13131 | return new OnDisconnect(ref._repo, ref._path);
|
13132 | }
|
13133 |
|
13134 |
|
13135 |
|
13136 |
|
13137 |
|
13138 |
|
13139 |
|
13140 |
|
13141 |
|
13142 |
|
13143 |
|
13144 |
|
13145 |
|
13146 |
|
13147 |
|
13148 |
|
13149 |
|
13150 |
|
13151 |
|
13152 |
|
13153 |
|
13154 |
|
13155 |
|
13156 | function push(parent, value) {
|
13157 | parent = util.getModularInstance(parent);
|
13158 | validateWritablePath('push', parent._path);
|
13159 | validateFirebaseDataArg('push', value, parent._path, true);
|
13160 | var now = repoServerTime(parent._repo);
|
13161 | var name = nextPushId(now);
|
13162 |
|
13163 |
|
13164 |
|
13165 |
|
13166 |
|
13167 |
|
13168 | var thennablePushRef = child(parent, name);
|
13169 | var pushRef = child(parent, name);
|
13170 | var promise;
|
13171 | if (value != null) {
|
13172 | promise = set(pushRef, value).then(function () { return pushRef; });
|
13173 | }
|
13174 | else {
|
13175 | promise = Promise.resolve(pushRef);
|
13176 | }
|
13177 | thennablePushRef.then = promise.then.bind(promise);
|
13178 | thennablePushRef.catch = promise.then.bind(promise, undefined);
|
13179 | return thennablePushRef;
|
13180 | }
|
13181 |
|
13182 |
|
13183 |
|
13184 |
|
13185 |
|
13186 |
|
13187 |
|
13188 |
|
13189 |
|
13190 |
|
13191 |
|
13192 |
|
13193 |
|
13194 |
|
13195 | function remove(ref) {
|
13196 | validateWritablePath('remove', ref._path);
|
13197 | return set(ref, null);
|
13198 | }
|
13199 | /**
|
13200 | * Writes data to this Database location.
|
13201 | *
|
13202 | * This will overwrite any data at this location and all child locations.
|
13203 | *
|
13204 | * The effect of the write will be visible immediately, and the corresponding
|
13205 | * events ("value", "child_added", etc.) will be triggered. Synchronization of
|
13206 | * the data to the Firebase servers will also be started, and the returned
|
13207 | * Promise will resolve when complete. If provided, the `onComplete` callback
|
13208 | * will be called asynchronously after synchronization has finished.
|
13209 | *
|
13210 | * Passing `null` for the new value is equivalent to calling `remove()`; namely,
|
13211 | * all data at this location and all child locations will be deleted.
|
13212 | *
|
13213 | * `set()` will remove any priority stored at this location, so if priority is
|
13214 | * meant to be preserved, you need to use `setWithPriority()` instead.
|
13215 | *
|
13216 | * Note that modifying data with `set()` will cancel any pending transactions
|
13217 | * at that location, so extreme care should be taken if mixing `set()` and
|
13218 | * `transaction()` to modify the same data.
|
13219 | *
|
13220 | * A single `set()` will generate a single "value" event at the location where
|
13221 | * the `set()` was performed.
|
13222 | *
|
13223 | * @param ref - The location to write to.
|
13224 | * @param value - The value to be written (string, number, boolean, object,
|
13225 | * array, or null).
|
13226 | * @returns Resolves when write to server is complete.
|
13227 | */
|
13228 | function set(ref, value) {
|
13229 | ref = util.getModularInstance(ref);
|
13230 | validateWritablePath('set', ref._path);
|
13231 | validateFirebaseDataArg('set', value, ref._path, false);
|
13232 | var deferred = new util.Deferred();
|
13233 | repoSetWithPriority(ref._repo, ref._path, value,
|
13234 | null, deferred.wrapCallback(function () { }));
|
13235 | return deferred.promise;
|
13236 | }
|
13237 |
|
13238 |
|
13239 |
|
13240 |
|
13241 |
|
13242 |
|
13243 |
|
13244 |
|
13245 |
|
13246 |
|
13247 |
|
13248 |
|
13249 | function setPriority(ref, priority) {
|
13250 | ref = util.getModularInstance(ref);
|
13251 | validateWritablePath('setPriority', ref._path);
|
13252 | validatePriority('setPriority', priority, false);
|
13253 | var deferred = new util.Deferred();
|
13254 | repoSetWithPriority(ref._repo, pathChild(ref._path, '.priority'), priority, null, deferred.wrapCallback(function () { }));
|
13255 | return deferred.promise;
|
13256 | }
|
13257 |
|
13258 |
|
13259 |
|
13260 |
|
13261 |
|
13262 |
|
13263 |
|
13264 |
|
13265 |
|
13266 |
|
13267 |
|
13268 |
|
13269 |
|
13270 |
|
13271 |
|
13272 | function setWithPriority(ref, value, priority) {
|
13273 | validateWritablePath('setWithPriority', ref._path);
|
13274 | validateFirebaseDataArg('setWithPriority', value, ref._path, false);
|
13275 | validatePriority('setWithPriority', priority, false);
|
13276 | if (ref.key === '.length' || ref.key === '.keys') {
|
13277 | throw 'setWithPriority failed: ' + ref.key + ' is a read-only object.';
|
13278 | }
|
13279 | var deferred = new util.Deferred();
|
13280 | repoSetWithPriority(ref._repo, ref._path, value, priority, deferred.wrapCallback(function () { }));
|
13281 | return deferred.promise;
|
13282 | }
|
13283 |
|
13284 |
|
13285 |
|
13286 |
|
13287 |
|
13288 |
|
13289 |
|
13290 |
|
13291 |
|
13292 |
|
13293 |
|
13294 |
|
13295 |
|
13296 |
|
13297 |
|
13298 |
|
13299 |
|
13300 |
|
13301 |
|
13302 |
|
13303 |
|
13304 |
|
13305 |
|
13306 |
|
13307 |
|
13308 |
|
13309 |
|
13310 |
|
13311 |
|
13312 |
|
13313 |
|
13314 |
|
13315 |
|
13316 |
|
13317 |
|
13318 | function update(ref, values) {
|
13319 | validateFirebaseMergeDataArg('update', values, ref._path, false);
|
13320 | var deferred = new util.Deferred();
|
13321 | repoUpdate(ref._repo, ref._path, values, deferred.wrapCallback(function () { }));
|
13322 | return deferred.promise;
|
13323 | }
|
13324 |
|
13325 |
|
13326 |
|
13327 |
|
13328 |
|
13329 |
|
13330 |
|
13331 |
|
13332 | function get(query) {
|
13333 | query = util.getModularInstance(query);
|
13334 | return repoGetValue(query._repo, query).then(function (node) {
|
13335 | return new DataSnapshot(node, new ReferenceImpl(query._repo, query._path), query._queryParams.getIndex());
|
13336 | });
|
13337 | }
|
13338 |
|
13339 |
|
13340 |
|
13341 | var ValueEventRegistration = (function () {
|
13342 | function ValueEventRegistration(callbackContext) {
|
13343 | this.callbackContext = callbackContext;
|
13344 | }
|
13345 | ValueEventRegistration.prototype.respondsTo = function (eventType) {
|
13346 | return eventType === 'value';
|
13347 | };
|
13348 | ValueEventRegistration.prototype.createEvent = function (change, query) {
|
13349 | var index = query._queryParams.getIndex();
|
13350 | return new DataEvent('value', this, new DataSnapshot(change.snapshotNode, new ReferenceImpl(query._repo, query._path), index));
|
13351 | };
|
13352 | ValueEventRegistration.prototype.getEventRunner = function (eventData) {
|
13353 | var _this = this;
|
13354 | if (eventData.getEventType() === 'cancel') {
|
13355 | return function () {
|
13356 | return _this.callbackContext.onCancel(eventData.error);
|
13357 | };
|
13358 | }
|
13359 | else {
|
13360 | return function () {
|
13361 | return _this.callbackContext.onValue(eventData.snapshot, null);
|
13362 | };
|
13363 | }
|
13364 | };
|
13365 | ValueEventRegistration.prototype.createCancelEvent = function (error, path) {
|
13366 | if (this.callbackContext.hasCancelCallback) {
|
13367 | return new CancelEvent(this, error, path);
|
13368 | }
|
13369 | else {
|
13370 | return null;
|
13371 | }
|
13372 | };
|
13373 | ValueEventRegistration.prototype.matches = function (other) {
|
13374 | if (!(other instanceof ValueEventRegistration)) {
|
13375 | return false;
|
13376 | }
|
13377 | else if (!other.callbackContext || !this.callbackContext) {
|
13378 |
|
13379 | return true;
|
13380 | }
|
13381 | else {
|
13382 | return other.callbackContext.matches(this.callbackContext);
|
13383 | }
|
13384 | };
|
13385 | ValueEventRegistration.prototype.hasAnyCallback = function () {
|
13386 | return this.callbackContext !== null;
|
13387 | };
|
13388 | return ValueEventRegistration;
|
13389 | }());
|
13390 |
|
13391 |
|
13392 |
|
13393 | var ChildEventRegistration = (function () {
|
13394 | function ChildEventRegistration(eventType, callbackContext) {
|
13395 | this.eventType = eventType;
|
13396 | this.callbackContext = callbackContext;
|
13397 | }
|
13398 | ChildEventRegistration.prototype.respondsTo = function (eventType) {
|
13399 | var eventToCheck = eventType === 'children_added' ? 'child_added' : eventType;
|
13400 | eventToCheck =
|
13401 | eventToCheck === 'children_removed' ? 'child_removed' : eventToCheck;
|
13402 | return this.eventType === eventToCheck;
|
13403 | };
|
13404 | ChildEventRegistration.prototype.createCancelEvent = function (error, path) {
|
13405 | if (this.callbackContext.hasCancelCallback) {
|
13406 | return new CancelEvent(this, error, path);
|
13407 | }
|
13408 | else {
|
13409 | return null;
|
13410 | }
|
13411 | };
|
13412 | ChildEventRegistration.prototype.createEvent = function (change, query) {
|
13413 | util.assert(change.childName != null, 'Child events should have a childName.');
|
13414 | var childRef = child(new ReferenceImpl(query._repo, query._path), change.childName);
|
13415 | var index = query._queryParams.getIndex();
|
13416 | return new DataEvent(change.type, this, new DataSnapshot(change.snapshotNode, childRef, index), change.prevName);
|
13417 | };
|
13418 | ChildEventRegistration.prototype.getEventRunner = function (eventData) {
|
13419 | var _this = this;
|
13420 | if (eventData.getEventType() === 'cancel') {
|
13421 | return function () {
|
13422 | return _this.callbackContext.onCancel(eventData.error);
|
13423 | };
|
13424 | }
|
13425 | else {
|
13426 | return function () {
|
13427 | return _this.callbackContext.onValue(eventData.snapshot, eventData.prevName);
|
13428 | };
|
13429 | }
|
13430 | };
|
13431 | ChildEventRegistration.prototype.matches = function (other) {
|
13432 | if (other instanceof ChildEventRegistration) {
|
13433 | return (this.eventType === other.eventType &&
|
13434 | (!this.callbackContext ||
|
13435 | !other.callbackContext ||
|
13436 | this.callbackContext.matches(other.callbackContext)));
|
13437 | }
|
13438 | return false;
|
13439 | };
|
13440 | ChildEventRegistration.prototype.hasAnyCallback = function () {
|
13441 | return !!this.callbackContext;
|
13442 | };
|
13443 | return ChildEventRegistration;
|
13444 | }());
|
13445 | function addEventListener(query, eventType, callback, cancelCallbackOrListenOptions, options) {
|
13446 | var cancelCallback;
|
13447 | if (typeof cancelCallbackOrListenOptions === 'object') {
|
13448 | cancelCallback = undefined;
|
13449 | options = cancelCallbackOrListenOptions;
|
13450 | }
|
13451 | if (typeof cancelCallbackOrListenOptions === 'function') {
|
13452 | cancelCallback = cancelCallbackOrListenOptions;
|
13453 | }
|
13454 | if (options && options.onlyOnce) {
|
13455 | var userCallback_1 = callback;
|
13456 | var onceCallback = function (dataSnapshot, previousChildName) {
|
13457 | repoRemoveEventCallbackForQuery(query._repo, query, container);
|
13458 | userCallback_1(dataSnapshot, previousChildName);
|
13459 | };
|
13460 | onceCallback.userCallback = callback.userCallback;
|
13461 | onceCallback.context = callback.context;
|
13462 | callback = onceCallback;
|
13463 | }
|
13464 | var callbackContext = new CallbackContext(callback, cancelCallback || undefined);
|
13465 | var container = eventType === 'value'
|
13466 | ? new ValueEventRegistration(callbackContext)
|
13467 | : new ChildEventRegistration(eventType, callbackContext);
|
13468 | repoAddEventCallbackForQuery(query._repo, query, container);
|
13469 | return function () { return repoRemoveEventCallbackForQuery(query._repo, query, container); };
|
13470 | }
|
13471 | function onValue(query, callback, cancelCallbackOrListenOptions, options) {
|
13472 | return addEventListener(query, 'value', callback, cancelCallbackOrListenOptions, options);
|
13473 | }
|
13474 | function onChildAdded(query, callback, cancelCallbackOrListenOptions, options) {
|
13475 | return addEventListener(query, 'child_added', callback, cancelCallbackOrListenOptions, options);
|
13476 | }
|
13477 | function onChildChanged(query, callback, cancelCallbackOrListenOptions, options) {
|
13478 | return addEventListener(query, 'child_changed', callback, cancelCallbackOrListenOptions, options);
|
13479 | }
|
13480 | function onChildMoved(query, callback, cancelCallbackOrListenOptions, options) {
|
13481 | return addEventListener(query, 'child_moved', callback, cancelCallbackOrListenOptions, options);
|
13482 | }
|
13483 | function onChildRemoved(query, callback, cancelCallbackOrListenOptions, options) {
|
13484 | return addEventListener(query, 'child_removed', callback, cancelCallbackOrListenOptions, options);
|
13485 | }
|
13486 |
|
13487 |
|
13488 |
|
13489 |
|
13490 |
|
13491 |
|
13492 |
|
13493 |
|
13494 |
|
13495 |
|
13496 |
|
13497 |
|
13498 |
|
13499 |
|
13500 |
|
13501 |
|
13502 |
|
13503 |
|
13504 |
|
13505 |
|
13506 |
|
13507 |
|
13508 |
|
13509 |
|
13510 | function off(query, eventType, callback) {
|
13511 | var container = null;
|
13512 | var expCallback = callback ? new CallbackContext(callback) : null;
|
13513 | if (eventType === 'value') {
|
13514 | container = new ValueEventRegistration(expCallback);
|
13515 | }
|
13516 | else if (eventType) {
|
13517 | container = new ChildEventRegistration(eventType, expCallback);
|
13518 | }
|
13519 | repoRemoveEventCallbackForQuery(query._repo, query, container);
|
13520 | }
|
13521 |
|
13522 |
|
13523 |
|
13524 |
|
13525 |
|
13526 |
|
13527 |
|
13528 |
|
13529 |
|
13530 |
|
13531 | var QueryConstraint = (function () {
|
13532 | function QueryConstraint() {
|
13533 | }
|
13534 | return QueryConstraint;
|
13535 | }());
|
13536 | var QueryEndAtConstraint = (function (_super) {
|
13537 | tslib.__extends(QueryEndAtConstraint, _super);
|
13538 | function QueryEndAtConstraint(_value, _key) {
|
13539 | var _this = _super.call(this) || this;
|
13540 | _this._value = _value;
|
13541 | _this._key = _key;
|
13542 | return _this;
|
13543 | }
|
13544 | QueryEndAtConstraint.prototype._apply = function (query) {
|
13545 | validateFirebaseDataArg('endAt', this._value, query._path, true);
|
13546 | var newParams = queryParamsEndAt(query._queryParams, this._value, this._key);
|
13547 | validateLimit(newParams);
|
13548 | validateQueryEndpoints(newParams);
|
13549 | if (query._queryParams.hasEnd()) {
|
13550 | throw new Error('endAt: Starting point was already set (by another call to endAt, ' +
|
13551 | 'endBefore or equalTo).');
|
13552 | }
|
13553 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13554 | };
|
13555 | return QueryEndAtConstraint;
|
13556 | }(QueryConstraint));
|
13557 |
|
13558 |
|
13559 |
|
13560 |
|
13561 |
|
13562 |
|
13563 |
|
13564 |
|
13565 |
|
13566 |
|
13567 |
|
13568 |
|
13569 |
|
13570 |
|
13571 |
|
13572 |
|
13573 |
|
13574 |
|
13575 |
|
13576 |
|
13577 |
|
13578 |
|
13579 |
|
13580 | function endAt(value, key) {
|
13581 | validateKey('endAt', 'key', key, true);
|
13582 | return new QueryEndAtConstraint(value, key);
|
13583 | }
|
13584 | var QueryEndBeforeConstraint = (function (_super) {
|
13585 | tslib.__extends(QueryEndBeforeConstraint, _super);
|
13586 | function QueryEndBeforeConstraint(_value, _key) {
|
13587 | var _this = _super.call(this) || this;
|
13588 | _this._value = _value;
|
13589 | _this._key = _key;
|
13590 | return _this;
|
13591 | }
|
13592 | QueryEndBeforeConstraint.prototype._apply = function (query) {
|
13593 | validateFirebaseDataArg('endBefore', this._value, query._path, false);
|
13594 | var newParams = queryParamsEndBefore(query._queryParams, this._value, this._key);
|
13595 | validateLimit(newParams);
|
13596 | validateQueryEndpoints(newParams);
|
13597 | if (query._queryParams.hasEnd()) {
|
13598 | throw new Error('endBefore: Starting point was already set (by another call to endAt, ' +
|
13599 | 'endBefore or equalTo).');
|
13600 | }
|
13601 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13602 | };
|
13603 | return QueryEndBeforeConstraint;
|
13604 | }(QueryConstraint));
|
13605 |
|
13606 |
|
13607 |
|
13608 |
|
13609 |
|
13610 |
|
13611 |
|
13612 |
|
13613 |
|
13614 |
|
13615 |
|
13616 |
|
13617 |
|
13618 |
|
13619 |
|
13620 |
|
13621 |
|
13622 |
|
13623 |
|
13624 | function endBefore(value, key) {
|
13625 | validateKey('endBefore', 'key', key, true);
|
13626 | return new QueryEndBeforeConstraint(value, key);
|
13627 | }
|
13628 | var QueryStartAtConstraint = (function (_super) {
|
13629 | tslib.__extends(QueryStartAtConstraint, _super);
|
13630 | function QueryStartAtConstraint(_value, _key) {
|
13631 | var _this = _super.call(this) || this;
|
13632 | _this._value = _value;
|
13633 | _this._key = _key;
|
13634 | return _this;
|
13635 | }
|
13636 | QueryStartAtConstraint.prototype._apply = function (query) {
|
13637 | validateFirebaseDataArg('startAt', this._value, query._path, true);
|
13638 | var newParams = queryParamsStartAt(query._queryParams, this._value, this._key);
|
13639 | validateLimit(newParams);
|
13640 | validateQueryEndpoints(newParams);
|
13641 | if (query._queryParams.hasStart()) {
|
13642 | throw new Error('startAt: Starting point was already set (by another call to startAt, ' +
|
13643 | 'startBefore or equalTo).');
|
13644 | }
|
13645 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13646 | };
|
13647 | return QueryStartAtConstraint;
|
13648 | }(QueryConstraint));
|
13649 |
|
13650 |
|
13651 |
|
13652 |
|
13653 |
|
13654 |
|
13655 |
|
13656 |
|
13657 |
|
13658 |
|
13659 |
|
13660 |
|
13661 |
|
13662 |
|
13663 |
|
13664 |
|
13665 |
|
13666 |
|
13667 |
|
13668 |
|
13669 |
|
13670 |
|
13671 | function startAt(value, key) {
|
13672 | if (value === void 0) { value = null; }
|
13673 | validateKey('startAt', 'key', key, true);
|
13674 | return new QueryStartAtConstraint(value, key);
|
13675 | }
|
13676 | var QueryStartAfterConstraint = (function (_super) {
|
13677 | tslib.__extends(QueryStartAfterConstraint, _super);
|
13678 | function QueryStartAfterConstraint(_value, _key) {
|
13679 | var _this = _super.call(this) || this;
|
13680 | _this._value = _value;
|
13681 | _this._key = _key;
|
13682 | return _this;
|
13683 | }
|
13684 | QueryStartAfterConstraint.prototype._apply = function (query) {
|
13685 | validateFirebaseDataArg('startAfter', this._value, query._path, false);
|
13686 | var newParams = queryParamsStartAfter(query._queryParams, this._value, this._key);
|
13687 | validateLimit(newParams);
|
13688 | validateQueryEndpoints(newParams);
|
13689 | if (query._queryParams.hasStart()) {
|
13690 | throw new Error('startAfter: Starting point was already set (by another call to startAt, ' +
|
13691 | 'startAfter, or equalTo).');
|
13692 | }
|
13693 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13694 | };
|
13695 | return QueryStartAfterConstraint;
|
13696 | }(QueryConstraint));
|
13697 |
|
13698 |
|
13699 |
|
13700 |
|
13701 |
|
13702 |
|
13703 |
|
13704 |
|
13705 |
|
13706 |
|
13707 |
|
13708 |
|
13709 |
|
13710 |
|
13711 |
|
13712 |
|
13713 |
|
13714 |
|
13715 | function startAfter(value, key) {
|
13716 | validateKey('startAfter', 'key', key, true);
|
13717 | return new QueryStartAfterConstraint(value, key);
|
13718 | }
|
13719 | var QueryLimitToFirstConstraint = (function (_super) {
|
13720 | tslib.__extends(QueryLimitToFirstConstraint, _super);
|
13721 | function QueryLimitToFirstConstraint(_limit) {
|
13722 | var _this = _super.call(this) || this;
|
13723 | _this._limit = _limit;
|
13724 | return _this;
|
13725 | }
|
13726 | QueryLimitToFirstConstraint.prototype._apply = function (query) {
|
13727 | if (query._queryParams.hasLimit()) {
|
13728 | throw new Error('limitToFirst: Limit was already set (by another call to limitToFirst ' +
|
13729 | 'or limitToLast).');
|
13730 | }
|
13731 | return new QueryImpl(query._repo, query._path, queryParamsLimitToFirst(query._queryParams, this._limit), query._orderByCalled);
|
13732 | };
|
13733 | return QueryLimitToFirstConstraint;
|
13734 | }(QueryConstraint));
|
13735 |
|
13736 |
|
13737 |
|
13738 |
|
13739 |
|
13740 |
|
13741 |
|
13742 |
|
13743 |
|
13744 |
|
13745 |
|
13746 |
|
13747 |
|
13748 |
|
13749 |
|
13750 |
|
13751 |
|
13752 |
|
13753 | function limitToFirst(limit) {
|
13754 | if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
|
13755 | throw new Error('limitToFirst: First argument must be a positive integer.');
|
13756 | }
|
13757 | return new QueryLimitToFirstConstraint(limit);
|
13758 | }
|
13759 | var QueryLimitToLastConstraint = (function (_super) {
|
13760 | tslib.__extends(QueryLimitToLastConstraint, _super);
|
13761 | function QueryLimitToLastConstraint(_limit) {
|
13762 | var _this = _super.call(this) || this;
|
13763 | _this._limit = _limit;
|
13764 | return _this;
|
13765 | }
|
13766 | QueryLimitToLastConstraint.prototype._apply = function (query) {
|
13767 | if (query._queryParams.hasLimit()) {
|
13768 | throw new Error('limitToLast: Limit was already set (by another call to limitToFirst ' +
|
13769 | 'or limitToLast).');
|
13770 | }
|
13771 | return new QueryImpl(query._repo, query._path, queryParamsLimitToLast(query._queryParams, this._limit), query._orderByCalled);
|
13772 | };
|
13773 | return QueryLimitToLastConstraint;
|
13774 | }(QueryConstraint));
|
13775 |
|
13776 |
|
13777 |
|
13778 |
|
13779 |
|
13780 |
|
13781 |
|
13782 |
|
13783 |
|
13784 |
|
13785 |
|
13786 |
|
13787 |
|
13788 |
|
13789 |
|
13790 |
|
13791 |
|
13792 |
|
13793 | function limitToLast(limit) {
|
13794 | if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
|
13795 | throw new Error('limitToLast: First argument must be a positive integer.');
|
13796 | }
|
13797 | return new QueryLimitToLastConstraint(limit);
|
13798 | }
|
13799 | var QueryOrderByChildConstraint = (function (_super) {
|
13800 | tslib.__extends(QueryOrderByChildConstraint, _super);
|
13801 | function QueryOrderByChildConstraint(_path) {
|
13802 | var _this = _super.call(this) || this;
|
13803 | _this._path = _path;
|
13804 | return _this;
|
13805 | }
|
13806 | QueryOrderByChildConstraint.prototype._apply = function (query) {
|
13807 | validateNoPreviousOrderByCall(query, 'orderByChild');
|
13808 | var parsedPath = new Path(this._path);
|
13809 | if (pathIsEmpty(parsedPath)) {
|
13810 | throw new Error('orderByChild: cannot pass in empty path. Use orderByValue() instead.');
|
13811 | }
|
13812 | var index = new PathIndex(parsedPath);
|
13813 | var newParams = queryParamsOrderBy(query._queryParams, index);
|
13814 | validateQueryEndpoints(newParams);
|
13815 | return new QueryImpl(query._repo, query._path, newParams,
|
13816 | true);
|
13817 | };
|
13818 | return QueryOrderByChildConstraint;
|
13819 | }(QueryConstraint));
|
13820 |
|
13821 |
|
13822 |
|
13823 |
|
13824 |
|
13825 |
|
13826 |
|
13827 |
|
13828 |
|
13829 |
|
13830 |
|
13831 |
|
13832 |
|
13833 |
|
13834 |
|
13835 |
|
13836 |
|
13837 | function orderByChild(path) {
|
13838 | if (path === '$key') {
|
13839 | throw new Error('orderByChild: "$key" is invalid. Use orderByKey() instead.');
|
13840 | }
|
13841 | else if (path === '$priority') {
|
13842 | throw new Error('orderByChild: "$priority" is invalid. Use orderByPriority() instead.');
|
13843 | }
|
13844 | else if (path === '$value') {
|
13845 | throw new Error('orderByChild: "$value" is invalid. Use orderByValue() instead.');
|
13846 | }
|
13847 | validatePathString('orderByChild', 'path', path, false);
|
13848 | return new QueryOrderByChildConstraint(path);
|
13849 | }
|
13850 | var QueryOrderByKeyConstraint = (function (_super) {
|
13851 | tslib.__extends(QueryOrderByKeyConstraint, _super);
|
13852 | function QueryOrderByKeyConstraint() {
|
13853 | return _super !== null && _super.apply(this, arguments) || this;
|
13854 | }
|
13855 | QueryOrderByKeyConstraint.prototype._apply = function (query) {
|
13856 | validateNoPreviousOrderByCall(query, 'orderByKey');
|
13857 | var newParams = queryParamsOrderBy(query._queryParams, KEY_INDEX);
|
13858 | validateQueryEndpoints(newParams);
|
13859 | return new QueryImpl(query._repo, query._path, newParams,
|
13860 | true);
|
13861 | };
|
13862 | return QueryOrderByKeyConstraint;
|
13863 | }(QueryConstraint));
|
13864 |
|
13865 |
|
13866 |
|
13867 |
|
13868 |
|
13869 |
|
13870 |
|
13871 |
|
13872 | function orderByKey() {
|
13873 | return new QueryOrderByKeyConstraint();
|
13874 | }
|
13875 | var QueryOrderByPriorityConstraint = (function (_super) {
|
13876 | tslib.__extends(QueryOrderByPriorityConstraint, _super);
|
13877 | function QueryOrderByPriorityConstraint() {
|
13878 | return _super !== null && _super.apply(this, arguments) || this;
|
13879 | }
|
13880 | QueryOrderByPriorityConstraint.prototype._apply = function (query) {
|
13881 | validateNoPreviousOrderByCall(query, 'orderByPriority');
|
13882 | var newParams = queryParamsOrderBy(query._queryParams, PRIORITY_INDEX);
|
13883 | validateQueryEndpoints(newParams);
|
13884 | return new QueryImpl(query._repo, query._path, newParams,
|
13885 | true);
|
13886 | };
|
13887 | return QueryOrderByPriorityConstraint;
|
13888 | }(QueryConstraint));
|
13889 |
|
13890 |
|
13891 |
|
13892 |
|
13893 |
|
13894 |
|
13895 |
|
13896 |
|
13897 | function orderByPriority() {
|
13898 | return new QueryOrderByPriorityConstraint();
|
13899 | }
|
13900 | var QueryOrderByValueConstraint = (function (_super) {
|
13901 | tslib.__extends(QueryOrderByValueConstraint, _super);
|
13902 | function QueryOrderByValueConstraint() {
|
13903 | return _super !== null && _super.apply(this, arguments) || this;
|
13904 | }
|
13905 | QueryOrderByValueConstraint.prototype._apply = function (query) {
|
13906 | validateNoPreviousOrderByCall(query, 'orderByValue');
|
13907 | var newParams = queryParamsOrderBy(query._queryParams, VALUE_INDEX);
|
13908 | validateQueryEndpoints(newParams);
|
13909 | return new QueryImpl(query._repo, query._path, newParams,
|
13910 | true);
|
13911 | };
|
13912 | return QueryOrderByValueConstraint;
|
13913 | }(QueryConstraint));
|
13914 |
|
13915 |
|
13916 |
|
13917 |
|
13918 |
|
13919 |
|
13920 |
|
13921 |
|
13922 |
|
13923 | function orderByValue() {
|
13924 | return new QueryOrderByValueConstraint();
|
13925 | }
|
13926 | var QueryEqualToValueConstraint = (function (_super) {
|
13927 | tslib.__extends(QueryEqualToValueConstraint, _super);
|
13928 | function QueryEqualToValueConstraint(_value, _key) {
|
13929 | var _this = _super.call(this) || this;
|
13930 | _this._value = _value;
|
13931 | _this._key = _key;
|
13932 | return _this;
|
13933 | }
|
13934 | QueryEqualToValueConstraint.prototype._apply = function (query) {
|
13935 | validateFirebaseDataArg('equalTo', this._value, query._path, false);
|
13936 | if (query._queryParams.hasStart()) {
|
13937 | throw new Error('equalTo: Starting point was already set (by another call to startAt/startAfter or ' +
|
13938 | 'equalTo).');
|
13939 | }
|
13940 | if (query._queryParams.hasEnd()) {
|
13941 | throw new Error('equalTo: Ending point was already set (by another call to endAt/endBefore or ' +
|
13942 | 'equalTo).');
|
13943 | }
|
13944 | return new QueryEndAtConstraint(this._value, this._key)._apply(new QueryStartAtConstraint(this._value, this._key)._apply(query));
|
13945 | };
|
13946 | return QueryEqualToValueConstraint;
|
13947 | }(QueryConstraint));
|
13948 |
|
13949 |
|
13950 |
|
13951 |
|
13952 |
|
13953 |
|
13954 |
|
13955 |
|
13956 |
|
13957 |
|
13958 |
|
13959 |
|
13960 |
|
13961 |
|
13962 |
|
13963 |
|
13964 |
|
13965 |
|
13966 |
|
13967 |
|
13968 |
|
13969 |
|
13970 |
|
13971 | function equalTo(value, key) {
|
13972 | validateKey('equalTo', 'key', key, true);
|
13973 | return new QueryEqualToValueConstraint(value, key);
|
13974 | }
|
13975 |
|
13976 |
|
13977 |
|
13978 |
|
13979 |
|
13980 |
|
13981 |
|
13982 |
|
13983 |
|
13984 | function query(query) {
|
13985 | var e_1, _a;
|
13986 | var queryConstraints = [];
|
13987 | for (var _i = 1; _i < arguments.length; _i++) {
|
13988 | queryConstraints[_i - 1] = arguments[_i];
|
13989 | }
|
13990 | var queryImpl = util.getModularInstance(query);
|
13991 | try {
|
13992 | for (var queryConstraints_1 = tslib.__values(queryConstraints), queryConstraints_1_1 = queryConstraints_1.next(); !queryConstraints_1_1.done; queryConstraints_1_1 = queryConstraints_1.next()) {
|
13993 | var constraint = queryConstraints_1_1.value;
|
13994 | queryImpl = constraint._apply(queryImpl);
|
13995 | }
|
13996 | }
|
13997 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
13998 | finally {
|
13999 | try {
|
14000 | if (queryConstraints_1_1 && !queryConstraints_1_1.done && (_a = queryConstraints_1.return)) _a.call(queryConstraints_1);
|
14001 | }
|
14002 | finally { if (e_1) throw e_1.error; }
|
14003 | }
|
14004 | return queryImpl;
|
14005 | }
|
14006 |
|
14007 |
|
14008 |
|
14009 |
|
14010 |
|
14011 |
|
14012 | syncPointSetReferenceConstructor(ReferenceImpl);
|
14013 | syncTreeSetReferenceConstructor(ReferenceImpl);
|
14014 |
|
14015 |
|
14016 |
|
14017 |
|
14018 |
|
14019 |
|
14020 |
|
14021 |
|
14022 |
|
14023 |
|
14024 |
|
14025 |
|
14026 |
|
14027 |
|
14028 |
|
14029 |
|
14030 |
|
14031 |
|
14032 |
|
14033 |
|
14034 |
|
14035 |
|
14036 |
|
14037 |
|
14038 |
|
14039 | var FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
|
14040 |
|
14041 |
|
14042 |
|
14043 | var repos = {};
|
14044 |
|
14045 |
|
14046 |
|
14047 | var useRestClient = false;
|
14048 |
|
14049 |
|
14050 |
|
14051 | function repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider) {
|
14052 | repo.repoInfo_ = new RepoInfo(host + ":" + port,
|
14053 | false, repo.repoInfo_.namespace, repo.repoInfo_.webSocketOnly, repo.repoInfo_.nodeAdmin, repo.repoInfo_.persistenceKey, repo.repoInfo_.includeNamespaceInQueryParams);
|
14054 | if (tokenProvider) {
|
14055 | repo.authTokenProvider_ = tokenProvider;
|
14056 | }
|
14057 | }
|
14058 |
|
14059 |
|
14060 |
|
14061 |
|
14062 | function repoManagerDatabaseFromApp(app, authProvider, appCheckProvider, url, nodeAdmin) {
|
14063 | var dbUrl = url || app.options.databaseURL;
|
14064 | if (dbUrl === undefined) {
|
14065 | if (!app.options.projectId) {
|
14066 | fatal("Can't determine Firebase Database URL. Be sure to include " +
|
14067 | ' a Project ID when calling firebase.initializeApp().');
|
14068 | }
|
14069 | log('Using default host for project ', app.options.projectId);
|
14070 | dbUrl = app.options.projectId + "-default-rtdb.firebaseio.com";
|
14071 | }
|
14072 | var parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
|
14073 | var repoInfo = parsedUrl.repoInfo;
|
14074 | var isEmulator;
|
14075 | var dbEmulatorHost = undefined;
|
14076 | if (typeof process !== 'undefined' && process.env) {
|
14077 | dbEmulatorHost = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
|
14078 | }
|
14079 | if (dbEmulatorHost) {
|
14080 | isEmulator = true;
|
14081 | dbUrl = "http://" + dbEmulatorHost + "?ns=" + repoInfo.namespace;
|
14082 | parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
|
14083 | repoInfo = parsedUrl.repoInfo;
|
14084 | }
|
14085 | else {
|
14086 | isEmulator = !parsedUrl.repoInfo.secure;
|
14087 | }
|
14088 | var authTokenProvider = nodeAdmin && isEmulator
|
14089 | ? new EmulatorTokenProvider(EmulatorTokenProvider.OWNER)
|
14090 | : new FirebaseAuthTokenProvider(app.name, app.options, authProvider);
|
14091 | validateUrl('Invalid Firebase Database URL', parsedUrl);
|
14092 | if (!pathIsEmpty(parsedUrl.path)) {
|
14093 | fatal('Database URL must point to the root of a Firebase Database ' +
|
14094 | '(not including a child path).');
|
14095 | }
|
14096 | var repo = repoManagerCreateRepo(repoInfo, app, authTokenProvider, new AppCheckTokenProvider(app.name, appCheckProvider));
|
14097 | return new Database(repo, app);
|
14098 | }
|
14099 |
|
14100 |
|
14101 |
|
14102 |
|
14103 | function repoManagerDeleteRepo(repo, appName) {
|
14104 | var appRepos = repos[appName];
|
14105 |
|
14106 | if (!appRepos || appRepos[repo.key] !== repo) {
|
14107 | fatal("Database " + appName + "(" + repo.repoInfo_ + ") has already been deleted.");
|
14108 | }
|
14109 | repoInterrupt(repo);
|
14110 | delete appRepos[repo.key];
|
14111 | }
|
14112 |
|
14113 |
|
14114 |
|
14115 |
|
14116 |
|
14117 |
|
14118 |
|
14119 | function repoManagerCreateRepo(repoInfo, app, authTokenProvider, appCheckProvider) {
|
14120 | var appRepos = repos[app.name];
|
14121 | if (!appRepos) {
|
14122 | appRepos = {};
|
14123 | repos[app.name] = appRepos;
|
14124 | }
|
14125 | var repo = appRepos[repoInfo.toURLString()];
|
14126 | if (repo) {
|
14127 | fatal('Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.');
|
14128 | }
|
14129 | repo = new Repo(repoInfo, useRestClient, authTokenProvider, appCheckProvider);
|
14130 | appRepos[repoInfo.toURLString()] = repo;
|
14131 | return repo;
|
14132 | }
|
14133 |
|
14134 |
|
14135 |
|
14136 | function repoManagerForceRestClient(forceRestClient) {
|
14137 | useRestClient = forceRestClient;
|
14138 | }
|
14139 |
|
14140 |
|
14141 |
|
14142 | var Database = (function () {
|
14143 |
|
14144 | function Database(_repoInternal,
|
14145 | /** The {@link @firebase/app#FirebaseApp} associated with this Realtime Database instance. */
|
14146 | app) {
|
14147 | this._repoInternal = _repoInternal;
|
14148 | this.app = app;
|
14149 |
|
14150 | this['type'] = 'database';
|
14151 |
|
14152 | this._instanceStarted = false;
|
14153 | }
|
14154 | Object.defineProperty(Database.prototype, "_repo", {
|
14155 | get: function () {
|
14156 | if (!this._instanceStarted) {
|
14157 | repoStart(this._repoInternal, this.app.options.appId, this.app.options['databaseAuthVariableOverride']);
|
14158 | this._instanceStarted = true;
|
14159 | }
|
14160 | return this._repoInternal;
|
14161 | },
|
14162 | enumerable: false,
|
14163 | configurable: true
|
14164 | });
|
14165 | Object.defineProperty(Database.prototype, "_root", {
|
14166 | get: function () {
|
14167 | if (!this._rootInternal) {
|
14168 | this._rootInternal = new ReferenceImpl(this._repo, newEmptyPath());
|
14169 | }
|
14170 | return this._rootInternal;
|
14171 | },
|
14172 | enumerable: false,
|
14173 | configurable: true
|
14174 | });
|
14175 | Database.prototype._delete = function () {
|
14176 | if (this._rootInternal !== null) {
|
14177 | repoManagerDeleteRepo(this._repo, this.app.name);
|
14178 | this._repoInternal = null;
|
14179 | this._rootInternal = null;
|
14180 | }
|
14181 | return Promise.resolve();
|
14182 | };
|
14183 | Database.prototype._checkNotDeleted = function (apiName) {
|
14184 | if (this._rootInternal === null) {
|
14185 | fatal('Cannot call ' + apiName + ' on a deleted database.');
|
14186 | }
|
14187 | };
|
14188 | return Database;
|
14189 | }());
|
14190 | function checkTransportInit() {
|
14191 | if (TransportManager.IS_TRANSPORT_INITIALIZED) {
|
14192 | warn('Transport has already been initialized. Please call this function before calling ref or setting up a listener');
|
14193 | }
|
14194 | }
|
14195 |
|
14196 |
|
14197 |
|
14198 | function forceWebSockets() {
|
14199 | checkTransportInit();
|
14200 | BrowserPollConnection.forceDisallow();
|
14201 | }
|
14202 |
|
14203 |
|
14204 |
|
14205 | function forceLongPolling() {
|
14206 | checkTransportInit();
|
14207 | WebSocketConnection.forceDisallow();
|
14208 | BrowserPollConnection.forceAllow();
|
14209 | }
|
14210 |
|
14211 |
|
14212 |
|
14213 |
|
14214 |
|
14215 |
|
14216 |
|
14217 |
|
14218 |
|
14219 |
|
14220 |
|
14221 | function connectDatabaseEmulator(db, host, port, options) {
|
14222 | if (options === void 0) { options = {}; }
|
14223 | db = util.getModularInstance(db);
|
14224 | db._checkNotDeleted('useEmulator');
|
14225 | if (db._instanceStarted) {
|
14226 | fatal('Cannot call useEmulator() after instance has already been initialized.');
|
14227 | }
|
14228 | var repo = db._repoInternal;
|
14229 | var tokenProvider = undefined;
|
14230 | if (repo.repoInfo_.nodeAdmin) {
|
14231 | if (options.mockUserToken) {
|
14232 | fatal('mockUserToken is not supported by the Admin SDK. For client access with mock users, please use the "firebase" package instead of "firebase-admin".');
|
14233 | }
|
14234 | tokenProvider = new EmulatorTokenProvider(EmulatorTokenProvider.OWNER);
|
14235 | }
|
14236 | else if (options.mockUserToken) {
|
14237 | var token = typeof options.mockUserToken === 'string'
|
14238 | ? options.mockUserToken
|
14239 | : util.createMockUserToken(options.mockUserToken, db.app.options.projectId);
|
14240 | tokenProvider = new EmulatorTokenProvider(token);
|
14241 | }
|
14242 |
|
14243 | repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
|
14244 | }
|
14245 |
|
14246 |
|
14247 |
|
14248 |
|
14249 |
|
14250 |
|
14251 |
|
14252 |
|
14253 |
|
14254 |
|
14255 |
|
14256 |
|
14257 |
|
14258 |
|
14259 |
|
14260 |
|
14261 |
|
14262 |
|
14263 |
|
14264 |
|
14265 |
|
14266 | function goOffline(db) {
|
14267 | db = util.getModularInstance(db);
|
14268 | db._checkNotDeleted('goOffline');
|
14269 | repoInterrupt(db._repo);
|
14270 | }
|
14271 |
|
14272 |
|
14273 |
|
14274 |
|
14275 |
|
14276 |
|
14277 |
|
14278 |
|
14279 |
|
14280 |
|
14281 |
|
14282 | function goOnline(db) {
|
14283 | db = util.getModularInstance(db);
|
14284 | db._checkNotDeleted('goOnline');
|
14285 | repoResume(db._repo);
|
14286 | }
|
14287 | function enableLogging(logger, persistent) {
|
14288 | enableLogging$1(logger, persistent);
|
14289 | }
|
14290 |
|
14291 |
|
14292 |
|
14293 |
|
14294 |
|
14295 |
|
14296 |
|
14297 |
|
14298 |
|
14299 |
|
14300 |
|
14301 |
|
14302 |
|
14303 |
|
14304 |
|
14305 |
|
14306 |
|
14307 | var SERVER_TIMESTAMP = {
|
14308 | '.sv': 'timestamp'
|
14309 | };
|
14310 |
|
14311 |
|
14312 |
|
14313 |
|
14314 |
|
14315 | function serverTimestamp() {
|
14316 | return SERVER_TIMESTAMP;
|
14317 | }
|
14318 |
|
14319 |
|
14320 |
|
14321 |
|
14322 |
|
14323 |
|
14324 |
|
14325 | function increment(delta) {
|
14326 | return {
|
14327 | '.sv': {
|
14328 | 'increment': delta
|
14329 | }
|
14330 | };
|
14331 | }
|
14332 |
|
14333 |
|
14334 |
|
14335 |
|
14336 |
|
14337 |
|
14338 |
|
14339 |
|
14340 |
|
14341 |
|
14342 |
|
14343 |
|
14344 |
|
14345 |
|
14346 |
|
14347 |
|
14348 |
|
14349 |
|
14350 |
|
14351 |
|
14352 | var TransactionResult = (function () {
|
14353 |
|
14354 | function TransactionResult(
|
14355 | /** Whether the transaction was successfully committed. */
|
14356 | committed,
|
14357 | /** The resulting data snapshot. */
|
14358 | snapshot) {
|
14359 | this.committed = committed;
|
14360 | this.snapshot = snapshot;
|
14361 | }
|
14362 |
|
14363 | TransactionResult.prototype.toJSON = function () {
|
14364 | return { committed: this.committed, snapshot: this.snapshot.toJSON() };
|
14365 | };
|
14366 | return TransactionResult;
|
14367 | }());
|
14368 |
|
14369 |
|
14370 |
|
14371 |
|
14372 |
|
14373 |
|
14374 |
|
14375 |
|
14376 |
|
14377 |
|
14378 |
|
14379 |
|
14380 |
|
14381 |
|
14382 |
|
14383 |
|
14384 |
|
14385 |
|
14386 |
|
14387 |
|
14388 |
|
14389 |
|
14390 |
|
14391 |
|
14392 |
|
14393 |
|
14394 |
|
14395 |
|
14396 |
|
14397 |
|
14398 |
|
14399 |
|
14400 |
|
14401 |
|
14402 |
|
14403 |
|
14404 |
|
14405 | function runTransaction(ref,
|
14406 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
14407 | transactionUpdate, options) {
|
14408 | var _a;
|
14409 | ref = util.getModularInstance(ref);
|
14410 | validateWritablePath('Reference.transaction', ref._path);
|
14411 | if (ref.key === '.length' || ref.key === '.keys') {
|
14412 | throw ('Reference.transaction failed: ' + ref.key + ' is a read-only object.');
|
14413 | }
|
14414 | var applyLocally = (_a = options === null || options === void 0 ? void 0 : options.applyLocally) !== null && _a !== void 0 ? _a : true;
|
14415 | var deferred = new util.Deferred();
|
14416 | var promiseComplete = function (error, committed, node) {
|
14417 | var dataSnapshot = null;
|
14418 | if (error) {
|
14419 | deferred.reject(error);
|
14420 | }
|
14421 | else {
|
14422 | dataSnapshot = new DataSnapshot(node, new ReferenceImpl(ref._repo, ref._path), PRIORITY_INDEX);
|
14423 | deferred.resolve(new TransactionResult(committed, dataSnapshot));
|
14424 | }
|
14425 | };
|
14426 |
|
14427 | var unwatcher = onValue(ref, function () { });
|
14428 | repoStartTransaction(ref._repo, ref._path, transactionUpdate, promiseComplete, unwatcher, applyLocally);
|
14429 | return deferred.promise;
|
14430 | }
|
14431 |
|
14432 |
|
14433 |
|
14434 |
|
14435 |
|
14436 |
|
14437 |
|
14438 |
|
14439 |
|
14440 |
|
14441 |
|
14442 |
|
14443 |
|
14444 |
|
14445 |
|
14446 |
|
14447 |
|
14448 |
|
14449 | PersistentConnection.prototype.simpleListen = function (pathString, onComplete) {
|
14450 | this.sendRequest('q', { p: pathString }, onComplete);
|
14451 | };
|
14452 |
|
14453 | PersistentConnection.prototype.echo = function (data, onEcho) {
|
14454 | this.sendRequest('echo', { d: data }, onEcho);
|
14455 | };
|
14456 |
|
14457 |
|
14458 |
|
14459 | var hijackHash = function (newHash) {
|
14460 | var oldPut = PersistentConnection.prototype.put;
|
14461 | PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
|
14462 | if (hash !== undefined) {
|
14463 | hash = newHash();
|
14464 | }
|
14465 | oldPut.call(this, pathString, data, onComplete, hash);
|
14466 | };
|
14467 | return function () {
|
14468 | PersistentConnection.prototype.put = oldPut;
|
14469 | };
|
14470 | };
|
14471 |
|
14472 |
|
14473 |
|
14474 |
|
14475 | var forceRestClient = function (forceRestClient) {
|
14476 | repoManagerForceRestClient(forceRestClient);
|
14477 | };
|
14478 |
|
14479 |
|
14480 |
|
14481 |
|
14482 |
|
14483 |
|
14484 |
|
14485 |
|
14486 |
|
14487 |
|
14488 |
|
14489 |
|
14490 |
|
14491 |
|
14492 |
|
14493 |
|
14494 |
|
14495 | setWebSocketImpl(Websocket__default["default"].Client);
|
14496 |
|
14497 | exports.DataSnapshot = DataSnapshot;
|
14498 | exports.Database = Database;
|
14499 | exports.OnDisconnect = OnDisconnect;
|
14500 | exports.QueryConstraint = QueryConstraint;
|
14501 | exports.TransactionResult = TransactionResult;
|
14502 | exports._QueryImpl = QueryImpl;
|
14503 | exports._QueryParams = QueryParams;
|
14504 | exports._ReferenceImpl = ReferenceImpl;
|
14505 | exports._TEST_ACCESS_forceRestClient = forceRestClient;
|
14506 | exports._TEST_ACCESS_hijackHash = hijackHash;
|
14507 | exports._repoManagerDatabaseFromApp = repoManagerDatabaseFromApp;
|
14508 | exports._setSDKVersion = setSDKVersion;
|
14509 | exports._validatePathString = validatePathString;
|
14510 | exports._validateWritablePath = validateWritablePath;
|
14511 | exports.child = child;
|
14512 | exports.connectDatabaseEmulator = connectDatabaseEmulator;
|
14513 | exports.enableLogging = enableLogging;
|
14514 | exports.endAt = endAt;
|
14515 | exports.endBefore = endBefore;
|
14516 | exports.equalTo = equalTo;
|
14517 | exports.forceLongPolling = forceLongPolling;
|
14518 | exports.forceWebSockets = forceWebSockets;
|
14519 | exports.get = get;
|
14520 | exports.goOffline = goOffline;
|
14521 | exports.goOnline = goOnline;
|
14522 | exports.increment = increment;
|
14523 | exports.limitToFirst = limitToFirst;
|
14524 | exports.limitToLast = limitToLast;
|
14525 | exports.off = off;
|
14526 | exports.onChildAdded = onChildAdded;
|
14527 | exports.onChildChanged = onChildChanged;
|
14528 | exports.onChildMoved = onChildMoved;
|
14529 | exports.onChildRemoved = onChildRemoved;
|
14530 | exports.onDisconnect = onDisconnect;
|
14531 | exports.onValue = onValue;
|
14532 | exports.orderByChild = orderByChild;
|
14533 | exports.orderByKey = orderByKey;
|
14534 | exports.orderByPriority = orderByPriority;
|
14535 | exports.orderByValue = orderByValue;
|
14536 | exports.push = push;
|
14537 | exports.query = query;
|
14538 | exports.ref = ref;
|
14539 | exports.refFromURL = refFromURL;
|
14540 | exports.remove = remove;
|
14541 | exports.runTransaction = runTransaction;
|
14542 | exports.serverTimestamp = serverTimestamp;
|
14543 | exports.set = set;
|
14544 | exports.setPriority = setPriority;
|
14545 | exports.setWithPriority = setWithPriority;
|
14546 | exports.startAfter = startAfter;
|
14547 | exports.startAt = startAt;
|
14548 | exports.update = update;
|
14549 | //# sourceMappingURL=index.standalone.js.map
|