1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var component = require('@firebase/component');
|
6 | var util = require('@firebase/util');
|
7 | var fayeWebsocket = require('faye-websocket');
|
8 | var tslib = require('tslib');
|
9 | var logger$1 = require('@firebase/logger');
|
10 |
|
11 | var name = "@firebase/database";
|
12 | var version = "0.10.3";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var SDK_VERSION = '';
|
32 |
|
33 | function setSDKVersion(version) {
|
34 | SDK_VERSION = version;
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | var DOMStorageWrapper = (function () {
|
63 | |
64 |
|
65 |
|
66 | function DOMStorageWrapper(domStorage_) {
|
67 | this.domStorage_ = domStorage_;
|
68 |
|
69 | this.prefix_ = 'firebase:';
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 | DOMStorageWrapper.prototype.set = function (key, value) {
|
76 | if (value == null) {
|
77 | this.domStorage_.removeItem(this.prefixedName_(key));
|
78 | }
|
79 | else {
|
80 | this.domStorage_.setItem(this.prefixedName_(key), util.stringify(value));
|
81 | }
|
82 | };
|
83 | |
84 |
|
85 |
|
86 | DOMStorageWrapper.prototype.get = function (key) {
|
87 | var storedVal = this.domStorage_.getItem(this.prefixedName_(key));
|
88 | if (storedVal == null) {
|
89 | return null;
|
90 | }
|
91 | else {
|
92 | return util.jsonEval(storedVal);
|
93 | }
|
94 | };
|
95 | DOMStorageWrapper.prototype.remove = function (key) {
|
96 | this.domStorage_.removeItem(this.prefixedName_(key));
|
97 | };
|
98 | DOMStorageWrapper.prototype.prefixedName_ = function (name) {
|
99 | return this.prefix_ + name;
|
100 | };
|
101 | DOMStorageWrapper.prototype.toString = function () {
|
102 | return this.domStorage_.toString();
|
103 | };
|
104 | return DOMStorageWrapper;
|
105 | }());
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | var MemoryStorage = (function () {
|
128 | function MemoryStorage() {
|
129 | this.cache_ = {};
|
130 | this.isInMemoryStorage = true;
|
131 | }
|
132 | MemoryStorage.prototype.set = function (key, value) {
|
133 | if (value == null) {
|
134 | delete this.cache_[key];
|
135 | }
|
136 | else {
|
137 | this.cache_[key] = value;
|
138 | }
|
139 | };
|
140 | MemoryStorage.prototype.get = function (key) {
|
141 | if (util.contains(this.cache_, key)) {
|
142 | return this.cache_[key];
|
143 | }
|
144 | return null;
|
145 | };
|
146 | MemoryStorage.prototype.remove = function (key) {
|
147 | delete this.cache_[key];
|
148 | };
|
149 | return MemoryStorage;
|
150 | }());
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | var createStoragefor = function (domStorageName) {
|
178 | try {
|
179 |
|
180 |
|
181 | if (typeof window !== 'undefined' &&
|
182 | typeof window[domStorageName] !== 'undefined') {
|
183 |
|
184 | var domStorage = window[domStorageName];
|
185 | domStorage.setItem('firebase:sentinel', 'cache');
|
186 | domStorage.removeItem('firebase:sentinel');
|
187 | return new DOMStorageWrapper(domStorage);
|
188 | }
|
189 | }
|
190 | catch (e) { }
|
191 |
|
192 |
|
193 | return new MemoryStorage();
|
194 | };
|
195 |
|
196 | var PersistentStorage = createStoragefor('localStorage');
|
197 |
|
198 | var SessionStorage = createStoragefor('sessionStorage');
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | var logClient = new logger$1.Logger('@firebase/database');
|
217 |
|
218 |
|
219 |
|
220 | var LUIDGenerator = (function () {
|
221 | var id = 1;
|
222 | return function () {
|
223 | return id++;
|
224 | };
|
225 | })();
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | var sha1 = function (str) {
|
232 | var utf8Bytes = util.stringToByteArray(str);
|
233 | var sha1 = new util.Sha1();
|
234 | sha1.update(utf8Bytes);
|
235 | var sha1Bytes = sha1.digest();
|
236 | return util.base64.encodeByteArray(sha1Bytes);
|
237 | };
|
238 | var buildLogMessage_ = function () {
|
239 | var varArgs = [];
|
240 | for (var _i = 0; _i < arguments.length; _i++) {
|
241 | varArgs[_i] = arguments[_i];
|
242 | }
|
243 | var message = '';
|
244 | for (var i = 0; i < varArgs.length; i++) {
|
245 | var arg = varArgs[i];
|
246 | if (Array.isArray(arg) ||
|
247 | (arg &&
|
248 | typeof arg === 'object' &&
|
249 |
|
250 | typeof arg.length === 'number')) {
|
251 | message += buildLogMessage_.apply(null, arg);
|
252 | }
|
253 | else if (typeof arg === 'object') {
|
254 | message += util.stringify(arg);
|
255 | }
|
256 | else {
|
257 | message += arg;
|
258 | }
|
259 | message += ' ';
|
260 | }
|
261 | return message;
|
262 | };
|
263 |
|
264 |
|
265 |
|
266 | var logger = null;
|
267 |
|
268 |
|
269 |
|
270 | var firstLog_ = true;
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | var enableLogging = function (logger_, persistent) {
|
277 | util.assert(!persistent || logger_ === true || logger_ === false, "Can't turn on custom loggers persistently.");
|
278 | if (logger_ === true) {
|
279 | logClient.logLevel = logger$1.LogLevel.VERBOSE;
|
280 | logger = logClient.log.bind(logClient);
|
281 | if (persistent) {
|
282 | SessionStorage.set('logging_enabled', true);
|
283 | }
|
284 | }
|
285 | else if (typeof logger_ === 'function') {
|
286 | logger = logger_;
|
287 | }
|
288 | else {
|
289 | logger = null;
|
290 | SessionStorage.remove('logging_enabled');
|
291 | }
|
292 | };
|
293 | var log = function () {
|
294 | var varArgs = [];
|
295 | for (var _i = 0; _i < arguments.length; _i++) {
|
296 | varArgs[_i] = arguments[_i];
|
297 | }
|
298 | if (firstLog_ === true) {
|
299 | firstLog_ = false;
|
300 | if (logger === null && SessionStorage.get('logging_enabled') === true) {
|
301 | enableLogging(true);
|
302 | }
|
303 | }
|
304 | if (logger) {
|
305 | var message = buildLogMessage_.apply(null, varArgs);
|
306 | logger(message);
|
307 | }
|
308 | };
|
309 | var logWrapper = function (prefix) {
|
310 | return function () {
|
311 | var varArgs = [];
|
312 | for (var _i = 0; _i < arguments.length; _i++) {
|
313 | varArgs[_i] = arguments[_i];
|
314 | }
|
315 | log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs)));
|
316 | };
|
317 | };
|
318 | var error = function () {
|
319 | var varArgs = [];
|
320 | for (var _i = 0; _i < arguments.length; _i++) {
|
321 | varArgs[_i] = arguments[_i];
|
322 | }
|
323 | var message = 'FIREBASE INTERNAL ERROR: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs)));
|
324 | logClient.error(message);
|
325 | };
|
326 | var fatal = function () {
|
327 | var varArgs = [];
|
328 | for (var _i = 0; _i < arguments.length; _i++) {
|
329 | varArgs[_i] = arguments[_i];
|
330 | }
|
331 | var message = "FIREBASE FATAL ERROR: " + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs)));
|
332 | logClient.error(message);
|
333 | throw new Error(message);
|
334 | };
|
335 | var warn = function () {
|
336 | var varArgs = [];
|
337 | for (var _i = 0; _i < arguments.length; _i++) {
|
338 | varArgs[_i] = arguments[_i];
|
339 | }
|
340 | var message = 'FIREBASE WARNING: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs)));
|
341 | logClient.warn(message);
|
342 | };
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | var warnIfPageIsSecure = function () {
|
348 |
|
349 | if (typeof window !== 'undefined' &&
|
350 | window.location &&
|
351 | window.location.protocol &&
|
352 | window.location.protocol.indexOf('https:') !== -1) {
|
353 | warn('Insecure Firebase access from a secure page. ' +
|
354 | 'Please use https in calls to new Firebase().');
|
355 | }
|
356 | };
|
357 |
|
358 |
|
359 |
|
360 | var isInvalidJSONNumber = function (data) {
|
361 | return (typeof data === 'number' &&
|
362 | (data !== data ||
|
363 | data === Number.POSITIVE_INFINITY ||
|
364 | data === Number.NEGATIVE_INFINITY));
|
365 | };
|
366 | var executeWhenDOMReady = function (fn) {
|
367 | if (util.isNodeSdk() || document.readyState === 'complete') {
|
368 | fn();
|
369 | }
|
370 | else {
|
371 |
|
372 |
|
373 | var called_1 = false;
|
374 | var wrappedFn_1 = function () {
|
375 | if (!document.body) {
|
376 | setTimeout(wrappedFn_1, Math.floor(10));
|
377 | return;
|
378 | }
|
379 | if (!called_1) {
|
380 | called_1 = true;
|
381 | fn();
|
382 | }
|
383 | };
|
384 | if (document.addEventListener) {
|
385 | document.addEventListener('DOMContentLoaded', wrappedFn_1, false);
|
386 |
|
387 | window.addEventListener('load', wrappedFn_1, false);
|
388 |
|
389 | }
|
390 | else if (document.attachEvent) {
|
391 |
|
392 |
|
393 | document.attachEvent('onreadystatechange', function () {
|
394 | if (document.readyState === 'complete') {
|
395 | wrappedFn_1();
|
396 | }
|
397 | });
|
398 |
|
399 |
|
400 | window.attachEvent('onload', wrappedFn_1);
|
401 |
|
402 |
|
403 |
|
404 | }
|
405 | }
|
406 | };
|
407 |
|
408 |
|
409 |
|
410 | var MIN_NAME = '[MIN_NAME]';
|
411 |
|
412 |
|
413 |
|
414 | var MAX_NAME = '[MAX_NAME]';
|
415 |
|
416 |
|
417 |
|
418 | var nameCompare = function (a, b) {
|
419 | if (a === b) {
|
420 | return 0;
|
421 | }
|
422 | else if (a === MIN_NAME || b === MAX_NAME) {
|
423 | return -1;
|
424 | }
|
425 | else if (b === MIN_NAME || a === MAX_NAME) {
|
426 | return 1;
|
427 | }
|
428 | else {
|
429 | var aAsInt = tryParseInt(a), bAsInt = tryParseInt(b);
|
430 | if (aAsInt !== null) {
|
431 | if (bAsInt !== null) {
|
432 | return aAsInt - bAsInt === 0 ? a.length - b.length : aAsInt - bAsInt;
|
433 | }
|
434 | else {
|
435 | return -1;
|
436 | }
|
437 | }
|
438 | else if (bAsInt !== null) {
|
439 | return 1;
|
440 | }
|
441 | else {
|
442 | return a < b ? -1 : 1;
|
443 | }
|
444 | }
|
445 | };
|
446 |
|
447 |
|
448 |
|
449 | var stringCompare = function (a, b) {
|
450 | if (a === b) {
|
451 | return 0;
|
452 | }
|
453 | else if (a < b) {
|
454 | return -1;
|
455 | }
|
456 | else {
|
457 | return 1;
|
458 | }
|
459 | };
|
460 | var requireKey = function (key, obj) {
|
461 | if (obj && key in obj) {
|
462 | return obj[key];
|
463 | }
|
464 | else {
|
465 | throw new Error('Missing required key (' + key + ') in object: ' + util.stringify(obj));
|
466 | }
|
467 | };
|
468 | var ObjectToUniqueKey = function (obj) {
|
469 | if (typeof obj !== 'object' || obj === null) {
|
470 | return util.stringify(obj);
|
471 | }
|
472 | var keys = [];
|
473 |
|
474 | for (var k in obj) {
|
475 | keys.push(k);
|
476 | }
|
477 |
|
478 | keys.sort();
|
479 | var key = '{';
|
480 | for (var i = 0; i < keys.length; i++) {
|
481 | if (i !== 0) {
|
482 | key += ',';
|
483 | }
|
484 | key += util.stringify(keys[i]);
|
485 | key += ':';
|
486 | key += ObjectToUniqueKey(obj[keys[i]]);
|
487 | }
|
488 | key += '}';
|
489 | return key;
|
490 | };
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 | var splitStringBySize = function (str, segsize) {
|
498 | var len = str.length;
|
499 | if (len <= segsize) {
|
500 | return [str];
|
501 | }
|
502 | var dataSegs = [];
|
503 | for (var c = 0; c < len; c += segsize) {
|
504 | if (c + segsize > len) {
|
505 | dataSegs.push(str.substring(c, len));
|
506 | }
|
507 | else {
|
508 | dataSegs.push(str.substring(c, c + segsize));
|
509 | }
|
510 | }
|
511 | return dataSegs;
|
512 | };
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 | function each(obj, fn) {
|
520 | for (var key in obj) {
|
521 | if (obj.hasOwnProperty(key)) {
|
522 | fn(key, obj[key]);
|
523 | }
|
524 | }
|
525 | }
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 | var doubleToIEEE754String = function (v) {
|
534 | util.assert(!isInvalidJSONNumber(v), 'Invalid JSON number');
|
535 | var ebits = 11, fbits = 52;
|
536 | var bias = (1 << (ebits - 1)) - 1;
|
537 | var s, e, f, ln, i;
|
538 |
|
539 |
|
540 | if (v === 0) {
|
541 | e = 0;
|
542 | f = 0;
|
543 | s = 1 / v === -Infinity ? 1 : 0;
|
544 | }
|
545 | else {
|
546 | s = v < 0;
|
547 | v = Math.abs(v);
|
548 | if (v >= Math.pow(2, 1 - bias)) {
|
549 |
|
550 | ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
|
551 | e = ln + bias;
|
552 | f = Math.round(v * Math.pow(2, fbits - ln) - Math.pow(2, fbits));
|
553 | }
|
554 | else {
|
555 |
|
556 | e = 0;
|
557 | f = Math.round(v / Math.pow(2, 1 - bias - fbits));
|
558 | }
|
559 | }
|
560 |
|
561 | var bits = [];
|
562 | for (i = fbits; i; i -= 1) {
|
563 | bits.push(f % 2 ? 1 : 0);
|
564 | f = Math.floor(f / 2);
|
565 | }
|
566 | for (i = ebits; i; i -= 1) {
|
567 | bits.push(e % 2 ? 1 : 0);
|
568 | e = Math.floor(e / 2);
|
569 | }
|
570 | bits.push(s ? 1 : 0);
|
571 | bits.reverse();
|
572 | var str = bits.join('');
|
573 |
|
574 | var hexByteString = '';
|
575 | for (i = 0; i < 64; i += 8) {
|
576 | var hexByte = parseInt(str.substr(i, 8), 2).toString(16);
|
577 | if (hexByte.length === 1) {
|
578 | hexByte = '0' + hexByte;
|
579 | }
|
580 | hexByteString = hexByteString + hexByte;
|
581 | }
|
582 | return hexByteString.toLowerCase();
|
583 | };
|
584 |
|
585 |
|
586 |
|
587 |
|
588 | var isChromeExtensionContentScript = function () {
|
589 | return !!(typeof window === 'object' &&
|
590 | window['chrome'] &&
|
591 | window['chrome']['extension'] &&
|
592 | !/^chrome/.test(window.location.href));
|
593 | };
|
594 |
|
595 |
|
596 |
|
597 | var isWindowsStoreApp = function () {
|
598 |
|
599 | return typeof Windows === 'object' && typeof Windows.UI === 'object';
|
600 | };
|
601 |
|
602 |
|
603 |
|
604 | function errorForServerCode(code, query) {
|
605 | var reason = 'Unknown Error';
|
606 | if (code === 'too_big') {
|
607 | reason =
|
608 | 'The data requested exceeds the maximum size ' +
|
609 | 'that can be accessed with a single request.';
|
610 | }
|
611 | else if (code === 'permission_denied') {
|
612 | reason = "Client doesn't have permission to access the desired data.";
|
613 | }
|
614 | else if (code === 'unavailable') {
|
615 | reason = 'The service is unavailable';
|
616 | }
|
617 | var error = new Error(code + ' at ' + query._path.toString() + ': ' + reason);
|
618 |
|
619 | error.code = code.toUpperCase();
|
620 | return error;
|
621 | }
|
622 |
|
623 |
|
624 |
|
625 | var INTEGER_REGEXP_ = new RegExp('^-?(0*)\\d{1,10}$');
|
626 |
|
627 |
|
628 |
|
629 | var INTEGER_32_MIN = -2147483648;
|
630 |
|
631 |
|
632 |
|
633 | var INTEGER_32_MAX = 2147483647;
|
634 |
|
635 |
|
636 |
|
637 | var tryParseInt = function (str) {
|
638 | if (INTEGER_REGEXP_.test(str)) {
|
639 | var intVal = Number(str);
|
640 | if (intVal >= INTEGER_32_MIN && intVal <= INTEGER_32_MAX) {
|
641 | return intVal;
|
642 | }
|
643 | }
|
644 | return null;
|
645 | };
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 | var exceptionGuard = function (fn) {
|
664 | try {
|
665 | fn();
|
666 | }
|
667 | catch (e) {
|
668 |
|
669 | setTimeout(function () {
|
670 |
|
671 |
|
672 |
|
673 |
|
674 | var stack = e.stack || '';
|
675 | warn('Exception was thrown by user callback.', stack);
|
676 | throw e;
|
677 | }, Math.floor(0));
|
678 | }
|
679 | };
|
680 |
|
681 |
|
682 |
|
683 | var beingCrawled = function () {
|
684 | var userAgent = (typeof window === 'object' &&
|
685 | window['navigator'] &&
|
686 | window['navigator']['userAgent']) ||
|
687 | '';
|
688 |
|
689 |
|
690 |
|
691 |
|
692 | return (userAgent.search(/googlebot|google webmaster tools|bingbot|yahoo! slurp|baiduspider|yandexbot|duckduckbot/i) >= 0);
|
693 | };
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 | var setTimeoutNonBlocking = function (fn, time) {
|
704 | var timeout = setTimeout(fn, time);
|
705 |
|
706 | if (typeof timeout === 'object' && timeout['unref']) {
|
707 |
|
708 | timeout['unref']();
|
709 | }
|
710 | return timeout;
|
711 | };
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 | var AppCheckTokenProvider = (function () {
|
733 | function AppCheckTokenProvider(appName_, appCheckProvider) {
|
734 | var _this = this;
|
735 | this.appName_ = appName_;
|
736 | this.appCheckProvider = appCheckProvider;
|
737 | this.appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
|
738 | if (!this.appCheck) {
|
739 | appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); });
|
740 | }
|
741 | }
|
742 | AppCheckTokenProvider.prototype.getToken = function (forceRefresh) {
|
743 | var _this = this;
|
744 | if (!this.appCheck) {
|
745 | return new Promise(function (resolve, reject) {
|
746 |
|
747 |
|
748 |
|
749 |
|
750 | setTimeout(function () {
|
751 | if (_this.appCheck) {
|
752 | _this.getToken(forceRefresh).then(resolve, reject);
|
753 | }
|
754 | else {
|
755 | resolve(null);
|
756 | }
|
757 | }, 0);
|
758 | });
|
759 | }
|
760 | return this.appCheck.getToken(forceRefresh);
|
761 | };
|
762 | AppCheckTokenProvider.prototype.addTokenChangeListener = function (listener) {
|
763 | var _a;
|
764 | (_a = this.appCheckProvider) === null || _a === void 0 ? void 0 : _a.get().then(function (appCheck) { return appCheck.addTokenListener(listener); });
|
765 | };
|
766 | AppCheckTokenProvider.prototype.notifyForInvalidToken = function () {
|
767 | warn("Provided AppCheck credentials for the app named \"" + this.appName_ + "\" " +
|
768 | 'are invalid. This usually indicates your app was not initialized correctly.');
|
769 | };
|
770 | return AppCheckTokenProvider;
|
771 | }());
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 | var FirebaseAuthTokenProvider = (function () {
|
793 | function FirebaseAuthTokenProvider(appName_, firebaseOptions_, authProvider_) {
|
794 | var _this = this;
|
795 | this.appName_ = appName_;
|
796 | this.firebaseOptions_ = firebaseOptions_;
|
797 | this.authProvider_ = authProvider_;
|
798 | this.auth_ = null;
|
799 | this.auth_ = authProvider_.getImmediate({ optional: true });
|
800 | if (!this.auth_) {
|
801 | authProvider_.onInit(function (auth) { return (_this.auth_ = auth); });
|
802 | }
|
803 | }
|
804 | FirebaseAuthTokenProvider.prototype.getToken = function (forceRefresh) {
|
805 | var _this = this;
|
806 | if (!this.auth_) {
|
807 | return new Promise(function (resolve, reject) {
|
808 |
|
809 |
|
810 |
|
811 |
|
812 | setTimeout(function () {
|
813 | if (_this.auth_) {
|
814 | _this.getToken(forceRefresh).then(resolve, reject);
|
815 | }
|
816 | else {
|
817 | resolve(null);
|
818 | }
|
819 | }, 0);
|
820 | });
|
821 | }
|
822 | return this.auth_.getToken(forceRefresh).catch(function (error) {
|
823 |
|
824 |
|
825 | if (error && error.code === 'auth/token-not-initialized') {
|
826 | log('Got auth/token-not-initialized error. Treating as null token.');
|
827 | return null;
|
828 | }
|
829 | else {
|
830 | return Promise.reject(error);
|
831 | }
|
832 | });
|
833 | };
|
834 | FirebaseAuthTokenProvider.prototype.addTokenChangeListener = function (listener) {
|
835 |
|
836 |
|
837 | if (this.auth_) {
|
838 | this.auth_.addAuthTokenListener(listener);
|
839 | }
|
840 | else {
|
841 | this.authProvider_
|
842 | .get()
|
843 | .then(function (auth) { return auth.addAuthTokenListener(listener); });
|
844 | }
|
845 | };
|
846 | FirebaseAuthTokenProvider.prototype.removeTokenChangeListener = function (listener) {
|
847 | this.authProvider_
|
848 | .get()
|
849 | .then(function (auth) { return auth.removeAuthTokenListener(listener); });
|
850 | };
|
851 | FirebaseAuthTokenProvider.prototype.notifyForInvalidToken = function () {
|
852 | var errorMessage = 'Provided authentication credentials for the app named "' +
|
853 | this.appName_ +
|
854 | '" are invalid. This usually indicates your app was not ' +
|
855 | 'initialized correctly. ';
|
856 | if ('credential' in this.firebaseOptions_) {
|
857 | errorMessage +=
|
858 | 'Make sure the "credential" property provided to initializeApp() ' +
|
859 | 'is authorized to access the specified "databaseURL" and is from the correct ' +
|
860 | 'project.';
|
861 | }
|
862 | else if ('serviceAccount' in this.firebaseOptions_) {
|
863 | errorMessage +=
|
864 | 'Make sure the "serviceAccount" property provided to initializeApp() ' +
|
865 | 'is authorized to access the specified "databaseURL" and is from the correct ' +
|
866 | 'project.';
|
867 | }
|
868 | else {
|
869 | errorMessage +=
|
870 | 'Make sure the "apiKey" and "databaseURL" properties provided to ' +
|
871 | 'initializeApp() match the values provided for your app at ' +
|
872 | 'https://console.firebase.google.com/.';
|
873 | }
|
874 | warn(errorMessage);
|
875 | };
|
876 | return FirebaseAuthTokenProvider;
|
877 | }());
|
878 |
|
879 | var EmulatorTokenProvider = (function () {
|
880 | function EmulatorTokenProvider(accessToken) {
|
881 | this.accessToken = accessToken;
|
882 | }
|
883 | EmulatorTokenProvider.prototype.getToken = function (forceRefresh) {
|
884 | return Promise.resolve({
|
885 | accessToken: this.accessToken
|
886 | });
|
887 | };
|
888 | EmulatorTokenProvider.prototype.addTokenChangeListener = function (listener) {
|
889 |
|
890 |
|
891 | listener(this.accessToken);
|
892 | };
|
893 | EmulatorTokenProvider.prototype.removeTokenChangeListener = function (listener) { };
|
894 | EmulatorTokenProvider.prototype.notifyForInvalidToken = function () { };
|
895 |
|
896 | EmulatorTokenProvider.OWNER = 'owner';
|
897 | return EmulatorTokenProvider;
|
898 | }());
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 | var PROTOCOL_VERSION = '5';
|
917 | var VERSION_PARAM = 'v';
|
918 | var TRANSPORT_SESSION_PARAM = 's';
|
919 | var REFERER_PARAM = 'r';
|
920 | var FORGE_REF = 'f';
|
921 |
|
922 |
|
923 | var FORGE_DOMAIN_RE = /(console\.firebase|firebase-console-\w+\.corp|firebase\.corp)\.google\.com/;
|
924 | var LAST_SESSION_PARAM = 'ls';
|
925 | var APPLICATION_ID_PARAM = 'p';
|
926 | var APP_CHECK_TOKEN_PARAM = 'ac';
|
927 | var WEBSOCKET = 'websocket';
|
928 | var LONG_POLLING = 'long_polling';
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 | var RepoInfo = (function () {
|
950 | |
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 | function RepoInfo(host, secure, namespace, webSocketOnly, nodeAdmin, persistenceKey, includeNamespaceInQueryParams) {
|
959 | if (nodeAdmin === void 0) { nodeAdmin = false; }
|
960 | if (persistenceKey === void 0) { persistenceKey = ''; }
|
961 | if (includeNamespaceInQueryParams === void 0) { includeNamespaceInQueryParams = false; }
|
962 | this.secure = secure;
|
963 | this.namespace = namespace;
|
964 | this.webSocketOnly = webSocketOnly;
|
965 | this.nodeAdmin = nodeAdmin;
|
966 | this.persistenceKey = persistenceKey;
|
967 | this.includeNamespaceInQueryParams = includeNamespaceInQueryParams;
|
968 | this._host = host.toLowerCase();
|
969 | this._domain = this._host.substr(this._host.indexOf('.') + 1);
|
970 | this.internalHost =
|
971 | PersistentStorage.get('host:' + host) || this._host;
|
972 | }
|
973 | RepoInfo.prototype.isCacheableHost = function () {
|
974 | return this.internalHost.substr(0, 2) === 's-';
|
975 | };
|
976 | RepoInfo.prototype.isCustomHost = function () {
|
977 | return (this._domain !== 'firebaseio.com' &&
|
978 | this._domain !== 'firebaseio-demo.com');
|
979 | };
|
980 | Object.defineProperty(RepoInfo.prototype, "host", {
|
981 | get: function () {
|
982 | return this._host;
|
983 | },
|
984 | set: function (newHost) {
|
985 | if (newHost !== this.internalHost) {
|
986 | this.internalHost = newHost;
|
987 | if (this.isCacheableHost()) {
|
988 | PersistentStorage.set('host:' + this._host, this.internalHost);
|
989 | }
|
990 | }
|
991 | },
|
992 | enumerable: false,
|
993 | configurable: true
|
994 | });
|
995 | RepoInfo.prototype.toString = function () {
|
996 | var str = this.toURLString();
|
997 | if (this.persistenceKey) {
|
998 | str += '<' + this.persistenceKey + '>';
|
999 | }
|
1000 | return str;
|
1001 | };
|
1002 | RepoInfo.prototype.toURLString = function () {
|
1003 | var protocol = this.secure ? 'https://' : 'http://';
|
1004 | var query = this.includeNamespaceInQueryParams
|
1005 | ? "?ns=" + this.namespace
|
1006 | : '';
|
1007 | return "" + protocol + this.host + "/" + query;
|
1008 | };
|
1009 | return RepoInfo;
|
1010 | }());
|
1011 | function repoInfoNeedsQueryParam(repoInfo) {
|
1012 | return (repoInfo.host !== repoInfo.internalHost ||
|
1013 | repoInfo.isCustomHost() ||
|
1014 | repoInfo.includeNamespaceInQueryParams);
|
1015 | }
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 | function repoInfoConnectionURL(repoInfo, type, params) {
|
1024 | util.assert(typeof type === 'string', 'typeof type must == string');
|
1025 | util.assert(typeof params === 'object', 'typeof params must == object');
|
1026 | var connURL;
|
1027 | if (type === WEBSOCKET) {
|
1028 | connURL =
|
1029 | (repoInfo.secure ? 'wss://' : 'ws://') + repoInfo.internalHost + '/.ws?';
|
1030 | }
|
1031 | else if (type === LONG_POLLING) {
|
1032 | connURL =
|
1033 | (repoInfo.secure ? 'https://' : 'http://') +
|
1034 | repoInfo.internalHost +
|
1035 | '/.lp?';
|
1036 | }
|
1037 | else {
|
1038 | throw new Error('Unknown connection type: ' + type);
|
1039 | }
|
1040 | if (repoInfoNeedsQueryParam(repoInfo)) {
|
1041 | params['ns'] = repoInfo.namespace;
|
1042 | }
|
1043 | var pairs = [];
|
1044 | each(params, function (key, value) {
|
1045 | pairs.push(key + '=' + value);
|
1046 | });
|
1047 | return connURL + pairs.join('&');
|
1048 | }
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 |
|
1069 | var StatsCollection = (function () {
|
1070 | function StatsCollection() {
|
1071 | this.counters_ = {};
|
1072 | }
|
1073 | StatsCollection.prototype.incrementCounter = function (name, amount) {
|
1074 | if (amount === void 0) { amount = 1; }
|
1075 | if (!util.contains(this.counters_, name)) {
|
1076 | this.counters_[name] = 0;
|
1077 | }
|
1078 | this.counters_[name] += amount;
|
1079 | };
|
1080 | StatsCollection.prototype.get = function () {
|
1081 | return util.deepCopy(this.counters_);
|
1082 | };
|
1083 | return StatsCollection;
|
1084 | }());
|
1085 |
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 | var collections = {};
|
1103 | var reporters = {};
|
1104 | function statsManagerGetCollection(repoInfo) {
|
1105 | var hashString = repoInfo.toString();
|
1106 | if (!collections[hashString]) {
|
1107 | collections[hashString] = new StatsCollection();
|
1108 | }
|
1109 | return collections[hashString];
|
1110 | }
|
1111 | function statsManagerGetOrCreateReporter(repoInfo, creatorFunction) {
|
1112 | var hashString = repoInfo.toString();
|
1113 | if (!reporters[hashString]) {
|
1114 | reporters[hashString] = creatorFunction();
|
1115 | }
|
1116 | return reporters[hashString];
|
1117 | }
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 |
|
1125 |
|
1126 |
|
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 | var PacketReceiver = (function () {
|
1140 | |
1141 |
|
1142 |
|
1143 | function PacketReceiver(onMessage_) {
|
1144 | this.onMessage_ = onMessage_;
|
1145 | this.pendingResponses = [];
|
1146 | this.currentResponseNum = 0;
|
1147 | this.closeAfterResponse = -1;
|
1148 | this.onClose = null;
|
1149 | }
|
1150 | PacketReceiver.prototype.closeAfter = function (responseNum, callback) {
|
1151 | this.closeAfterResponse = responseNum;
|
1152 | this.onClose = callback;
|
1153 | if (this.closeAfterResponse < this.currentResponseNum) {
|
1154 | this.onClose();
|
1155 | this.onClose = null;
|
1156 | }
|
1157 | };
|
1158 | |
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 | PacketReceiver.prototype.handleResponse = function (requestNum, data) {
|
1164 | var _this = this;
|
1165 | this.pendingResponses[requestNum] = data;
|
1166 | var _loop_1 = function () {
|
1167 | var toProcess = this_1.pendingResponses[this_1.currentResponseNum];
|
1168 | delete this_1.pendingResponses[this_1.currentResponseNum];
|
1169 | var _loop_2 = function (i) {
|
1170 | if (toProcess[i]) {
|
1171 | exceptionGuard(function () {
|
1172 | _this.onMessage_(toProcess[i]);
|
1173 | });
|
1174 | }
|
1175 | };
|
1176 | for (var i = 0; i < toProcess.length; ++i) {
|
1177 | _loop_2(i);
|
1178 | }
|
1179 | if (this_1.currentResponseNum === this_1.closeAfterResponse) {
|
1180 | if (this_1.onClose) {
|
1181 | this_1.onClose();
|
1182 | this_1.onClose = null;
|
1183 | }
|
1184 | return "break";
|
1185 | }
|
1186 | this_1.currentResponseNum++;
|
1187 | };
|
1188 | var this_1 = this;
|
1189 | while (this.pendingResponses[this.currentResponseNum]) {
|
1190 | var state_1 = _loop_1();
|
1191 | if (state_1 === "break")
|
1192 | break;
|
1193 | }
|
1194 | };
|
1195 | return PacketReceiver;
|
1196 | }());
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 | var FIREBASE_LONGPOLL_START_PARAM = 'start';
|
1216 | var FIREBASE_LONGPOLL_CLOSE_COMMAND = 'close';
|
1217 | var FIREBASE_LONGPOLL_COMMAND_CB_NAME = 'pLPCommand';
|
1218 | var FIREBASE_LONGPOLL_DATA_CB_NAME = 'pRTLPCB';
|
1219 | var FIREBASE_LONGPOLL_ID_PARAM = 'id';
|
1220 | var FIREBASE_LONGPOLL_PW_PARAM = 'pw';
|
1221 | var FIREBASE_LONGPOLL_SERIAL_PARAM = 'ser';
|
1222 | var FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = 'cb';
|
1223 | var FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = 'seg';
|
1224 | var FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = 'ts';
|
1225 | var FIREBASE_LONGPOLL_DATA_PARAM = 'd';
|
1226 | var FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = 'dframe';
|
1227 |
|
1228 |
|
1229 |
|
1230 | var MAX_URL_DATA_SIZE = 1870;
|
1231 | var SEG_HEADER_SIZE = 30;
|
1232 | var MAX_PAYLOAD_SIZE = MAX_URL_DATA_SIZE - SEG_HEADER_SIZE;
|
1233 |
|
1234 |
|
1235 |
|
1236 |
|
1237 |
|
1238 | var KEEPALIVE_REQUEST_INTERVAL = 25000;
|
1239 |
|
1240 |
|
1241 |
|
1242 | var LP_CONNECT_TIMEOUT = 30000;
|
1243 |
|
1244 |
|
1245 |
|
1246 | var BrowserPollConnection = (function () {
|
1247 | |
1248 |
|
1249 |
|
1250 |
|
1251 |
|
1252 |
|
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 | function BrowserPollConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
|
1259 | var _this = this;
|
1260 | this.connId = connId;
|
1261 | this.repoInfo = repoInfo;
|
1262 | this.applicationId = applicationId;
|
1263 | this.appCheckToken = appCheckToken;
|
1264 | this.authToken = authToken;
|
1265 | this.transportSessionId = transportSessionId;
|
1266 | this.lastSessionId = lastSessionId;
|
1267 | this.bytesSent = 0;
|
1268 | this.bytesReceived = 0;
|
1269 | this.everConnected_ = false;
|
1270 | this.log_ = logWrapper(connId);
|
1271 | this.stats_ = statsManagerGetCollection(repoInfo);
|
1272 | this.urlFn = function (params) {
|
1273 |
|
1274 | if (_this.appCheckToken) {
|
1275 | params[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
|
1276 | }
|
1277 | return repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
|
1278 | };
|
1279 | }
|
1280 | |
1281 |
|
1282 |
|
1283 |
|
1284 | BrowserPollConnection.prototype.open = function (onMessage, onDisconnect) {
|
1285 | var _this = this;
|
1286 | this.curSegmentNum = 0;
|
1287 | this.onDisconnect_ = onDisconnect;
|
1288 | this.myPacketOrderer = new PacketReceiver(onMessage);
|
1289 | this.isClosed_ = false;
|
1290 | this.connectTimeoutTimer_ = setTimeout(function () {
|
1291 | _this.log_('Timed out trying to connect.');
|
1292 |
|
1293 | _this.onClosed_();
|
1294 | _this.connectTimeoutTimer_ = null;
|
1295 |
|
1296 | }, Math.floor(LP_CONNECT_TIMEOUT));
|
1297 |
|
1298 | executeWhenDOMReady(function () {
|
1299 | if (_this.isClosed_) {
|
1300 | return;
|
1301 | }
|
1302 |
|
1303 | _this.scriptTagHolder = new FirebaseIFrameScriptHolder(function () {
|
1304 | var args = [];
|
1305 | for (var _i = 0; _i < arguments.length; _i++) {
|
1306 | args[_i] = arguments[_i];
|
1307 | }
|
1308 | var _a = tslib.__read(args, 5), command = _a[0], arg1 = _a[1], arg2 = _a[2], arg3 = _a[3], arg4 = _a[4];
|
1309 | _this.incrementIncomingBytes_(args);
|
1310 | if (!_this.scriptTagHolder) {
|
1311 | return;
|
1312 | }
|
1313 | if (_this.connectTimeoutTimer_) {
|
1314 | clearTimeout(_this.connectTimeoutTimer_);
|
1315 | _this.connectTimeoutTimer_ = null;
|
1316 | }
|
1317 | _this.everConnected_ = true;
|
1318 | if (command === FIREBASE_LONGPOLL_START_PARAM) {
|
1319 | _this.id = arg1;
|
1320 | _this.password = arg2;
|
1321 | }
|
1322 | else if (command === FIREBASE_LONGPOLL_CLOSE_COMMAND) {
|
1323 |
|
1324 | if (arg1) {
|
1325 |
|
1326 |
|
1327 | _this.scriptTagHolder.sendNewPolls = false;
|
1328 |
|
1329 |
|
1330 | _this.myPacketOrderer.closeAfter(arg1, function () {
|
1331 | _this.onClosed_();
|
1332 | });
|
1333 | }
|
1334 | else {
|
1335 | _this.onClosed_();
|
1336 | }
|
1337 | }
|
1338 | else {
|
1339 | throw new Error('Unrecognized command received: ' + command);
|
1340 | }
|
1341 | }, function () {
|
1342 | var args = [];
|
1343 | for (var _i = 0; _i < arguments.length; _i++) {
|
1344 | args[_i] = arguments[_i];
|
1345 | }
|
1346 | var _a = tslib.__read(args, 2), pN = _a[0], data = _a[1];
|
1347 | _this.incrementIncomingBytes_(args);
|
1348 | _this.myPacketOrderer.handleResponse(pN, data);
|
1349 | }, function () {
|
1350 | _this.onClosed_();
|
1351 | }, _this.urlFn);
|
1352 |
|
1353 |
|
1354 | var urlParams = {};
|
1355 | urlParams[FIREBASE_LONGPOLL_START_PARAM] = 't';
|
1356 | urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = Math.floor(Math.random() * 100000000);
|
1357 | if (_this.scriptTagHolder.uniqueCallbackIdentifier) {
|
1358 | urlParams[FIREBASE_LONGPOLL_CALLBACK_ID_PARAM] = _this.scriptTagHolder.uniqueCallbackIdentifier;
|
1359 | }
|
1360 | urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
|
1361 | if (_this.transportSessionId) {
|
1362 | urlParams[TRANSPORT_SESSION_PARAM] = _this.transportSessionId;
|
1363 | }
|
1364 | if (_this.lastSessionId) {
|
1365 | urlParams[LAST_SESSION_PARAM] = _this.lastSessionId;
|
1366 | }
|
1367 | if (_this.applicationId) {
|
1368 | urlParams[APPLICATION_ID_PARAM] = _this.applicationId;
|
1369 | }
|
1370 | if (_this.appCheckToken) {
|
1371 | urlParams[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
|
1372 | }
|
1373 | if (typeof location !== 'undefined' &&
|
1374 | location.hostname &&
|
1375 | FORGE_DOMAIN_RE.test(location.hostname)) {
|
1376 | urlParams[REFERER_PARAM] = FORGE_REF;
|
1377 | }
|
1378 | var connectURL = _this.urlFn(urlParams);
|
1379 | _this.log_('Connecting via long-poll to ' + connectURL);
|
1380 | _this.scriptTagHolder.addTag(connectURL, function () {
|
1381 |
|
1382 | });
|
1383 | });
|
1384 | };
|
1385 | |
1386 |
|
1387 |
|
1388 | BrowserPollConnection.prototype.start = function () {
|
1389 | this.scriptTagHolder.startLongPoll(this.id, this.password);
|
1390 | this.addDisconnectPingFrame(this.id, this.password);
|
1391 | };
|
1392 | |
1393 |
|
1394 |
|
1395 | BrowserPollConnection.forceAllow = function () {
|
1396 | BrowserPollConnection.forceAllow_ = true;
|
1397 | };
|
1398 | |
1399 |
|
1400 |
|
1401 | BrowserPollConnection.forceDisallow = function () {
|
1402 | BrowserPollConnection.forceDisallow_ = true;
|
1403 | };
|
1404 |
|
1405 | BrowserPollConnection.isAvailable = function () {
|
1406 | if (util.isNodeSdk()) {
|
1407 | return false;
|
1408 | }
|
1409 | else if (BrowserPollConnection.forceAllow_) {
|
1410 | return true;
|
1411 | }
|
1412 | else {
|
1413 |
|
1414 |
|
1415 | return (!BrowserPollConnection.forceDisallow_ &&
|
1416 | typeof document !== 'undefined' &&
|
1417 | document.createElement != null &&
|
1418 | !isChromeExtensionContentScript() &&
|
1419 | !isWindowsStoreApp());
|
1420 | }
|
1421 | };
|
1422 | |
1423 |
|
1424 |
|
1425 | BrowserPollConnection.prototype.markConnectionHealthy = function () { };
|
1426 | |
1427 |
|
1428 |
|
1429 | BrowserPollConnection.prototype.shutdown_ = function () {
|
1430 | this.isClosed_ = true;
|
1431 | if (this.scriptTagHolder) {
|
1432 | this.scriptTagHolder.close();
|
1433 | this.scriptTagHolder = null;
|
1434 | }
|
1435 |
|
1436 | if (this.myDisconnFrame) {
|
1437 | document.body.removeChild(this.myDisconnFrame);
|
1438 | this.myDisconnFrame = null;
|
1439 | }
|
1440 | if (this.connectTimeoutTimer_) {
|
1441 | clearTimeout(this.connectTimeoutTimer_);
|
1442 | this.connectTimeoutTimer_ = null;
|
1443 | }
|
1444 | };
|
1445 | |
1446 |
|
1447 |
|
1448 | BrowserPollConnection.prototype.onClosed_ = function () {
|
1449 | if (!this.isClosed_) {
|
1450 | this.log_('Longpoll is closing itself');
|
1451 | this.shutdown_();
|
1452 | if (this.onDisconnect_) {
|
1453 | this.onDisconnect_(this.everConnected_);
|
1454 | this.onDisconnect_ = null;
|
1455 | }
|
1456 | }
|
1457 | };
|
1458 | |
1459 |
|
1460 |
|
1461 |
|
1462 | BrowserPollConnection.prototype.close = function () {
|
1463 | if (!this.isClosed_) {
|
1464 | this.log_('Longpoll is being closed.');
|
1465 | this.shutdown_();
|
1466 | }
|
1467 | };
|
1468 | |
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 | BrowserPollConnection.prototype.send = function (data) {
|
1474 | var dataStr = util.stringify(data);
|
1475 | this.bytesSent += dataStr.length;
|
1476 | this.stats_.incrementCounter('bytes_sent', dataStr.length);
|
1477 |
|
1478 | var base64data = util.base64Encode(dataStr);
|
1479 |
|
1480 |
|
1481 | var dataSegs = splitStringBySize(base64data, MAX_PAYLOAD_SIZE);
|
1482 |
|
1483 |
|
1484 | for (var i = 0; i < dataSegs.length; i++) {
|
1485 | this.scriptTagHolder.enqueueSegment(this.curSegmentNum, dataSegs.length, dataSegs[i]);
|
1486 | this.curSegmentNum++;
|
1487 | }
|
1488 | };
|
1489 | |
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 | BrowserPollConnection.prototype.addDisconnectPingFrame = function (id, pw) {
|
1495 | if (util.isNodeSdk()) {
|
1496 | return;
|
1497 | }
|
1498 | this.myDisconnFrame = document.createElement('iframe');
|
1499 | var urlParams = {};
|
1500 | urlParams[FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM] = 't';
|
1501 | urlParams[FIREBASE_LONGPOLL_ID_PARAM] = id;
|
1502 | urlParams[FIREBASE_LONGPOLL_PW_PARAM] = pw;
|
1503 | this.myDisconnFrame.src = this.urlFn(urlParams);
|
1504 | this.myDisconnFrame.style.display = 'none';
|
1505 | document.body.appendChild(this.myDisconnFrame);
|
1506 | };
|
1507 | |
1508 |
|
1509 |
|
1510 | BrowserPollConnection.prototype.incrementIncomingBytes_ = function (args) {
|
1511 |
|
1512 | var bytesReceived = util.stringify(args).length;
|
1513 | this.bytesReceived += bytesReceived;
|
1514 | this.stats_.incrementCounter('bytes_received', bytesReceived);
|
1515 | };
|
1516 | return BrowserPollConnection;
|
1517 | }());
|
1518 |
|
1519 |
|
1520 |
|
1521 | var FirebaseIFrameScriptHolder = (function () {
|
1522 | |
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 |
|
1528 | function FirebaseIFrameScriptHolder(commandCB, onMessageCB, onDisconnect, urlFn) {
|
1529 | this.onDisconnect = onDisconnect;
|
1530 | this.urlFn = urlFn;
|
1531 |
|
1532 |
|
1533 | this.outstandingRequests = new Set();
|
1534 |
|
1535 | this.pendingSegs = [];
|
1536 |
|
1537 |
|
1538 |
|
1539 |
|
1540 |
|
1541 | this.currentSerial = Math.floor(Math.random() * 100000000);
|
1542 |
|
1543 |
|
1544 | this.sendNewPolls = true;
|
1545 | if (!util.isNodeSdk()) {
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 | this.uniqueCallbackIdentifier = LUIDGenerator();
|
1551 | window[FIREBASE_LONGPOLL_COMMAND_CB_NAME + this.uniqueCallbackIdentifier] = commandCB;
|
1552 | window[FIREBASE_LONGPOLL_DATA_CB_NAME + this.uniqueCallbackIdentifier] = onMessageCB;
|
1553 |
|
1554 | this.myIFrame = FirebaseIFrameScriptHolder.createIFrame_();
|
1555 |
|
1556 | var script = '';
|
1557 |
|
1558 |
|
1559 | if (this.myIFrame.src &&
|
1560 | this.myIFrame.src.substr(0, 'javascript:'.length) === 'javascript:') {
|
1561 | var currentDomain = document.domain;
|
1562 | script = '<script>document.domain="' + currentDomain + '";</script>';
|
1563 | }
|
1564 | var iframeContents = '<html><body>' + script + '</body></html>';
|
1565 | try {
|
1566 | this.myIFrame.doc.open();
|
1567 | this.myIFrame.doc.write(iframeContents);
|
1568 | this.myIFrame.doc.close();
|
1569 | }
|
1570 | catch (e) {
|
1571 | log('frame writing exception');
|
1572 | if (e.stack) {
|
1573 | log(e.stack);
|
1574 | }
|
1575 | log(e);
|
1576 | }
|
1577 | }
|
1578 | else {
|
1579 | this.commandCB = commandCB;
|
1580 | this.onMessageCB = onMessageCB;
|
1581 | }
|
1582 | }
|
1583 | |
1584 |
|
1585 |
|
1586 |
|
1587 | FirebaseIFrameScriptHolder.createIFrame_ = function () {
|
1588 | var iframe = document.createElement('iframe');
|
1589 | iframe.style.display = 'none';
|
1590 |
|
1591 | if (document.body) {
|
1592 | document.body.appendChild(iframe);
|
1593 | try {
|
1594 |
|
1595 |
|
1596 |
|
1597 | var a = iframe.contentWindow.document;
|
1598 | if (!a) {
|
1599 |
|
1600 | log('No IE domain setting required');
|
1601 | }
|
1602 | }
|
1603 | catch (e) {
|
1604 | var domain = document.domain;
|
1605 | iframe.src =
|
1606 | "javascript:void((function(){document.open();document.domain='" +
|
1607 | domain +
|
1608 | "';document.close();})())";
|
1609 | }
|
1610 | }
|
1611 | else {
|
1612 |
|
1613 |
|
1614 | throw 'Document body has not initialized. Wait to initialize Firebase until after the document is ready.';
|
1615 | }
|
1616 |
|
1617 | if (iframe.contentDocument) {
|
1618 | iframe.doc = iframe.contentDocument;
|
1619 | }
|
1620 | else if (iframe.contentWindow) {
|
1621 | iframe.doc = iframe.contentWindow.document;
|
1622 |
|
1623 | }
|
1624 | else if (iframe.document) {
|
1625 |
|
1626 | iframe.doc = iframe.document;
|
1627 | }
|
1628 | return iframe;
|
1629 | };
|
1630 | |
1631 |
|
1632 |
|
1633 | FirebaseIFrameScriptHolder.prototype.close = function () {
|
1634 | var _this = this;
|
1635 |
|
1636 | this.alive = false;
|
1637 | if (this.myIFrame) {
|
1638 |
|
1639 |
|
1640 |
|
1641 | this.myIFrame.doc.body.innerHTML = '';
|
1642 | setTimeout(function () {
|
1643 | if (_this.myIFrame !== null) {
|
1644 | document.body.removeChild(_this.myIFrame);
|
1645 | _this.myIFrame = null;
|
1646 | }
|
1647 | }, Math.floor(0));
|
1648 | }
|
1649 |
|
1650 | var onDisconnect = this.onDisconnect;
|
1651 | if (onDisconnect) {
|
1652 | this.onDisconnect = null;
|
1653 | onDisconnect();
|
1654 | }
|
1655 | };
|
1656 | |
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 | FirebaseIFrameScriptHolder.prototype.startLongPoll = function (id, pw) {
|
1662 | this.myID = id;
|
1663 | this.myPW = pw;
|
1664 | this.alive = true;
|
1665 |
|
1666 | while (this.newRequest_()) { }
|
1667 | };
|
1668 | |
1669 |
|
1670 |
|
1671 |
|
1672 |
|
1673 |
|
1674 |
|
1675 | FirebaseIFrameScriptHolder.prototype.newRequest_ = function () {
|
1676 |
|
1677 |
|
1678 |
|
1679 | if (this.alive &&
|
1680 | this.sendNewPolls &&
|
1681 | this.outstandingRequests.size < (this.pendingSegs.length > 0 ? 2 : 1)) {
|
1682 |
|
1683 | this.currentSerial++;
|
1684 | var urlParams = {};
|
1685 | urlParams[FIREBASE_LONGPOLL_ID_PARAM] = this.myID;
|
1686 | urlParams[FIREBASE_LONGPOLL_PW_PARAM] = this.myPW;
|
1687 | urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = this.currentSerial;
|
1688 | var theURL = this.urlFn(urlParams);
|
1689 |
|
1690 | var curDataString = '';
|
1691 | var i = 0;
|
1692 | while (this.pendingSegs.length > 0) {
|
1693 |
|
1694 | var nextSeg = this.pendingSegs[0];
|
1695 | if (nextSeg.d.length +
|
1696 | SEG_HEADER_SIZE +
|
1697 | curDataString.length <=
|
1698 | MAX_URL_DATA_SIZE) {
|
1699 |
|
1700 | var theSeg = this.pendingSegs.shift();
|
1701 | curDataString =
|
1702 | curDataString +
|
1703 | '&' +
|
1704 | FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM +
|
1705 | i +
|
1706 | '=' +
|
1707 | theSeg.seg +
|
1708 | '&' +
|
1709 | FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET +
|
1710 | i +
|
1711 | '=' +
|
1712 | theSeg.ts +
|
1713 | '&' +
|
1714 | FIREBASE_LONGPOLL_DATA_PARAM +
|
1715 | i +
|
1716 | '=' +
|
1717 | theSeg.d;
|
1718 | i++;
|
1719 | }
|
1720 | else {
|
1721 | break;
|
1722 | }
|
1723 | }
|
1724 | theURL = theURL + curDataString;
|
1725 | this.addLongPollTag_(theURL, this.currentSerial);
|
1726 | return true;
|
1727 | }
|
1728 | else {
|
1729 | return false;
|
1730 | }
|
1731 | };
|
1732 | |
1733 |
|
1734 |
|
1735 |
|
1736 |
|
1737 |
|
1738 | FirebaseIFrameScriptHolder.prototype.enqueueSegment = function (segnum, totalsegs, data) {
|
1739 |
|
1740 | this.pendingSegs.push({ seg: segnum, ts: totalsegs, d: data });
|
1741 |
|
1742 |
|
1743 | if (this.alive) {
|
1744 | this.newRequest_();
|
1745 | }
|
1746 | };
|
1747 | |
1748 |
|
1749 |
|
1750 |
|
1751 |
|
1752 | FirebaseIFrameScriptHolder.prototype.addLongPollTag_ = function (url, serial) {
|
1753 | var _this = this;
|
1754 |
|
1755 | this.outstandingRequests.add(serial);
|
1756 | var doNewRequest = function () {
|
1757 | _this.outstandingRequests.delete(serial);
|
1758 | _this.newRequest_();
|
1759 | };
|
1760 |
|
1761 |
|
1762 | var keepaliveTimeout = setTimeout(doNewRequest, Math.floor(KEEPALIVE_REQUEST_INTERVAL));
|
1763 | var readyStateCB = function () {
|
1764 |
|
1765 | clearTimeout(keepaliveTimeout);
|
1766 |
|
1767 | doNewRequest();
|
1768 | };
|
1769 | this.addTag(url, readyStateCB);
|
1770 | };
|
1771 | |
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 | FirebaseIFrameScriptHolder.prototype.addTag = function (url, loadCB) {
|
1777 | var _this = this;
|
1778 | if (util.isNodeSdk()) {
|
1779 |
|
1780 | this.doNodeLongPoll(url, loadCB);
|
1781 | }
|
1782 | else {
|
1783 | setTimeout(function () {
|
1784 | try {
|
1785 |
|
1786 | if (!_this.sendNewPolls) {
|
1787 | return;
|
1788 | }
|
1789 | var newScript_1 = _this.myIFrame.doc.createElement('script');
|
1790 | newScript_1.type = 'text/javascript';
|
1791 | newScript_1.async = true;
|
1792 | newScript_1.src = url;
|
1793 |
|
1794 | newScript_1.onload = newScript_1.onreadystatechange = function () {
|
1795 |
|
1796 | var rstate = newScript_1.readyState;
|
1797 | if (!rstate || rstate === 'loaded' || rstate === 'complete') {
|
1798 |
|
1799 | newScript_1.onload = newScript_1.onreadystatechange = null;
|
1800 | if (newScript_1.parentNode) {
|
1801 | newScript_1.parentNode.removeChild(newScript_1);
|
1802 | }
|
1803 | loadCB();
|
1804 | }
|
1805 | };
|
1806 | newScript_1.onerror = function () {
|
1807 | log('Long-poll script failed to load: ' + url);
|
1808 | _this.sendNewPolls = false;
|
1809 | _this.close();
|
1810 | };
|
1811 | _this.myIFrame.doc.body.appendChild(newScript_1);
|
1812 | }
|
1813 | catch (e) {
|
1814 |
|
1815 | }
|
1816 | }, Math.floor(1));
|
1817 | }
|
1818 | };
|
1819 | return FirebaseIFrameScriptHolder;
|
1820 | }());
|
1821 |
|
1822 |
|
1823 |
|
1824 |
|
1825 |
|
1826 |
|
1827 |
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 | var WEBSOCKET_MAX_FRAME_SIZE = 16384;
|
1839 | var WEBSOCKET_KEEPALIVE_INTERVAL = 45000;
|
1840 | var WebSocketImpl = null;
|
1841 | if (typeof MozWebSocket !== 'undefined') {
|
1842 | WebSocketImpl = MozWebSocket;
|
1843 | }
|
1844 | else if (typeof WebSocket !== 'undefined') {
|
1845 | WebSocketImpl = WebSocket;
|
1846 | }
|
1847 | function setWebSocketImpl(impl) {
|
1848 | WebSocketImpl = impl;
|
1849 | }
|
1850 |
|
1851 |
|
1852 |
|
1853 | var WebSocketConnection = (function () {
|
1854 | |
1855 |
|
1856 |
|
1857 |
|
1858 |
|
1859 |
|
1860 |
|
1861 |
|
1862 |
|
1863 |
|
1864 |
|
1865 | function WebSocketConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
|
1866 | this.connId = connId;
|
1867 | this.applicationId = applicationId;
|
1868 | this.appCheckToken = appCheckToken;
|
1869 | this.authToken = authToken;
|
1870 | this.keepaliveTimer = null;
|
1871 | this.frames = null;
|
1872 | this.totalFrames = 0;
|
1873 | this.bytesSent = 0;
|
1874 | this.bytesReceived = 0;
|
1875 | this.log_ = logWrapper(this.connId);
|
1876 | this.stats_ = statsManagerGetCollection(repoInfo);
|
1877 | this.connURL = WebSocketConnection.connectionURL_(repoInfo, transportSessionId, lastSessionId, appCheckToken);
|
1878 | this.nodeAdmin = repoInfo.nodeAdmin;
|
1879 | }
|
1880 | |
1881 |
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 | WebSocketConnection.connectionURL_ = function (repoInfo, transportSessionId, lastSessionId, appCheckToken) {
|
1888 | var urlParams = {};
|
1889 | urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
|
1890 | if (!util.isNodeSdk() &&
|
1891 | typeof location !== 'undefined' &&
|
1892 | location.hostname &&
|
1893 | FORGE_DOMAIN_RE.test(location.hostname)) {
|
1894 | urlParams[REFERER_PARAM] = FORGE_REF;
|
1895 | }
|
1896 | if (transportSessionId) {
|
1897 | urlParams[TRANSPORT_SESSION_PARAM] = transportSessionId;
|
1898 | }
|
1899 | if (lastSessionId) {
|
1900 | urlParams[LAST_SESSION_PARAM] = lastSessionId;
|
1901 | }
|
1902 | if (appCheckToken) {
|
1903 | urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
|
1904 | }
|
1905 | return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
|
1906 | };
|
1907 | |
1908 |
|
1909 |
|
1910 |
|
1911 | WebSocketConnection.prototype.open = function (onMessage, onDisconnect) {
|
1912 | var _this = this;
|
1913 | this.onDisconnect = onDisconnect;
|
1914 | this.onMessage = onMessage;
|
1915 | this.log_('Websocket connecting to ' + this.connURL);
|
1916 | this.everConnected_ = false;
|
1917 |
|
1918 | PersistentStorage.set('previous_websocket_failure', true);
|
1919 | try {
|
1920 | if (util.isNodeSdk()) {
|
1921 | var device = this.nodeAdmin ? 'AdminNode' : 'Node';
|
1922 |
|
1923 | var options = {
|
1924 | headers: {
|
1925 | 'User-Agent': "Firebase/" + PROTOCOL_VERSION + "/" + SDK_VERSION + "/" + process.platform + "/" + device,
|
1926 | 'X-Firebase-GMPID': this.applicationId || ''
|
1927 | }
|
1928 | };
|
1929 |
|
1930 |
|
1931 | if (this.nodeAdmin) {
|
1932 | options.headers['Authorization'] = this.authToken || '';
|
1933 | }
|
1934 | else {
|
1935 |
|
1936 |
|
1937 |
|
1938 |
|
1939 |
|
1940 | options.headers['X-Firebase-AppCheck'] = this.appCheckToken || '';
|
1941 | }
|
1942 |
|
1943 | var env = process['env'];
|
1944 | var proxy = this.connURL.indexOf('wss://') === 0
|
1945 | ? env['HTTPS_PROXY'] || env['https_proxy']
|
1946 | : env['HTTP_PROXY'] || env['http_proxy'];
|
1947 | if (proxy) {
|
1948 | options['proxy'] = { origin: proxy };
|
1949 | }
|
1950 | this.mySock = new WebSocketImpl(this.connURL, [], options);
|
1951 | }
|
1952 | else {
|
1953 | var options = {
|
1954 | headers: {
|
1955 | 'X-Firebase-GMPID': this.applicationId || '',
|
1956 | 'X-Firebase-AppCheck': this.appCheckToken || ''
|
1957 | }
|
1958 | };
|
1959 | this.mySock = new WebSocketImpl(this.connURL, [], options);
|
1960 | }
|
1961 | }
|
1962 | catch (e) {
|
1963 | this.log_('Error instantiating WebSocket.');
|
1964 | var error = e.message || e.data;
|
1965 | if (error) {
|
1966 | this.log_(error);
|
1967 | }
|
1968 | this.onClosed_();
|
1969 | return;
|
1970 | }
|
1971 | this.mySock.onopen = function () {
|
1972 | _this.log_('Websocket connected.');
|
1973 | _this.everConnected_ = true;
|
1974 | };
|
1975 | this.mySock.onclose = function () {
|
1976 | _this.log_('Websocket connection was disconnected.');
|
1977 | _this.mySock = null;
|
1978 | _this.onClosed_();
|
1979 | };
|
1980 | this.mySock.onmessage = function (m) {
|
1981 | _this.handleIncomingFrame(m);
|
1982 | };
|
1983 | this.mySock.onerror = function (e) {
|
1984 | _this.log_('WebSocket error. Closing connection.');
|
1985 |
|
1986 | var error = e.message || e.data;
|
1987 | if (error) {
|
1988 | _this.log_(error);
|
1989 | }
|
1990 | _this.onClosed_();
|
1991 | };
|
1992 | };
|
1993 | |
1994 |
|
1995 |
|
1996 | WebSocketConnection.prototype.start = function () { };
|
1997 | WebSocketConnection.forceDisallow = function () {
|
1998 | WebSocketConnection.forceDisallow_ = true;
|
1999 | };
|
2000 | WebSocketConnection.isAvailable = function () {
|
2001 | var isOldAndroid = false;
|
2002 | if (typeof navigator !== 'undefined' && navigator.userAgent) {
|
2003 | var oldAndroidRegex = /Android ([0-9]{0,}\.[0-9]{0,})/;
|
2004 | var oldAndroidMatch = navigator.userAgent.match(oldAndroidRegex);
|
2005 | if (oldAndroidMatch && oldAndroidMatch.length > 1) {
|
2006 | if (parseFloat(oldAndroidMatch[1]) < 4.4) {
|
2007 | isOldAndroid = true;
|
2008 | }
|
2009 | }
|
2010 | }
|
2011 | return (!isOldAndroid &&
|
2012 | WebSocketImpl !== null &&
|
2013 | !WebSocketConnection.forceDisallow_);
|
2014 | };
|
2015 | |
2016 |
|
2017 |
|
2018 | WebSocketConnection.previouslyFailed = function () {
|
2019 |
|
2020 |
|
2021 | return (PersistentStorage.isInMemoryStorage ||
|
2022 | PersistentStorage.get('previous_websocket_failure') === true);
|
2023 | };
|
2024 | WebSocketConnection.prototype.markConnectionHealthy = function () {
|
2025 | PersistentStorage.remove('previous_websocket_failure');
|
2026 | };
|
2027 | WebSocketConnection.prototype.appendFrame_ = function (data) {
|
2028 | this.frames.push(data);
|
2029 | if (this.frames.length === this.totalFrames) {
|
2030 | var fullMess = this.frames.join('');
|
2031 | this.frames = null;
|
2032 | var jsonMess = util.jsonEval(fullMess);
|
2033 |
|
2034 | this.onMessage(jsonMess);
|
2035 | }
|
2036 | };
|
2037 | |
2038 |
|
2039 |
|
2040 | WebSocketConnection.prototype.handleNewFrameCount_ = function (frameCount) {
|
2041 | this.totalFrames = frameCount;
|
2042 | this.frames = [];
|
2043 | };
|
2044 | |
2045 |
|
2046 |
|
2047 |
|
2048 | WebSocketConnection.prototype.extractFrameCount_ = function (data) {
|
2049 | util.assert(this.frames === null, 'We already have a frame buffer');
|
2050 |
|
2051 |
|
2052 | if (data.length <= 6) {
|
2053 | var frameCount = Number(data);
|
2054 | if (!isNaN(frameCount)) {
|
2055 | this.handleNewFrameCount_(frameCount);
|
2056 | return null;
|
2057 | }
|
2058 | }
|
2059 | this.handleNewFrameCount_(1);
|
2060 | return data;
|
2061 | };
|
2062 | |
2063 |
|
2064 |
|
2065 |
|
2066 | WebSocketConnection.prototype.handleIncomingFrame = function (mess) {
|
2067 | if (this.mySock === null) {
|
2068 | return;
|
2069 | }
|
2070 | var data = mess['data'];
|
2071 | this.bytesReceived += data.length;
|
2072 | this.stats_.incrementCounter('bytes_received', data.length);
|
2073 | this.resetKeepAlive();
|
2074 | if (this.frames !== null) {
|
2075 |
|
2076 | this.appendFrame_(data);
|
2077 | }
|
2078 | else {
|
2079 |
|
2080 | var remainingData = this.extractFrameCount_(data);
|
2081 | if (remainingData !== null) {
|
2082 | this.appendFrame_(remainingData);
|
2083 | }
|
2084 | }
|
2085 | };
|
2086 | |
2087 |
|
2088 |
|
2089 |
|
2090 | WebSocketConnection.prototype.send = function (data) {
|
2091 | this.resetKeepAlive();
|
2092 | var dataStr = util.stringify(data);
|
2093 | this.bytesSent += dataStr.length;
|
2094 | this.stats_.incrementCounter('bytes_sent', dataStr.length);
|
2095 |
|
2096 |
|
2097 | var dataSegs = splitStringBySize(dataStr, WEBSOCKET_MAX_FRAME_SIZE);
|
2098 |
|
2099 | if (dataSegs.length > 1) {
|
2100 | this.sendString_(String(dataSegs.length));
|
2101 | }
|
2102 |
|
2103 | for (var i = 0; i < dataSegs.length; i++) {
|
2104 | this.sendString_(dataSegs[i]);
|
2105 | }
|
2106 | };
|
2107 | WebSocketConnection.prototype.shutdown_ = function () {
|
2108 | this.isClosed_ = true;
|
2109 | if (this.keepaliveTimer) {
|
2110 | clearInterval(this.keepaliveTimer);
|
2111 | this.keepaliveTimer = null;
|
2112 | }
|
2113 | if (this.mySock) {
|
2114 | this.mySock.close();
|
2115 | this.mySock = null;
|
2116 | }
|
2117 | };
|
2118 | WebSocketConnection.prototype.onClosed_ = function () {
|
2119 | if (!this.isClosed_) {
|
2120 | this.log_('WebSocket is closing itself');
|
2121 | this.shutdown_();
|
2122 |
|
2123 | if (this.onDisconnect) {
|
2124 | this.onDisconnect(this.everConnected_);
|
2125 | this.onDisconnect = null;
|
2126 | }
|
2127 | }
|
2128 | };
|
2129 | |
2130 |
|
2131 |
|
2132 |
|
2133 | WebSocketConnection.prototype.close = function () {
|
2134 | if (!this.isClosed_) {
|
2135 | this.log_('WebSocket is being closed');
|
2136 | this.shutdown_();
|
2137 | }
|
2138 | };
|
2139 | |
2140 |
|
2141 |
|
2142 |
|
2143 | WebSocketConnection.prototype.resetKeepAlive = function () {
|
2144 | var _this = this;
|
2145 | clearInterval(this.keepaliveTimer);
|
2146 | this.keepaliveTimer = setInterval(function () {
|
2147 |
|
2148 | if (_this.mySock) {
|
2149 | _this.sendString_('0');
|
2150 | }
|
2151 | _this.resetKeepAlive();
|
2152 |
|
2153 | }, Math.floor(WEBSOCKET_KEEPALIVE_INTERVAL));
|
2154 | };
|
2155 | |
2156 |
|
2157 |
|
2158 |
|
2159 |
|
2160 | WebSocketConnection.prototype.sendString_ = function (str) {
|
2161 |
|
2162 |
|
2163 |
|
2164 | try {
|
2165 | this.mySock.send(str);
|
2166 | }
|
2167 | catch (e) {
|
2168 | this.log_('Exception thrown from WebSocket.send():', e.message || e.data, 'Closing connection.');
|
2169 | setTimeout(this.onClosed_.bind(this), 0);
|
2170 | }
|
2171 | };
|
2172 | |
2173 |
|
2174 |
|
2175 | WebSocketConnection.responsesRequiredToBeHealthy = 2;
|
2176 | |
2177 |
|
2178 |
|
2179 | WebSocketConnection.healthyTimeout = 30000;
|
2180 | return WebSocketConnection;
|
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 |
|
2206 | var TransportManager = (function () {
|
2207 | |
2208 |
|
2209 |
|
2210 | function TransportManager(repoInfo) {
|
2211 | this.initTransports_(repoInfo);
|
2212 | }
|
2213 | Object.defineProperty(TransportManager, "ALL_TRANSPORTS", {
|
2214 | get: function () {
|
2215 | return [BrowserPollConnection, WebSocketConnection];
|
2216 | },
|
2217 | enumerable: false,
|
2218 | configurable: true
|
2219 | });
|
2220 | TransportManager.prototype.initTransports_ = function (repoInfo) {
|
2221 | var e_1, _a;
|
2222 | var isWebSocketsAvailable = WebSocketConnection && WebSocketConnection['isAvailable']();
|
2223 | var isSkipPollConnection = isWebSocketsAvailable && !WebSocketConnection.previouslyFailed();
|
2224 | if (repoInfo.webSocketOnly) {
|
2225 | if (!isWebSocketsAvailable) {
|
2226 | warn("wss:// URL used, but browser isn't known to support websockets. Trying anyway.");
|
2227 | }
|
2228 | isSkipPollConnection = true;
|
2229 | }
|
2230 | if (isSkipPollConnection) {
|
2231 | this.transports_ = [WebSocketConnection];
|
2232 | }
|
2233 | else {
|
2234 | var transports = (this.transports_ = []);
|
2235 | try {
|
2236 | for (var _b = tslib.__values(TransportManager.ALL_TRANSPORTS), _c = _b.next(); !_c.done; _c = _b.next()) {
|
2237 | var transport = _c.value;
|
2238 | if (transport && transport['isAvailable']()) {
|
2239 | transports.push(transport);
|
2240 | }
|
2241 | }
|
2242 | }
|
2243 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
2244 | finally {
|
2245 | try {
|
2246 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
2247 | }
|
2248 | finally { if (e_1) throw e_1.error; }
|
2249 | }
|
2250 | }
|
2251 | };
|
2252 | |
2253 |
|
2254 |
|
2255 | TransportManager.prototype.initialTransport = function () {
|
2256 | if (this.transports_.length > 0) {
|
2257 | return this.transports_[0];
|
2258 | }
|
2259 | else {
|
2260 | throw new Error('No transports available');
|
2261 | }
|
2262 | };
|
2263 | |
2264 |
|
2265 |
|
2266 | TransportManager.prototype.upgradeTransport = function () {
|
2267 | if (this.transports_.length > 1) {
|
2268 | return this.transports_[1];
|
2269 | }
|
2270 | else {
|
2271 | return null;
|
2272 | }
|
2273 | };
|
2274 | return TransportManager;
|
2275 | }());
|
2276 |
|
2277 |
|
2278 |
|
2279 |
|
2280 |
|
2281 |
|
2282 |
|
2283 |
|
2284 |
|
2285 |
|
2286 |
|
2287 |
|
2288 |
|
2289 |
|
2290 |
|
2291 |
|
2292 |
|
2293 |
|
2294 | var UPGRADE_TIMEOUT = 60000;
|
2295 |
|
2296 |
|
2297 | var DELAY_BEFORE_SENDING_EXTRA_REQUESTS = 5000;
|
2298 |
|
2299 |
|
2300 |
|
2301 | var BYTES_SENT_HEALTHY_OVERRIDE = 10 * 1024;
|
2302 | var BYTES_RECEIVED_HEALTHY_OVERRIDE = 100 * 1024;
|
2303 | var MESSAGE_TYPE = 't';
|
2304 | var MESSAGE_DATA = 'd';
|
2305 | var CONTROL_SHUTDOWN = 's';
|
2306 | var CONTROL_RESET = 'r';
|
2307 | var CONTROL_ERROR = 'e';
|
2308 | var CONTROL_PONG = 'o';
|
2309 | var SWITCH_ACK = 'a';
|
2310 | var END_TRANSMISSION = 'n';
|
2311 | var PING = 'p';
|
2312 | var SERVER_HELLO = 'h';
|
2313 |
|
2314 |
|
2315 |
|
2316 |
|
2317 | var Connection = (function () {
|
2318 | |
2319 |
|
2320 |
|
2321 |
|
2322 |
|
2323 |
|
2324 |
|
2325 |
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 | function Connection(id, repoInfo_, applicationId_, appCheckToken_, authToken_, onMessage_, onReady_, onDisconnect_, onKill_, lastSessionId) {
|
2331 | this.id = id;
|
2332 | this.repoInfo_ = repoInfo_;
|
2333 | this.applicationId_ = applicationId_;
|
2334 | this.appCheckToken_ = appCheckToken_;
|
2335 | this.authToken_ = authToken_;
|
2336 | this.onMessage_ = onMessage_;
|
2337 | this.onReady_ = onReady_;
|
2338 | this.onDisconnect_ = onDisconnect_;
|
2339 | this.onKill_ = onKill_;
|
2340 | this.lastSessionId = lastSessionId;
|
2341 | this.connectionCount = 0;
|
2342 | this.pendingDataMessages = [];
|
2343 | this.state_ = 0 ;
|
2344 | this.log_ = logWrapper('c:' + this.id + ':');
|
2345 | this.transportManager_ = new TransportManager(repoInfo_);
|
2346 | this.log_('Connection created');
|
2347 | this.start_();
|
2348 | }
|
2349 | |
2350 |
|
2351 |
|
2352 | Connection.prototype.start_ = function () {
|
2353 | var _this = this;
|
2354 | var conn = this.transportManager_.initialTransport();
|
2355 | this.conn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.lastSessionId);
|
2356 |
|
2357 |
|
2358 | this.primaryResponsesRequired_ = conn['responsesRequiredToBeHealthy'] || 0;
|
2359 | var onMessageReceived = this.connReceiver_(this.conn_);
|
2360 | var onConnectionLost = this.disconnReceiver_(this.conn_);
|
2361 | this.tx_ = this.conn_;
|
2362 | this.rx_ = this.conn_;
|
2363 | this.secondaryConn_ = null;
|
2364 | this.isHealthy_ = false;
|
2365 | |
2366 |
|
2367 |
|
2368 |
|
2369 |
|
2370 |
|
2371 | setTimeout(function () {
|
2372 |
|
2373 | _this.conn_ && _this.conn_.open(onMessageReceived, onConnectionLost);
|
2374 | }, Math.floor(0));
|
2375 | var healthyTimeoutMS = conn['healthyTimeout'] || 0;
|
2376 | if (healthyTimeoutMS > 0) {
|
2377 | this.healthyTimeout_ = setTimeoutNonBlocking(function () {
|
2378 | _this.healthyTimeout_ = null;
|
2379 | if (!_this.isHealthy_) {
|
2380 | if (_this.conn_ &&
|
2381 | _this.conn_.bytesReceived > BYTES_RECEIVED_HEALTHY_OVERRIDE) {
|
2382 | _this.log_('Connection exceeded healthy timeout but has received ' +
|
2383 | _this.conn_.bytesReceived +
|
2384 | ' bytes. Marking connection healthy.');
|
2385 | _this.isHealthy_ = true;
|
2386 | _this.conn_.markConnectionHealthy();
|
2387 | }
|
2388 | else if (_this.conn_ &&
|
2389 | _this.conn_.bytesSent > BYTES_SENT_HEALTHY_OVERRIDE) {
|
2390 | _this.log_('Connection exceeded healthy timeout but has sent ' +
|
2391 | _this.conn_.bytesSent +
|
2392 | ' bytes. Leaving connection alive.');
|
2393 |
|
2394 |
|
2395 | }
|
2396 | else {
|
2397 | _this.log_('Closing unhealthy connection after timeout.');
|
2398 | _this.close();
|
2399 | }
|
2400 | }
|
2401 |
|
2402 | }, Math.floor(healthyTimeoutMS));
|
2403 | }
|
2404 | };
|
2405 | Connection.prototype.nextTransportId_ = function () {
|
2406 | return 'c:' + this.id + ':' + this.connectionCount++;
|
2407 | };
|
2408 | Connection.prototype.disconnReceiver_ = function (conn) {
|
2409 | var _this = this;
|
2410 | return function (everConnected) {
|
2411 | if (conn === _this.conn_) {
|
2412 | _this.onConnectionLost_(everConnected);
|
2413 | }
|
2414 | else if (conn === _this.secondaryConn_) {
|
2415 | _this.log_('Secondary connection lost.');
|
2416 | _this.onSecondaryConnectionLost_();
|
2417 | }
|
2418 | else {
|
2419 | _this.log_('closing an old connection');
|
2420 | }
|
2421 | };
|
2422 | };
|
2423 | Connection.prototype.connReceiver_ = function (conn) {
|
2424 | var _this = this;
|
2425 | return function (message) {
|
2426 | if (_this.state_ !== 2 ) {
|
2427 | if (conn === _this.rx_) {
|
2428 | _this.onPrimaryMessageReceived_(message);
|
2429 | }
|
2430 | else if (conn === _this.secondaryConn_) {
|
2431 | _this.onSecondaryMessageReceived_(message);
|
2432 | }
|
2433 | else {
|
2434 | _this.log_('message on old connection');
|
2435 | }
|
2436 | }
|
2437 | };
|
2438 | };
|
2439 | |
2440 |
|
2441 |
|
2442 | Connection.prototype.sendRequest = function (dataMsg) {
|
2443 |
|
2444 | var msg = { t: 'd', d: dataMsg };
|
2445 | this.sendData_(msg);
|
2446 | };
|
2447 | Connection.prototype.tryCleanupConnection = function () {
|
2448 | if (this.tx_ === this.secondaryConn_ && this.rx_ === this.secondaryConn_) {
|
2449 | this.log_('cleaning up and promoting a connection: ' + this.secondaryConn_.connId);
|
2450 | this.conn_ = this.secondaryConn_;
|
2451 | this.secondaryConn_ = null;
|
2452 |
|
2453 | }
|
2454 | };
|
2455 | Connection.prototype.onSecondaryControl_ = function (controlData) {
|
2456 | if (MESSAGE_TYPE in controlData) {
|
2457 | var cmd = controlData[MESSAGE_TYPE];
|
2458 | if (cmd === SWITCH_ACK) {
|
2459 | this.upgradeIfSecondaryHealthy_();
|
2460 | }
|
2461 | else if (cmd === CONTROL_RESET) {
|
2462 |
|
2463 | this.log_('Got a reset on secondary, closing it');
|
2464 | this.secondaryConn_.close();
|
2465 |
|
2466 | if (this.tx_ === this.secondaryConn_ ||
|
2467 | this.rx_ === this.secondaryConn_) {
|
2468 | this.close();
|
2469 | }
|
2470 | }
|
2471 | else if (cmd === CONTROL_PONG) {
|
2472 | this.log_('got pong on secondary.');
|
2473 | this.secondaryResponsesRequired_--;
|
2474 | this.upgradeIfSecondaryHealthy_();
|
2475 | }
|
2476 | }
|
2477 | };
|
2478 | Connection.prototype.onSecondaryMessageReceived_ = function (parsedData) {
|
2479 | var layer = requireKey('t', parsedData);
|
2480 | var data = requireKey('d', parsedData);
|
2481 | if (layer === 'c') {
|
2482 | this.onSecondaryControl_(data);
|
2483 | }
|
2484 | else if (layer === 'd') {
|
2485 |
|
2486 | this.pendingDataMessages.push(data);
|
2487 | }
|
2488 | else {
|
2489 | throw new Error('Unknown protocol layer: ' + layer);
|
2490 | }
|
2491 | };
|
2492 | Connection.prototype.upgradeIfSecondaryHealthy_ = function () {
|
2493 | if (this.secondaryResponsesRequired_ <= 0) {
|
2494 | this.log_('Secondary connection is healthy.');
|
2495 | this.isHealthy_ = true;
|
2496 | this.secondaryConn_.markConnectionHealthy();
|
2497 | this.proceedWithUpgrade_();
|
2498 | }
|
2499 | else {
|
2500 |
|
2501 | this.log_('sending ping on secondary.');
|
2502 | this.secondaryConn_.send({ t: 'c', d: { t: PING, d: {} } });
|
2503 | }
|
2504 | };
|
2505 | Connection.prototype.proceedWithUpgrade_ = function () {
|
2506 |
|
2507 | this.secondaryConn_.start();
|
2508 |
|
2509 | this.log_('sending client ack on secondary');
|
2510 | this.secondaryConn_.send({ t: 'c', d: { t: SWITCH_ACK, d: {} } });
|
2511 |
|
2512 |
|
2513 | this.log_('Ending transmission on primary');
|
2514 | this.conn_.send({ t: 'c', d: { t: END_TRANSMISSION, d: {} } });
|
2515 | this.tx_ = this.secondaryConn_;
|
2516 | this.tryCleanupConnection();
|
2517 | };
|
2518 | Connection.prototype.onPrimaryMessageReceived_ = function (parsedData) {
|
2519 |
|
2520 | var layer = requireKey('t', parsedData);
|
2521 | var data = requireKey('d', parsedData);
|
2522 | if (layer === 'c') {
|
2523 | this.onControl_(data);
|
2524 | }
|
2525 | else if (layer === 'd') {
|
2526 | this.onDataMessage_(data);
|
2527 | }
|
2528 | };
|
2529 | Connection.prototype.onDataMessage_ = function (message) {
|
2530 | this.onPrimaryResponse_();
|
2531 |
|
2532 | this.onMessage_(message);
|
2533 | };
|
2534 | Connection.prototype.onPrimaryResponse_ = function () {
|
2535 | if (!this.isHealthy_) {
|
2536 | this.primaryResponsesRequired_--;
|
2537 | if (this.primaryResponsesRequired_ <= 0) {
|
2538 | this.log_('Primary connection is healthy.');
|
2539 | this.isHealthy_ = true;
|
2540 | this.conn_.markConnectionHealthy();
|
2541 | }
|
2542 | }
|
2543 | };
|
2544 | Connection.prototype.onControl_ = function (controlData) {
|
2545 | var cmd = requireKey(MESSAGE_TYPE, controlData);
|
2546 | if (MESSAGE_DATA in controlData) {
|
2547 | var payload = controlData[MESSAGE_DATA];
|
2548 | if (cmd === SERVER_HELLO) {
|
2549 | this.onHandshake_(payload);
|
2550 | }
|
2551 | else if (cmd === END_TRANSMISSION) {
|
2552 | this.log_('recvd end transmission on primary');
|
2553 | this.rx_ = this.secondaryConn_;
|
2554 | for (var i = 0; i < this.pendingDataMessages.length; ++i) {
|
2555 | this.onDataMessage_(this.pendingDataMessages[i]);
|
2556 | }
|
2557 | this.pendingDataMessages = [];
|
2558 | this.tryCleanupConnection();
|
2559 | }
|
2560 | else if (cmd === CONTROL_SHUTDOWN) {
|
2561 |
|
2562 |
|
2563 | this.onConnectionShutdown_(payload);
|
2564 | }
|
2565 | else if (cmd === CONTROL_RESET) {
|
2566 |
|
2567 | this.onReset_(payload);
|
2568 | }
|
2569 | else if (cmd === CONTROL_ERROR) {
|
2570 | error('Server Error: ' + payload);
|
2571 | }
|
2572 | else if (cmd === CONTROL_PONG) {
|
2573 | this.log_('got pong on primary.');
|
2574 | this.onPrimaryResponse_();
|
2575 | this.sendPingOnPrimaryIfNecessary_();
|
2576 | }
|
2577 | else {
|
2578 | error('Unknown control packet command: ' + cmd);
|
2579 | }
|
2580 | }
|
2581 | };
|
2582 | |
2583 |
|
2584 |
|
2585 | Connection.prototype.onHandshake_ = function (handshake) {
|
2586 | var timestamp = handshake.ts;
|
2587 | var version = handshake.v;
|
2588 | var host = handshake.h;
|
2589 | this.sessionId = handshake.s;
|
2590 | this.repoInfo_.host = host;
|
2591 |
|
2592 | if (this.state_ === 0 ) {
|
2593 | this.conn_.start();
|
2594 | this.onConnectionEstablished_(this.conn_, timestamp);
|
2595 | if (PROTOCOL_VERSION !== version) {
|
2596 | warn('Protocol version mismatch detected');
|
2597 | }
|
2598 |
|
2599 | this.tryStartUpgrade_();
|
2600 | }
|
2601 | };
|
2602 | Connection.prototype.tryStartUpgrade_ = function () {
|
2603 | var conn = this.transportManager_.upgradeTransport();
|
2604 | if (conn) {
|
2605 | this.startUpgrade_(conn);
|
2606 | }
|
2607 | };
|
2608 | Connection.prototype.startUpgrade_ = function (conn) {
|
2609 | var _this = this;
|
2610 | this.secondaryConn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, this.sessionId);
|
2611 |
|
2612 |
|
2613 | this.secondaryResponsesRequired_ =
|
2614 | conn['responsesRequiredToBeHealthy'] || 0;
|
2615 | var onMessage = this.connReceiver_(this.secondaryConn_);
|
2616 | var onDisconnect = this.disconnReceiver_(this.secondaryConn_);
|
2617 | this.secondaryConn_.open(onMessage, onDisconnect);
|
2618 |
|
2619 | setTimeoutNonBlocking(function () {
|
2620 | if (_this.secondaryConn_) {
|
2621 | _this.log_('Timed out trying to upgrade.');
|
2622 | _this.secondaryConn_.close();
|
2623 | }
|
2624 | }, Math.floor(UPGRADE_TIMEOUT));
|
2625 | };
|
2626 | Connection.prototype.onReset_ = function (host) {
|
2627 | this.log_('Reset packet received. New host: ' + host);
|
2628 | this.repoInfo_.host = host;
|
2629 |
|
2630 |
|
2631 | if (this.state_ === 1 ) {
|
2632 | this.close();
|
2633 | }
|
2634 | else {
|
2635 |
|
2636 | this.closeConnections_();
|
2637 | this.start_();
|
2638 | }
|
2639 | };
|
2640 | Connection.prototype.onConnectionEstablished_ = function (conn, timestamp) {
|
2641 | var _this = this;
|
2642 | this.log_('Realtime connection established.');
|
2643 | this.conn_ = conn;
|
2644 | this.state_ = 1 ;
|
2645 | if (this.onReady_) {
|
2646 | this.onReady_(timestamp, this.sessionId);
|
2647 | this.onReady_ = null;
|
2648 | }
|
2649 |
|
2650 |
|
2651 | if (this.primaryResponsesRequired_ === 0) {
|
2652 | this.log_('Primary connection is healthy.');
|
2653 | this.isHealthy_ = true;
|
2654 | }
|
2655 | else {
|
2656 | setTimeoutNonBlocking(function () {
|
2657 | _this.sendPingOnPrimaryIfNecessary_();
|
2658 | }, Math.floor(DELAY_BEFORE_SENDING_EXTRA_REQUESTS));
|
2659 | }
|
2660 | };
|
2661 | Connection.prototype.sendPingOnPrimaryIfNecessary_ = function () {
|
2662 |
|
2663 | if (!this.isHealthy_ && this.state_ === 1 ) {
|
2664 | this.log_('sending ping on primary.');
|
2665 | this.sendData_({ t: 'c', d: { t: PING, d: {} } });
|
2666 | }
|
2667 | };
|
2668 | Connection.prototype.onSecondaryConnectionLost_ = function () {
|
2669 | var conn = this.secondaryConn_;
|
2670 | this.secondaryConn_ = null;
|
2671 | if (this.tx_ === conn || this.rx_ === conn) {
|
2672 |
|
2673 | this.close();
|
2674 | }
|
2675 | };
|
2676 | |
2677 |
|
2678 |
|
2679 |
|
2680 | Connection.prototype.onConnectionLost_ = function (everConnected) {
|
2681 | this.conn_ = null;
|
2682 |
|
2683 |
|
2684 | if (!everConnected && this.state_ === 0 ) {
|
2685 | this.log_('Realtime connection failed.');
|
2686 |
|
2687 | if (this.repoInfo_.isCacheableHost()) {
|
2688 | PersistentStorage.remove('host:' + this.repoInfo_.host);
|
2689 |
|
2690 | this.repoInfo_.internalHost = this.repoInfo_.host;
|
2691 | }
|
2692 | }
|
2693 | else if (this.state_ === 1 ) {
|
2694 | this.log_('Realtime connection lost.');
|
2695 | }
|
2696 | this.close();
|
2697 | };
|
2698 | Connection.prototype.onConnectionShutdown_ = function (reason) {
|
2699 | this.log_('Connection shutdown command received. Shutting down...');
|
2700 | if (this.onKill_) {
|
2701 | this.onKill_(reason);
|
2702 | this.onKill_ = null;
|
2703 | }
|
2704 |
|
2705 |
|
2706 | this.onDisconnect_ = null;
|
2707 | this.close();
|
2708 | };
|
2709 | Connection.prototype.sendData_ = function (data) {
|
2710 | if (this.state_ !== 1 ) {
|
2711 | throw 'Connection is not connected';
|
2712 | }
|
2713 | else {
|
2714 | this.tx_.send(data);
|
2715 | }
|
2716 | };
|
2717 | |
2718 |
|
2719 |
|
2720 | Connection.prototype.close = function () {
|
2721 | if (this.state_ !== 2 ) {
|
2722 | this.log_('Closing realtime connection.');
|
2723 | this.state_ = 2 ;
|
2724 | this.closeConnections_();
|
2725 | if (this.onDisconnect_) {
|
2726 | this.onDisconnect_();
|
2727 | this.onDisconnect_ = null;
|
2728 | }
|
2729 | }
|
2730 | };
|
2731 | Connection.prototype.closeConnections_ = function () {
|
2732 | this.log_('Shutting down all connections');
|
2733 | if (this.conn_) {
|
2734 | this.conn_.close();
|
2735 | this.conn_ = null;
|
2736 | }
|
2737 | if (this.secondaryConn_) {
|
2738 | this.secondaryConn_.close();
|
2739 | this.secondaryConn_ = null;
|
2740 | }
|
2741 | if (this.healthyTimeout_) {
|
2742 | clearTimeout(this.healthyTimeout_);
|
2743 | this.healthyTimeout_ = null;
|
2744 | }
|
2745 | };
|
2746 | return Connection;
|
2747 | }());
|
2748 |
|
2749 |
|
2750 |
|
2751 |
|
2752 |
|
2753 |
|
2754 |
|
2755 |
|
2756 |
|
2757 |
|
2758 |
|
2759 |
|
2760 |
|
2761 |
|
2762 |
|
2763 |
|
2764 |
|
2765 |
|
2766 |
|
2767 |
|
2768 |
|
2769 |
|
2770 |
|
2771 | var ServerActions = (function () {
|
2772 | function ServerActions() {
|
2773 | }
|
2774 | ServerActions.prototype.put = function (pathString, data, onComplete, hash) { };
|
2775 | ServerActions.prototype.merge = function (pathString, data, onComplete, hash) { };
|
2776 | |
2777 |
|
2778 |
|
2779 |
|
2780 | ServerActions.prototype.refreshAuthToken = function (token) { };
|
2781 | |
2782 |
|
2783 |
|
2784 |
|
2785 | ServerActions.prototype.refreshAppCheckToken = function (token) { };
|
2786 | ServerActions.prototype.onDisconnectPut = function (pathString, data, onComplete) { };
|
2787 | ServerActions.prototype.onDisconnectMerge = function (pathString, data, onComplete) { };
|
2788 | ServerActions.prototype.onDisconnectCancel = function (pathString, onComplete) { };
|
2789 | ServerActions.prototype.reportStats = function (stats) { };
|
2790 | return ServerActions;
|
2791 | }());
|
2792 |
|
2793 |
|
2794 |
|
2795 |
|
2796 |
|
2797 |
|
2798 |
|
2799 |
|
2800 |
|
2801 |
|
2802 |
|
2803 |
|
2804 |
|
2805 |
|
2806 |
|
2807 |
|
2808 |
|
2809 |
|
2810 |
|
2811 |
|
2812 |
|
2813 | var EventEmitter = (function () {
|
2814 | function EventEmitter(allowedEvents_) {
|
2815 | this.allowedEvents_ = allowedEvents_;
|
2816 | this.listeners_ = {};
|
2817 | util.assert(Array.isArray(allowedEvents_) && allowedEvents_.length > 0, 'Requires a non-empty array');
|
2818 | }
|
2819 | |
2820 |
|
2821 |
|
2822 | EventEmitter.prototype.trigger = function (eventType) {
|
2823 | var varArgs = [];
|
2824 | for (var _i = 1; _i < arguments.length; _i++) {
|
2825 | varArgs[_i - 1] = arguments[_i];
|
2826 | }
|
2827 | if (Array.isArray(this.listeners_[eventType])) {
|
2828 |
|
2829 | var listeners = tslib.__spreadArray([], tslib.__read(this.listeners_[eventType]));
|
2830 | for (var i = 0; i < listeners.length; i++) {
|
2831 | listeners[i].callback.apply(listeners[i].context, varArgs);
|
2832 | }
|
2833 | }
|
2834 | };
|
2835 | EventEmitter.prototype.on = function (eventType, callback, context) {
|
2836 | this.validateEventType_(eventType);
|
2837 | this.listeners_[eventType] = this.listeners_[eventType] || [];
|
2838 | this.listeners_[eventType].push({ callback: callback, context: context });
|
2839 | var eventData = this.getInitialEvent(eventType);
|
2840 | if (eventData) {
|
2841 | callback.apply(context, eventData);
|
2842 | }
|
2843 | };
|
2844 | EventEmitter.prototype.off = function (eventType, callback, context) {
|
2845 | this.validateEventType_(eventType);
|
2846 | var listeners = this.listeners_[eventType] || [];
|
2847 | for (var i = 0; i < listeners.length; i++) {
|
2848 | if (listeners[i].callback === callback &&
|
2849 | (!context || context === listeners[i].context)) {
|
2850 | listeners.splice(i, 1);
|
2851 | return;
|
2852 | }
|
2853 | }
|
2854 | };
|
2855 | EventEmitter.prototype.validateEventType_ = function (eventType) {
|
2856 | util.assert(this.allowedEvents_.find(function (et) {
|
2857 | return et === eventType;
|
2858 | }), 'Unknown event: ' + eventType);
|
2859 | };
|
2860 | return EventEmitter;
|
2861 | }());
|
2862 |
|
2863 |
|
2864 |
|
2865 |
|
2866 |
|
2867 |
|
2868 |
|
2869 |
|
2870 |
|
2871 |
|
2872 |
|
2873 |
|
2874 |
|
2875 |
|
2876 |
|
2877 |
|
2878 |
|
2879 |
|
2880 |
|
2881 |
|
2882 |
|
2883 |
|
2884 |
|
2885 |
|
2886 | var OnlineMonitor = (function (_super) {
|
2887 | tslib.__extends(OnlineMonitor, _super);
|
2888 | function OnlineMonitor() {
|
2889 | var _this = _super.call(this, ['online']) || this;
|
2890 | _this.online_ = true;
|
2891 |
|
2892 |
|
2893 |
|
2894 |
|
2895 | if (typeof window !== 'undefined' &&
|
2896 | typeof window.addEventListener !== 'undefined' &&
|
2897 | !util.isMobileCordova()) {
|
2898 | window.addEventListener('online', function () {
|
2899 | if (!_this.online_) {
|
2900 | _this.online_ = true;
|
2901 | _this.trigger('online', true);
|
2902 | }
|
2903 | }, false);
|
2904 | window.addEventListener('offline', function () {
|
2905 | if (_this.online_) {
|
2906 | _this.online_ = false;
|
2907 | _this.trigger('online', false);
|
2908 | }
|
2909 | }, false);
|
2910 | }
|
2911 | return _this;
|
2912 | }
|
2913 | OnlineMonitor.getInstance = function () {
|
2914 | return new OnlineMonitor();
|
2915 | };
|
2916 | OnlineMonitor.prototype.getInitialEvent = function (eventType) {
|
2917 | util.assert(eventType === 'online', 'Unknown event type: ' + eventType);
|
2918 | return [this.online_];
|
2919 | };
|
2920 | OnlineMonitor.prototype.currentlyOnline = function () {
|
2921 | return this.online_;
|
2922 | };
|
2923 | return OnlineMonitor;
|
2924 | }(EventEmitter));
|
2925 |
|
2926 |
|
2927 |
|
2928 |
|
2929 |
|
2930 |
|
2931 |
|
2932 |
|
2933 |
|
2934 |
|
2935 |
|
2936 |
|
2937 |
|
2938 |
|
2939 |
|
2940 |
|
2941 |
|
2942 |
|
2943 | var MAX_PATH_DEPTH = 32;
|
2944 |
|
2945 | var MAX_PATH_LENGTH_BYTES = 768;
|
2946 |
|
2947 |
|
2948 |
|
2949 |
|
2950 |
|
2951 | var Path = (function () {
|
2952 | |
2953 |
|
2954 |
|
2955 |
|
2956 | function Path(pathOrString, pieceNum) {
|
2957 | if (pieceNum === void 0) {
|
2958 | this.pieces_ = pathOrString.split('/');
|
2959 |
|
2960 | var copyTo = 0;
|
2961 | for (var i = 0; i < this.pieces_.length; i++) {
|
2962 | if (this.pieces_[i].length > 0) {
|
2963 | this.pieces_[copyTo] = this.pieces_[i];
|
2964 | copyTo++;
|
2965 | }
|
2966 | }
|
2967 | this.pieces_.length = copyTo;
|
2968 | this.pieceNum_ = 0;
|
2969 | }
|
2970 | else {
|
2971 | this.pieces_ = pathOrString;
|
2972 | this.pieceNum_ = pieceNum;
|
2973 | }
|
2974 | }
|
2975 | Path.prototype.toString = function () {
|
2976 | var pathString = '';
|
2977 | for (var i = this.pieceNum_; i < this.pieces_.length; i++) {
|
2978 | if (this.pieces_[i] !== '') {
|
2979 | pathString += '/' + this.pieces_[i];
|
2980 | }
|
2981 | }
|
2982 | return pathString || '/';
|
2983 | };
|
2984 | return Path;
|
2985 | }());
|
2986 | function newEmptyPath() {
|
2987 | return new Path('');
|
2988 | }
|
2989 | function pathGetFront(path) {
|
2990 | if (path.pieceNum_ >= path.pieces_.length) {
|
2991 | return null;
|
2992 | }
|
2993 | return path.pieces_[path.pieceNum_];
|
2994 | }
|
2995 |
|
2996 |
|
2997 |
|
2998 | function pathGetLength(path) {
|
2999 | return path.pieces_.length - path.pieceNum_;
|
3000 | }
|
3001 | function pathPopFront(path) {
|
3002 | var pieceNum = path.pieceNum_;
|
3003 | if (pieceNum < path.pieces_.length) {
|
3004 | pieceNum++;
|
3005 | }
|
3006 | return new Path(path.pieces_, pieceNum);
|
3007 | }
|
3008 | function pathGetBack(path) {
|
3009 | if (path.pieceNum_ < path.pieces_.length) {
|
3010 | return path.pieces_[path.pieces_.length - 1];
|
3011 | }
|
3012 | return null;
|
3013 | }
|
3014 | function pathToUrlEncodedString(path) {
|
3015 | var pathString = '';
|
3016 | for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
|
3017 | if (path.pieces_[i] !== '') {
|
3018 | pathString += '/' + encodeURIComponent(String(path.pieces_[i]));
|
3019 | }
|
3020 | }
|
3021 | return pathString || '/';
|
3022 | }
|
3023 |
|
3024 |
|
3025 |
|
3026 |
|
3027 | function pathSlice(path, begin) {
|
3028 | if (begin === void 0) { begin = 0; }
|
3029 | return path.pieces_.slice(path.pieceNum_ + begin);
|
3030 | }
|
3031 | function pathParent(path) {
|
3032 | if (path.pieceNum_ >= path.pieces_.length) {
|
3033 | return null;
|
3034 | }
|
3035 | var pieces = [];
|
3036 | for (var i = path.pieceNum_; i < path.pieces_.length - 1; i++) {
|
3037 | pieces.push(path.pieces_[i]);
|
3038 | }
|
3039 | return new Path(pieces, 0);
|
3040 | }
|
3041 | function pathChild(path, childPathObj) {
|
3042 | var pieces = [];
|
3043 | for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
|
3044 | pieces.push(path.pieces_[i]);
|
3045 | }
|
3046 | if (childPathObj instanceof Path) {
|
3047 | for (var i = childPathObj.pieceNum_; i < childPathObj.pieces_.length; i++) {
|
3048 | pieces.push(childPathObj.pieces_[i]);
|
3049 | }
|
3050 | }
|
3051 | else {
|
3052 | var childPieces = childPathObj.split('/');
|
3053 | for (var i = 0; i < childPieces.length; i++) {
|
3054 | if (childPieces[i].length > 0) {
|
3055 | pieces.push(childPieces[i]);
|
3056 | }
|
3057 | }
|
3058 | }
|
3059 | return new Path(pieces, 0);
|
3060 | }
|
3061 |
|
3062 |
|
3063 |
|
3064 | function pathIsEmpty(path) {
|
3065 | return path.pieceNum_ >= path.pieces_.length;
|
3066 | }
|
3067 |
|
3068 |
|
3069 |
|
3070 | function newRelativePath(outerPath, innerPath) {
|
3071 | var outer = pathGetFront(outerPath), inner = pathGetFront(innerPath);
|
3072 | if (outer === null) {
|
3073 | return innerPath;
|
3074 | }
|
3075 | else if (outer === inner) {
|
3076 | return newRelativePath(pathPopFront(outerPath), pathPopFront(innerPath));
|
3077 | }
|
3078 | else {
|
3079 | throw new Error('INTERNAL ERROR: innerPath (' +
|
3080 | innerPath +
|
3081 | ') is not within ' +
|
3082 | 'outerPath (' +
|
3083 | outerPath +
|
3084 | ')');
|
3085 | }
|
3086 | }
|
3087 |
|
3088 |
|
3089 |
|
3090 | function pathCompare(left, right) {
|
3091 | var leftKeys = pathSlice(left, 0);
|
3092 | var rightKeys = pathSlice(right, 0);
|
3093 | for (var i = 0; i < leftKeys.length && i < rightKeys.length; i++) {
|
3094 | var cmp = nameCompare(leftKeys[i], rightKeys[i]);
|
3095 | if (cmp !== 0) {
|
3096 | return cmp;
|
3097 | }
|
3098 | }
|
3099 | if (leftKeys.length === rightKeys.length) {
|
3100 | return 0;
|
3101 | }
|
3102 | return leftKeys.length < rightKeys.length ? -1 : 1;
|
3103 | }
|
3104 |
|
3105 |
|
3106 |
|
3107 | function pathEquals(path, other) {
|
3108 | if (pathGetLength(path) !== pathGetLength(other)) {
|
3109 | return false;
|
3110 | }
|
3111 | for (var i = path.pieceNum_, j = other.pieceNum_; i <= path.pieces_.length; i++, j++) {
|
3112 | if (path.pieces_[i] !== other.pieces_[j]) {
|
3113 | return false;
|
3114 | }
|
3115 | }
|
3116 | return true;
|
3117 | }
|
3118 |
|
3119 |
|
3120 |
|
3121 | function pathContains(path, other) {
|
3122 | var i = path.pieceNum_;
|
3123 | var j = other.pieceNum_;
|
3124 | if (pathGetLength(path) > pathGetLength(other)) {
|
3125 | return false;
|
3126 | }
|
3127 | while (i < path.pieces_.length) {
|
3128 | if (path.pieces_[i] !== other.pieces_[j]) {
|
3129 | return false;
|
3130 | }
|
3131 | ++i;
|
3132 | ++j;
|
3133 | }
|
3134 | return true;
|
3135 | }
|
3136 |
|
3137 |
|
3138 |
|
3139 |
|
3140 |
|
3141 |
|
3142 |
|
3143 |
|
3144 |
|
3145 |
|
3146 | var ValidationPath = (function () {
|
3147 | |
3148 |
|
3149 |
|
3150 |
|
3151 | function ValidationPath(path, errorPrefix_) {
|
3152 | this.errorPrefix_ = errorPrefix_;
|
3153 | this.parts_ = pathSlice(path, 0);
|
3154 |
|
3155 | this.byteLength_ = Math.max(1, this.parts_.length);
|
3156 | for (var i = 0; i < this.parts_.length; i++) {
|
3157 | this.byteLength_ += util.stringLength(this.parts_[i]);
|
3158 | }
|
3159 | validationPathCheckValid(this);
|
3160 | }
|
3161 | return ValidationPath;
|
3162 | }());
|
3163 | function validationPathPush(validationPath, child) {
|
3164 |
|
3165 | if (validationPath.parts_.length > 0) {
|
3166 | validationPath.byteLength_ += 1;
|
3167 | }
|
3168 | validationPath.parts_.push(child);
|
3169 | validationPath.byteLength_ += util.stringLength(child);
|
3170 | validationPathCheckValid(validationPath);
|
3171 | }
|
3172 | function validationPathPop(validationPath) {
|
3173 | var last = validationPath.parts_.pop();
|
3174 | validationPath.byteLength_ -= util.stringLength(last);
|
3175 |
|
3176 | if (validationPath.parts_.length > 0) {
|
3177 | validationPath.byteLength_ -= 1;
|
3178 | }
|
3179 | }
|
3180 | function validationPathCheckValid(validationPath) {
|
3181 | if (validationPath.byteLength_ > MAX_PATH_LENGTH_BYTES) {
|
3182 | throw new Error(validationPath.errorPrefix_ +
|
3183 | 'has a key path longer than ' +
|
3184 | MAX_PATH_LENGTH_BYTES +
|
3185 | ' bytes (' +
|
3186 | validationPath.byteLength_ +
|
3187 | ').');
|
3188 | }
|
3189 | if (validationPath.parts_.length > MAX_PATH_DEPTH) {
|
3190 | throw new Error(validationPath.errorPrefix_ +
|
3191 | 'path specified exceeds the maximum depth that can be written (' +
|
3192 | MAX_PATH_DEPTH +
|
3193 | ') or object contains a cycle ' +
|
3194 | validationPathToErrorString(validationPath));
|
3195 | }
|
3196 | }
|
3197 |
|
3198 |
|
3199 |
|
3200 | function validationPathToErrorString(validationPath) {
|
3201 | if (validationPath.parts_.length === 0) {
|
3202 | return '';
|
3203 | }
|
3204 | return "in property '" + validationPath.parts_.join('.') + "'";
|
3205 | }
|
3206 |
|
3207 |
|
3208 |
|
3209 |
|
3210 |
|
3211 |
|
3212 |
|
3213 |
|
3214 |
|
3215 |
|
3216 |
|
3217 |
|
3218 |
|
3219 |
|
3220 |
|
3221 |
|
3222 |
|
3223 | var VisibilityMonitor = (function (_super) {
|
3224 | tslib.__extends(VisibilityMonitor, _super);
|
3225 | function VisibilityMonitor() {
|
3226 | var _this = _super.call(this, ['visible']) || this;
|
3227 | var hidden;
|
3228 | var visibilityChange;
|
3229 | if (typeof document !== 'undefined' &&
|
3230 | typeof document.addEventListener !== 'undefined') {
|
3231 | if (typeof document['hidden'] !== 'undefined') {
|
3232 |
|
3233 | visibilityChange = 'visibilitychange';
|
3234 | hidden = 'hidden';
|
3235 | }
|
3236 | else if (typeof document['mozHidden'] !== 'undefined') {
|
3237 | visibilityChange = 'mozvisibilitychange';
|
3238 | hidden = 'mozHidden';
|
3239 | }
|
3240 | else if (typeof document['msHidden'] !== 'undefined') {
|
3241 | visibilityChange = 'msvisibilitychange';
|
3242 | hidden = 'msHidden';
|
3243 | }
|
3244 | else if (typeof document['webkitHidden'] !== 'undefined') {
|
3245 | visibilityChange = 'webkitvisibilitychange';
|
3246 | hidden = 'webkitHidden';
|
3247 | }
|
3248 | }
|
3249 |
|
3250 |
|
3251 |
|
3252 |
|
3253 | _this.visible_ = true;
|
3254 | if (visibilityChange) {
|
3255 | document.addEventListener(visibilityChange, function () {
|
3256 | var visible = !document[hidden];
|
3257 | if (visible !== _this.visible_) {
|
3258 | _this.visible_ = visible;
|
3259 | _this.trigger('visible', visible);
|
3260 | }
|
3261 | }, false);
|
3262 | }
|
3263 | return _this;
|
3264 | }
|
3265 | VisibilityMonitor.getInstance = function () {
|
3266 | return new VisibilityMonitor();
|
3267 | };
|
3268 | VisibilityMonitor.prototype.getInitialEvent = function (eventType) {
|
3269 | util.assert(eventType === 'visible', 'Unknown event type: ' + eventType);
|
3270 | return [this.visible_];
|
3271 | };
|
3272 | return VisibilityMonitor;
|
3273 | }(EventEmitter));
|
3274 |
|
3275 |
|
3276 |
|
3277 |
|
3278 |
|
3279 |
|
3280 |
|
3281 |
|
3282 |
|
3283 |
|
3284 |
|
3285 |
|
3286 |
|
3287 |
|
3288 |
|
3289 |
|
3290 |
|
3291 | var RECONNECT_MIN_DELAY = 1000;
|
3292 | var RECONNECT_MAX_DELAY_DEFAULT = 60 * 5 * 1000;
|
3293 | var GET_CONNECT_TIMEOUT = 3 * 1000;
|
3294 | var RECONNECT_MAX_DELAY_FOR_ADMINS = 30 * 1000;
|
3295 | var RECONNECT_DELAY_MULTIPLIER = 1.3;
|
3296 | var RECONNECT_DELAY_RESET_TIMEOUT = 30000;
|
3297 | var SERVER_KILL_INTERRUPT_REASON = 'server_kill';
|
3298 |
|
3299 | var INVALID_TOKEN_THRESHOLD = 3;
|
3300 |
|
3301 |
|
3302 |
|
3303 |
|
3304 |
|
3305 |
|
3306 | var PersistentConnection = (function (_super) {
|
3307 | tslib.__extends(PersistentConnection, _super);
|
3308 | |
3309 |
|
3310 |
|
3311 |
|
3312 |
|
3313 | function PersistentConnection(repoInfo_, applicationId_, onDataUpdate_, onConnectStatus_, onServerInfoUpdate_, authTokenProvider_, appCheckTokenProvider_, authOverride_) {
|
3314 | var _this = _super.call(this) || this;
|
3315 | _this.repoInfo_ = repoInfo_;
|
3316 | _this.applicationId_ = applicationId_;
|
3317 | _this.onDataUpdate_ = onDataUpdate_;
|
3318 | _this.onConnectStatus_ = onConnectStatus_;
|
3319 | _this.onServerInfoUpdate_ = onServerInfoUpdate_;
|
3320 | _this.authTokenProvider_ = authTokenProvider_;
|
3321 | _this.appCheckTokenProvider_ = appCheckTokenProvider_;
|
3322 | _this.authOverride_ = authOverride_;
|
3323 |
|
3324 | _this.id = PersistentConnection.nextPersistentConnectionId_++;
|
3325 | _this.log_ = logWrapper('p:' + _this.id + ':');
|
3326 | _this.interruptReasons_ = {};
|
3327 | _this.listens = new Map();
|
3328 | _this.outstandingPuts_ = [];
|
3329 | _this.outstandingGets_ = [];
|
3330 | _this.outstandingPutCount_ = 0;
|
3331 | _this.outstandingGetCount_ = 0;
|
3332 | _this.onDisconnectRequestQueue_ = [];
|
3333 | _this.connected_ = false;
|
3334 | _this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3335 | _this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_DEFAULT;
|
3336 | _this.securityDebugCallback_ = null;
|
3337 | _this.lastSessionId = null;
|
3338 | _this.establishConnectionTimer_ = null;
|
3339 | _this.visible_ = false;
|
3340 |
|
3341 | _this.requestCBHash_ = {};
|
3342 | _this.requestNumber_ = 0;
|
3343 | _this.realtime_ = null;
|
3344 | _this.authToken_ = null;
|
3345 | _this.appCheckToken_ = null;
|
3346 | _this.forceTokenRefresh_ = false;
|
3347 | _this.invalidAuthTokenCount_ = 0;
|
3348 | _this.invalidAppCheckTokenCount_ = 0;
|
3349 | _this.firstConnection_ = true;
|
3350 | _this.lastConnectionAttemptTime_ = null;
|
3351 | _this.lastConnectionEstablishedTime_ = null;
|
3352 | if (authOverride_ && !util.isNodeSdk()) {
|
3353 | throw new Error('Auth override specified in options, but not supported on non Node.js platforms');
|
3354 | }
|
3355 | VisibilityMonitor.getInstance().on('visible', _this.onVisible_, _this);
|
3356 | if (repoInfo_.host.indexOf('fblocal') === -1) {
|
3357 | OnlineMonitor.getInstance().on('online', _this.onOnline_, _this);
|
3358 | }
|
3359 | return _this;
|
3360 | }
|
3361 | PersistentConnection.prototype.sendRequest = function (action, body, onResponse) {
|
3362 | var curReqNum = ++this.requestNumber_;
|
3363 | var msg = { r: curReqNum, a: action, b: body };
|
3364 | this.log_(util.stringify(msg));
|
3365 | util.assert(this.connected_, "sendRequest call when we're not connected not allowed.");
|
3366 | this.realtime_.sendRequest(msg);
|
3367 | if (onResponse) {
|
3368 | this.requestCBHash_[curReqNum] = onResponse;
|
3369 | }
|
3370 | };
|
3371 | PersistentConnection.prototype.get = function (query) {
|
3372 | var _this = this;
|
3373 | this.initConnection_();
|
3374 | var deferred = new util.Deferred();
|
3375 | var request = {
|
3376 | p: query._path.toString(),
|
3377 | q: query._queryObject
|
3378 | };
|
3379 | var outstandingGet = {
|
3380 | action: 'g',
|
3381 | request: request,
|
3382 | onComplete: function (message) {
|
3383 | var payload = message['d'];
|
3384 | if (message['s'] === 'ok') {
|
3385 | _this.onDataUpdate_(request['p'], payload,
|
3386 | false,
|
3387 | null);
|
3388 | deferred.resolve(payload);
|
3389 | }
|
3390 | else {
|
3391 | deferred.reject(payload);
|
3392 | }
|
3393 | }
|
3394 | };
|
3395 | this.outstandingGets_.push(outstandingGet);
|
3396 | this.outstandingGetCount_++;
|
3397 | var index = this.outstandingGets_.length - 1;
|
3398 | if (!this.connected_) {
|
3399 | setTimeout(function () {
|
3400 | var get = _this.outstandingGets_[index];
|
3401 | if (get === undefined || outstandingGet !== get) {
|
3402 | return;
|
3403 | }
|
3404 | delete _this.outstandingGets_[index];
|
3405 | _this.outstandingGetCount_--;
|
3406 | if (_this.outstandingGetCount_ === 0) {
|
3407 | _this.outstandingGets_ = [];
|
3408 | }
|
3409 | _this.log_('get ' + index + ' timed out on connection');
|
3410 | deferred.reject(new Error('Client is offline.'));
|
3411 | }, GET_CONNECT_TIMEOUT);
|
3412 | }
|
3413 | if (this.connected_) {
|
3414 | this.sendGet_(index);
|
3415 | }
|
3416 | return deferred.promise;
|
3417 | };
|
3418 | PersistentConnection.prototype.listen = function (query, currentHashFn, tag, onComplete) {
|
3419 | this.initConnection_();
|
3420 | var queryId = query._queryIdentifier;
|
3421 | var pathString = query._path.toString();
|
3422 | this.log_('Listen called for ' + pathString + ' ' + queryId);
|
3423 | if (!this.listens.has(pathString)) {
|
3424 | this.listens.set(pathString, new Map());
|
3425 | }
|
3426 | util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'listen() called for non-default but complete query');
|
3427 | util.assert(!this.listens.get(pathString).has(queryId), 'listen() called twice for same path/queryId.');
|
3428 | var listenSpec = {
|
3429 | onComplete: onComplete,
|
3430 | hashFn: currentHashFn,
|
3431 | query: query,
|
3432 | tag: tag
|
3433 | };
|
3434 | this.listens.get(pathString).set(queryId, listenSpec);
|
3435 | if (this.connected_) {
|
3436 | this.sendListen_(listenSpec);
|
3437 | }
|
3438 | };
|
3439 | PersistentConnection.prototype.sendGet_ = function (index) {
|
3440 | var _this = this;
|
3441 | var get = this.outstandingGets_[index];
|
3442 | this.sendRequest('g', get.request, function (message) {
|
3443 | delete _this.outstandingGets_[index];
|
3444 | _this.outstandingGetCount_--;
|
3445 | if (_this.outstandingGetCount_ === 0) {
|
3446 | _this.outstandingGets_ = [];
|
3447 | }
|
3448 | if (get.onComplete) {
|
3449 | get.onComplete(message);
|
3450 | }
|
3451 | });
|
3452 | };
|
3453 | PersistentConnection.prototype.sendListen_ = function (listenSpec) {
|
3454 | var _this = this;
|
3455 | var query = listenSpec.query;
|
3456 | var pathString = query._path.toString();
|
3457 | var queryId = query._queryIdentifier;
|
3458 | this.log_('Listen on ' + pathString + ' for ' + queryId);
|
3459 | var req = { p: pathString };
|
3460 | var action = 'q';
|
3461 |
|
3462 | if (listenSpec.tag) {
|
3463 | req['q'] = query._queryObject;
|
3464 | req['t'] = listenSpec.tag;
|
3465 | }
|
3466 | req[ 'h'] = listenSpec.hashFn();
|
3467 | this.sendRequest(action, req, function (message) {
|
3468 | var payload = message[ 'd'];
|
3469 | var status = message[ 's'];
|
3470 |
|
3471 | PersistentConnection.warnOnListenWarnings_(payload, query);
|
3472 | var currentListenSpec = _this.listens.get(pathString) &&
|
3473 | _this.listens.get(pathString).get(queryId);
|
3474 |
|
3475 | if (currentListenSpec === listenSpec) {
|
3476 | _this.log_('listen response', message);
|
3477 | if (status !== 'ok') {
|
3478 | _this.removeListen_(pathString, queryId);
|
3479 | }
|
3480 | if (listenSpec.onComplete) {
|
3481 | listenSpec.onComplete(status, payload);
|
3482 | }
|
3483 | }
|
3484 | });
|
3485 | };
|
3486 | PersistentConnection.warnOnListenWarnings_ = function (payload, query) {
|
3487 | if (payload && typeof payload === 'object' && util.contains(payload, 'w')) {
|
3488 |
|
3489 | var warnings = util.safeGet(payload, 'w');
|
3490 | if (Array.isArray(warnings) && ~warnings.indexOf('no_index')) {
|
3491 | var indexSpec = '".indexOn": "' + query._queryParams.getIndex().toString() + '"';
|
3492 | var indexPath = query._path.toString();
|
3493 | warn("Using an unspecified index. Your data will be downloaded and " +
|
3494 | ("filtered on the client. Consider adding " + indexSpec + " at ") +
|
3495 | (indexPath + " to your security rules for better performance."));
|
3496 | }
|
3497 | }
|
3498 | };
|
3499 | PersistentConnection.prototype.refreshAuthToken = function (token) {
|
3500 | this.authToken_ = token;
|
3501 | this.log_('Auth token refreshed');
|
3502 | if (this.authToken_) {
|
3503 | this.tryAuth();
|
3504 | }
|
3505 | else {
|
3506 |
|
3507 |
|
3508 | if (this.connected_) {
|
3509 | this.sendRequest('unauth', {}, function () { });
|
3510 | }
|
3511 | }
|
3512 | this.reduceReconnectDelayIfAdminCredential_(token);
|
3513 | };
|
3514 | PersistentConnection.prototype.reduceReconnectDelayIfAdminCredential_ = function (credential) {
|
3515 |
|
3516 |
|
3517 | var isFirebaseSecret = credential && credential.length === 40;
|
3518 | if (isFirebaseSecret || util.isAdmin(credential)) {
|
3519 | this.log_('Admin auth credential detected. Reducing max reconnect time.');
|
3520 | this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
|
3521 | }
|
3522 | };
|
3523 | PersistentConnection.prototype.refreshAppCheckToken = function (token) {
|
3524 | this.appCheckToken_ = token;
|
3525 | this.log_('App check token refreshed');
|
3526 | if (this.appCheckToken_) {
|
3527 | this.tryAppCheck();
|
3528 | }
|
3529 | else {
|
3530 |
|
3531 |
|
3532 |
|
3533 | if (this.connected_) {
|
3534 | this.sendRequest('unappeck', {}, function () { });
|
3535 | }
|
3536 | }
|
3537 | };
|
3538 | |
3539 |
|
3540 |
|
3541 |
|
3542 | PersistentConnection.prototype.tryAuth = function () {
|
3543 | var _this = this;
|
3544 | if (this.connected_ && this.authToken_) {
|
3545 | var token_1 = this.authToken_;
|
3546 | var authMethod = util.isValidFormat(token_1) ? 'auth' : 'gauth';
|
3547 | var requestData = { cred: token_1 };
|
3548 | if (this.authOverride_ === null) {
|
3549 | requestData['noauth'] = true;
|
3550 | }
|
3551 | else if (typeof this.authOverride_ === 'object') {
|
3552 | requestData['authvar'] = this.authOverride_;
|
3553 | }
|
3554 | this.sendRequest(authMethod, requestData, function (res) {
|
3555 | var status = res[ 's'];
|
3556 | var data = res[ 'd'] || 'error';
|
3557 | if (_this.authToken_ === token_1) {
|
3558 | if (status === 'ok') {
|
3559 | _this.invalidAuthTokenCount_ = 0;
|
3560 | }
|
3561 | else {
|
3562 |
|
3563 | _this.onAuthRevoked_(status, data);
|
3564 | }
|
3565 | }
|
3566 | });
|
3567 | }
|
3568 | };
|
3569 | |
3570 |
|
3571 |
|
3572 |
|
3573 |
|
3574 | PersistentConnection.prototype.tryAppCheck = function () {
|
3575 | var _this = this;
|
3576 | if (this.connected_ && this.appCheckToken_) {
|
3577 | this.sendRequest('appcheck', { 'token': this.appCheckToken_ }, function (res) {
|
3578 | var status = res[ 's'];
|
3579 | var data = res[ 'd'] || 'error';
|
3580 | if (status === 'ok') {
|
3581 | _this.invalidAppCheckTokenCount_ = 0;
|
3582 | }
|
3583 | else {
|
3584 | _this.onAppCheckRevoked_(status, data);
|
3585 | }
|
3586 | });
|
3587 | }
|
3588 | };
|
3589 | |
3590 |
|
3591 |
|
3592 | PersistentConnection.prototype.unlisten = function (query, tag) {
|
3593 | var pathString = query._path.toString();
|
3594 | var queryId = query._queryIdentifier;
|
3595 | this.log_('Unlisten called for ' + pathString + ' ' + queryId);
|
3596 | util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'unlisten() called for non-default but complete query');
|
3597 | var listen = this.removeListen_(pathString, queryId);
|
3598 | if (listen && this.connected_) {
|
3599 | this.sendUnlisten_(pathString, queryId, query._queryObject, tag);
|
3600 | }
|
3601 | };
|
3602 | PersistentConnection.prototype.sendUnlisten_ = function (pathString, queryId, queryObj, tag) {
|
3603 | this.log_('Unlisten on ' + pathString + ' for ' + queryId);
|
3604 | var req = { p: pathString };
|
3605 | var action = 'n';
|
3606 |
|
3607 | if (tag) {
|
3608 | req['q'] = queryObj;
|
3609 | req['t'] = tag;
|
3610 | }
|
3611 | this.sendRequest(action, req);
|
3612 | };
|
3613 | PersistentConnection.prototype.onDisconnectPut = function (pathString, data, onComplete) {
|
3614 | this.initConnection_();
|
3615 | if (this.connected_) {
|
3616 | this.sendOnDisconnect_('o', pathString, data, onComplete);
|
3617 | }
|
3618 | else {
|
3619 | this.onDisconnectRequestQueue_.push({
|
3620 | pathString: pathString,
|
3621 | action: 'o',
|
3622 | data: data,
|
3623 | onComplete: onComplete
|
3624 | });
|
3625 | }
|
3626 | };
|
3627 | PersistentConnection.prototype.onDisconnectMerge = function (pathString, data, onComplete) {
|
3628 | this.initConnection_();
|
3629 | if (this.connected_) {
|
3630 | this.sendOnDisconnect_('om', pathString, data, onComplete);
|
3631 | }
|
3632 | else {
|
3633 | this.onDisconnectRequestQueue_.push({
|
3634 | pathString: pathString,
|
3635 | action: 'om',
|
3636 | data: data,
|
3637 | onComplete: onComplete
|
3638 | });
|
3639 | }
|
3640 | };
|
3641 | PersistentConnection.prototype.onDisconnectCancel = function (pathString, onComplete) {
|
3642 | this.initConnection_();
|
3643 | if (this.connected_) {
|
3644 | this.sendOnDisconnect_('oc', pathString, null, onComplete);
|
3645 | }
|
3646 | else {
|
3647 | this.onDisconnectRequestQueue_.push({
|
3648 | pathString: pathString,
|
3649 | action: 'oc',
|
3650 | data: null,
|
3651 | onComplete: onComplete
|
3652 | });
|
3653 | }
|
3654 | };
|
3655 | PersistentConnection.prototype.sendOnDisconnect_ = function (action, pathString, data, onComplete) {
|
3656 | var request = { p: pathString, d: data };
|
3657 | this.log_('onDisconnect ' + action, request);
|
3658 | this.sendRequest(action, request, function (response) {
|
3659 | if (onComplete) {
|
3660 | setTimeout(function () {
|
3661 | onComplete(response[ 's'], response[ 'd']);
|
3662 | }, Math.floor(0));
|
3663 | }
|
3664 | });
|
3665 | };
|
3666 | PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
|
3667 | this.putInternal('p', pathString, data, onComplete, hash);
|
3668 | };
|
3669 | PersistentConnection.prototype.merge = function (pathString, data, onComplete, hash) {
|
3670 | this.putInternal('m', pathString, data, onComplete, hash);
|
3671 | };
|
3672 | PersistentConnection.prototype.putInternal = function (action, pathString, data, onComplete, hash) {
|
3673 | this.initConnection_();
|
3674 | var request = {
|
3675 | p: pathString,
|
3676 | d: data
|
3677 | };
|
3678 | if (hash !== undefined) {
|
3679 | request[ 'h'] = hash;
|
3680 | }
|
3681 |
|
3682 | this.outstandingPuts_.push({
|
3683 | action: action,
|
3684 | request: request,
|
3685 | onComplete: onComplete
|
3686 | });
|
3687 | this.outstandingPutCount_++;
|
3688 | var index = this.outstandingPuts_.length - 1;
|
3689 | if (this.connected_) {
|
3690 | this.sendPut_(index);
|
3691 | }
|
3692 | else {
|
3693 | this.log_('Buffering put: ' + pathString);
|
3694 | }
|
3695 | };
|
3696 | PersistentConnection.prototype.sendPut_ = function (index) {
|
3697 | var _this = this;
|
3698 | var action = this.outstandingPuts_[index].action;
|
3699 | var request = this.outstandingPuts_[index].request;
|
3700 | var onComplete = this.outstandingPuts_[index].onComplete;
|
3701 | this.outstandingPuts_[index].queued = this.connected_;
|
3702 | this.sendRequest(action, request, function (message) {
|
3703 | _this.log_(action + ' response', message);
|
3704 | delete _this.outstandingPuts_[index];
|
3705 | _this.outstandingPutCount_--;
|
3706 |
|
3707 | if (_this.outstandingPutCount_ === 0) {
|
3708 | _this.outstandingPuts_ = [];
|
3709 | }
|
3710 | if (onComplete) {
|
3711 | onComplete(message[ 's'], message[ 'd']);
|
3712 | }
|
3713 | });
|
3714 | };
|
3715 | PersistentConnection.prototype.reportStats = function (stats) {
|
3716 | var _this = this;
|
3717 |
|
3718 | if (this.connected_) {
|
3719 | var request = { c: stats };
|
3720 | this.log_('reportStats', request);
|
3721 | this.sendRequest( 's', request, function (result) {
|
3722 | var status = result[ 's'];
|
3723 | if (status !== 'ok') {
|
3724 | var errorReason = result[ 'd'];
|
3725 | _this.log_('reportStats', 'Error sending stats: ' + errorReason);
|
3726 | }
|
3727 | });
|
3728 | }
|
3729 | };
|
3730 | PersistentConnection.prototype.onDataMessage_ = function (message) {
|
3731 | if ('r' in message) {
|
3732 |
|
3733 | this.log_('from server: ' + util.stringify(message));
|
3734 | var reqNum = message['r'];
|
3735 | var onResponse = this.requestCBHash_[reqNum];
|
3736 | if (onResponse) {
|
3737 | delete this.requestCBHash_[reqNum];
|
3738 | onResponse(message[ 'b']);
|
3739 | }
|
3740 | }
|
3741 | else if ('error' in message) {
|
3742 | throw 'A server-side error has occurred: ' + message['error'];
|
3743 | }
|
3744 | else if ('a' in message) {
|
3745 |
|
3746 | this.onDataPush_(message['a'], message['b']);
|
3747 | }
|
3748 | };
|
3749 | PersistentConnection.prototype.onDataPush_ = function (action, body) {
|
3750 | this.log_('handleServerMessage', action, body);
|
3751 | if (action === 'd') {
|
3752 | this.onDataUpdate_(body[ 'p'], body[ 'd'],
|
3753 | false, body['t']);
|
3754 | }
|
3755 | else if (action === 'm') {
|
3756 | this.onDataUpdate_(body[ 'p'], body[ 'd'],
|
3757 | true, body['t']);
|
3758 | }
|
3759 | else if (action === 'c') {
|
3760 | this.onListenRevoked_(body[ 'p'], body[ 'q']);
|
3761 | }
|
3762 | else if (action === 'ac') {
|
3763 | this.onAuthRevoked_(body[ 's'], body[ 'd']);
|
3764 | }
|
3765 | else if (action === 'apc') {
|
3766 | this.onAppCheckRevoked_(body[ 's'], body[ 'd']);
|
3767 | }
|
3768 | else if (action === 'sd') {
|
3769 | this.onSecurityDebugPacket_(body);
|
3770 | }
|
3771 | else {
|
3772 | error('Unrecognized action received from server: ' +
|
3773 | util.stringify(action) +
|
3774 | '\nAre you using the latest client?');
|
3775 | }
|
3776 | };
|
3777 | PersistentConnection.prototype.onReady_ = function (timestamp, sessionId) {
|
3778 | this.log_('connection ready');
|
3779 | this.connected_ = true;
|
3780 | this.lastConnectionEstablishedTime_ = new Date().getTime();
|
3781 | this.handleTimestamp_(timestamp);
|
3782 | this.lastSessionId = sessionId;
|
3783 | if (this.firstConnection_) {
|
3784 | this.sendConnectStats_();
|
3785 | }
|
3786 | this.restoreState_();
|
3787 | this.firstConnection_ = false;
|
3788 | this.onConnectStatus_(true);
|
3789 | };
|
3790 | PersistentConnection.prototype.scheduleConnect_ = function (timeout) {
|
3791 | var _this = this;
|
3792 | util.assert(!this.realtime_, "Scheduling a connect when we're already connected/ing?");
|
3793 | if (this.establishConnectionTimer_) {
|
3794 | clearTimeout(this.establishConnectionTimer_);
|
3795 | }
|
3796 |
|
3797 |
|
3798 | this.establishConnectionTimer_ = setTimeout(function () {
|
3799 | _this.establishConnectionTimer_ = null;
|
3800 | _this.establishConnection_();
|
3801 |
|
3802 | }, Math.floor(timeout));
|
3803 | };
|
3804 | PersistentConnection.prototype.initConnection_ = function () {
|
3805 | if (!this.realtime_ && this.firstConnection_) {
|
3806 | this.scheduleConnect_(0);
|
3807 | }
|
3808 | };
|
3809 | PersistentConnection.prototype.onVisible_ = function (visible) {
|
3810 |
|
3811 | if (visible &&
|
3812 | !this.visible_ &&
|
3813 | this.reconnectDelay_ === this.maxReconnectDelay_) {
|
3814 | this.log_('Window became visible. Reducing delay.');
|
3815 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3816 | if (!this.realtime_) {
|
3817 | this.scheduleConnect_(0);
|
3818 | }
|
3819 | }
|
3820 | this.visible_ = visible;
|
3821 | };
|
3822 | PersistentConnection.prototype.onOnline_ = function (online) {
|
3823 | if (online) {
|
3824 | this.log_('Browser went online.');
|
3825 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3826 | if (!this.realtime_) {
|
3827 | this.scheduleConnect_(0);
|
3828 | }
|
3829 | }
|
3830 | else {
|
3831 | this.log_('Browser went offline. Killing connection.');
|
3832 | if (this.realtime_) {
|
3833 | this.realtime_.close();
|
3834 | }
|
3835 | }
|
3836 | };
|
3837 | PersistentConnection.prototype.onRealtimeDisconnect_ = function () {
|
3838 | this.log_('data client disconnected');
|
3839 | this.connected_ = false;
|
3840 | this.realtime_ = null;
|
3841 |
|
3842 | this.cancelSentTransactions_();
|
3843 |
|
3844 | this.requestCBHash_ = {};
|
3845 | if (this.shouldReconnect_()) {
|
3846 | if (!this.visible_) {
|
3847 | this.log_("Window isn't visible. Delaying reconnect.");
|
3848 | this.reconnectDelay_ = this.maxReconnectDelay_;
|
3849 | this.lastConnectionAttemptTime_ = new Date().getTime();
|
3850 | }
|
3851 | else if (this.lastConnectionEstablishedTime_) {
|
3852 |
|
3853 | var timeSinceLastConnectSucceeded = new Date().getTime() - this.lastConnectionEstablishedTime_;
|
3854 | if (timeSinceLastConnectSucceeded > RECONNECT_DELAY_RESET_TIMEOUT) {
|
3855 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3856 | }
|
3857 | this.lastConnectionEstablishedTime_ = null;
|
3858 | }
|
3859 | var timeSinceLastConnectAttempt = new Date().getTime() - this.lastConnectionAttemptTime_;
|
3860 | var reconnectDelay = Math.max(0, this.reconnectDelay_ - timeSinceLastConnectAttempt);
|
3861 | reconnectDelay = Math.random() * reconnectDelay;
|
3862 | this.log_('Trying to reconnect in ' + reconnectDelay + 'ms');
|
3863 | this.scheduleConnect_(reconnectDelay);
|
3864 |
|
3865 | this.reconnectDelay_ = Math.min(this.maxReconnectDelay_, this.reconnectDelay_ * RECONNECT_DELAY_MULTIPLIER);
|
3866 | }
|
3867 | this.onConnectStatus_(false);
|
3868 | };
|
3869 | PersistentConnection.prototype.establishConnection_ = function () {
|
3870 | return tslib.__awaiter(this, void 0, void 0, function () {
|
3871 | var onDataMessage, onReady, onDisconnect_1, connId, lastSessionId, canceled_1, connection_1, closeFn, sendRequestFn, forceRefresh, _a, authToken, appCheckToken, error_1;
|
3872 | var _this = this;
|
3873 | return tslib.__generator(this, function (_b) {
|
3874 | switch (_b.label) {
|
3875 | case 0:
|
3876 | if (!this.shouldReconnect_()) return [3 , 4];
|
3877 | this.log_('Making a connection attempt');
|
3878 | this.lastConnectionAttemptTime_ = new Date().getTime();
|
3879 | this.lastConnectionEstablishedTime_ = null;
|
3880 | onDataMessage = this.onDataMessage_.bind(this);
|
3881 | onReady = this.onReady_.bind(this);
|
3882 | onDisconnect_1 = this.onRealtimeDisconnect_.bind(this);
|
3883 | connId = this.id + ':' + PersistentConnection.nextConnectionId_++;
|
3884 | lastSessionId = this.lastSessionId;
|
3885 | canceled_1 = false;
|
3886 | connection_1 = null;
|
3887 | closeFn = function () {
|
3888 | if (connection_1) {
|
3889 | connection_1.close();
|
3890 | }
|
3891 | else {
|
3892 | canceled_1 = true;
|
3893 | onDisconnect_1();
|
3894 | }
|
3895 | };
|
3896 | sendRequestFn = function (msg) {
|
3897 | util.assert(connection_1, "sendRequest call when we're not connected not allowed.");
|
3898 | connection_1.sendRequest(msg);
|
3899 | };
|
3900 | this.realtime_ = {
|
3901 | close: closeFn,
|
3902 | sendRequest: sendRequestFn
|
3903 | };
|
3904 | forceRefresh = this.forceTokenRefresh_;
|
3905 | this.forceTokenRefresh_ = false;
|
3906 | _b.label = 1;
|
3907 | case 1:
|
3908 | _b.trys.push([1, 3, , 4]);
|
3909 | return [4 , Promise.all([
|
3910 | this.authTokenProvider_.getToken(forceRefresh),
|
3911 | this.appCheckTokenProvider_.getToken(forceRefresh)
|
3912 | ])];
|
3913 | case 2:
|
3914 | _a = tslib.__read.apply(void 0, [_b.sent(), 2]), authToken = _a[0], appCheckToken = _a[1];
|
3915 | if (!canceled_1) {
|
3916 | log('getToken() completed. Creating connection.');
|
3917 | this.authToken_ = authToken && authToken.accessToken;
|
3918 | this.appCheckToken_ = appCheckToken && appCheckToken.token;
|
3919 | connection_1 = new Connection(connId, this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, onDataMessage, onReady, onDisconnect_1,
|
3920 | function (reason) {
|
3921 | warn(reason + ' (' + _this.repoInfo_.toString() + ')');
|
3922 | _this.interrupt(SERVER_KILL_INTERRUPT_REASON);
|
3923 | }, lastSessionId);
|
3924 | }
|
3925 | else {
|
3926 | log('getToken() completed but was canceled');
|
3927 | }
|
3928 | return [3 , 4];
|
3929 | case 3:
|
3930 | error_1 = _b.sent();
|
3931 | this.log_('Failed to get token: ' + error_1);
|
3932 | if (!canceled_1) {
|
3933 | if (this.repoInfo_.nodeAdmin) {
|
3934 |
|
3935 |
|
3936 |
|
3937 | warn(error_1);
|
3938 | }
|
3939 | closeFn();
|
3940 | }
|
3941 | return [3 , 4];
|
3942 | case 4: return [2 ];
|
3943 | }
|
3944 | });
|
3945 | });
|
3946 | };
|
3947 | PersistentConnection.prototype.interrupt = function (reason) {
|
3948 | log('Interrupting connection for reason: ' + reason);
|
3949 | this.interruptReasons_[reason] = true;
|
3950 | if (this.realtime_) {
|
3951 | this.realtime_.close();
|
3952 | }
|
3953 | else {
|
3954 | if (this.establishConnectionTimer_) {
|
3955 | clearTimeout(this.establishConnectionTimer_);
|
3956 | this.establishConnectionTimer_ = null;
|
3957 | }
|
3958 | if (this.connected_) {
|
3959 | this.onRealtimeDisconnect_();
|
3960 | }
|
3961 | }
|
3962 | };
|
3963 | PersistentConnection.prototype.resume = function (reason) {
|
3964 | log('Resuming connection for reason: ' + reason);
|
3965 | delete this.interruptReasons_[reason];
|
3966 | if (util.isEmpty(this.interruptReasons_)) {
|
3967 | this.reconnectDelay_ = RECONNECT_MIN_DELAY;
|
3968 | if (!this.realtime_) {
|
3969 | this.scheduleConnect_(0);
|
3970 | }
|
3971 | }
|
3972 | };
|
3973 | PersistentConnection.prototype.handleTimestamp_ = function (timestamp) {
|
3974 | var delta = timestamp - new Date().getTime();
|
3975 | this.onServerInfoUpdate_({ serverTimeOffset: delta });
|
3976 | };
|
3977 | PersistentConnection.prototype.cancelSentTransactions_ = function () {
|
3978 | for (var i = 0; i < this.outstandingPuts_.length; i++) {
|
3979 | var put = this.outstandingPuts_[i];
|
3980 | if (put && 'h' in put.request && put.queued) {
|
3981 | if (put.onComplete) {
|
3982 | put.onComplete('disconnect');
|
3983 | }
|
3984 | delete this.outstandingPuts_[i];
|
3985 | this.outstandingPutCount_--;
|
3986 | }
|
3987 | }
|
3988 |
|
3989 | if (this.outstandingPutCount_ === 0) {
|
3990 | this.outstandingPuts_ = [];
|
3991 | }
|
3992 | };
|
3993 | PersistentConnection.prototype.onListenRevoked_ = function (pathString, query) {
|
3994 |
|
3995 | var queryId;
|
3996 | if (!query) {
|
3997 | queryId = 'default';
|
3998 | }
|
3999 | else {
|
4000 | queryId = query.map(function (q) { return ObjectToUniqueKey(q); }).join('$');
|
4001 | }
|
4002 | var listen = this.removeListen_(pathString, queryId);
|
4003 | if (listen && listen.onComplete) {
|
4004 | listen.onComplete('permission_denied');
|
4005 | }
|
4006 | };
|
4007 | PersistentConnection.prototype.removeListen_ = function (pathString, queryId) {
|
4008 | var normalizedPathString = new Path(pathString).toString();
|
4009 | var listen;
|
4010 | if (this.listens.has(normalizedPathString)) {
|
4011 | var map = this.listens.get(normalizedPathString);
|
4012 | listen = map.get(queryId);
|
4013 | map.delete(queryId);
|
4014 | if (map.size === 0) {
|
4015 | this.listens.delete(normalizedPathString);
|
4016 | }
|
4017 | }
|
4018 | else {
|
4019 |
|
4020 | listen = undefined;
|
4021 | }
|
4022 | return listen;
|
4023 | };
|
4024 | PersistentConnection.prototype.onAuthRevoked_ = function (statusCode, explanation) {
|
4025 | log('Auth token revoked: ' + statusCode + '/' + explanation);
|
4026 | this.authToken_ = null;
|
4027 | this.forceTokenRefresh_ = true;
|
4028 | this.realtime_.close();
|
4029 | if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
|
4030 |
|
4031 |
|
4032 |
|
4033 | this.invalidAuthTokenCount_++;
|
4034 | if (this.invalidAuthTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
|
4035 |
|
4036 | this.reconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
|
4037 |
|
4038 |
|
4039 | this.authTokenProvider_.notifyForInvalidToken();
|
4040 | }
|
4041 | }
|
4042 | };
|
4043 | PersistentConnection.prototype.onAppCheckRevoked_ = function (statusCode, explanation) {
|
4044 | log('App check token revoked: ' + statusCode + '/' + explanation);
|
4045 | this.appCheckToken_ = null;
|
4046 | this.forceTokenRefresh_ = true;
|
4047 |
|
4048 |
|
4049 | if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
|
4050 |
|
4051 |
|
4052 |
|
4053 | this.invalidAppCheckTokenCount_++;
|
4054 | if (this.invalidAppCheckTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
|
4055 | this.appCheckTokenProvider_.notifyForInvalidToken();
|
4056 | }
|
4057 | }
|
4058 | };
|
4059 | PersistentConnection.prototype.onSecurityDebugPacket_ = function (body) {
|
4060 | if (this.securityDebugCallback_) {
|
4061 | this.securityDebugCallback_(body);
|
4062 | }
|
4063 | else {
|
4064 | if ('msg' in body) {
|
4065 | console.log('FIREBASE: ' + body['msg'].replace('\n', '\nFIREBASE: '));
|
4066 | }
|
4067 | }
|
4068 | };
|
4069 | PersistentConnection.prototype.restoreState_ = function () {
|
4070 | var e_1, _a, e_2, _b;
|
4071 |
|
4072 | this.tryAuth();
|
4073 | this.tryAppCheck();
|
4074 | try {
|
4075 |
|
4076 |
|
4077 | for (var _c = tslib.__values(this.listens.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
|
4078 | var queries = _d.value;
|
4079 | try {
|
4080 | for (var _e = (e_2 = void 0, tslib.__values(queries.values())), _f = _e.next(); !_f.done; _f = _e.next()) {
|
4081 | var listenSpec = _f.value;
|
4082 | this.sendListen_(listenSpec);
|
4083 | }
|
4084 | }
|
4085 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
4086 | finally {
|
4087 | try {
|
4088 | if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
4089 | }
|
4090 | finally { if (e_2) throw e_2.error; }
|
4091 | }
|
4092 | }
|
4093 | }
|
4094 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
4095 | finally {
|
4096 | try {
|
4097 | if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
4098 | }
|
4099 | finally { if (e_1) throw e_1.error; }
|
4100 | }
|
4101 | for (var i = 0; i < this.outstandingPuts_.length; i++) {
|
4102 | if (this.outstandingPuts_[i]) {
|
4103 | this.sendPut_(i);
|
4104 | }
|
4105 | }
|
4106 | while (this.onDisconnectRequestQueue_.length) {
|
4107 | var request = this.onDisconnectRequestQueue_.shift();
|
4108 | this.sendOnDisconnect_(request.action, request.pathString, request.data, request.onComplete);
|
4109 | }
|
4110 | for (var i = 0; i < this.outstandingGets_.length; i++) {
|
4111 | if (this.outstandingGets_[i]) {
|
4112 | this.sendGet_(i);
|
4113 | }
|
4114 | }
|
4115 | };
|
4116 | |
4117 |
|
4118 |
|
4119 | PersistentConnection.prototype.sendConnectStats_ = function () {
|
4120 | var stats = {};
|
4121 | var clientName = 'js';
|
4122 | if (util.isNodeSdk()) {
|
4123 | if (this.repoInfo_.nodeAdmin) {
|
4124 | clientName = 'admin_node';
|
4125 | }
|
4126 | else {
|
4127 | clientName = 'node';
|
4128 | }
|
4129 | }
|
4130 | stats['sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-')] = 1;
|
4131 | if (util.isMobileCordova()) {
|
4132 | stats['framework.cordova'] = 1;
|
4133 | }
|
4134 | else if (util.isReactNative()) {
|
4135 | stats['framework.reactnative'] = 1;
|
4136 | }
|
4137 | this.reportStats(stats);
|
4138 | };
|
4139 | PersistentConnection.prototype.shouldReconnect_ = function () {
|
4140 | var online = OnlineMonitor.getInstance().currentlyOnline();
|
4141 | return util.isEmpty(this.interruptReasons_) && online;
|
4142 | };
|
4143 | PersistentConnection.nextPersistentConnectionId_ = 0;
|
4144 | |
4145 |
|
4146 |
|
4147 | PersistentConnection.nextConnectionId_ = 0;
|
4148 | return PersistentConnection;
|
4149 | }(ServerActions));
|
4150 |
|
4151 |
|
4152 |
|
4153 |
|
4154 |
|
4155 |
|
4156 |
|
4157 |
|
4158 |
|
4159 |
|
4160 |
|
4161 |
|
4162 |
|
4163 |
|
4164 |
|
4165 |
|
4166 |
|
4167 | var NamedNode = (function () {
|
4168 | function NamedNode(name, node) {
|
4169 | this.name = name;
|
4170 | this.node = node;
|
4171 | }
|
4172 | NamedNode.Wrap = function (name, node) {
|
4173 | return new NamedNode(name, node);
|
4174 | };
|
4175 | return NamedNode;
|
4176 | }());
|
4177 |
|
4178 |
|
4179 |
|
4180 |
|
4181 |
|
4182 |
|
4183 |
|
4184 |
|
4185 |
|
4186 |
|
4187 |
|
4188 |
|
4189 |
|
4190 |
|
4191 |
|
4192 |
|
4193 |
|
4194 | var Index = (function () {
|
4195 | function Index() {
|
4196 | }
|
4197 | |
4198 |
|
4199 |
|
4200 |
|
4201 | Index.prototype.getCompare = function () {
|
4202 | return this.compare.bind(this);
|
4203 | };
|
4204 | |
4205 |
|
4206 |
|
4207 |
|
4208 |
|
4209 |
|
4210 |
|
4211 | Index.prototype.indexedValueChanged = function (oldNode, newNode) {
|
4212 | var oldWrapped = new NamedNode(MIN_NAME, oldNode);
|
4213 | var newWrapped = new NamedNode(MIN_NAME, newNode);
|
4214 | return this.compare(oldWrapped, newWrapped) !== 0;
|
4215 | };
|
4216 | |
4217 |
|
4218 |
|
4219 |
|
4220 | Index.prototype.minPost = function () {
|
4221 |
|
4222 | return NamedNode.MIN;
|
4223 | };
|
4224 | return Index;
|
4225 | }());
|
4226 |
|
4227 |
|
4228 |
|
4229 |
|
4230 |
|
4231 |
|
4232 |
|
4233 |
|
4234 |
|
4235 |
|
4236 |
|
4237 |
|
4238 |
|
4239 |
|
4240 |
|
4241 |
|
4242 |
|
4243 | var __EMPTY_NODE;
|
4244 | var KeyIndex = (function (_super) {
|
4245 | tslib.__extends(KeyIndex, _super);
|
4246 | function KeyIndex() {
|
4247 | return _super !== null && _super.apply(this, arguments) || this;
|
4248 | }
|
4249 | Object.defineProperty(KeyIndex, "__EMPTY_NODE", {
|
4250 | get: function () {
|
4251 | return __EMPTY_NODE;
|
4252 | },
|
4253 | set: function (val) {
|
4254 | __EMPTY_NODE = val;
|
4255 | },
|
4256 | enumerable: false,
|
4257 | configurable: true
|
4258 | });
|
4259 | KeyIndex.prototype.compare = function (a, b) {
|
4260 | return nameCompare(a.name, b.name);
|
4261 | };
|
4262 | KeyIndex.prototype.isDefinedOn = function (node) {
|
4263 |
|
4264 |
|
4265 | throw util.assertionError('KeyIndex.isDefinedOn not expected to be called.');
|
4266 | };
|
4267 | KeyIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
|
4268 | return false;
|
4269 | };
|
4270 | KeyIndex.prototype.minPost = function () {
|
4271 |
|
4272 | return NamedNode.MIN;
|
4273 | };
|
4274 | KeyIndex.prototype.maxPost = function () {
|
4275 |
|
4276 |
|
4277 | return new NamedNode(MAX_NAME, __EMPTY_NODE);
|
4278 | };
|
4279 | KeyIndex.prototype.makePost = function (indexValue, name) {
|
4280 | util.assert(typeof indexValue === 'string', 'KeyIndex indexValue must always be a string.');
|
4281 |
|
4282 | return new NamedNode(indexValue, __EMPTY_NODE);
|
4283 | };
|
4284 | |
4285 |
|
4286 |
|
4287 | KeyIndex.prototype.toString = function () {
|
4288 | return '.key';
|
4289 | };
|
4290 | return KeyIndex;
|
4291 | }(Index));
|
4292 | var KEY_INDEX = new KeyIndex();
|
4293 |
|
4294 |
|
4295 |
|
4296 |
|
4297 |
|
4298 |
|
4299 |
|
4300 |
|
4301 |
|
4302 |
|
4303 |
|
4304 |
|
4305 |
|
4306 |
|
4307 |
|
4308 |
|
4309 |
|
4310 |
|
4311 |
|
4312 |
|
4313 | var SortedMapIterator = (function () {
|
4314 | |
4315 |
|
4316 |
|
4317 |
|
4318 | function SortedMapIterator(node, startKey, comparator, isReverse_, resultGenerator_) {
|
4319 | if (resultGenerator_ === void 0) { resultGenerator_ = null; }
|
4320 | this.isReverse_ = isReverse_;
|
4321 | this.resultGenerator_ = resultGenerator_;
|
4322 | this.nodeStack_ = [];
|
4323 | var cmp = 1;
|
4324 | while (!node.isEmpty()) {
|
4325 | node = node;
|
4326 | cmp = startKey ? comparator(node.key, startKey) : 1;
|
4327 |
|
4328 | if (isReverse_) {
|
4329 | cmp *= -1;
|
4330 | }
|
4331 | if (cmp < 0) {
|
4332 |
|
4333 | if (this.isReverse_) {
|
4334 | node = node.left;
|
4335 | }
|
4336 | else {
|
4337 | node = node.right;
|
4338 | }
|
4339 | }
|
4340 | else if (cmp === 0) {
|
4341 |
|
4342 | this.nodeStack_.push(node);
|
4343 | break;
|
4344 | }
|
4345 | else {
|
4346 |
|
4347 | this.nodeStack_.push(node);
|
4348 | if (this.isReverse_) {
|
4349 | node = node.right;
|
4350 | }
|
4351 | else {
|
4352 | node = node.left;
|
4353 | }
|
4354 | }
|
4355 | }
|
4356 | }
|
4357 | SortedMapIterator.prototype.getNext = function () {
|
4358 | if (this.nodeStack_.length === 0) {
|
4359 | return null;
|
4360 | }
|
4361 | var node = this.nodeStack_.pop();
|
4362 | var result;
|
4363 | if (this.resultGenerator_) {
|
4364 | result = this.resultGenerator_(node.key, node.value);
|
4365 | }
|
4366 | else {
|
4367 | result = { key: node.key, value: node.value };
|
4368 | }
|
4369 | if (this.isReverse_) {
|
4370 | node = node.left;
|
4371 | while (!node.isEmpty()) {
|
4372 | this.nodeStack_.push(node);
|
4373 | node = node.right;
|
4374 | }
|
4375 | }
|
4376 | else {
|
4377 | node = node.right;
|
4378 | while (!node.isEmpty()) {
|
4379 | this.nodeStack_.push(node);
|
4380 | node = node.left;
|
4381 | }
|
4382 | }
|
4383 | return result;
|
4384 | };
|
4385 | SortedMapIterator.prototype.hasNext = function () {
|
4386 | return this.nodeStack_.length > 0;
|
4387 | };
|
4388 | SortedMapIterator.prototype.peek = function () {
|
4389 | if (this.nodeStack_.length === 0) {
|
4390 | return null;
|
4391 | }
|
4392 | var node = this.nodeStack_[this.nodeStack_.length - 1];
|
4393 | if (this.resultGenerator_) {
|
4394 | return this.resultGenerator_(node.key, node.value);
|
4395 | }
|
4396 | else {
|
4397 | return { key: node.key, value: node.value };
|
4398 | }
|
4399 | };
|
4400 | return SortedMapIterator;
|
4401 | }());
|
4402 |
|
4403 |
|
4404 |
|
4405 | var LLRBNode = (function () {
|
4406 | |
4407 |
|
4408 |
|
4409 |
|
4410 |
|
4411 |
|
4412 |
|
4413 | function LLRBNode(key, value, color, left, right) {
|
4414 | this.key = key;
|
4415 | this.value = value;
|
4416 | this.color = color != null ? color : LLRBNode.RED;
|
4417 | this.left =
|
4418 | left != null ? left : SortedMap.EMPTY_NODE;
|
4419 | this.right =
|
4420 | right != null ? right : SortedMap.EMPTY_NODE;
|
4421 | }
|
4422 | |
4423 |
|
4424 |
|
4425 |
|
4426 |
|
4427 |
|
4428 |
|
4429 |
|
4430 |
|
4431 |
|
4432 | LLRBNode.prototype.copy = function (key, value, color, left, right) {
|
4433 | 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);
|
4434 | };
|
4435 | |
4436 |
|
4437 |
|
4438 | LLRBNode.prototype.count = function () {
|
4439 | return this.left.count() + 1 + this.right.count();
|
4440 | };
|
4441 | |
4442 |
|
4443 |
|
4444 | LLRBNode.prototype.isEmpty = function () {
|
4445 | return false;
|
4446 | };
|
4447 | |
4448 |
|
4449 |
|
4450 |
|
4451 |
|
4452 |
|
4453 |
|
4454 |
|
4455 |
|
4456 | LLRBNode.prototype.inorderTraversal = function (action) {
|
4457 | return (this.left.inorderTraversal(action) ||
|
4458 | !!action(this.key, this.value) ||
|
4459 | this.right.inorderTraversal(action));
|
4460 | };
|
4461 | |
4462 |
|
4463 |
|
4464 |
|
4465 |
|
4466 |
|
4467 |
|
4468 |
|
4469 | LLRBNode.prototype.reverseTraversal = function (action) {
|
4470 | return (this.right.reverseTraversal(action) ||
|
4471 | action(this.key, this.value) ||
|
4472 | this.left.reverseTraversal(action));
|
4473 | };
|
4474 | |
4475 |
|
4476 |
|
4477 | LLRBNode.prototype.min_ = function () {
|
4478 | if (this.left.isEmpty()) {
|
4479 | return this;
|
4480 | }
|
4481 | else {
|
4482 | return this.left.min_();
|
4483 | }
|
4484 | };
|
4485 | |
4486 |
|
4487 |
|
4488 | LLRBNode.prototype.minKey = function () {
|
4489 | return this.min_().key;
|
4490 | };
|
4491 | |
4492 |
|
4493 |
|
4494 | LLRBNode.prototype.maxKey = function () {
|
4495 | if (this.right.isEmpty()) {
|
4496 | return this.key;
|
4497 | }
|
4498 | else {
|
4499 | return this.right.maxKey();
|
4500 | }
|
4501 | };
|
4502 | |
4503 |
|
4504 |
|
4505 |
|
4506 |
|
4507 |
|
4508 | LLRBNode.prototype.insert = function (key, value, comparator) {
|
4509 | var n = this;
|
4510 | var cmp = comparator(key, n.key);
|
4511 | if (cmp < 0) {
|
4512 | n = n.copy(null, null, null, n.left.insert(key, value, comparator), null);
|
4513 | }
|
4514 | else if (cmp === 0) {
|
4515 | n = n.copy(null, value, null, null, null);
|
4516 | }
|
4517 | else {
|
4518 | n = n.copy(null, null, null, null, n.right.insert(key, value, comparator));
|
4519 | }
|
4520 | return n.fixUp_();
|
4521 | };
|
4522 | |
4523 |
|
4524 |
|
4525 | LLRBNode.prototype.removeMin_ = function () {
|
4526 | if (this.left.isEmpty()) {
|
4527 | return SortedMap.EMPTY_NODE;
|
4528 | }
|
4529 | var n = this;
|
4530 | if (!n.left.isRed_() && !n.left.left.isRed_()) {
|
4531 | n = n.moveRedLeft_();
|
4532 | }
|
4533 | n = n.copy(null, null, null, n.left.removeMin_(), null);
|
4534 | return n.fixUp_();
|
4535 | };
|
4536 | |
4537 |
|
4538 |
|
4539 |
|
4540 |
|
4541 | LLRBNode.prototype.remove = function (key, comparator) {
|
4542 | var n, smallest;
|
4543 | n = this;
|
4544 | if (comparator(key, n.key) < 0) {
|
4545 | if (!n.left.isEmpty() && !n.left.isRed_() && !n.left.left.isRed_()) {
|
4546 | n = n.moveRedLeft_();
|
4547 | }
|
4548 | n = n.copy(null, null, null, n.left.remove(key, comparator), null);
|
4549 | }
|
4550 | else {
|
4551 | if (n.left.isRed_()) {
|
4552 | n = n.rotateRight_();
|
4553 | }
|
4554 | if (!n.right.isEmpty() && !n.right.isRed_() && !n.right.left.isRed_()) {
|
4555 | n = n.moveRedRight_();
|
4556 | }
|
4557 | if (comparator(key, n.key) === 0) {
|
4558 | if (n.right.isEmpty()) {
|
4559 | return SortedMap.EMPTY_NODE;
|
4560 | }
|
4561 | else {
|
4562 | smallest = n.right.min_();
|
4563 | n = n.copy(smallest.key, smallest.value, null, null, n.right.removeMin_());
|
4564 | }
|
4565 | }
|
4566 | n = n.copy(null, null, null, null, n.right.remove(key, comparator));
|
4567 | }
|
4568 | return n.fixUp_();
|
4569 | };
|
4570 | |
4571 |
|
4572 |
|
4573 | LLRBNode.prototype.isRed_ = function () {
|
4574 | return this.color;
|
4575 | };
|
4576 | |
4577 |
|
4578 |
|
4579 | LLRBNode.prototype.fixUp_ = function () {
|
4580 | var n = this;
|
4581 | if (n.right.isRed_() && !n.left.isRed_()) {
|
4582 | n = n.rotateLeft_();
|
4583 | }
|
4584 | if (n.left.isRed_() && n.left.left.isRed_()) {
|
4585 | n = n.rotateRight_();
|
4586 | }
|
4587 | if (n.left.isRed_() && n.right.isRed_()) {
|
4588 | n = n.colorFlip_();
|
4589 | }
|
4590 | return n;
|
4591 | };
|
4592 | |
4593 |
|
4594 |
|
4595 | LLRBNode.prototype.moveRedLeft_ = function () {
|
4596 | var n = this.colorFlip_();
|
4597 | if (n.right.left.isRed_()) {
|
4598 | n = n.copy(null, null, null, null, n.right.rotateRight_());
|
4599 | n = n.rotateLeft_();
|
4600 | n = n.colorFlip_();
|
4601 | }
|
4602 | return n;
|
4603 | };
|
4604 | |
4605 |
|
4606 |
|
4607 | LLRBNode.prototype.moveRedRight_ = function () {
|
4608 | var n = this.colorFlip_();
|
4609 | if (n.left.left.isRed_()) {
|
4610 | n = n.rotateRight_();
|
4611 | n = n.colorFlip_();
|
4612 | }
|
4613 | return n;
|
4614 | };
|
4615 | |
4616 |
|
4617 |
|
4618 | LLRBNode.prototype.rotateLeft_ = function () {
|
4619 | var nl = this.copy(null, null, LLRBNode.RED, null, this.right.left);
|
4620 | return this.right.copy(null, null, this.color, nl, null);
|
4621 | };
|
4622 | |
4623 |
|
4624 |
|
4625 | LLRBNode.prototype.rotateRight_ = function () {
|
4626 | var nr = this.copy(null, null, LLRBNode.RED, this.left.right, null);
|
4627 | return this.left.copy(null, null, this.color, null, nr);
|
4628 | };
|
4629 | |
4630 |
|
4631 |
|
4632 | LLRBNode.prototype.colorFlip_ = function () {
|
4633 | var left = this.left.copy(null, null, !this.left.color, null, null);
|
4634 | var right = this.right.copy(null, null, !this.right.color, null, null);
|
4635 | return this.copy(null, null, !this.color, left, right);
|
4636 | };
|
4637 | |
4638 |
|
4639 |
|
4640 |
|
4641 |
|
4642 | LLRBNode.prototype.checkMaxDepth_ = function () {
|
4643 | var blackDepth = this.check_();
|
4644 | return Math.pow(2.0, blackDepth) <= this.count() + 1;
|
4645 | };
|
4646 | LLRBNode.prototype.check_ = function () {
|
4647 | if (this.isRed_() && this.left.isRed_()) {
|
4648 | throw new Error('Red node has red child(' + this.key + ',' + this.value + ')');
|
4649 | }
|
4650 | if (this.right.isRed_()) {
|
4651 | throw new Error('Right child of (' + this.key + ',' + this.value + ') is red');
|
4652 | }
|
4653 | var blackDepth = this.left.check_();
|
4654 | if (blackDepth !== this.right.check_()) {
|
4655 | throw new Error('Black depths differ');
|
4656 | }
|
4657 | else {
|
4658 | return blackDepth + (this.isRed_() ? 0 : 1);
|
4659 | }
|
4660 | };
|
4661 | LLRBNode.RED = true;
|
4662 | LLRBNode.BLACK = false;
|
4663 | return LLRBNode;
|
4664 | }());
|
4665 |
|
4666 |
|
4667 |
|
4668 | var LLRBEmptyNode = (function () {
|
4669 | function LLRBEmptyNode() {
|
4670 | }
|
4671 | |
4672 |
|
4673 |
|
4674 |
|
4675 |
|
4676 | LLRBEmptyNode.prototype.copy = function (key, value, color, left, right) {
|
4677 | return this;
|
4678 | };
|
4679 | |
4680 |
|
4681 |
|
4682 |
|
4683 |
|
4684 |
|
4685 |
|
4686 |
|
4687 | LLRBEmptyNode.prototype.insert = function (key, value, comparator) {
|
4688 | return new LLRBNode(key, value, null);
|
4689 | };
|
4690 | |
4691 |
|
4692 |
|
4693 |
|
4694 |
|
4695 |
|
4696 |
|
4697 | LLRBEmptyNode.prototype.remove = function (key, comparator) {
|
4698 | return this;
|
4699 | };
|
4700 | |
4701 |
|
4702 |
|
4703 | LLRBEmptyNode.prototype.count = function () {
|
4704 | return 0;
|
4705 | };
|
4706 | |
4707 |
|
4708 |
|
4709 | LLRBEmptyNode.prototype.isEmpty = function () {
|
4710 | return true;
|
4711 | };
|
4712 | |
4713 |
|
4714 |
|
4715 |
|
4716 |
|
4717 |
|
4718 |
|
4719 |
|
4720 | LLRBEmptyNode.prototype.inorderTraversal = function (action) {
|
4721 | return false;
|
4722 | };
|
4723 | |
4724 |
|
4725 |
|
4726 |
|
4727 |
|
4728 |
|
4729 |
|
4730 |
|
4731 | LLRBEmptyNode.prototype.reverseTraversal = function (action) {
|
4732 | return false;
|
4733 | };
|
4734 | LLRBEmptyNode.prototype.minKey = function () {
|
4735 | return null;
|
4736 | };
|
4737 | LLRBEmptyNode.prototype.maxKey = function () {
|
4738 | return null;
|
4739 | };
|
4740 | LLRBEmptyNode.prototype.check_ = function () {
|
4741 | return 0;
|
4742 | };
|
4743 | |
4744 |
|
4745 |
|
4746 | LLRBEmptyNode.prototype.isRed_ = function () {
|
4747 | return false;
|
4748 | };
|
4749 | return LLRBEmptyNode;
|
4750 | }());
|
4751 |
|
4752 |
|
4753 |
|
4754 |
|
4755 | var SortedMap = (function () {
|
4756 | |
4757 |
|
4758 |
|
4759 |
|
4760 | function SortedMap(comparator_, root_) {
|
4761 | if (root_ === void 0) { root_ = SortedMap.EMPTY_NODE; }
|
4762 | this.comparator_ = comparator_;
|
4763 | this.root_ = root_;
|
4764 | }
|
4765 | |
4766 |
|
4767 |
|
4768 |
|
4769 |
|
4770 |
|
4771 |
|
4772 |
|
4773 | SortedMap.prototype.insert = function (key, value) {
|
4774 | return new SortedMap(this.comparator_, this.root_
|
4775 | .insert(key, value, this.comparator_)
|
4776 | .copy(null, null, LLRBNode.BLACK, null, null));
|
4777 | };
|
4778 | |
4779 |
|
4780 |
|
4781 |
|
4782 |
|
4783 |
|
4784 | SortedMap.prototype.remove = function (key) {
|
4785 | return new SortedMap(this.comparator_, this.root_
|
4786 | .remove(key, this.comparator_)
|
4787 | .copy(null, null, LLRBNode.BLACK, null, null));
|
4788 | };
|
4789 | |
4790 |
|
4791 |
|
4792 |
|
4793 |
|
4794 |
|
4795 |
|
4796 | SortedMap.prototype.get = function (key) {
|
4797 | var cmp;
|
4798 | var node = this.root_;
|
4799 | while (!node.isEmpty()) {
|
4800 | cmp = this.comparator_(key, node.key);
|
4801 | if (cmp === 0) {
|
4802 | return node.value;
|
4803 | }
|
4804 | else if (cmp < 0) {
|
4805 | node = node.left;
|
4806 | }
|
4807 | else if (cmp > 0) {
|
4808 | node = node.right;
|
4809 | }
|
4810 | }
|
4811 | return null;
|
4812 | };
|
4813 | |
4814 |
|
4815 |
|
4816 |
|
4817 |
|
4818 | SortedMap.prototype.getPredecessorKey = function (key) {
|
4819 | var cmp, node = this.root_, rightParent = null;
|
4820 | while (!node.isEmpty()) {
|
4821 | cmp = this.comparator_(key, node.key);
|
4822 | if (cmp === 0) {
|
4823 | if (!node.left.isEmpty()) {
|
4824 | node = node.left;
|
4825 | while (!node.right.isEmpty()) {
|
4826 | node = node.right;
|
4827 | }
|
4828 | return node.key;
|
4829 | }
|
4830 | else if (rightParent) {
|
4831 | return rightParent.key;
|
4832 | }
|
4833 | else {
|
4834 | return null;
|
4835 | }
|
4836 | }
|
4837 | else if (cmp < 0) {
|
4838 | node = node.left;
|
4839 | }
|
4840 | else if (cmp > 0) {
|
4841 | rightParent = node;
|
4842 | node = node.right;
|
4843 | }
|
4844 | }
|
4845 | throw new Error('Attempted to find predecessor key for a nonexistent key. What gives?');
|
4846 | };
|
4847 | |
4848 |
|
4849 |
|
4850 | SortedMap.prototype.isEmpty = function () {
|
4851 | return this.root_.isEmpty();
|
4852 | };
|
4853 | |
4854 |
|
4855 |
|
4856 | SortedMap.prototype.count = function () {
|
4857 | return this.root_.count();
|
4858 | };
|
4859 | |
4860 |
|
4861 |
|
4862 | SortedMap.prototype.minKey = function () {
|
4863 | return this.root_.minKey();
|
4864 | };
|
4865 | |
4866 |
|
4867 |
|
4868 | SortedMap.prototype.maxKey = function () {
|
4869 | return this.root_.maxKey();
|
4870 | };
|
4871 | |
4872 |
|
4873 |
|
4874 |
|
4875 |
|
4876 |
|
4877 |
|
4878 |
|
4879 |
|
4880 | SortedMap.prototype.inorderTraversal = function (action) {
|
4881 | return this.root_.inorderTraversal(action);
|
4882 | };
|
4883 | |
4884 |
|
4885 |
|
4886 |
|
4887 |
|
4888 |
|
4889 |
|
4890 |
|
4891 | SortedMap.prototype.reverseTraversal = function (action) {
|
4892 | return this.root_.reverseTraversal(action);
|
4893 | };
|
4894 | |
4895 |
|
4896 |
|
4897 |
|
4898 | SortedMap.prototype.getIterator = function (resultGenerator) {
|
4899 | return new SortedMapIterator(this.root_, null, this.comparator_, false, resultGenerator);
|
4900 | };
|
4901 | SortedMap.prototype.getIteratorFrom = function (key, resultGenerator) {
|
4902 | return new SortedMapIterator(this.root_, key, this.comparator_, false, resultGenerator);
|
4903 | };
|
4904 | SortedMap.prototype.getReverseIteratorFrom = function (key, resultGenerator) {
|
4905 | return new SortedMapIterator(this.root_, key, this.comparator_, true, resultGenerator);
|
4906 | };
|
4907 | SortedMap.prototype.getReverseIterator = function (resultGenerator) {
|
4908 | return new SortedMapIterator(this.root_, null, this.comparator_, true, resultGenerator);
|
4909 | };
|
4910 | |
4911 |
|
4912 |
|
4913 | SortedMap.EMPTY_NODE = new LLRBEmptyNode();
|
4914 | return SortedMap;
|
4915 | }());
|
4916 |
|
4917 |
|
4918 |
|
4919 |
|
4920 |
|
4921 |
|
4922 |
|
4923 |
|
4924 |
|
4925 |
|
4926 |
|
4927 |
|
4928 |
|
4929 |
|
4930 |
|
4931 |
|
4932 |
|
4933 | function NAME_ONLY_COMPARATOR(left, right) {
|
4934 | return nameCompare(left.name, right.name);
|
4935 | }
|
4936 | function NAME_COMPARATOR(left, right) {
|
4937 | return nameCompare(left, right);
|
4938 | }
|
4939 |
|
4940 |
|
4941 |
|
4942 |
|
4943 |
|
4944 |
|
4945 |
|
4946 |
|
4947 |
|
4948 |
|
4949 |
|
4950 |
|
4951 |
|
4952 |
|
4953 |
|
4954 |
|
4955 |
|
4956 | var MAX_NODE;
|
4957 | function setMaxNode(val) {
|
4958 | MAX_NODE = val;
|
4959 | }
|
4960 | var priorityHashText = function (priority) {
|
4961 | if (typeof priority === 'number') {
|
4962 | return 'number:' + doubleToIEEE754String(priority);
|
4963 | }
|
4964 | else {
|
4965 | return 'string:' + priority;
|
4966 | }
|
4967 | };
|
4968 |
|
4969 |
|
4970 |
|
4971 | var validatePriorityNode = function (priorityNode) {
|
4972 | if (priorityNode.isLeafNode()) {
|
4973 | var val = priorityNode.val();
|
4974 | util.assert(typeof val === 'string' ||
|
4975 | typeof val === 'number' ||
|
4976 | (typeof val === 'object' && util.contains(val, '.sv')), 'Priority must be a string or number.');
|
4977 | }
|
4978 | else {
|
4979 | util.assert(priorityNode === MAX_NODE || priorityNode.isEmpty(), 'priority of unexpected type.');
|
4980 | }
|
4981 |
|
4982 | util.assert(priorityNode === MAX_NODE || priorityNode.getPriority().isEmpty(), "Priority nodes can't have a priority of their own.");
|
4983 | };
|
4984 |
|
4985 |
|
4986 |
|
4987 |
|
4988 |
|
4989 |
|
4990 |
|
4991 |
|
4992 |
|
4993 |
|
4994 |
|
4995 |
|
4996 |
|
4997 |
|
4998 |
|
4999 |
|
5000 |
|
5001 | var __childrenNodeConstructor;
|
5002 |
|
5003 |
|
5004 |
|
5005 |
|
5006 |
|
5007 | var LeafNode = (function () {
|
5008 | |
5009 |
|
5010 |
|
5011 |
|
5012 |
|
5013 | function LeafNode(value_, priorityNode_) {
|
5014 | if (priorityNode_ === void 0) { priorityNode_ = LeafNode.__childrenNodeConstructor.EMPTY_NODE; }
|
5015 | this.value_ = value_;
|
5016 | this.priorityNode_ = priorityNode_;
|
5017 | this.lazyHash_ = null;
|
5018 | util.assert(this.value_ !== undefined && this.value_ !== null, "LeafNode shouldn't be created with null/undefined value.");
|
5019 | validatePriorityNode(this.priorityNode_);
|
5020 | }
|
5021 | Object.defineProperty(LeafNode, "__childrenNodeConstructor", {
|
5022 | get: function () {
|
5023 | return __childrenNodeConstructor;
|
5024 | },
|
5025 | set: function (val) {
|
5026 | __childrenNodeConstructor = val;
|
5027 | },
|
5028 | enumerable: false,
|
5029 | configurable: true
|
5030 | });
|
5031 |
|
5032 | LeafNode.prototype.isLeafNode = function () {
|
5033 | return true;
|
5034 | };
|
5035 |
|
5036 | LeafNode.prototype.getPriority = function () {
|
5037 | return this.priorityNode_;
|
5038 | };
|
5039 |
|
5040 | LeafNode.prototype.updatePriority = function (newPriorityNode) {
|
5041 | return new LeafNode(this.value_, newPriorityNode);
|
5042 | };
|
5043 |
|
5044 | LeafNode.prototype.getImmediateChild = function (childName) {
|
5045 |
|
5046 | if (childName === '.priority') {
|
5047 | return this.priorityNode_;
|
5048 | }
|
5049 | else {
|
5050 | return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
|
5051 | }
|
5052 | };
|
5053 |
|
5054 | LeafNode.prototype.getChild = function (path) {
|
5055 | if (pathIsEmpty(path)) {
|
5056 | return this;
|
5057 | }
|
5058 | else if (pathGetFront(path) === '.priority') {
|
5059 | return this.priorityNode_;
|
5060 | }
|
5061 | else {
|
5062 | return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
|
5063 | }
|
5064 | };
|
5065 | LeafNode.prototype.hasChild = function () {
|
5066 | return false;
|
5067 | };
|
5068 |
|
5069 | LeafNode.prototype.getPredecessorChildName = function (childName, childNode) {
|
5070 | return null;
|
5071 | };
|
5072 |
|
5073 | LeafNode.prototype.updateImmediateChild = function (childName, newChildNode) {
|
5074 | if (childName === '.priority') {
|
5075 | return this.updatePriority(newChildNode);
|
5076 | }
|
5077 | else if (newChildNode.isEmpty() && childName !== '.priority') {
|
5078 | return this;
|
5079 | }
|
5080 | else {
|
5081 | return LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateImmediateChild(childName, newChildNode).updatePriority(this.priorityNode_);
|
5082 | }
|
5083 | };
|
5084 |
|
5085 | LeafNode.prototype.updateChild = function (path, newChildNode) {
|
5086 | var front = pathGetFront(path);
|
5087 | if (front === null) {
|
5088 | return newChildNode;
|
5089 | }
|
5090 | else if (newChildNode.isEmpty() && front !== '.priority') {
|
5091 | return this;
|
5092 | }
|
5093 | else {
|
5094 | util.assert(front !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
|
5095 | return this.updateImmediateChild(front, LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateChild(pathPopFront(path), newChildNode));
|
5096 | }
|
5097 | };
|
5098 |
|
5099 | LeafNode.prototype.isEmpty = function () {
|
5100 | return false;
|
5101 | };
|
5102 |
|
5103 | LeafNode.prototype.numChildren = function () {
|
5104 | return 0;
|
5105 | };
|
5106 |
|
5107 | LeafNode.prototype.forEachChild = function (index, action) {
|
5108 | return false;
|
5109 | };
|
5110 | LeafNode.prototype.val = function (exportFormat) {
|
5111 | if (exportFormat && !this.getPriority().isEmpty()) {
|
5112 | return {
|
5113 | '.value': this.getValue(),
|
5114 | '.priority': this.getPriority().val()
|
5115 | };
|
5116 | }
|
5117 | else {
|
5118 | return this.getValue();
|
5119 | }
|
5120 | };
|
5121 |
|
5122 | LeafNode.prototype.hash = function () {
|
5123 | if (this.lazyHash_ === null) {
|
5124 | var toHash = '';
|
5125 | if (!this.priorityNode_.isEmpty()) {
|
5126 | toHash +=
|
5127 | 'priority:' +
|
5128 | priorityHashText(this.priorityNode_.val()) +
|
5129 | ':';
|
5130 | }
|
5131 | var type = typeof this.value_;
|
5132 | toHash += type + ':';
|
5133 | if (type === 'number') {
|
5134 | toHash += doubleToIEEE754String(this.value_);
|
5135 | }
|
5136 | else {
|
5137 | toHash += this.value_;
|
5138 | }
|
5139 | this.lazyHash_ = sha1(toHash);
|
5140 | }
|
5141 | return this.lazyHash_;
|
5142 | };
|
5143 | |
5144 |
|
5145 |
|
5146 |
|
5147 | LeafNode.prototype.getValue = function () {
|
5148 | return this.value_;
|
5149 | };
|
5150 | LeafNode.prototype.compareTo = function (other) {
|
5151 | if (other === LeafNode.__childrenNodeConstructor.EMPTY_NODE) {
|
5152 | return 1;
|
5153 | }
|
5154 | else if (other instanceof LeafNode.__childrenNodeConstructor) {
|
5155 | return -1;
|
5156 | }
|
5157 | else {
|
5158 | util.assert(other.isLeafNode(), 'Unknown node type');
|
5159 | return this.compareToLeafNode_(other);
|
5160 | }
|
5161 | };
|
5162 | |
5163 |
|
5164 |
|
5165 | LeafNode.prototype.compareToLeafNode_ = function (otherLeaf) {
|
5166 | var otherLeafType = typeof otherLeaf.value_;
|
5167 | var thisLeafType = typeof this.value_;
|
5168 | var otherIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(otherLeafType);
|
5169 | var thisIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(thisLeafType);
|
5170 | util.assert(otherIndex >= 0, 'Unknown leaf type: ' + otherLeafType);
|
5171 | util.assert(thisIndex >= 0, 'Unknown leaf type: ' + thisLeafType);
|
5172 | if (otherIndex === thisIndex) {
|
5173 |
|
5174 | if (thisLeafType === 'object') {
|
5175 |
|
5176 | return 0;
|
5177 | }
|
5178 | else {
|
5179 |
|
5180 | if (this.value_ < otherLeaf.value_) {
|
5181 | return -1;
|
5182 | }
|
5183 | else if (this.value_ === otherLeaf.value_) {
|
5184 | return 0;
|
5185 | }
|
5186 | else {
|
5187 | return 1;
|
5188 | }
|
5189 | }
|
5190 | }
|
5191 | else {
|
5192 | return thisIndex - otherIndex;
|
5193 | }
|
5194 | };
|
5195 | LeafNode.prototype.withIndex = function () {
|
5196 | return this;
|
5197 | };
|
5198 | LeafNode.prototype.isIndexed = function () {
|
5199 | return true;
|
5200 | };
|
5201 | LeafNode.prototype.equals = function (other) {
|
5202 | if (other === this) {
|
5203 | return true;
|
5204 | }
|
5205 | else if (other.isLeafNode()) {
|
5206 | var otherLeaf = other;
|
5207 | return (this.value_ === otherLeaf.value_ &&
|
5208 | this.priorityNode_.equals(otherLeaf.priorityNode_));
|
5209 | }
|
5210 | else {
|
5211 | return false;
|
5212 | }
|
5213 | };
|
5214 | |
5215 |
|
5216 |
|
5217 |
|
5218 | LeafNode.VALUE_TYPE_ORDER = ['object', 'boolean', 'number', 'string'];
|
5219 | return LeafNode;
|
5220 | }());
|
5221 |
|
5222 |
|
5223 |
|
5224 |
|
5225 |
|
5226 |
|
5227 |
|
5228 |
|
5229 |
|
5230 |
|
5231 |
|
5232 |
|
5233 |
|
5234 |
|
5235 |
|
5236 |
|
5237 |
|
5238 | var nodeFromJSON;
|
5239 | var MAX_NODE$1;
|
5240 | function setNodeFromJSON(val) {
|
5241 | nodeFromJSON = val;
|
5242 | }
|
5243 | function setMaxNode$1(val) {
|
5244 | MAX_NODE$1 = val;
|
5245 | }
|
5246 | var PriorityIndex = (function (_super) {
|
5247 | tslib.__extends(PriorityIndex, _super);
|
5248 | function PriorityIndex() {
|
5249 | return _super !== null && _super.apply(this, arguments) || this;
|
5250 | }
|
5251 | PriorityIndex.prototype.compare = function (a, b) {
|
5252 | var aPriority = a.node.getPriority();
|
5253 | var bPriority = b.node.getPriority();
|
5254 | var indexCmp = aPriority.compareTo(bPriority);
|
5255 | if (indexCmp === 0) {
|
5256 | return nameCompare(a.name, b.name);
|
5257 | }
|
5258 | else {
|
5259 | return indexCmp;
|
5260 | }
|
5261 | };
|
5262 | PriorityIndex.prototype.isDefinedOn = function (node) {
|
5263 | return !node.getPriority().isEmpty();
|
5264 | };
|
5265 | PriorityIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
|
5266 | return !oldNode.getPriority().equals(newNode.getPriority());
|
5267 | };
|
5268 | PriorityIndex.prototype.minPost = function () {
|
5269 |
|
5270 | return NamedNode.MIN;
|
5271 | };
|
5272 | PriorityIndex.prototype.maxPost = function () {
|
5273 | return new NamedNode(MAX_NAME, new LeafNode('[PRIORITY-POST]', MAX_NODE$1));
|
5274 | };
|
5275 | PriorityIndex.prototype.makePost = function (indexValue, name) {
|
5276 | var priorityNode = nodeFromJSON(indexValue);
|
5277 | return new NamedNode(name, new LeafNode('[PRIORITY-POST]', priorityNode));
|
5278 | };
|
5279 | |
5280 |
|
5281 |
|
5282 | PriorityIndex.prototype.toString = function () {
|
5283 | return '.priority';
|
5284 | };
|
5285 | return PriorityIndex;
|
5286 | }(Index));
|
5287 | var PRIORITY_INDEX = new PriorityIndex();
|
5288 |
|
5289 |
|
5290 |
|
5291 |
|
5292 |
|
5293 |
|
5294 |
|
5295 |
|
5296 |
|
5297 |
|
5298 |
|
5299 |
|
5300 |
|
5301 |
|
5302 |
|
5303 |
|
5304 |
|
5305 | var LOG_2 = Math.log(2);
|
5306 | var Base12Num = (function () {
|
5307 | function Base12Num(length) {
|
5308 | var logBase2 = function (num) {
|
5309 |
|
5310 | return parseInt((Math.log(num) / LOG_2), 10);
|
5311 | };
|
5312 | var bitMask = function (bits) { return parseInt(Array(bits + 1).join('1'), 2); };
|
5313 | this.count = logBase2(length + 1);
|
5314 | this.current_ = this.count - 1;
|
5315 | var mask = bitMask(this.count);
|
5316 | this.bits_ = (length + 1) & mask;
|
5317 | }
|
5318 | Base12Num.prototype.nextBitIsOne = function () {
|
5319 |
|
5320 | var result = !(this.bits_ & (0x1 << this.current_));
|
5321 | this.current_--;
|
5322 | return result;
|
5323 | };
|
5324 | return Base12Num;
|
5325 | }());
|
5326 |
|
5327 |
|
5328 |
|
5329 |
|
5330 |
|
5331 |
|
5332 |
|
5333 |
|
5334 |
|
5335 |
|
5336 |
|
5337 |
|
5338 |
|
5339 | var buildChildSet = function (childList, cmp, keyFn, mapSortFn) {
|
5340 | childList.sort(cmp);
|
5341 | var buildBalancedTree = function (low, high) {
|
5342 | var length = high - low;
|
5343 | var namedNode;
|
5344 | var key;
|
5345 | if (length === 0) {
|
5346 | return null;
|
5347 | }
|
5348 | else if (length === 1) {
|
5349 | namedNode = childList[low];
|
5350 | key = keyFn ? keyFn(namedNode) : namedNode;
|
5351 | return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, null, null);
|
5352 | }
|
5353 | else {
|
5354 |
|
5355 | var middle = parseInt((length / 2), 10) + low;
|
5356 | var left = buildBalancedTree(low, middle);
|
5357 | var right = buildBalancedTree(middle + 1, high);
|
5358 | namedNode = childList[middle];
|
5359 | key = keyFn ? keyFn(namedNode) : namedNode;
|
5360 | return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, left, right);
|
5361 | }
|
5362 | };
|
5363 | var buildFrom12Array = function (base12) {
|
5364 | var node = null;
|
5365 | var root = null;
|
5366 | var index = childList.length;
|
5367 | var buildPennant = function (chunkSize, color) {
|
5368 | var low = index - chunkSize;
|
5369 | var high = index;
|
5370 | index -= chunkSize;
|
5371 | var childTree = buildBalancedTree(low + 1, high);
|
5372 | var namedNode = childList[low];
|
5373 | var key = keyFn ? keyFn(namedNode) : namedNode;
|
5374 | attachPennant(new LLRBNode(key, namedNode.node, color, null, childTree));
|
5375 | };
|
5376 | var attachPennant = function (pennant) {
|
5377 | if (node) {
|
5378 | node.left = pennant;
|
5379 | node = pennant;
|
5380 | }
|
5381 | else {
|
5382 | root = pennant;
|
5383 | node = pennant;
|
5384 | }
|
5385 | };
|
5386 | for (var i = 0; i < base12.count; ++i) {
|
5387 | var isOne = base12.nextBitIsOne();
|
5388 |
|
5389 | var chunkSize = Math.pow(2, base12.count - (i + 1));
|
5390 | if (isOne) {
|
5391 | buildPennant(chunkSize, LLRBNode.BLACK);
|
5392 | }
|
5393 | else {
|
5394 |
|
5395 | buildPennant(chunkSize, LLRBNode.BLACK);
|
5396 | buildPennant(chunkSize, LLRBNode.RED);
|
5397 | }
|
5398 | }
|
5399 | return root;
|
5400 | };
|
5401 | var base12 = new Base12Num(childList.length);
|
5402 | var root = buildFrom12Array(base12);
|
5403 |
|
5404 | return new SortedMap(mapSortFn || cmp, root);
|
5405 | };
|
5406 |
|
5407 |
|
5408 |
|
5409 |
|
5410 |
|
5411 |
|
5412 |
|
5413 |
|
5414 |
|
5415 |
|
5416 |
|
5417 |
|
5418 |
|
5419 |
|
5420 |
|
5421 |
|
5422 |
|
5423 | var _defaultIndexMap;
|
5424 | var fallbackObject = {};
|
5425 | var IndexMap = (function () {
|
5426 | function IndexMap(indexes_, indexSet_) {
|
5427 | this.indexes_ = indexes_;
|
5428 | this.indexSet_ = indexSet_;
|
5429 | }
|
5430 | Object.defineProperty(IndexMap, "Default", {
|
5431 | |
5432 |
|
5433 |
|
5434 | get: function () {
|
5435 | util.assert(fallbackObject && PRIORITY_INDEX, 'ChildrenNode.ts has not been loaded');
|
5436 | _defaultIndexMap =
|
5437 | _defaultIndexMap ||
|
5438 | new IndexMap({ '.priority': fallbackObject }, { '.priority': PRIORITY_INDEX });
|
5439 | return _defaultIndexMap;
|
5440 | },
|
5441 | enumerable: false,
|
5442 | configurable: true
|
5443 | });
|
5444 | IndexMap.prototype.get = function (indexKey) {
|
5445 | var sortedMap = util.safeGet(this.indexes_, indexKey);
|
5446 | if (!sortedMap) {
|
5447 | throw new Error('No index defined for ' + indexKey);
|
5448 | }
|
5449 | if (sortedMap instanceof SortedMap) {
|
5450 | return sortedMap;
|
5451 | }
|
5452 | else {
|
5453 |
|
5454 |
|
5455 | return null;
|
5456 | }
|
5457 | };
|
5458 | IndexMap.prototype.hasIndex = function (indexDefinition) {
|
5459 | return util.contains(this.indexSet_, indexDefinition.toString());
|
5460 | };
|
5461 | IndexMap.prototype.addIndex = function (indexDefinition, existingChildren) {
|
5462 | util.assert(indexDefinition !== KEY_INDEX, "KeyIndex always exists and isn't meant to be added to the IndexMap.");
|
5463 | var childList = [];
|
5464 | var sawIndexedValue = false;
|
5465 | var iter = existingChildren.getIterator(NamedNode.Wrap);
|
5466 | var next = iter.getNext();
|
5467 | while (next) {
|
5468 | sawIndexedValue =
|
5469 | sawIndexedValue || indexDefinition.isDefinedOn(next.node);
|
5470 | childList.push(next);
|
5471 | next = iter.getNext();
|
5472 | }
|
5473 | var newIndex;
|
5474 | if (sawIndexedValue) {
|
5475 | newIndex = buildChildSet(childList, indexDefinition.getCompare());
|
5476 | }
|
5477 | else {
|
5478 | newIndex = fallbackObject;
|
5479 | }
|
5480 | var indexName = indexDefinition.toString();
|
5481 | var newIndexSet = tslib.__assign({}, this.indexSet_);
|
5482 | newIndexSet[indexName] = indexDefinition;
|
5483 | var newIndexes = tslib.__assign({}, this.indexes_);
|
5484 | newIndexes[indexName] = newIndex;
|
5485 | return new IndexMap(newIndexes, newIndexSet);
|
5486 | };
|
5487 | |
5488 |
|
5489 |
|
5490 | IndexMap.prototype.addToIndexes = function (namedNode, existingChildren) {
|
5491 | var _this = this;
|
5492 | var newIndexes = util.map(this.indexes_, function (indexedChildren, indexName) {
|
5493 | var index = util.safeGet(_this.indexSet_, indexName);
|
5494 | util.assert(index, 'Missing index implementation for ' + indexName);
|
5495 | if (indexedChildren === fallbackObject) {
|
5496 |
|
5497 | if (index.isDefinedOn(namedNode.node)) {
|
5498 |
|
5499 | var childList = [];
|
5500 | var iter = existingChildren.getIterator(NamedNode.Wrap);
|
5501 | var next = iter.getNext();
|
5502 | while (next) {
|
5503 | if (next.name !== namedNode.name) {
|
5504 | childList.push(next);
|
5505 | }
|
5506 | next = iter.getNext();
|
5507 | }
|
5508 | childList.push(namedNode);
|
5509 | return buildChildSet(childList, index.getCompare());
|
5510 | }
|
5511 | else {
|
5512 |
|
5513 | return fallbackObject;
|
5514 | }
|
5515 | }
|
5516 | else {
|
5517 | var existingSnap = existingChildren.get(namedNode.name);
|
5518 | var newChildren = indexedChildren;
|
5519 | if (existingSnap) {
|
5520 | newChildren = newChildren.remove(new NamedNode(namedNode.name, existingSnap));
|
5521 | }
|
5522 | return newChildren.insert(namedNode, namedNode.node);
|
5523 | }
|
5524 | });
|
5525 | return new IndexMap(newIndexes, this.indexSet_);
|
5526 | };
|
5527 | |
5528 |
|
5529 |
|
5530 | IndexMap.prototype.removeFromIndexes = function (namedNode, existingChildren) {
|
5531 | var newIndexes = util.map(this.indexes_, function (indexedChildren) {
|
5532 | if (indexedChildren === fallbackObject) {
|
5533 |
|
5534 | return indexedChildren;
|
5535 | }
|
5536 | else {
|
5537 | var existingSnap = existingChildren.get(namedNode.name);
|
5538 | if (existingSnap) {
|
5539 | return indexedChildren.remove(new NamedNode(namedNode.name, existingSnap));
|
5540 | }
|
5541 | else {
|
5542 |
|
5543 | return indexedChildren;
|
5544 | }
|
5545 | }
|
5546 | });
|
5547 | return new IndexMap(newIndexes, this.indexSet_);
|
5548 | };
|
5549 | return IndexMap;
|
5550 | }());
|
5551 |
|
5552 |
|
5553 |
|
5554 |
|
5555 |
|
5556 |
|
5557 |
|
5558 |
|
5559 |
|
5560 |
|
5561 |
|
5562 |
|
5563 |
|
5564 |
|
5565 |
|
5566 |
|
5567 |
|
5568 |
|
5569 | var EMPTY_NODE;
|
5570 |
|
5571 |
|
5572 |
|
5573 |
|
5574 |
|
5575 | var ChildrenNode = (function () {
|
5576 | |
5577 |
|
5578 |
|
5579 |
|
5580 | function ChildrenNode(children_, priorityNode_, indexMap_) {
|
5581 | this.children_ = children_;
|
5582 | this.priorityNode_ = priorityNode_;
|
5583 | this.indexMap_ = indexMap_;
|
5584 | this.lazyHash_ = null;
|
5585 | |
5586 |
|
5587 |
|
5588 |
|
5589 |
|
5590 | if (this.priorityNode_) {
|
5591 | validatePriorityNode(this.priorityNode_);
|
5592 | }
|
5593 | if (this.children_.isEmpty()) {
|
5594 | util.assert(!this.priorityNode_ || this.priorityNode_.isEmpty(), 'An empty node cannot have a priority');
|
5595 | }
|
5596 | }
|
5597 | Object.defineProperty(ChildrenNode, "EMPTY_NODE", {
|
5598 | get: function () {
|
5599 | return (EMPTY_NODE ||
|
5600 | (EMPTY_NODE = new ChildrenNode(new SortedMap(NAME_COMPARATOR), null, IndexMap.Default)));
|
5601 | },
|
5602 | enumerable: false,
|
5603 | configurable: true
|
5604 | });
|
5605 |
|
5606 | ChildrenNode.prototype.isLeafNode = function () {
|
5607 | return false;
|
5608 | };
|
5609 |
|
5610 | ChildrenNode.prototype.getPriority = function () {
|
5611 | return this.priorityNode_ || EMPTY_NODE;
|
5612 | };
|
5613 |
|
5614 | ChildrenNode.prototype.updatePriority = function (newPriorityNode) {
|
5615 | if (this.children_.isEmpty()) {
|
5616 |
|
5617 | return this;
|
5618 | }
|
5619 | else {
|
5620 | return new ChildrenNode(this.children_, newPriorityNode, this.indexMap_);
|
5621 | }
|
5622 | };
|
5623 |
|
5624 | ChildrenNode.prototype.getImmediateChild = function (childName) {
|
5625 |
|
5626 | if (childName === '.priority') {
|
5627 | return this.getPriority();
|
5628 | }
|
5629 | else {
|
5630 | var child = this.children_.get(childName);
|
5631 | return child === null ? EMPTY_NODE : child;
|
5632 | }
|
5633 | };
|
5634 |
|
5635 | ChildrenNode.prototype.getChild = function (path) {
|
5636 | var front = pathGetFront(path);
|
5637 | if (front === null) {
|
5638 | return this;
|
5639 | }
|
5640 | return this.getImmediateChild(front).getChild(pathPopFront(path));
|
5641 | };
|
5642 |
|
5643 | ChildrenNode.prototype.hasChild = function (childName) {
|
5644 | return this.children_.get(childName) !== null;
|
5645 | };
|
5646 |
|
5647 | ChildrenNode.prototype.updateImmediateChild = function (childName, newChildNode) {
|
5648 | util.assert(newChildNode, 'We should always be passing snapshot nodes');
|
5649 | if (childName === '.priority') {
|
5650 | return this.updatePriority(newChildNode);
|
5651 | }
|
5652 | else {
|
5653 | var namedNode = new NamedNode(childName, newChildNode);
|
5654 | var newChildren = void 0, newIndexMap = void 0;
|
5655 | if (newChildNode.isEmpty()) {
|
5656 | newChildren = this.children_.remove(childName);
|
5657 | newIndexMap = this.indexMap_.removeFromIndexes(namedNode, this.children_);
|
5658 | }
|
5659 | else {
|
5660 | newChildren = this.children_.insert(childName, newChildNode);
|
5661 | newIndexMap = this.indexMap_.addToIndexes(namedNode, this.children_);
|
5662 | }
|
5663 | var newPriority = newChildren.isEmpty()
|
5664 | ? EMPTY_NODE
|
5665 | : this.priorityNode_;
|
5666 | return new ChildrenNode(newChildren, newPriority, newIndexMap);
|
5667 | }
|
5668 | };
|
5669 |
|
5670 | ChildrenNode.prototype.updateChild = function (path, newChildNode) {
|
5671 | var front = pathGetFront(path);
|
5672 | if (front === null) {
|
5673 | return newChildNode;
|
5674 | }
|
5675 | else {
|
5676 | util.assert(pathGetFront(path) !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
|
5677 | var newImmediateChild = this.getImmediateChild(front).updateChild(pathPopFront(path), newChildNode);
|
5678 | return this.updateImmediateChild(front, newImmediateChild);
|
5679 | }
|
5680 | };
|
5681 |
|
5682 | ChildrenNode.prototype.isEmpty = function () {
|
5683 | return this.children_.isEmpty();
|
5684 | };
|
5685 |
|
5686 | ChildrenNode.prototype.numChildren = function () {
|
5687 | return this.children_.count();
|
5688 | };
|
5689 |
|
5690 | ChildrenNode.prototype.val = function (exportFormat) {
|
5691 | if (this.isEmpty()) {
|
5692 | return null;
|
5693 | }
|
5694 | var obj = {};
|
5695 | var numKeys = 0, maxKey = 0, allIntegerKeys = true;
|
5696 | this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
5697 | obj[key] = childNode.val(exportFormat);
|
5698 | numKeys++;
|
5699 | if (allIntegerKeys && ChildrenNode.INTEGER_REGEXP_.test(key)) {
|
5700 | maxKey = Math.max(maxKey, Number(key));
|
5701 | }
|
5702 | else {
|
5703 | allIntegerKeys = false;
|
5704 | }
|
5705 | });
|
5706 | if (!exportFormat && allIntegerKeys && maxKey < 2 * numKeys) {
|
5707 |
|
5708 | var array = [];
|
5709 |
|
5710 | for (var key in obj) {
|
5711 | array[key] = obj[key];
|
5712 | }
|
5713 | return array;
|
5714 | }
|
5715 | else {
|
5716 | if (exportFormat && !this.getPriority().isEmpty()) {
|
5717 | obj['.priority'] = this.getPriority().val();
|
5718 | }
|
5719 | return obj;
|
5720 | }
|
5721 | };
|
5722 |
|
5723 | ChildrenNode.prototype.hash = function () {
|
5724 | if (this.lazyHash_ === null) {
|
5725 | var toHash_1 = '';
|
5726 | if (!this.getPriority().isEmpty()) {
|
5727 | toHash_1 +=
|
5728 | 'priority:' +
|
5729 | priorityHashText(this.getPriority().val()) +
|
5730 | ':';
|
5731 | }
|
5732 | this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
5733 | var childHash = childNode.hash();
|
5734 | if (childHash !== '') {
|
5735 | toHash_1 += ':' + key + ':' + childHash;
|
5736 | }
|
5737 | });
|
5738 | this.lazyHash_ = toHash_1 === '' ? '' : sha1(toHash_1);
|
5739 | }
|
5740 | return this.lazyHash_;
|
5741 | };
|
5742 |
|
5743 | ChildrenNode.prototype.getPredecessorChildName = function (childName, childNode, index) {
|
5744 | var idx = this.resolveIndex_(index);
|
5745 | if (idx) {
|
5746 | var predecessor = idx.getPredecessorKey(new NamedNode(childName, childNode));
|
5747 | return predecessor ? predecessor.name : null;
|
5748 | }
|
5749 | else {
|
5750 | return this.children_.getPredecessorKey(childName);
|
5751 | }
|
5752 | };
|
5753 | ChildrenNode.prototype.getFirstChildName = function (indexDefinition) {
|
5754 | var idx = this.resolveIndex_(indexDefinition);
|
5755 | if (idx) {
|
5756 | var minKey = idx.minKey();
|
5757 | return minKey && minKey.name;
|
5758 | }
|
5759 | else {
|
5760 | return this.children_.minKey();
|
5761 | }
|
5762 | };
|
5763 | ChildrenNode.prototype.getFirstChild = function (indexDefinition) {
|
5764 | var minKey = this.getFirstChildName(indexDefinition);
|
5765 | if (minKey) {
|
5766 | return new NamedNode(minKey, this.children_.get(minKey));
|
5767 | }
|
5768 | else {
|
5769 | return null;
|
5770 | }
|
5771 | };
|
5772 | |
5773 |
|
5774 |
|
5775 | ChildrenNode.prototype.getLastChildName = function (indexDefinition) {
|
5776 | var idx = this.resolveIndex_(indexDefinition);
|
5777 | if (idx) {
|
5778 | var maxKey = idx.maxKey();
|
5779 | return maxKey && maxKey.name;
|
5780 | }
|
5781 | else {
|
5782 | return this.children_.maxKey();
|
5783 | }
|
5784 | };
|
5785 | ChildrenNode.prototype.getLastChild = function (indexDefinition) {
|
5786 | var maxKey = this.getLastChildName(indexDefinition);
|
5787 | if (maxKey) {
|
5788 | return new NamedNode(maxKey, this.children_.get(maxKey));
|
5789 | }
|
5790 | else {
|
5791 | return null;
|
5792 | }
|
5793 | };
|
5794 | ChildrenNode.prototype.forEachChild = function (index, action) {
|
5795 | var idx = this.resolveIndex_(index);
|
5796 | if (idx) {
|
5797 | return idx.inorderTraversal(function (wrappedNode) {
|
5798 | return action(wrappedNode.name, wrappedNode.node);
|
5799 | });
|
5800 | }
|
5801 | else {
|
5802 | return this.children_.inorderTraversal(action);
|
5803 | }
|
5804 | };
|
5805 | ChildrenNode.prototype.getIterator = function (indexDefinition) {
|
5806 | return this.getIteratorFrom(indexDefinition.minPost(), indexDefinition);
|
5807 | };
|
5808 | ChildrenNode.prototype.getIteratorFrom = function (startPost, indexDefinition) {
|
5809 | var idx = this.resolveIndex_(indexDefinition);
|
5810 | if (idx) {
|
5811 | return idx.getIteratorFrom(startPost, function (key) { return key; });
|
5812 | }
|
5813 | else {
|
5814 | var iterator = this.children_.getIteratorFrom(startPost.name, NamedNode.Wrap);
|
5815 | var next = iterator.peek();
|
5816 | while (next != null && indexDefinition.compare(next, startPost) < 0) {
|
5817 | iterator.getNext();
|
5818 | next = iterator.peek();
|
5819 | }
|
5820 | return iterator;
|
5821 | }
|
5822 | };
|
5823 | ChildrenNode.prototype.getReverseIterator = function (indexDefinition) {
|
5824 | return this.getReverseIteratorFrom(indexDefinition.maxPost(), indexDefinition);
|
5825 | };
|
5826 | ChildrenNode.prototype.getReverseIteratorFrom = function (endPost, indexDefinition) {
|
5827 | var idx = this.resolveIndex_(indexDefinition);
|
5828 | if (idx) {
|
5829 | return idx.getReverseIteratorFrom(endPost, function (key) {
|
5830 | return key;
|
5831 | });
|
5832 | }
|
5833 | else {
|
5834 | var iterator = this.children_.getReverseIteratorFrom(endPost.name, NamedNode.Wrap);
|
5835 | var next = iterator.peek();
|
5836 | while (next != null && indexDefinition.compare(next, endPost) > 0) {
|
5837 | iterator.getNext();
|
5838 | next = iterator.peek();
|
5839 | }
|
5840 | return iterator;
|
5841 | }
|
5842 | };
|
5843 | ChildrenNode.prototype.compareTo = function (other) {
|
5844 | if (this.isEmpty()) {
|
5845 | if (other.isEmpty()) {
|
5846 | return 0;
|
5847 | }
|
5848 | else {
|
5849 | return -1;
|
5850 | }
|
5851 | }
|
5852 | else if (other.isLeafNode() || other.isEmpty()) {
|
5853 | return 1;
|
5854 | }
|
5855 | else if (other === MAX_NODE$2) {
|
5856 | return -1;
|
5857 | }
|
5858 | else {
|
5859 |
|
5860 | return 0;
|
5861 | }
|
5862 | };
|
5863 | ChildrenNode.prototype.withIndex = function (indexDefinition) {
|
5864 | if (indexDefinition === KEY_INDEX ||
|
5865 | this.indexMap_.hasIndex(indexDefinition)) {
|
5866 | return this;
|
5867 | }
|
5868 | else {
|
5869 | var newIndexMap = this.indexMap_.addIndex(indexDefinition, this.children_);
|
5870 | return new ChildrenNode(this.children_, this.priorityNode_, newIndexMap);
|
5871 | }
|
5872 | };
|
5873 | ChildrenNode.prototype.isIndexed = function (index) {
|
5874 | return index === KEY_INDEX || this.indexMap_.hasIndex(index);
|
5875 | };
|
5876 | ChildrenNode.prototype.equals = function (other) {
|
5877 | if (other === this) {
|
5878 | return true;
|
5879 | }
|
5880 | else if (other.isLeafNode()) {
|
5881 | return false;
|
5882 | }
|
5883 | else {
|
5884 | var otherChildrenNode = other;
|
5885 | if (!this.getPriority().equals(otherChildrenNode.getPriority())) {
|
5886 | return false;
|
5887 | }
|
5888 | else if (this.children_.count() === otherChildrenNode.children_.count()) {
|
5889 | var thisIter = this.getIterator(PRIORITY_INDEX);
|
5890 | var otherIter = otherChildrenNode.getIterator(PRIORITY_INDEX);
|
5891 | var thisCurrent = thisIter.getNext();
|
5892 | var otherCurrent = otherIter.getNext();
|
5893 | while (thisCurrent && otherCurrent) {
|
5894 | if (thisCurrent.name !== otherCurrent.name ||
|
5895 | !thisCurrent.node.equals(otherCurrent.node)) {
|
5896 | return false;
|
5897 | }
|
5898 | thisCurrent = thisIter.getNext();
|
5899 | otherCurrent = otherIter.getNext();
|
5900 | }
|
5901 | return thisCurrent === null && otherCurrent === null;
|
5902 | }
|
5903 | else {
|
5904 | return false;
|
5905 | }
|
5906 | }
|
5907 | };
|
5908 | |
5909 |
|
5910 |
|
5911 |
|
5912 |
|
5913 | ChildrenNode.prototype.resolveIndex_ = function (indexDefinition) {
|
5914 | if (indexDefinition === KEY_INDEX) {
|
5915 | return null;
|
5916 | }
|
5917 | else {
|
5918 | return this.indexMap_.get(indexDefinition.toString());
|
5919 | }
|
5920 | };
|
5921 | ChildrenNode.INTEGER_REGEXP_ = /^(0|[1-9]\d*)$/;
|
5922 | return ChildrenNode;
|
5923 | }());
|
5924 | var MaxNode = (function (_super) {
|
5925 | tslib.__extends(MaxNode, _super);
|
5926 | function MaxNode() {
|
5927 | return _super.call(this, new SortedMap(NAME_COMPARATOR), ChildrenNode.EMPTY_NODE, IndexMap.Default) || this;
|
5928 | }
|
5929 | MaxNode.prototype.compareTo = function (other) {
|
5930 | if (other === this) {
|
5931 | return 0;
|
5932 | }
|
5933 | else {
|
5934 | return 1;
|
5935 | }
|
5936 | };
|
5937 | MaxNode.prototype.equals = function (other) {
|
5938 |
|
5939 | return other === this;
|
5940 | };
|
5941 | MaxNode.prototype.getPriority = function () {
|
5942 | return this;
|
5943 | };
|
5944 | MaxNode.prototype.getImmediateChild = function (childName) {
|
5945 | return ChildrenNode.EMPTY_NODE;
|
5946 | };
|
5947 | MaxNode.prototype.isEmpty = function () {
|
5948 | return false;
|
5949 | };
|
5950 | return MaxNode;
|
5951 | }(ChildrenNode));
|
5952 |
|
5953 |
|
5954 |
|
5955 | var MAX_NODE$2 = new MaxNode();
|
5956 | Object.defineProperties(NamedNode, {
|
5957 | MIN: {
|
5958 | value: new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE)
|
5959 | },
|
5960 | MAX: {
|
5961 | value: new NamedNode(MAX_NAME, MAX_NODE$2)
|
5962 | }
|
5963 | });
|
5964 |
|
5965 |
|
5966 |
|
5967 | KeyIndex.__EMPTY_NODE = ChildrenNode.EMPTY_NODE;
|
5968 | LeafNode.__childrenNodeConstructor = ChildrenNode;
|
5969 | setMaxNode(MAX_NODE$2);
|
5970 | setMaxNode$1(MAX_NODE$2);
|
5971 |
|
5972 |
|
5973 |
|
5974 |
|
5975 |
|
5976 |
|
5977 |
|
5978 |
|
5979 |
|
5980 |
|
5981 |
|
5982 |
|
5983 |
|
5984 |
|
5985 |
|
5986 |
|
5987 |
|
5988 | var USE_HINZE = true;
|
5989 |
|
5990 |
|
5991 |
|
5992 |
|
5993 |
|
5994 |
|
5995 | function nodeFromJSON$1(json, priority) {
|
5996 | if (priority === void 0) { priority = null; }
|
5997 | if (json === null) {
|
5998 | return ChildrenNode.EMPTY_NODE;
|
5999 | }
|
6000 | if (typeof json === 'object' && '.priority' in json) {
|
6001 | priority = json['.priority'];
|
6002 | }
|
6003 | util.assert(priority === null ||
|
6004 | typeof priority === 'string' ||
|
6005 | typeof priority === 'number' ||
|
6006 | (typeof priority === 'object' && '.sv' in priority), 'Invalid priority type found: ' + typeof priority);
|
6007 | if (typeof json === 'object' && '.value' in json && json['.value'] !== null) {
|
6008 | json = json['.value'];
|
6009 | }
|
6010 |
|
6011 | if (typeof json !== 'object' || '.sv' in json) {
|
6012 | var jsonLeaf = json;
|
6013 | return new LeafNode(jsonLeaf, nodeFromJSON$1(priority));
|
6014 | }
|
6015 | if (!(json instanceof Array) && USE_HINZE) {
|
6016 | var children_1 = [];
|
6017 | var childrenHavePriority_1 = false;
|
6018 | var hinzeJsonObj = json;
|
6019 | each(hinzeJsonObj, function (key, child) {
|
6020 | if (key.substring(0, 1) !== '.') {
|
6021 |
|
6022 | var childNode = nodeFromJSON$1(child);
|
6023 | if (!childNode.isEmpty()) {
|
6024 | childrenHavePriority_1 =
|
6025 | childrenHavePriority_1 || !childNode.getPriority().isEmpty();
|
6026 | children_1.push(new NamedNode(key, childNode));
|
6027 | }
|
6028 | }
|
6029 | });
|
6030 | if (children_1.length === 0) {
|
6031 | return ChildrenNode.EMPTY_NODE;
|
6032 | }
|
6033 | var childSet = buildChildSet(children_1, NAME_ONLY_COMPARATOR, function (namedNode) { return namedNode.name; }, NAME_COMPARATOR);
|
6034 | if (childrenHavePriority_1) {
|
6035 | var sortedChildSet = buildChildSet(children_1, PRIORITY_INDEX.getCompare());
|
6036 | return new ChildrenNode(childSet, nodeFromJSON$1(priority), new IndexMap({ '.priority': sortedChildSet }, { '.priority': PRIORITY_INDEX }));
|
6037 | }
|
6038 | else {
|
6039 | return new ChildrenNode(childSet, nodeFromJSON$1(priority), IndexMap.Default);
|
6040 | }
|
6041 | }
|
6042 | else {
|
6043 | var node_1 = ChildrenNode.EMPTY_NODE;
|
6044 | each(json, function (key, childData) {
|
6045 | if (util.contains(json, key)) {
|
6046 | if (key.substring(0, 1) !== '.') {
|
6047 |
|
6048 | var childNode = nodeFromJSON$1(childData);
|
6049 | if (childNode.isLeafNode() || !childNode.isEmpty()) {
|
6050 | node_1 = node_1.updateImmediateChild(key, childNode);
|
6051 | }
|
6052 | }
|
6053 | }
|
6054 | });
|
6055 | return node_1.updatePriority(nodeFromJSON$1(priority));
|
6056 | }
|
6057 | }
|
6058 | setNodeFromJSON(nodeFromJSON$1);
|
6059 |
|
6060 |
|
6061 |
|
6062 |
|
6063 |
|
6064 |
|
6065 |
|
6066 |
|
6067 |
|
6068 |
|
6069 |
|
6070 |
|
6071 |
|
6072 |
|
6073 |
|
6074 |
|
6075 |
|
6076 | var PathIndex = (function (_super) {
|
6077 | tslib.__extends(PathIndex, _super);
|
6078 | function PathIndex(indexPath_) {
|
6079 | var _this = _super.call(this) || this;
|
6080 | _this.indexPath_ = indexPath_;
|
6081 | util.assert(!pathIsEmpty(indexPath_) && pathGetFront(indexPath_) !== '.priority', "Can't create PathIndex with empty path or .priority key");
|
6082 | return _this;
|
6083 | }
|
6084 | PathIndex.prototype.extractChild = function (snap) {
|
6085 | return snap.getChild(this.indexPath_);
|
6086 | };
|
6087 | PathIndex.prototype.isDefinedOn = function (node) {
|
6088 | return !node.getChild(this.indexPath_).isEmpty();
|
6089 | };
|
6090 | PathIndex.prototype.compare = function (a, b) {
|
6091 | var aChild = this.extractChild(a.node);
|
6092 | var bChild = this.extractChild(b.node);
|
6093 | var indexCmp = aChild.compareTo(bChild);
|
6094 | if (indexCmp === 0) {
|
6095 | return nameCompare(a.name, b.name);
|
6096 | }
|
6097 | else {
|
6098 | return indexCmp;
|
6099 | }
|
6100 | };
|
6101 | PathIndex.prototype.makePost = function (indexValue, name) {
|
6102 | var valueNode = nodeFromJSON$1(indexValue);
|
6103 | var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, valueNode);
|
6104 | return new NamedNode(name, node);
|
6105 | };
|
6106 | PathIndex.prototype.maxPost = function () {
|
6107 | var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, MAX_NODE$2);
|
6108 | return new NamedNode(MAX_NAME, node);
|
6109 | };
|
6110 | PathIndex.prototype.toString = function () {
|
6111 | return pathSlice(this.indexPath_, 0).join('/');
|
6112 | };
|
6113 | return PathIndex;
|
6114 | }(Index));
|
6115 |
|
6116 |
|
6117 |
|
6118 |
|
6119 |
|
6120 |
|
6121 |
|
6122 |
|
6123 |
|
6124 |
|
6125 |
|
6126 |
|
6127 |
|
6128 |
|
6129 |
|
6130 |
|
6131 |
|
6132 | var ValueIndex = (function (_super) {
|
6133 | tslib.__extends(ValueIndex, _super);
|
6134 | function ValueIndex() {
|
6135 | return _super !== null && _super.apply(this, arguments) || this;
|
6136 | }
|
6137 | ValueIndex.prototype.compare = function (a, b) {
|
6138 | var indexCmp = a.node.compareTo(b.node);
|
6139 | if (indexCmp === 0) {
|
6140 | return nameCompare(a.name, b.name);
|
6141 | }
|
6142 | else {
|
6143 | return indexCmp;
|
6144 | }
|
6145 | };
|
6146 | ValueIndex.prototype.isDefinedOn = function (node) {
|
6147 | return true;
|
6148 | };
|
6149 | ValueIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
|
6150 | return !oldNode.equals(newNode);
|
6151 | };
|
6152 | ValueIndex.prototype.minPost = function () {
|
6153 |
|
6154 | return NamedNode.MIN;
|
6155 | };
|
6156 | ValueIndex.prototype.maxPost = function () {
|
6157 |
|
6158 | return NamedNode.MAX;
|
6159 | };
|
6160 | ValueIndex.prototype.makePost = function (indexValue, name) {
|
6161 | var valueNode = nodeFromJSON$1(indexValue);
|
6162 | return new NamedNode(name, valueNode);
|
6163 | };
|
6164 | |
6165 |
|
6166 |
|
6167 | ValueIndex.prototype.toString = function () {
|
6168 | return '.value';
|
6169 | };
|
6170 | return ValueIndex;
|
6171 | }(Index));
|
6172 | var VALUE_INDEX = new ValueIndex();
|
6173 |
|
6174 |
|
6175 |
|
6176 |
|
6177 |
|
6178 |
|
6179 |
|
6180 |
|
6181 |
|
6182 |
|
6183 |
|
6184 |
|
6185 |
|
6186 |
|
6187 |
|
6188 |
|
6189 |
|
6190 |
|
6191 | var PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
|
6192 | var MIN_PUSH_CHAR = '-';
|
6193 | var MAX_PUSH_CHAR = 'z';
|
6194 | var MAX_KEY_LEN = 786;
|
6195 |
|
6196 |
|
6197 |
|
6198 |
|
6199 |
|
6200 |
|
6201 |
|
6202 |
|
6203 |
|
6204 |
|
6205 |
|
6206 |
|
6207 |
|
6208 |
|
6209 | var nextPushId = (function () {
|
6210 |
|
6211 |
|
6212 | var lastPushTime = 0;
|
6213 |
|
6214 |
|
6215 |
|
6216 |
|
6217 | var lastRandChars = [];
|
6218 | return function (now) {
|
6219 | var duplicateTime = now === lastPushTime;
|
6220 | lastPushTime = now;
|
6221 | var i;
|
6222 | var timeStampChars = new Array(8);
|
6223 | for (i = 7; i >= 0; i--) {
|
6224 | timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
|
6225 |
|
6226 |
|
6227 | now = Math.floor(now / 64);
|
6228 | }
|
6229 | util.assert(now === 0, 'Cannot push at time == 0');
|
6230 | var id = timeStampChars.join('');
|
6231 | if (!duplicateTime) {
|
6232 | for (i = 0; i < 12; i++) {
|
6233 | lastRandChars[i] = Math.floor(Math.random() * 64);
|
6234 | }
|
6235 | }
|
6236 | else {
|
6237 |
|
6238 |
|
6239 | for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
|
6240 | lastRandChars[i] = 0;
|
6241 | }
|
6242 | lastRandChars[i]++;
|
6243 | }
|
6244 | for (i = 0; i < 12; i++) {
|
6245 | id += PUSH_CHARS.charAt(lastRandChars[i]);
|
6246 | }
|
6247 | util.assert(id.length === 20, 'nextPushId: Length should be 20.');
|
6248 | return id;
|
6249 | };
|
6250 | })();
|
6251 | var successor = function (key) {
|
6252 | if (key === '' + INTEGER_32_MAX) {
|
6253 |
|
6254 | return MIN_PUSH_CHAR;
|
6255 | }
|
6256 | var keyAsInt = tryParseInt(key);
|
6257 | if (keyAsInt != null) {
|
6258 | return '' + (keyAsInt + 1);
|
6259 | }
|
6260 | var next = new Array(key.length);
|
6261 | for (var i_1 = 0; i_1 < next.length; i_1++) {
|
6262 | next[i_1] = key.charAt(i_1);
|
6263 | }
|
6264 | if (next.length < MAX_KEY_LEN) {
|
6265 | next.push(MIN_PUSH_CHAR);
|
6266 | return next.join('');
|
6267 | }
|
6268 | var i = next.length - 1;
|
6269 | while (i >= 0 && next[i] === MAX_PUSH_CHAR) {
|
6270 | i--;
|
6271 | }
|
6272 |
|
6273 |
|
6274 | if (i === -1) {
|
6275 | return MAX_NAME;
|
6276 | }
|
6277 | var source = next[i];
|
6278 | var sourcePlusOne = PUSH_CHARS.charAt(PUSH_CHARS.indexOf(source) + 1);
|
6279 | next[i] = sourcePlusOne;
|
6280 | return next.slice(0, i + 1).join('');
|
6281 | };
|
6282 |
|
6283 | var predecessor = function (key) {
|
6284 | if (key === '' + INTEGER_32_MIN) {
|
6285 | return MIN_NAME;
|
6286 | }
|
6287 | var keyAsInt = tryParseInt(key);
|
6288 | if (keyAsInt != null) {
|
6289 | return '' + (keyAsInt - 1);
|
6290 | }
|
6291 | var next = new Array(key.length);
|
6292 | for (var i = 0; i < next.length; i++) {
|
6293 | next[i] = key.charAt(i);
|
6294 | }
|
6295 |
|
6296 |
|
6297 |
|
6298 |
|
6299 |
|
6300 |
|
6301 |
|
6302 |
|
6303 |
|
6304 |
|
6305 |
|
6306 | if (next[next.length - 1] === MIN_PUSH_CHAR) {
|
6307 | if (next.length === 1) {
|
6308 |
|
6309 | return '' + INTEGER_32_MAX;
|
6310 | }
|
6311 | delete next[next.length - 1];
|
6312 | return next.join('');
|
6313 | }
|
6314 |
|
6315 |
|
6316 |
|
6317 | next[next.length - 1] = PUSH_CHARS.charAt(PUSH_CHARS.indexOf(next[next.length - 1]) - 1);
|
6318 | return next.join('') + MAX_PUSH_CHAR.repeat(MAX_KEY_LEN - next.length);
|
6319 | };
|
6320 |
|
6321 |
|
6322 |
|
6323 |
|
6324 |
|
6325 |
|
6326 |
|
6327 |
|
6328 |
|
6329 |
|
6330 |
|
6331 |
|
6332 |
|
6333 |
|
6334 |
|
6335 |
|
6336 |
|
6337 | function changeValue(snapshotNode) {
|
6338 | return { type: "value" , snapshotNode: snapshotNode };
|
6339 | }
|
6340 | function changeChildAdded(childName, snapshotNode) {
|
6341 | return { type: "child_added" , snapshotNode: snapshotNode, childName: childName };
|
6342 | }
|
6343 | function changeChildRemoved(childName, snapshotNode) {
|
6344 | return { type: "child_removed" , snapshotNode: snapshotNode, childName: childName };
|
6345 | }
|
6346 | function changeChildChanged(childName, snapshotNode, oldSnap) {
|
6347 | return {
|
6348 | type: "child_changed" ,
|
6349 | snapshotNode: snapshotNode,
|
6350 | childName: childName,
|
6351 | oldSnap: oldSnap
|
6352 | };
|
6353 | }
|
6354 | function changeChildMoved(childName, snapshotNode) {
|
6355 | return { type: "child_moved" , snapshotNode: snapshotNode, childName: childName };
|
6356 | }
|
6357 |
|
6358 |
|
6359 |
|
6360 |
|
6361 |
|
6362 |
|
6363 |
|
6364 |
|
6365 |
|
6366 |
|
6367 |
|
6368 |
|
6369 |
|
6370 |
|
6371 |
|
6372 |
|
6373 |
|
6374 |
|
6375 |
|
6376 |
|
6377 | var IndexedFilter = (function () {
|
6378 | function IndexedFilter(index_) {
|
6379 | this.index_ = index_;
|
6380 | }
|
6381 | IndexedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
|
6382 | util.assert(snap.isIndexed(this.index_), 'A node must be indexed if only a child is updated');
|
6383 | var oldChild = snap.getImmediateChild(key);
|
6384 |
|
6385 | if (oldChild.getChild(affectedPath).equals(newChild.getChild(affectedPath))) {
|
6386 |
|
6387 |
|
6388 |
|
6389 | if (oldChild.isEmpty() === newChild.isEmpty()) {
|
6390 |
|
6391 |
|
6392 |
|
6393 | return snap;
|
6394 | }
|
6395 | }
|
6396 | if (optChangeAccumulator != null) {
|
6397 | if (newChild.isEmpty()) {
|
6398 | if (snap.hasChild(key)) {
|
6399 | optChangeAccumulator.trackChildChange(changeChildRemoved(key, oldChild));
|
6400 | }
|
6401 | else {
|
6402 | util.assert(snap.isLeafNode(), 'A child remove without an old child only makes sense on a leaf node');
|
6403 | }
|
6404 | }
|
6405 | else if (oldChild.isEmpty()) {
|
6406 | optChangeAccumulator.trackChildChange(changeChildAdded(key, newChild));
|
6407 | }
|
6408 | else {
|
6409 | optChangeAccumulator.trackChildChange(changeChildChanged(key, newChild, oldChild));
|
6410 | }
|
6411 | }
|
6412 | if (snap.isLeafNode() && newChild.isEmpty()) {
|
6413 | return snap;
|
6414 | }
|
6415 | else {
|
6416 |
|
6417 | return snap.updateImmediateChild(key, newChild).withIndex(this.index_);
|
6418 | }
|
6419 | };
|
6420 | IndexedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
|
6421 | if (optChangeAccumulator != null) {
|
6422 | if (!oldSnap.isLeafNode()) {
|
6423 | oldSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
6424 | if (!newSnap.hasChild(key)) {
|
6425 | optChangeAccumulator.trackChildChange(changeChildRemoved(key, childNode));
|
6426 | }
|
6427 | });
|
6428 | }
|
6429 | if (!newSnap.isLeafNode()) {
|
6430 | newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
6431 | if (oldSnap.hasChild(key)) {
|
6432 | var oldChild = oldSnap.getImmediateChild(key);
|
6433 | if (!oldChild.equals(childNode)) {
|
6434 | optChangeAccumulator.trackChildChange(changeChildChanged(key, childNode, oldChild));
|
6435 | }
|
6436 | }
|
6437 | else {
|
6438 | optChangeAccumulator.trackChildChange(changeChildAdded(key, childNode));
|
6439 | }
|
6440 | });
|
6441 | }
|
6442 | }
|
6443 | return newSnap.withIndex(this.index_);
|
6444 | };
|
6445 | IndexedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
|
6446 | if (oldSnap.isEmpty()) {
|
6447 | return ChildrenNode.EMPTY_NODE;
|
6448 | }
|
6449 | else {
|
6450 | return oldSnap.updatePriority(newPriority);
|
6451 | }
|
6452 | };
|
6453 | IndexedFilter.prototype.filtersNodes = function () {
|
6454 | return false;
|
6455 | };
|
6456 | IndexedFilter.prototype.getIndexedFilter = function () {
|
6457 | return this;
|
6458 | };
|
6459 | IndexedFilter.prototype.getIndex = function () {
|
6460 | return this.index_;
|
6461 | };
|
6462 | return IndexedFilter;
|
6463 | }());
|
6464 |
|
6465 |
|
6466 |
|
6467 |
|
6468 |
|
6469 |
|
6470 |
|
6471 |
|
6472 |
|
6473 |
|
6474 |
|
6475 |
|
6476 |
|
6477 |
|
6478 |
|
6479 |
|
6480 |
|
6481 |
|
6482 |
|
6483 |
|
6484 | var RangedFilter = (function () {
|
6485 | function RangedFilter(params) {
|
6486 | this.indexedFilter_ = new IndexedFilter(params.getIndex());
|
6487 | this.index_ = params.getIndex();
|
6488 | this.startPost_ = RangedFilter.getStartPost_(params);
|
6489 | this.endPost_ = RangedFilter.getEndPost_(params);
|
6490 | }
|
6491 | RangedFilter.prototype.getStartPost = function () {
|
6492 | return this.startPost_;
|
6493 | };
|
6494 | RangedFilter.prototype.getEndPost = function () {
|
6495 | return this.endPost_;
|
6496 | };
|
6497 | RangedFilter.prototype.matches = function (node) {
|
6498 | return (this.index_.compare(this.getStartPost(), node) <= 0 &&
|
6499 | this.index_.compare(node, this.getEndPost()) <= 0);
|
6500 | };
|
6501 | RangedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
|
6502 | if (!this.matches(new NamedNode(key, newChild))) {
|
6503 | newChild = ChildrenNode.EMPTY_NODE;
|
6504 | }
|
6505 | return this.indexedFilter_.updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
|
6506 | };
|
6507 | RangedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
|
6508 | if (newSnap.isLeafNode()) {
|
6509 |
|
6510 | newSnap = ChildrenNode.EMPTY_NODE;
|
6511 | }
|
6512 | var filtered = newSnap.withIndex(this.index_);
|
6513 |
|
6514 | filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
|
6515 | var self = this;
|
6516 | newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
6517 | if (!self.matches(new NamedNode(key, childNode))) {
|
6518 | filtered = filtered.updateImmediateChild(key, ChildrenNode.EMPTY_NODE);
|
6519 | }
|
6520 | });
|
6521 | return this.indexedFilter_.updateFullNode(oldSnap, filtered, optChangeAccumulator);
|
6522 | };
|
6523 | RangedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
|
6524 |
|
6525 | return oldSnap;
|
6526 | };
|
6527 | RangedFilter.prototype.filtersNodes = function () {
|
6528 | return true;
|
6529 | };
|
6530 | RangedFilter.prototype.getIndexedFilter = function () {
|
6531 | return this.indexedFilter_;
|
6532 | };
|
6533 | RangedFilter.prototype.getIndex = function () {
|
6534 | return this.index_;
|
6535 | };
|
6536 | RangedFilter.getStartPost_ = function (params) {
|
6537 | if (params.hasStart()) {
|
6538 | var startName = params.getIndexStartName();
|
6539 | return params.getIndex().makePost(params.getIndexStartValue(), startName);
|
6540 | }
|
6541 | else {
|
6542 | return params.getIndex().minPost();
|
6543 | }
|
6544 | };
|
6545 | RangedFilter.getEndPost_ = function (params) {
|
6546 | if (params.hasEnd()) {
|
6547 | var endName = params.getIndexEndName();
|
6548 | return params.getIndex().makePost(params.getIndexEndValue(), endName);
|
6549 | }
|
6550 | else {
|
6551 | return params.getIndex().maxPost();
|
6552 | }
|
6553 | };
|
6554 | return RangedFilter;
|
6555 | }());
|
6556 |
|
6557 |
|
6558 |
|
6559 |
|
6560 |
|
6561 |
|
6562 |
|
6563 |
|
6564 |
|
6565 |
|
6566 |
|
6567 |
|
6568 |
|
6569 |
|
6570 |
|
6571 |
|
6572 |
|
6573 |
|
6574 |
|
6575 |
|
6576 | var LimitedFilter = (function () {
|
6577 | function LimitedFilter(params) {
|
6578 | this.rangedFilter_ = new RangedFilter(params);
|
6579 | this.index_ = params.getIndex();
|
6580 | this.limit_ = params.getLimit();
|
6581 | this.reverse_ = !params.isViewFromLeft();
|
6582 | }
|
6583 | LimitedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
|
6584 | if (!this.rangedFilter_.matches(new NamedNode(key, newChild))) {
|
6585 | newChild = ChildrenNode.EMPTY_NODE;
|
6586 | }
|
6587 | if (snap.getImmediateChild(key).equals(newChild)) {
|
6588 |
|
6589 | return snap;
|
6590 | }
|
6591 | else if (snap.numChildren() < this.limit_) {
|
6592 | return this.rangedFilter_
|
6593 | .getIndexedFilter()
|
6594 | .updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
|
6595 | }
|
6596 | else {
|
6597 | return this.fullLimitUpdateChild_(snap, key, newChild, source, optChangeAccumulator);
|
6598 | }
|
6599 | };
|
6600 | LimitedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
|
6601 | var filtered;
|
6602 | if (newSnap.isLeafNode() || newSnap.isEmpty()) {
|
6603 |
|
6604 | filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
|
6605 | }
|
6606 | else {
|
6607 | if (this.limit_ * 2 < newSnap.numChildren() &&
|
6608 | newSnap.isIndexed(this.index_)) {
|
6609 |
|
6610 | filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
|
6611 |
|
6612 | var iterator = void 0;
|
6613 | if (this.reverse_) {
|
6614 | iterator = newSnap.getReverseIteratorFrom(this.rangedFilter_.getEndPost(), this.index_);
|
6615 | }
|
6616 | else {
|
6617 | iterator = newSnap.getIteratorFrom(this.rangedFilter_.getStartPost(), this.index_);
|
6618 | }
|
6619 | var count = 0;
|
6620 | while (iterator.hasNext() && count < this.limit_) {
|
6621 | var next = iterator.getNext();
|
6622 | var inRange = void 0;
|
6623 | if (this.reverse_) {
|
6624 | inRange =
|
6625 | this.index_.compare(this.rangedFilter_.getStartPost(), next) <= 0;
|
6626 | }
|
6627 | else {
|
6628 | inRange =
|
6629 | this.index_.compare(next, this.rangedFilter_.getEndPost()) <= 0;
|
6630 | }
|
6631 | if (inRange) {
|
6632 | filtered = filtered.updateImmediateChild(next.name, next.node);
|
6633 | count++;
|
6634 | }
|
6635 | else {
|
6636 |
|
6637 | break;
|
6638 | }
|
6639 | }
|
6640 | }
|
6641 | else {
|
6642 |
|
6643 | filtered = newSnap.withIndex(this.index_);
|
6644 |
|
6645 | filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
|
6646 | var startPost = void 0;
|
6647 | var endPost = void 0;
|
6648 | var cmp = void 0;
|
6649 | var iterator = void 0;
|
6650 | if (this.reverse_) {
|
6651 | iterator = filtered.getReverseIterator(this.index_);
|
6652 | startPost = this.rangedFilter_.getEndPost();
|
6653 | endPost = this.rangedFilter_.getStartPost();
|
6654 | var indexCompare_1 = this.index_.getCompare();
|
6655 | cmp = function (a, b) { return indexCompare_1(b, a); };
|
6656 | }
|
6657 | else {
|
6658 | iterator = filtered.getIterator(this.index_);
|
6659 | startPost = this.rangedFilter_.getStartPost();
|
6660 | endPost = this.rangedFilter_.getEndPost();
|
6661 | cmp = this.index_.getCompare();
|
6662 | }
|
6663 | var count = 0;
|
6664 | var foundStartPost = false;
|
6665 | while (iterator.hasNext()) {
|
6666 | var next = iterator.getNext();
|
6667 | if (!foundStartPost && cmp(startPost, next) <= 0) {
|
6668 |
|
6669 | foundStartPost = true;
|
6670 | }
|
6671 | var inRange = foundStartPost && count < this.limit_ && cmp(next, endPost) <= 0;
|
6672 | if (inRange) {
|
6673 | count++;
|
6674 | }
|
6675 | else {
|
6676 | filtered = filtered.updateImmediateChild(next.name, ChildrenNode.EMPTY_NODE);
|
6677 | }
|
6678 | }
|
6679 | }
|
6680 | }
|
6681 | return this.rangedFilter_
|
6682 | .getIndexedFilter()
|
6683 | .updateFullNode(oldSnap, filtered, optChangeAccumulator);
|
6684 | };
|
6685 | LimitedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
|
6686 |
|
6687 | return oldSnap;
|
6688 | };
|
6689 | LimitedFilter.prototype.filtersNodes = function () {
|
6690 | return true;
|
6691 | };
|
6692 | LimitedFilter.prototype.getIndexedFilter = function () {
|
6693 | return this.rangedFilter_.getIndexedFilter();
|
6694 | };
|
6695 | LimitedFilter.prototype.getIndex = function () {
|
6696 | return this.index_;
|
6697 | };
|
6698 | LimitedFilter.prototype.fullLimitUpdateChild_ = function (snap, childKey, childSnap, source, changeAccumulator) {
|
6699 |
|
6700 | var cmp;
|
6701 | if (this.reverse_) {
|
6702 | var indexCmp_1 = this.index_.getCompare();
|
6703 | cmp = function (a, b) { return indexCmp_1(b, a); };
|
6704 | }
|
6705 | else {
|
6706 | cmp = this.index_.getCompare();
|
6707 | }
|
6708 | var oldEventCache = snap;
|
6709 | util.assert(oldEventCache.numChildren() === this.limit_, '');
|
6710 | var newChildNamedNode = new NamedNode(childKey, childSnap);
|
6711 | var windowBoundary = this.reverse_
|
6712 | ? oldEventCache.getFirstChild(this.index_)
|
6713 | : oldEventCache.getLastChild(this.index_);
|
6714 | var inRange = this.rangedFilter_.matches(newChildNamedNode);
|
6715 | if (oldEventCache.hasChild(childKey)) {
|
6716 | var oldChildSnap = oldEventCache.getImmediateChild(childKey);
|
6717 | var nextChild = source.getChildAfterChild(this.index_, windowBoundary, this.reverse_);
|
6718 | while (nextChild != null &&
|
6719 | (nextChild.name === childKey || oldEventCache.hasChild(nextChild.name))) {
|
6720 |
|
6721 |
|
6722 |
|
6723 | nextChild = source.getChildAfterChild(this.index_, nextChild, this.reverse_);
|
6724 | }
|
6725 | var compareNext = nextChild == null ? 1 : cmp(nextChild, newChildNamedNode);
|
6726 | var remainsInWindow = inRange && !childSnap.isEmpty() && compareNext >= 0;
|
6727 | if (remainsInWindow) {
|
6728 | if (changeAccumulator != null) {
|
6729 | changeAccumulator.trackChildChange(changeChildChanged(childKey, childSnap, oldChildSnap));
|
6730 | }
|
6731 | return oldEventCache.updateImmediateChild(childKey, childSnap);
|
6732 | }
|
6733 | else {
|
6734 | if (changeAccumulator != null) {
|
6735 | changeAccumulator.trackChildChange(changeChildRemoved(childKey, oldChildSnap));
|
6736 | }
|
6737 | var newEventCache = oldEventCache.updateImmediateChild(childKey, ChildrenNode.EMPTY_NODE);
|
6738 | var nextChildInRange = nextChild != null && this.rangedFilter_.matches(nextChild);
|
6739 | if (nextChildInRange) {
|
6740 | if (changeAccumulator != null) {
|
6741 | changeAccumulator.trackChildChange(changeChildAdded(nextChild.name, nextChild.node));
|
6742 | }
|
6743 | return newEventCache.updateImmediateChild(nextChild.name, nextChild.node);
|
6744 | }
|
6745 | else {
|
6746 | return newEventCache;
|
6747 | }
|
6748 | }
|
6749 | }
|
6750 | else if (childSnap.isEmpty()) {
|
6751 |
|
6752 | return snap;
|
6753 | }
|
6754 | else if (inRange) {
|
6755 | if (cmp(windowBoundary, newChildNamedNode) >= 0) {
|
6756 | if (changeAccumulator != null) {
|
6757 | changeAccumulator.trackChildChange(changeChildRemoved(windowBoundary.name, windowBoundary.node));
|
6758 | changeAccumulator.trackChildChange(changeChildAdded(childKey, childSnap));
|
6759 | }
|
6760 | return oldEventCache
|
6761 | .updateImmediateChild(childKey, childSnap)
|
6762 | .updateImmediateChild(windowBoundary.name, ChildrenNode.EMPTY_NODE);
|
6763 | }
|
6764 | else {
|
6765 | return snap;
|
6766 | }
|
6767 | }
|
6768 | else {
|
6769 | return snap;
|
6770 | }
|
6771 | };
|
6772 | return LimitedFilter;
|
6773 | }());
|
6774 |
|
6775 |
|
6776 |
|
6777 |
|
6778 |
|
6779 |
|
6780 |
|
6781 |
|
6782 |
|
6783 |
|
6784 |
|
6785 |
|
6786 |
|
6787 |
|
6788 |
|
6789 |
|
6790 |
|
6791 |
|
6792 |
|
6793 |
|
6794 |
|
6795 |
|
6796 | var QueryParams = (function () {
|
6797 | function QueryParams() {
|
6798 | this.limitSet_ = false;
|
6799 | this.startSet_ = false;
|
6800 | this.startNameSet_ = false;
|
6801 | this.startAfterSet_ = false;
|
6802 | this.endSet_ = false;
|
6803 | this.endNameSet_ = false;
|
6804 | this.endBeforeSet_ = false;
|
6805 | this.limit_ = 0;
|
6806 | this.viewFrom_ = '';
|
6807 | this.indexStartValue_ = null;
|
6808 | this.indexStartName_ = '';
|
6809 | this.indexEndValue_ = null;
|
6810 | this.indexEndName_ = '';
|
6811 | this.index_ = PRIORITY_INDEX;
|
6812 | }
|
6813 | QueryParams.prototype.hasStart = function () {
|
6814 | return this.startSet_;
|
6815 | };
|
6816 | QueryParams.prototype.hasStartAfter = function () {
|
6817 | return this.startAfterSet_;
|
6818 | };
|
6819 | QueryParams.prototype.hasEndBefore = function () {
|
6820 | return this.endBeforeSet_;
|
6821 | };
|
6822 | |
6823 |
|
6824 |
|
6825 | QueryParams.prototype.isViewFromLeft = function () {
|
6826 | if (this.viewFrom_ === '') {
|
6827 |
|
6828 |
|
6829 |
|
6830 |
|
6831 | return this.startSet_;
|
6832 | }
|
6833 | else {
|
6834 | return this.viewFrom_ === "l" ;
|
6835 | }
|
6836 | };
|
6837 | |
6838 |
|
6839 |
|
6840 | QueryParams.prototype.getIndexStartValue = function () {
|
6841 | util.assert(this.startSet_, 'Only valid if start has been set');
|
6842 | return this.indexStartValue_;
|
6843 | };
|
6844 | |
6845 |
|
6846 |
|
6847 |
|
6848 | QueryParams.prototype.getIndexStartName = function () {
|
6849 | util.assert(this.startSet_, 'Only valid if start has been set');
|
6850 | if (this.startNameSet_) {
|
6851 | return this.indexStartName_;
|
6852 | }
|
6853 | else {
|
6854 | return MIN_NAME;
|
6855 | }
|
6856 | };
|
6857 | QueryParams.prototype.hasEnd = function () {
|
6858 | return this.endSet_;
|
6859 | };
|
6860 | |
6861 |
|
6862 |
|
6863 | QueryParams.prototype.getIndexEndValue = function () {
|
6864 | util.assert(this.endSet_, 'Only valid if end has been set');
|
6865 | return this.indexEndValue_;
|
6866 | };
|
6867 | |
6868 |
|
6869 |
|
6870 |
|
6871 | QueryParams.prototype.getIndexEndName = function () {
|
6872 | util.assert(this.endSet_, 'Only valid if end has been set');
|
6873 | if (this.endNameSet_) {
|
6874 | return this.indexEndName_;
|
6875 | }
|
6876 | else {
|
6877 | return MAX_NAME;
|
6878 | }
|
6879 | };
|
6880 | QueryParams.prototype.hasLimit = function () {
|
6881 | return this.limitSet_;
|
6882 | };
|
6883 | |
6884 |
|
6885 |
|
6886 | QueryParams.prototype.hasAnchoredLimit = function () {
|
6887 | return this.limitSet_ && this.viewFrom_ !== '';
|
6888 | };
|
6889 | |
6890 |
|
6891 |
|
6892 | QueryParams.prototype.getLimit = function () {
|
6893 | util.assert(this.limitSet_, 'Only valid if limit has been set');
|
6894 | return this.limit_;
|
6895 | };
|
6896 | QueryParams.prototype.getIndex = function () {
|
6897 | return this.index_;
|
6898 | };
|
6899 | QueryParams.prototype.loadsAllData = function () {
|
6900 | return !(this.startSet_ || this.endSet_ || this.limitSet_);
|
6901 | };
|
6902 | QueryParams.prototype.isDefault = function () {
|
6903 | return this.loadsAllData() && this.index_ === PRIORITY_INDEX;
|
6904 | };
|
6905 | QueryParams.prototype.copy = function () {
|
6906 | var copy = new QueryParams();
|
6907 | copy.limitSet_ = this.limitSet_;
|
6908 | copy.limit_ = this.limit_;
|
6909 | copy.startSet_ = this.startSet_;
|
6910 | copy.indexStartValue_ = this.indexStartValue_;
|
6911 | copy.startNameSet_ = this.startNameSet_;
|
6912 | copy.indexStartName_ = this.indexStartName_;
|
6913 | copy.endSet_ = this.endSet_;
|
6914 | copy.indexEndValue_ = this.indexEndValue_;
|
6915 | copy.endNameSet_ = this.endNameSet_;
|
6916 | copy.indexEndName_ = this.indexEndName_;
|
6917 | copy.index_ = this.index_;
|
6918 | copy.viewFrom_ = this.viewFrom_;
|
6919 | return copy;
|
6920 | };
|
6921 | return QueryParams;
|
6922 | }());
|
6923 | function queryParamsGetNodeFilter(queryParams) {
|
6924 | if (queryParams.loadsAllData()) {
|
6925 | return new IndexedFilter(queryParams.getIndex());
|
6926 | }
|
6927 | else if (queryParams.hasLimit()) {
|
6928 | return new LimitedFilter(queryParams);
|
6929 | }
|
6930 | else {
|
6931 | return new RangedFilter(queryParams);
|
6932 | }
|
6933 | }
|
6934 | function queryParamsLimitToFirst(queryParams, newLimit) {
|
6935 | var newParams = queryParams.copy();
|
6936 | newParams.limitSet_ = true;
|
6937 | newParams.limit_ = newLimit;
|
6938 | newParams.viewFrom_ = "l" ;
|
6939 | return newParams;
|
6940 | }
|
6941 | function queryParamsLimitToLast(queryParams, newLimit) {
|
6942 | var newParams = queryParams.copy();
|
6943 | newParams.limitSet_ = true;
|
6944 | newParams.limit_ = newLimit;
|
6945 | newParams.viewFrom_ = "r" ;
|
6946 | return newParams;
|
6947 | }
|
6948 | function queryParamsStartAt(queryParams, indexValue, key) {
|
6949 | var newParams = queryParams.copy();
|
6950 | newParams.startSet_ = true;
|
6951 | if (indexValue === undefined) {
|
6952 | indexValue = null;
|
6953 | }
|
6954 | newParams.indexStartValue_ = indexValue;
|
6955 | if (key != null) {
|
6956 | newParams.startNameSet_ = true;
|
6957 | newParams.indexStartName_ = key;
|
6958 | }
|
6959 | else {
|
6960 | newParams.startNameSet_ = false;
|
6961 | newParams.indexStartName_ = '';
|
6962 | }
|
6963 | return newParams;
|
6964 | }
|
6965 | function queryParamsStartAfter(queryParams, indexValue, key) {
|
6966 | var params;
|
6967 | if (queryParams.index_ === KEY_INDEX) {
|
6968 | if (typeof indexValue === 'string') {
|
6969 | indexValue = successor(indexValue);
|
6970 | }
|
6971 | params = queryParamsStartAt(queryParams, indexValue, key);
|
6972 | }
|
6973 | else {
|
6974 | var childKey = void 0;
|
6975 | if (key == null) {
|
6976 | childKey = MAX_NAME;
|
6977 | }
|
6978 | else {
|
6979 | childKey = successor(key);
|
6980 | }
|
6981 | params = queryParamsStartAt(queryParams, indexValue, childKey);
|
6982 | }
|
6983 | params.startAfterSet_ = true;
|
6984 | return params;
|
6985 | }
|
6986 | function queryParamsEndAt(queryParams, indexValue, key) {
|
6987 | var newParams = queryParams.copy();
|
6988 | newParams.endSet_ = true;
|
6989 | if (indexValue === undefined) {
|
6990 | indexValue = null;
|
6991 | }
|
6992 | newParams.indexEndValue_ = indexValue;
|
6993 | if (key !== undefined) {
|
6994 | newParams.endNameSet_ = true;
|
6995 | newParams.indexEndName_ = key;
|
6996 | }
|
6997 | else {
|
6998 | newParams.endNameSet_ = false;
|
6999 | newParams.indexEndName_ = '';
|
7000 | }
|
7001 | return newParams;
|
7002 | }
|
7003 | function queryParamsEndBefore(queryParams, indexValue, key) {
|
7004 | var childKey;
|
7005 | var params;
|
7006 | if (queryParams.index_ === KEY_INDEX) {
|
7007 | if (typeof indexValue === 'string') {
|
7008 | indexValue = predecessor(indexValue);
|
7009 | }
|
7010 | params = queryParamsEndAt(queryParams, indexValue, key);
|
7011 | }
|
7012 | else {
|
7013 | if (key == null) {
|
7014 | childKey = MIN_NAME;
|
7015 | }
|
7016 | else {
|
7017 | childKey = predecessor(key);
|
7018 | }
|
7019 | params = queryParamsEndAt(queryParams, indexValue, childKey);
|
7020 | }
|
7021 | params.endBeforeSet_ = true;
|
7022 | return params;
|
7023 | }
|
7024 | function queryParamsOrderBy(queryParams, index) {
|
7025 | var newParams = queryParams.copy();
|
7026 | newParams.index_ = index;
|
7027 | return newParams;
|
7028 | }
|
7029 |
|
7030 |
|
7031 |
|
7032 |
|
7033 |
|
7034 | function queryParamsToRestQueryStringParameters(queryParams) {
|
7035 | var qs = {};
|
7036 | if (queryParams.isDefault()) {
|
7037 | return qs;
|
7038 | }
|
7039 | var orderBy;
|
7040 | if (queryParams.index_ === PRIORITY_INDEX) {
|
7041 | orderBy = "$priority" ;
|
7042 | }
|
7043 | else if (queryParams.index_ === VALUE_INDEX) {
|
7044 | orderBy = "$value" ;
|
7045 | }
|
7046 | else if (queryParams.index_ === KEY_INDEX) {
|
7047 | orderBy = "$key" ;
|
7048 | }
|
7049 | else {
|
7050 | util.assert(queryParams.index_ instanceof PathIndex, 'Unrecognized index type!');
|
7051 | orderBy = queryParams.index_.toString();
|
7052 | }
|
7053 | qs["orderBy" ] = util.stringify(orderBy);
|
7054 | if (queryParams.startSet_) {
|
7055 | qs["startAt" ] = util.stringify(queryParams.indexStartValue_);
|
7056 | if (queryParams.startNameSet_) {
|
7057 | qs["startAt" ] +=
|
7058 | ',' + util.stringify(queryParams.indexStartName_);
|
7059 | }
|
7060 | }
|
7061 | if (queryParams.endSet_) {
|
7062 | qs["endAt" ] = util.stringify(queryParams.indexEndValue_);
|
7063 | if (queryParams.endNameSet_) {
|
7064 | qs["endAt" ] +=
|
7065 | ',' + util.stringify(queryParams.indexEndName_);
|
7066 | }
|
7067 | }
|
7068 | if (queryParams.limitSet_) {
|
7069 | if (queryParams.isViewFromLeft()) {
|
7070 | qs["limitToFirst" ] = queryParams.limit_;
|
7071 | }
|
7072 | else {
|
7073 | qs["limitToLast" ] = queryParams.limit_;
|
7074 | }
|
7075 | }
|
7076 | return qs;
|
7077 | }
|
7078 | function queryParamsGetQueryObject(queryParams) {
|
7079 | var obj = {};
|
7080 | if (queryParams.startSet_) {
|
7081 | obj["sp" ] =
|
7082 | queryParams.indexStartValue_;
|
7083 | if (queryParams.startNameSet_) {
|
7084 | obj["sn" ] =
|
7085 | queryParams.indexStartName_;
|
7086 | }
|
7087 | }
|
7088 | if (queryParams.endSet_) {
|
7089 | obj["ep" ] = queryParams.indexEndValue_;
|
7090 | if (queryParams.endNameSet_) {
|
7091 | obj["en" ] = queryParams.indexEndName_;
|
7092 | }
|
7093 | }
|
7094 | if (queryParams.limitSet_) {
|
7095 | obj["l" ] = queryParams.limit_;
|
7096 | var viewFrom = queryParams.viewFrom_;
|
7097 | if (viewFrom === '') {
|
7098 | if (queryParams.isViewFromLeft()) {
|
7099 | viewFrom = "l" ;
|
7100 | }
|
7101 | else {
|
7102 | viewFrom = "r" ;
|
7103 | }
|
7104 | }
|
7105 | obj["vf" ] = viewFrom;
|
7106 | }
|
7107 |
|
7108 | if (queryParams.index_ !== PRIORITY_INDEX) {
|
7109 | obj["i" ] = queryParams.index_.toString();
|
7110 | }
|
7111 | return obj;
|
7112 | }
|
7113 |
|
7114 |
|
7115 |
|
7116 |
|
7117 |
|
7118 |
|
7119 |
|
7120 |
|
7121 |
|
7122 |
|
7123 |
|
7124 |
|
7125 |
|
7126 |
|
7127 |
|
7128 |
|
7129 |
|
7130 |
|
7131 |
|
7132 |
|
7133 |
|
7134 |
|
7135 | var ReadonlyRestClient = (function (_super) {
|
7136 | tslib.__extends(ReadonlyRestClient, _super);
|
7137 | |
7138 |
|
7139 |
|
7140 |
|
7141 | function ReadonlyRestClient(repoInfo_, onDataUpdate_, authTokenProvider_, appCheckTokenProvider_) {
|
7142 | var _this = _super.call(this) || this;
|
7143 | _this.repoInfo_ = repoInfo_;
|
7144 | _this.onDataUpdate_ = onDataUpdate_;
|
7145 | _this.authTokenProvider_ = authTokenProvider_;
|
7146 | _this.appCheckTokenProvider_ = appCheckTokenProvider_;
|
7147 |
|
7148 | _this.log_ = logWrapper('p:rest:');
|
7149 | |
7150 |
|
7151 |
|
7152 |
|
7153 | _this.listens_ = {};
|
7154 | return _this;
|
7155 | }
|
7156 | ReadonlyRestClient.prototype.reportStats = function (stats) {
|
7157 | throw new Error('Method not implemented.');
|
7158 | };
|
7159 | ReadonlyRestClient.getListenId_ = function (query, tag) {
|
7160 | if (tag !== undefined) {
|
7161 | return 'tag$' + tag;
|
7162 | }
|
7163 | else {
|
7164 | util.assert(query._queryParams.isDefault(), "should have a tag if it's not a default query.");
|
7165 | return query._path.toString();
|
7166 | }
|
7167 | };
|
7168 |
|
7169 | ReadonlyRestClient.prototype.listen = function (query, currentHashFn, tag, onComplete) {
|
7170 | var _this = this;
|
7171 | var pathString = query._path.toString();
|
7172 | this.log_('Listen called for ' + pathString + ' ' + query._queryIdentifier);
|
7173 |
|
7174 | var listenId = ReadonlyRestClient.getListenId_(query, tag);
|
7175 | var thisListen = {};
|
7176 | this.listens_[listenId] = thisListen;
|
7177 | var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
|
7178 | this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
|
7179 | var data = result;
|
7180 | if (error === 404) {
|
7181 | data = null;
|
7182 | error = null;
|
7183 | }
|
7184 | if (error === null) {
|
7185 | _this.onDataUpdate_(pathString, data, false, tag);
|
7186 | }
|
7187 | if (util.safeGet(_this.listens_, listenId) === thisListen) {
|
7188 | var status_1;
|
7189 | if (!error) {
|
7190 | status_1 = 'ok';
|
7191 | }
|
7192 | else if (error === 401) {
|
7193 | status_1 = 'permission_denied';
|
7194 | }
|
7195 | else {
|
7196 | status_1 = 'rest_error:' + error;
|
7197 | }
|
7198 | onComplete(status_1, null);
|
7199 | }
|
7200 | });
|
7201 | };
|
7202 |
|
7203 | ReadonlyRestClient.prototype.unlisten = function (query, tag) {
|
7204 | var listenId = ReadonlyRestClient.getListenId_(query, tag);
|
7205 | delete this.listens_[listenId];
|
7206 | };
|
7207 | ReadonlyRestClient.prototype.get = function (query) {
|
7208 | var _this = this;
|
7209 | var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
|
7210 | var pathString = query._path.toString();
|
7211 | var deferred = new util.Deferred();
|
7212 | this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
|
7213 | var data = result;
|
7214 | if (error === 404) {
|
7215 | data = null;
|
7216 | error = null;
|
7217 | }
|
7218 | if (error === null) {
|
7219 | _this.onDataUpdate_(pathString, data,
|
7220 | false,
|
7221 | null);
|
7222 | deferred.resolve(data);
|
7223 | }
|
7224 | else {
|
7225 | deferred.reject(new Error(data));
|
7226 | }
|
7227 | });
|
7228 | return deferred.promise;
|
7229 | };
|
7230 |
|
7231 | ReadonlyRestClient.prototype.refreshAuthToken = function (token) {
|
7232 |
|
7233 | };
|
7234 | |
7235 |
|
7236 |
|
7237 |
|
7238 | ReadonlyRestClient.prototype.restRequest_ = function (pathString, queryStringParameters, callback) {
|
7239 | var _this = this;
|
7240 | if (queryStringParameters === void 0) { queryStringParameters = {}; }
|
7241 | queryStringParameters['format'] = 'export';
|
7242 | return Promise.all([
|
7243 | this.authTokenProvider_.getToken( false),
|
7244 | this.appCheckTokenProvider_.getToken( false)
|
7245 | ]).then(function (_a) {
|
7246 | var _b = tslib.__read(_a, 2), authToken = _b[0], appCheckToken = _b[1];
|
7247 | if (authToken && authToken.accessToken) {
|
7248 | queryStringParameters['auth'] = authToken.accessToken;
|
7249 | }
|
7250 | if (appCheckToken && appCheckToken.token) {
|
7251 | queryStringParameters['ac'] = appCheckToken.token;
|
7252 | }
|
7253 | var url = (_this.repoInfo_.secure ? 'https://' : 'http://') +
|
7254 | _this.repoInfo_.host +
|
7255 | pathString +
|
7256 | '?' +
|
7257 | 'ns=' +
|
7258 | _this.repoInfo_.namespace +
|
7259 | util.querystring(queryStringParameters);
|
7260 | _this.log_('Sending REST request for ' + url);
|
7261 | var xhr = new XMLHttpRequest();
|
7262 | xhr.onreadystatechange = function () {
|
7263 | if (callback && xhr.readyState === 4) {
|
7264 | _this.log_('REST Response for ' + url + ' received. status:', xhr.status, 'response:', xhr.responseText);
|
7265 | var res = null;
|
7266 | if (xhr.status >= 200 && xhr.status < 300) {
|
7267 | try {
|
7268 | res = util.jsonEval(xhr.responseText);
|
7269 | }
|
7270 | catch (e) {
|
7271 | warn('Failed to parse JSON response for ' +
|
7272 | url +
|
7273 | ': ' +
|
7274 | xhr.responseText);
|
7275 | }
|
7276 | callback(null, res);
|
7277 | }
|
7278 | else {
|
7279 |
|
7280 | if (xhr.status !== 401 && xhr.status !== 404) {
|
7281 | warn('Got unsuccessful REST response for ' +
|
7282 | url +
|
7283 | ' Status: ' +
|
7284 | xhr.status);
|
7285 | }
|
7286 | callback(xhr.status);
|
7287 | }
|
7288 | callback = null;
|
7289 | }
|
7290 | };
|
7291 | xhr.open('GET', url, true);
|
7292 | xhr.send();
|
7293 | });
|
7294 | };
|
7295 | return ReadonlyRestClient;
|
7296 | }(ServerActions));
|
7297 |
|
7298 |
|
7299 |
|
7300 |
|
7301 |
|
7302 |
|
7303 |
|
7304 |
|
7305 |
|
7306 |
|
7307 |
|
7308 |
|
7309 |
|
7310 |
|
7311 |
|
7312 |
|
7313 |
|
7314 |
|
7315 |
|
7316 |
|
7317 | var SnapshotHolder = (function () {
|
7318 | function SnapshotHolder() {
|
7319 | this.rootNode_ = ChildrenNode.EMPTY_NODE;
|
7320 | }
|
7321 | SnapshotHolder.prototype.getNode = function (path) {
|
7322 | return this.rootNode_.getChild(path);
|
7323 | };
|
7324 | SnapshotHolder.prototype.updateSnapshot = function (path, newSnapshotNode) {
|
7325 | this.rootNode_ = this.rootNode_.updateChild(path, newSnapshotNode);
|
7326 | };
|
7327 | return SnapshotHolder;
|
7328 | }());
|
7329 |
|
7330 |
|
7331 |
|
7332 |
|
7333 |
|
7334 |
|
7335 |
|
7336 |
|
7337 |
|
7338 |
|
7339 |
|
7340 |
|
7341 |
|
7342 |
|
7343 |
|
7344 |
|
7345 |
|
7346 | function newSparseSnapshotTree() {
|
7347 | return {
|
7348 | value: null,
|
7349 | children: new Map()
|
7350 | };
|
7351 | }
|
7352 |
|
7353 |
|
7354 |
|
7355 |
|
7356 |
|
7357 |
|
7358 |
|
7359 | function sparseSnapshotTreeRemember(sparseSnapshotTree, path, data) {
|
7360 | if (pathIsEmpty(path)) {
|
7361 | sparseSnapshotTree.value = data;
|
7362 | sparseSnapshotTree.children.clear();
|
7363 | }
|
7364 | else if (sparseSnapshotTree.value !== null) {
|
7365 | sparseSnapshotTree.value = sparseSnapshotTree.value.updateChild(path, data);
|
7366 | }
|
7367 | else {
|
7368 | var childKey = pathGetFront(path);
|
7369 | if (!sparseSnapshotTree.children.has(childKey)) {
|
7370 | sparseSnapshotTree.children.set(childKey, newSparseSnapshotTree());
|
7371 | }
|
7372 | var child = sparseSnapshotTree.children.get(childKey);
|
7373 | path = pathPopFront(path);
|
7374 | sparseSnapshotTreeRemember(child, path, data);
|
7375 | }
|
7376 | }
|
7377 |
|
7378 |
|
7379 |
|
7380 |
|
7381 |
|
7382 |
|
7383 | function sparseSnapshotTreeForget(sparseSnapshotTree, path) {
|
7384 | if (pathIsEmpty(path)) {
|
7385 | sparseSnapshotTree.value = null;
|
7386 | sparseSnapshotTree.children.clear();
|
7387 | return true;
|
7388 | }
|
7389 | else {
|
7390 | if (sparseSnapshotTree.value !== null) {
|
7391 | if (sparseSnapshotTree.value.isLeafNode()) {
|
7392 |
|
7393 | return false;
|
7394 | }
|
7395 | else {
|
7396 | var value = sparseSnapshotTree.value;
|
7397 | sparseSnapshotTree.value = null;
|
7398 | value.forEachChild(PRIORITY_INDEX, function (key, tree) {
|
7399 | sparseSnapshotTreeRemember(sparseSnapshotTree, new Path(key), tree);
|
7400 | });
|
7401 | return sparseSnapshotTreeForget(sparseSnapshotTree, path);
|
7402 | }
|
7403 | }
|
7404 | else if (sparseSnapshotTree.children.size > 0) {
|
7405 | var childKey = pathGetFront(path);
|
7406 | path = pathPopFront(path);
|
7407 | if (sparseSnapshotTree.children.has(childKey)) {
|
7408 | var safeToRemove = sparseSnapshotTreeForget(sparseSnapshotTree.children.get(childKey), path);
|
7409 | if (safeToRemove) {
|
7410 | sparseSnapshotTree.children.delete(childKey);
|
7411 | }
|
7412 | }
|
7413 | return sparseSnapshotTree.children.size === 0;
|
7414 | }
|
7415 | else {
|
7416 | return true;
|
7417 | }
|
7418 | }
|
7419 | }
|
7420 |
|
7421 |
|
7422 |
|
7423 |
|
7424 |
|
7425 |
|
7426 |
|
7427 | function sparseSnapshotTreeForEachTree(sparseSnapshotTree, prefixPath, func) {
|
7428 | if (sparseSnapshotTree.value !== null) {
|
7429 | func(prefixPath, sparseSnapshotTree.value);
|
7430 | }
|
7431 | else {
|
7432 | sparseSnapshotTreeForEachChild(sparseSnapshotTree, function (key, tree) {
|
7433 | var path = new Path(prefixPath.toString() + '/' + key);
|
7434 | sparseSnapshotTreeForEachTree(tree, path, func);
|
7435 | });
|
7436 | }
|
7437 | }
|
7438 |
|
7439 |
|
7440 |
|
7441 |
|
7442 |
|
7443 |
|
7444 | function sparseSnapshotTreeForEachChild(sparseSnapshotTree, func) {
|
7445 | sparseSnapshotTree.children.forEach(function (tree, key) {
|
7446 | func(key, tree);
|
7447 | });
|
7448 | }
|
7449 |
|
7450 |
|
7451 |
|
7452 |
|
7453 |
|
7454 |
|
7455 |
|
7456 |
|
7457 |
|
7458 |
|
7459 |
|
7460 |
|
7461 |
|
7462 |
|
7463 |
|
7464 |
|
7465 |
|
7466 |
|
7467 |
|
7468 |
|
7469 |
|
7470 |
|
7471 | var StatsListener = (function () {
|
7472 | function StatsListener(collection_) {
|
7473 | this.collection_ = collection_;
|
7474 | this.last_ = null;
|
7475 | }
|
7476 | StatsListener.prototype.get = function () {
|
7477 | var newStats = this.collection_.get();
|
7478 | var delta = tslib.__assign({}, newStats);
|
7479 | if (this.last_) {
|
7480 | each(this.last_, function (stat, value) {
|
7481 | delta[stat] = delta[stat] - value;
|
7482 | });
|
7483 | }
|
7484 | this.last_ = newStats;
|
7485 | return delta;
|
7486 | };
|
7487 | return StatsListener;
|
7488 | }());
|
7489 |
|
7490 |
|
7491 |
|
7492 |
|
7493 |
|
7494 |
|
7495 |
|
7496 |
|
7497 |
|
7498 |
|
7499 |
|
7500 |
|
7501 |
|
7502 |
|
7503 |
|
7504 |
|
7505 |
|
7506 |
|
7507 |
|
7508 |
|
7509 | var FIRST_STATS_MIN_TIME = 10 * 1000;
|
7510 | var FIRST_STATS_MAX_TIME = 30 * 1000;
|
7511 |
|
7512 | var REPORT_STATS_INTERVAL = 5 * 60 * 1000;
|
7513 | var StatsReporter = (function () {
|
7514 | function StatsReporter(collection, server_) {
|
7515 | this.server_ = server_;
|
7516 | this.statsToReport_ = {};
|
7517 | this.statsListener_ = new StatsListener(collection);
|
7518 | var timeout = FIRST_STATS_MIN_TIME +
|
7519 | (FIRST_STATS_MAX_TIME - FIRST_STATS_MIN_TIME) * Math.random();
|
7520 | setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(timeout));
|
7521 | }
|
7522 | StatsReporter.prototype.reportStats_ = function () {
|
7523 | var _this = this;
|
7524 | var stats = this.statsListener_.get();
|
7525 | var reportedStats = {};
|
7526 | var haveStatsToReport = false;
|
7527 | each(stats, function (stat, value) {
|
7528 | if (value > 0 && util.contains(_this.statsToReport_, stat)) {
|
7529 | reportedStats[stat] = value;
|
7530 | haveStatsToReport = true;
|
7531 | }
|
7532 | });
|
7533 | if (haveStatsToReport) {
|
7534 | this.server_.reportStats(reportedStats);
|
7535 | }
|
7536 |
|
7537 | setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(Math.random() * 2 * REPORT_STATS_INTERVAL));
|
7538 | };
|
7539 | return StatsReporter;
|
7540 | }());
|
7541 | function statsReporterIncludeStat(reporter, stat) {
|
7542 | reporter.statsToReport_[stat] = true;
|
7543 | }
|
7544 |
|
7545 |
|
7546 |
|
7547 |
|
7548 |
|
7549 |
|
7550 |
|
7551 |
|
7552 |
|
7553 |
|
7554 |
|
7555 |
|
7556 |
|
7557 |
|
7558 |
|
7559 |
|
7560 |
|
7561 |
|
7562 |
|
7563 |
|
7564 |
|
7565 | var OperationType;
|
7566 | (function (OperationType) {
|
7567 | OperationType[OperationType["OVERWRITE"] = 0] = "OVERWRITE";
|
7568 | OperationType[OperationType["MERGE"] = 1] = "MERGE";
|
7569 | OperationType[OperationType["ACK_USER_WRITE"] = 2] = "ACK_USER_WRITE";
|
7570 | OperationType[OperationType["LISTEN_COMPLETE"] = 3] = "LISTEN_COMPLETE";
|
7571 | })(OperationType || (OperationType = {}));
|
7572 | function newOperationSourceUser() {
|
7573 | return {
|
7574 | fromUser: true,
|
7575 | fromServer: false,
|
7576 | queryId: null,
|
7577 | tagged: false
|
7578 | };
|
7579 | }
|
7580 | function newOperationSourceServer() {
|
7581 | return {
|
7582 | fromUser: false,
|
7583 | fromServer: true,
|
7584 | queryId: null,
|
7585 | tagged: false
|
7586 | };
|
7587 | }
|
7588 | function newOperationSourceServerTaggedQuery(queryId) {
|
7589 | return {
|
7590 | fromUser: false,
|
7591 | fromServer: true,
|
7592 | queryId: queryId,
|
7593 | tagged: true
|
7594 | };
|
7595 | }
|
7596 |
|
7597 |
|
7598 |
|
7599 |
|
7600 |
|
7601 |
|
7602 |
|
7603 |
|
7604 |
|
7605 |
|
7606 |
|
7607 |
|
7608 |
|
7609 |
|
7610 |
|
7611 |
|
7612 |
|
7613 | var AckUserWrite = (function () {
|
7614 | |
7615 |
|
7616 |
|
7617 | function AckUserWrite(
|
7618 | /** @inheritDoc */ path,
|
7619 | /** @inheritDoc */ affectedTree,
|
7620 | /** @inheritDoc */ revert) {
|
7621 | this.path = path;
|
7622 | this.affectedTree = affectedTree;
|
7623 | this.revert = revert;
|
7624 |
|
7625 | this.type = OperationType.ACK_USER_WRITE;
|
7626 |
|
7627 | this.source = newOperationSourceUser();
|
7628 | }
|
7629 | AckUserWrite.prototype.operationForChild = function (childName) {
|
7630 | if (!pathIsEmpty(this.path)) {
|
7631 | util.assert(pathGetFront(this.path) === childName, 'operationForChild called for unrelated child.');
|
7632 | return new AckUserWrite(pathPopFront(this.path), this.affectedTree, this.revert);
|
7633 | }
|
7634 | else if (this.affectedTree.value != null) {
|
7635 | util.assert(this.affectedTree.children.isEmpty(), 'affectedTree should not have overlapping affected paths.');
|
7636 |
|
7637 | return this;
|
7638 | }
|
7639 | else {
|
7640 | var childTree = this.affectedTree.subtree(new Path(childName));
|
7641 | return new AckUserWrite(newEmptyPath(), childTree, this.revert);
|
7642 | }
|
7643 | };
|
7644 | return AckUserWrite;
|
7645 | }());
|
7646 |
|
7647 |
|
7648 |
|
7649 |
|
7650 |
|
7651 |
|
7652 |
|
7653 |
|
7654 |
|
7655 |
|
7656 |
|
7657 |
|
7658 |
|
7659 |
|
7660 |
|
7661 |
|
7662 |
|
7663 | var ListenComplete = (function () {
|
7664 | function ListenComplete(source, path) {
|
7665 | this.source = source;
|
7666 | this.path = path;
|
7667 |
|
7668 | this.type = OperationType.LISTEN_COMPLETE;
|
7669 | }
|
7670 | ListenComplete.prototype.operationForChild = function (childName) {
|
7671 | if (pathIsEmpty(this.path)) {
|
7672 | return new ListenComplete(this.source, newEmptyPath());
|
7673 | }
|
7674 | else {
|
7675 | return new ListenComplete(this.source, pathPopFront(this.path));
|
7676 | }
|
7677 | };
|
7678 | return ListenComplete;
|
7679 | }());
|
7680 |
|
7681 |
|
7682 |
|
7683 |
|
7684 |
|
7685 |
|
7686 |
|
7687 |
|
7688 |
|
7689 |
|
7690 |
|
7691 |
|
7692 |
|
7693 |
|
7694 |
|
7695 |
|
7696 |
|
7697 | var Overwrite = (function () {
|
7698 | function Overwrite(source, path, snap) {
|
7699 | this.source = source;
|
7700 | this.path = path;
|
7701 | this.snap = snap;
|
7702 |
|
7703 | this.type = OperationType.OVERWRITE;
|
7704 | }
|
7705 | Overwrite.prototype.operationForChild = function (childName) {
|
7706 | if (pathIsEmpty(this.path)) {
|
7707 | return new Overwrite(this.source, newEmptyPath(), this.snap.getImmediateChild(childName));
|
7708 | }
|
7709 | else {
|
7710 | return new Overwrite(this.source, pathPopFront(this.path), this.snap);
|
7711 | }
|
7712 | };
|
7713 | return Overwrite;
|
7714 | }());
|
7715 |
|
7716 |
|
7717 |
|
7718 |
|
7719 |
|
7720 |
|
7721 |
|
7722 |
|
7723 |
|
7724 |
|
7725 |
|
7726 |
|
7727 |
|
7728 |
|
7729 |
|
7730 |
|
7731 |
|
7732 | var Merge = (function () {
|
7733 | function Merge(
|
7734 | /** @inheritDoc */ source,
|
7735 | /** @inheritDoc */ path,
|
7736 | /** @inheritDoc */ children) {
|
7737 | this.source = source;
|
7738 | this.path = path;
|
7739 | this.children = children;
|
7740 |
|
7741 | this.type = OperationType.MERGE;
|
7742 | }
|
7743 | Merge.prototype.operationForChild = function (childName) {
|
7744 | if (pathIsEmpty(this.path)) {
|
7745 | var childTree = this.children.subtree(new Path(childName));
|
7746 | if (childTree.isEmpty()) {
|
7747 |
|
7748 | return null;
|
7749 | }
|
7750 | else if (childTree.value) {
|
7751 |
|
7752 | return new Overwrite(this.source, newEmptyPath(), childTree.value);
|
7753 | }
|
7754 | else {
|
7755 |
|
7756 | return new Merge(this.source, newEmptyPath(), childTree);
|
7757 | }
|
7758 | }
|
7759 | else {
|
7760 | util.assert(pathGetFront(this.path) === childName, "Can't get a merge for a child not on the path of the operation");
|
7761 | return new Merge(this.source, pathPopFront(this.path), this.children);
|
7762 | }
|
7763 | };
|
7764 | Merge.prototype.toString = function () {
|
7765 | return ('Operation(' +
|
7766 | this.path +
|
7767 | ': ' +
|
7768 | this.source.toString() +
|
7769 | ' merge: ' +
|
7770 | this.children.toString() +
|
7771 | ')');
|
7772 | };
|
7773 | return Merge;
|
7774 | }());
|
7775 |
|
7776 |
|
7777 |
|
7778 |
|
7779 |
|
7780 |
|
7781 |
|
7782 |
|
7783 |
|
7784 |
|
7785 |
|
7786 |
|
7787 |
|
7788 |
|
7789 |
|
7790 |
|
7791 |
|
7792 |
|
7793 |
|
7794 |
|
7795 |
|
7796 |
|
7797 |
|
7798 | var CacheNode = (function () {
|
7799 | function CacheNode(node_, fullyInitialized_, filtered_) {
|
7800 | this.node_ = node_;
|
7801 | this.fullyInitialized_ = fullyInitialized_;
|
7802 | this.filtered_ = filtered_;
|
7803 | }
|
7804 | |
7805 |
|
7806 |
|
7807 | CacheNode.prototype.isFullyInitialized = function () {
|
7808 | return this.fullyInitialized_;
|
7809 | };
|
7810 | |
7811 |
|
7812 |
|
7813 | CacheNode.prototype.isFiltered = function () {
|
7814 | return this.filtered_;
|
7815 | };
|
7816 | CacheNode.prototype.isCompleteForPath = function (path) {
|
7817 | if (pathIsEmpty(path)) {
|
7818 | return this.isFullyInitialized() && !this.filtered_;
|
7819 | }
|
7820 | var childKey = pathGetFront(path);
|
7821 | return this.isCompleteForChild(childKey);
|
7822 | };
|
7823 | CacheNode.prototype.isCompleteForChild = function (key) {
|
7824 | return ((this.isFullyInitialized() && !this.filtered_) || this.node_.hasChild(key));
|
7825 | };
|
7826 | CacheNode.prototype.getNode = function () {
|
7827 | return this.node_;
|
7828 | };
|
7829 | return CacheNode;
|
7830 | }());
|
7831 |
|
7832 |
|
7833 |
|
7834 |
|
7835 |
|
7836 |
|
7837 |
|
7838 |
|
7839 |
|
7840 |
|
7841 |
|
7842 |
|
7843 |
|
7844 |
|
7845 |
|
7846 |
|
7847 |
|
7848 |
|
7849 |
|
7850 |
|
7851 |
|
7852 |
|
7853 |
|
7854 | var EventGenerator = (function () {
|
7855 | function EventGenerator(query_) {
|
7856 | this.query_ = query_;
|
7857 | this.index_ = this.query_._queryParams.getIndex();
|
7858 | }
|
7859 | return EventGenerator;
|
7860 | }());
|
7861 |
|
7862 |
|
7863 |
|
7864 |
|
7865 |
|
7866 |
|
7867 |
|
7868 |
|
7869 |
|
7870 | function eventGeneratorGenerateEventsForChanges(eventGenerator, changes, eventCache, eventRegistrations) {
|
7871 | var events = [];
|
7872 | var moves = [];
|
7873 | changes.forEach(function (change) {
|
7874 | if (change.type === "child_changed" &&
|
7875 | eventGenerator.index_.indexedValueChanged(change.oldSnap, change.snapshotNode)) {
|
7876 | moves.push(changeChildMoved(change.childName, change.snapshotNode));
|
7877 | }
|
7878 | });
|
7879 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_removed" , changes, eventRegistrations, eventCache);
|
7880 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_added" , changes, eventRegistrations, eventCache);
|
7881 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_moved" , moves, eventRegistrations, eventCache);
|
7882 | eventGeneratorGenerateEventsForType(eventGenerator, events, "child_changed" , changes, eventRegistrations, eventCache);
|
7883 | eventGeneratorGenerateEventsForType(eventGenerator, events, "value" , changes, eventRegistrations, eventCache);
|
7884 | return events;
|
7885 | }
|
7886 |
|
7887 |
|
7888 |
|
7889 | function eventGeneratorGenerateEventsForType(eventGenerator, events, eventType, changes, registrations, eventCache) {
|
7890 | var filteredChanges = changes.filter(function (change) { return change.type === eventType; });
|
7891 | filteredChanges.sort(function (a, b) {
|
7892 | return eventGeneratorCompareChanges(eventGenerator, a, b);
|
7893 | });
|
7894 | filteredChanges.forEach(function (change) {
|
7895 | var materializedChange = eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache);
|
7896 | registrations.forEach(function (registration) {
|
7897 | if (registration.respondsTo(change.type)) {
|
7898 | events.push(registration.createEvent(materializedChange, eventGenerator.query_));
|
7899 | }
|
7900 | });
|
7901 | });
|
7902 | }
|
7903 | function eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache) {
|
7904 | if (change.type === 'value' || change.type === 'child_removed') {
|
7905 | return change;
|
7906 | }
|
7907 | else {
|
7908 | change.prevName = eventCache.getPredecessorChildName(change.childName, change.snapshotNode, eventGenerator.index_);
|
7909 | return change;
|
7910 | }
|
7911 | }
|
7912 | function eventGeneratorCompareChanges(eventGenerator, a, b) {
|
7913 | if (a.childName == null || b.childName == null) {
|
7914 | throw util.assertionError('Should only compare child_ events.');
|
7915 | }
|
7916 | var aWrapped = new NamedNode(a.childName, a.snapshotNode);
|
7917 | var bWrapped = new NamedNode(b.childName, b.snapshotNode);
|
7918 | return eventGenerator.index_.compare(aWrapped, bWrapped);
|
7919 | }
|
7920 |
|
7921 |
|
7922 |
|
7923 |
|
7924 |
|
7925 |
|
7926 |
|
7927 |
|
7928 |
|
7929 |
|
7930 |
|
7931 |
|
7932 |
|
7933 |
|
7934 |
|
7935 |
|
7936 |
|
7937 | function newViewCache(eventCache, serverCache) {
|
7938 | return { eventCache: eventCache, serverCache: serverCache };
|
7939 | }
|
7940 | function viewCacheUpdateEventSnap(viewCache, eventSnap, complete, filtered) {
|
7941 | return newViewCache(new CacheNode(eventSnap, complete, filtered), viewCache.serverCache);
|
7942 | }
|
7943 | function viewCacheUpdateServerSnap(viewCache, serverSnap, complete, filtered) {
|
7944 | return newViewCache(viewCache.eventCache, new CacheNode(serverSnap, complete, filtered));
|
7945 | }
|
7946 | function viewCacheGetCompleteEventSnap(viewCache) {
|
7947 | return viewCache.eventCache.isFullyInitialized()
|
7948 | ? viewCache.eventCache.getNode()
|
7949 | : null;
|
7950 | }
|
7951 | function viewCacheGetCompleteServerSnap(viewCache) {
|
7952 | return viewCache.serverCache.isFullyInitialized()
|
7953 | ? viewCache.serverCache.getNode()
|
7954 | : null;
|
7955 | }
|
7956 |
|
7957 |
|
7958 |
|
7959 |
|
7960 |
|
7961 |
|
7962 |
|
7963 |
|
7964 |
|
7965 |
|
7966 |
|
7967 |
|
7968 |
|
7969 |
|
7970 |
|
7971 |
|
7972 |
|
7973 | var emptyChildrenSingleton;
|
7974 |
|
7975 |
|
7976 |
|
7977 |
|
7978 | var EmptyChildren = function () {
|
7979 | if (!emptyChildrenSingleton) {
|
7980 | emptyChildrenSingleton = new SortedMap(stringCompare);
|
7981 | }
|
7982 | return emptyChildrenSingleton;
|
7983 | };
|
7984 |
|
7985 |
|
7986 |
|
7987 | var ImmutableTree = (function () {
|
7988 | function ImmutableTree(value, children) {
|
7989 | if (children === void 0) { children = EmptyChildren(); }
|
7990 | this.value = value;
|
7991 | this.children = children;
|
7992 | }
|
7993 | ImmutableTree.fromObject = function (obj) {
|
7994 | var tree = new ImmutableTree(null);
|
7995 | each(obj, function (childPath, childSnap) {
|
7996 | tree = tree.set(new Path(childPath), childSnap);
|
7997 | });
|
7998 | return tree;
|
7999 | };
|
8000 | |
8001 |
|
8002 |
|
8003 | ImmutableTree.prototype.isEmpty = function () {
|
8004 | return this.value === null && this.children.isEmpty();
|
8005 | };
|
8006 | |
8007 |
|
8008 |
|
8009 |
|
8010 |
|
8011 |
|
8012 |
|
8013 |
|
8014 |
|
8015 |
|
8016 | ImmutableTree.prototype.findRootMostMatchingPathAndValue = function (relativePath, predicate) {
|
8017 | if (this.value != null && predicate(this.value)) {
|
8018 | return { path: newEmptyPath(), value: this.value };
|
8019 | }
|
8020 | else {
|
8021 | if (pathIsEmpty(relativePath)) {
|
8022 | return null;
|
8023 | }
|
8024 | else {
|
8025 | var front = pathGetFront(relativePath);
|
8026 | var child = this.children.get(front);
|
8027 | if (child !== null) {
|
8028 | var childExistingPathAndValue = child.findRootMostMatchingPathAndValue(pathPopFront(relativePath), predicate);
|
8029 | if (childExistingPathAndValue != null) {
|
8030 | var fullPath = pathChild(new Path(front), childExistingPathAndValue.path);
|
8031 | return { path: fullPath, value: childExistingPathAndValue.value };
|
8032 | }
|
8033 | else {
|
8034 | return null;
|
8035 | }
|
8036 | }
|
8037 | else {
|
8038 | return null;
|
8039 | }
|
8040 | }
|
8041 | }
|
8042 | };
|
8043 | |
8044 |
|
8045 |
|
8046 |
|
8047 | ImmutableTree.prototype.findRootMostValueAndPath = function (relativePath) {
|
8048 | return this.findRootMostMatchingPathAndValue(relativePath, function () { return true; });
|
8049 | };
|
8050 | |
8051 |
|
8052 |
|
8053 | ImmutableTree.prototype.subtree = function (relativePath) {
|
8054 | if (pathIsEmpty(relativePath)) {
|
8055 | return this;
|
8056 | }
|
8057 | else {
|
8058 | var front = pathGetFront(relativePath);
|
8059 | var childTree = this.children.get(front);
|
8060 | if (childTree !== null) {
|
8061 | return childTree.subtree(pathPopFront(relativePath));
|
8062 | }
|
8063 | else {
|
8064 | return new ImmutableTree(null);
|
8065 | }
|
8066 | }
|
8067 | };
|
8068 | |
8069 |
|
8070 |
|
8071 |
|
8072 |
|
8073 |
|
8074 |
|
8075 | ImmutableTree.prototype.set = function (relativePath, toSet) {
|
8076 | if (pathIsEmpty(relativePath)) {
|
8077 | return new ImmutableTree(toSet, this.children);
|
8078 | }
|
8079 | else {
|
8080 | var front = pathGetFront(relativePath);
|
8081 | var child = this.children.get(front) || new ImmutableTree(null);
|
8082 | var newChild = child.set(pathPopFront(relativePath), toSet);
|
8083 | var newChildren = this.children.insert(front, newChild);
|
8084 | return new ImmutableTree(this.value, newChildren);
|
8085 | }
|
8086 | };
|
8087 | |
8088 |
|
8089 |
|
8090 |
|
8091 |
|
8092 |
|
8093 | ImmutableTree.prototype.remove = function (relativePath) {
|
8094 | if (pathIsEmpty(relativePath)) {
|
8095 | if (this.children.isEmpty()) {
|
8096 | return new ImmutableTree(null);
|
8097 | }
|
8098 | else {
|
8099 | return new ImmutableTree(null, this.children);
|
8100 | }
|
8101 | }
|
8102 | else {
|
8103 | var front = pathGetFront(relativePath);
|
8104 | var child = this.children.get(front);
|
8105 | if (child) {
|
8106 | var newChild = child.remove(pathPopFront(relativePath));
|
8107 | var newChildren = void 0;
|
8108 | if (newChild.isEmpty()) {
|
8109 | newChildren = this.children.remove(front);
|
8110 | }
|
8111 | else {
|
8112 | newChildren = this.children.insert(front, newChild);
|
8113 | }
|
8114 | if (this.value === null && newChildren.isEmpty()) {
|
8115 | return new ImmutableTree(null);
|
8116 | }
|
8117 | else {
|
8118 | return new ImmutableTree(this.value, newChildren);
|
8119 | }
|
8120 | }
|
8121 | else {
|
8122 | return this;
|
8123 | }
|
8124 | }
|
8125 | };
|
8126 | |
8127 |
|
8128 |
|
8129 |
|
8130 |
|
8131 |
|
8132 | ImmutableTree.prototype.get = function (relativePath) {
|
8133 | if (pathIsEmpty(relativePath)) {
|
8134 | return this.value;
|
8135 | }
|
8136 | else {
|
8137 | var front = pathGetFront(relativePath);
|
8138 | var child = this.children.get(front);
|
8139 | if (child) {
|
8140 | return child.get(pathPopFront(relativePath));
|
8141 | }
|
8142 | else {
|
8143 | return null;
|
8144 | }
|
8145 | }
|
8146 | };
|
8147 | |
8148 |
|
8149 |
|
8150 |
|
8151 |
|
8152 |
|
8153 |
|
8154 | ImmutableTree.prototype.setTree = function (relativePath, newTree) {
|
8155 | if (pathIsEmpty(relativePath)) {
|
8156 | return newTree;
|
8157 | }
|
8158 | else {
|
8159 | var front = pathGetFront(relativePath);
|
8160 | var child = this.children.get(front) || new ImmutableTree(null);
|
8161 | var newChild = child.setTree(pathPopFront(relativePath), newTree);
|
8162 | var newChildren = void 0;
|
8163 | if (newChild.isEmpty()) {
|
8164 | newChildren = this.children.remove(front);
|
8165 | }
|
8166 | else {
|
8167 | newChildren = this.children.insert(front, newChild);
|
8168 | }
|
8169 | return new ImmutableTree(this.value, newChildren);
|
8170 | }
|
8171 | };
|
8172 | |
8173 |
|
8174 |
|
8175 |
|
8176 |
|
8177 | ImmutableTree.prototype.fold = function (fn) {
|
8178 | return this.fold_(newEmptyPath(), fn);
|
8179 | };
|
8180 | |
8181 |
|
8182 |
|
8183 | ImmutableTree.prototype.fold_ = function (pathSoFar, fn) {
|
8184 | var accum = {};
|
8185 | this.children.inorderTraversal(function (childKey, childTree) {
|
8186 | accum[childKey] = childTree.fold_(pathChild(pathSoFar, childKey), fn);
|
8187 | });
|
8188 | return fn(pathSoFar, this.value, accum);
|
8189 | };
|
8190 | |
8191 |
|
8192 |
|
8193 | ImmutableTree.prototype.findOnPath = function (path, f) {
|
8194 | return this.findOnPath_(path, newEmptyPath(), f);
|
8195 | };
|
8196 | ImmutableTree.prototype.findOnPath_ = function (pathToFollow, pathSoFar, f) {
|
8197 | var result = this.value ? f(pathSoFar, this.value) : false;
|
8198 | if (result) {
|
8199 | return result;
|
8200 | }
|
8201 | else {
|
8202 | if (pathIsEmpty(pathToFollow)) {
|
8203 | return null;
|
8204 | }
|
8205 | else {
|
8206 | var front = pathGetFront(pathToFollow);
|
8207 | var nextChild = this.children.get(front);
|
8208 | if (nextChild) {
|
8209 | return nextChild.findOnPath_(pathPopFront(pathToFollow), pathChild(pathSoFar, front), f);
|
8210 | }
|
8211 | else {
|
8212 | return null;
|
8213 | }
|
8214 | }
|
8215 | }
|
8216 | };
|
8217 | ImmutableTree.prototype.foreachOnPath = function (path, f) {
|
8218 | return this.foreachOnPath_(path, newEmptyPath(), f);
|
8219 | };
|
8220 | ImmutableTree.prototype.foreachOnPath_ = function (pathToFollow, currentRelativePath, f) {
|
8221 | if (pathIsEmpty(pathToFollow)) {
|
8222 | return this;
|
8223 | }
|
8224 | else {
|
8225 | if (this.value) {
|
8226 | f(currentRelativePath, this.value);
|
8227 | }
|
8228 | var front = pathGetFront(pathToFollow);
|
8229 | var nextChild = this.children.get(front);
|
8230 | if (nextChild) {
|
8231 | return nextChild.foreachOnPath_(pathPopFront(pathToFollow), pathChild(currentRelativePath, front), f);
|
8232 | }
|
8233 | else {
|
8234 | return new ImmutableTree(null);
|
8235 | }
|
8236 | }
|
8237 | };
|
8238 | |
8239 |
|
8240 |
|
8241 |
|
8242 |
|
8243 |
|
8244 | ImmutableTree.prototype.foreach = function (f) {
|
8245 | this.foreach_(newEmptyPath(), f);
|
8246 | };
|
8247 | ImmutableTree.prototype.foreach_ = function (currentRelativePath, f) {
|
8248 | this.children.inorderTraversal(function (childName, childTree) {
|
8249 | childTree.foreach_(pathChild(currentRelativePath, childName), f);
|
8250 | });
|
8251 | if (this.value) {
|
8252 | f(currentRelativePath, this.value);
|
8253 | }
|
8254 | };
|
8255 | ImmutableTree.prototype.foreachChild = function (f) {
|
8256 | this.children.inorderTraversal(function (childName, childTree) {
|
8257 | if (childTree.value) {
|
8258 | f(childName, childTree.value);
|
8259 | }
|
8260 | });
|
8261 | };
|
8262 | return ImmutableTree;
|
8263 | }());
|
8264 |
|
8265 |
|
8266 |
|
8267 |
|
8268 |
|
8269 |
|
8270 |
|
8271 |
|
8272 |
|
8273 |
|
8274 |
|
8275 |
|
8276 |
|
8277 |
|
8278 |
|
8279 |
|
8280 |
|
8281 |
|
8282 |
|
8283 |
|
8284 |
|
8285 |
|
8286 |
|
8287 | var CompoundWrite = (function () {
|
8288 | function CompoundWrite(writeTree_) {
|
8289 | this.writeTree_ = writeTree_;
|
8290 | }
|
8291 | CompoundWrite.empty = function () {
|
8292 | return new CompoundWrite(new ImmutableTree(null));
|
8293 | };
|
8294 | return CompoundWrite;
|
8295 | }());
|
8296 | function compoundWriteAddWrite(compoundWrite, path, node) {
|
8297 | if (pathIsEmpty(path)) {
|
8298 | return new CompoundWrite(new ImmutableTree(node));
|
8299 | }
|
8300 | else {
|
8301 | var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
|
8302 | if (rootmost != null) {
|
8303 | var rootMostPath = rootmost.path;
|
8304 | var value = rootmost.value;
|
8305 | var relativePath = newRelativePath(rootMostPath, path);
|
8306 | value = value.updateChild(relativePath, node);
|
8307 | return new CompoundWrite(compoundWrite.writeTree_.set(rootMostPath, value));
|
8308 | }
|
8309 | else {
|
8310 | var subtree = new ImmutableTree(node);
|
8311 | var newWriteTree = compoundWrite.writeTree_.setTree(path, subtree);
|
8312 | return new CompoundWrite(newWriteTree);
|
8313 | }
|
8314 | }
|
8315 | }
|
8316 | function compoundWriteAddWrites(compoundWrite, path, updates) {
|
8317 | var newWrite = compoundWrite;
|
8318 | each(updates, function (childKey, node) {
|
8319 | newWrite = compoundWriteAddWrite(newWrite, pathChild(path, childKey), node);
|
8320 | });
|
8321 | return newWrite;
|
8322 | }
|
8323 |
|
8324 |
|
8325 |
|
8326 |
|
8327 |
|
8328 |
|
8329 |
|
8330 |
|
8331 | function compoundWriteRemoveWrite(compoundWrite, path) {
|
8332 | if (pathIsEmpty(path)) {
|
8333 | return CompoundWrite.empty();
|
8334 | }
|
8335 | else {
|
8336 | var newWriteTree = compoundWrite.writeTree_.setTree(path, new ImmutableTree(null));
|
8337 | return new CompoundWrite(newWriteTree);
|
8338 | }
|
8339 | }
|
8340 |
|
8341 |
|
8342 |
|
8343 |
|
8344 |
|
8345 |
|
8346 |
|
8347 |
|
8348 | function compoundWriteHasCompleteWrite(compoundWrite, path) {
|
8349 | return compoundWriteGetCompleteNode(compoundWrite, path) != null;
|
8350 | }
|
8351 |
|
8352 |
|
8353 |
|
8354 |
|
8355 |
|
8356 |
|
8357 |
|
8358 |
|
8359 | function compoundWriteGetCompleteNode(compoundWrite, path) {
|
8360 | var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
|
8361 | if (rootmost != null) {
|
8362 | return compoundWrite.writeTree_
|
8363 | .get(rootmost.path)
|
8364 | .getChild(newRelativePath(rootmost.path, path));
|
8365 | }
|
8366 | else {
|
8367 | return null;
|
8368 | }
|
8369 | }
|
8370 |
|
8371 |
|
8372 |
|
8373 |
|
8374 |
|
8375 |
|
8376 | function compoundWriteGetCompleteChildren(compoundWrite) {
|
8377 | var children = [];
|
8378 | var node = compoundWrite.writeTree_.value;
|
8379 | if (node != null) {
|
8380 |
|
8381 | if (!node.isLeafNode()) {
|
8382 | node.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
|
8383 | children.push(new NamedNode(childName, childNode));
|
8384 | });
|
8385 | }
|
8386 | }
|
8387 | else {
|
8388 | compoundWrite.writeTree_.children.inorderTraversal(function (childName, childTree) {
|
8389 | if (childTree.value != null) {
|
8390 | children.push(new NamedNode(childName, childTree.value));
|
8391 | }
|
8392 | });
|
8393 | }
|
8394 | return children;
|
8395 | }
|
8396 | function compoundWriteChildCompoundWrite(compoundWrite, path) {
|
8397 | if (pathIsEmpty(path)) {
|
8398 | return compoundWrite;
|
8399 | }
|
8400 | else {
|
8401 | var shadowingNode = compoundWriteGetCompleteNode(compoundWrite, path);
|
8402 | if (shadowingNode != null) {
|
8403 | return new CompoundWrite(new ImmutableTree(shadowingNode));
|
8404 | }
|
8405 | else {
|
8406 | return new CompoundWrite(compoundWrite.writeTree_.subtree(path));
|
8407 | }
|
8408 | }
|
8409 | }
|
8410 |
|
8411 |
|
8412 |
|
8413 |
|
8414 | function compoundWriteIsEmpty(compoundWrite) {
|
8415 | return compoundWrite.writeTree_.isEmpty();
|
8416 | }
|
8417 |
|
8418 |
|
8419 |
|
8420 |
|
8421 |
|
8422 |
|
8423 | function compoundWriteApply(compoundWrite, node) {
|
8424 | return applySubtreeWrite(newEmptyPath(), compoundWrite.writeTree_, node);
|
8425 | }
|
8426 | function applySubtreeWrite(relativePath, writeTree, node) {
|
8427 | if (writeTree.value != null) {
|
8428 |
|
8429 | return node.updateChild(relativePath, writeTree.value);
|
8430 | }
|
8431 | else {
|
8432 | var priorityWrite_1 = null;
|
8433 | writeTree.children.inorderTraversal(function (childKey, childTree) {
|
8434 | if (childKey === '.priority') {
|
8435 |
|
8436 |
|
8437 | util.assert(childTree.value !== null, 'Priority writes must always be leaf nodes');
|
8438 | priorityWrite_1 = childTree.value;
|
8439 | }
|
8440 | else {
|
8441 | node = applySubtreeWrite(pathChild(relativePath, childKey), childTree, node);
|
8442 | }
|
8443 | });
|
8444 |
|
8445 | if (!node.getChild(relativePath).isEmpty() && priorityWrite_1 !== null) {
|
8446 | node = node.updateChild(pathChild(relativePath, '.priority'), priorityWrite_1);
|
8447 | }
|
8448 | return node;
|
8449 | }
|
8450 | }
|
8451 |
|
8452 |
|
8453 |
|
8454 |
|
8455 |
|
8456 |
|
8457 |
|
8458 |
|
8459 |
|
8460 |
|
8461 |
|
8462 |
|
8463 |
|
8464 |
|
8465 |
|
8466 |
|
8467 |
|
8468 |
|
8469 |
|
8470 |
|
8471 |
|
8472 | function writeTreeChildWrites(writeTree, path) {
|
8473 | return newWriteTreeRef(path, writeTree);
|
8474 | }
|
8475 |
|
8476 |
|
8477 |
|
8478 |
|
8479 |
|
8480 | function writeTreeAddOverwrite(writeTree, path, snap, writeId, visible) {
|
8481 | util.assert(writeId > writeTree.lastWriteId, 'Stacking an older write on top of newer ones');
|
8482 | if (visible === undefined) {
|
8483 | visible = true;
|
8484 | }
|
8485 | writeTree.allWrites.push({
|
8486 | path: path,
|
8487 | snap: snap,
|
8488 | writeId: writeId,
|
8489 | visible: visible
|
8490 | });
|
8491 | if (visible) {
|
8492 | writeTree.visibleWrites = compoundWriteAddWrite(writeTree.visibleWrites, path, snap);
|
8493 | }
|
8494 | writeTree.lastWriteId = writeId;
|
8495 | }
|
8496 |
|
8497 |
|
8498 |
|
8499 | function writeTreeAddMerge(writeTree, path, changedChildren, writeId) {
|
8500 | util.assert(writeId > writeTree.lastWriteId, 'Stacking an older merge on top of newer ones');
|
8501 | writeTree.allWrites.push({
|
8502 | path: path,
|
8503 | children: changedChildren,
|
8504 | writeId: writeId,
|
8505 | visible: true
|
8506 | });
|
8507 | writeTree.visibleWrites = compoundWriteAddWrites(writeTree.visibleWrites, path, changedChildren);
|
8508 | writeTree.lastWriteId = writeId;
|
8509 | }
|
8510 | function writeTreeGetWrite(writeTree, writeId) {
|
8511 | for (var i = 0; i < writeTree.allWrites.length; i++) {
|
8512 | var record = writeTree.allWrites[i];
|
8513 | if (record.writeId === writeId) {
|
8514 | return record;
|
8515 | }
|
8516 | }
|
8517 | return null;
|
8518 | }
|
8519 |
|
8520 |
|
8521 |
|
8522 |
|
8523 |
|
8524 |
|
8525 |
|
8526 | function writeTreeRemoveWrite(writeTree, writeId) {
|
8527 |
|
8528 |
|
8529 |
|
8530 |
|
8531 | var idx = writeTree.allWrites.findIndex(function (s) {
|
8532 | return s.writeId === writeId;
|
8533 | });
|
8534 | util.assert(idx >= 0, 'removeWrite called with nonexistent writeId.');
|
8535 | var writeToRemove = writeTree.allWrites[idx];
|
8536 | writeTree.allWrites.splice(idx, 1);
|
8537 | var removedWriteWasVisible = writeToRemove.visible;
|
8538 | var removedWriteOverlapsWithOtherWrites = false;
|
8539 | var i = writeTree.allWrites.length - 1;
|
8540 | while (removedWriteWasVisible && i >= 0) {
|
8541 | var currentWrite = writeTree.allWrites[i];
|
8542 | if (currentWrite.visible) {
|
8543 | if (i >= idx &&
|
8544 | writeTreeRecordContainsPath_(currentWrite, writeToRemove.path)) {
|
8545 |
|
8546 | removedWriteWasVisible = false;
|
8547 | }
|
8548 | else if (pathContains(writeToRemove.path, currentWrite.path)) {
|
8549 |
|
8550 | removedWriteOverlapsWithOtherWrites = true;
|
8551 | }
|
8552 | }
|
8553 | i--;
|
8554 | }
|
8555 | if (!removedWriteWasVisible) {
|
8556 | return false;
|
8557 | }
|
8558 | else if (removedWriteOverlapsWithOtherWrites) {
|
8559 |
|
8560 | writeTreeResetTree_(writeTree);
|
8561 | return true;
|
8562 | }
|
8563 | else {
|
8564 |
|
8565 | if (writeToRemove.snap) {
|
8566 | writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, writeToRemove.path);
|
8567 | }
|
8568 | else {
|
8569 | var children = writeToRemove.children;
|
8570 | each(children, function (childName) {
|
8571 | writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, pathChild(writeToRemove.path, childName));
|
8572 | });
|
8573 | }
|
8574 | return true;
|
8575 | }
|
8576 | }
|
8577 | function writeTreeRecordContainsPath_(writeRecord, path) {
|
8578 | if (writeRecord.snap) {
|
8579 | return pathContains(writeRecord.path, path);
|
8580 | }
|
8581 | else {
|
8582 | for (var childName in writeRecord.children) {
|
8583 | if (writeRecord.children.hasOwnProperty(childName) &&
|
8584 | pathContains(pathChild(writeRecord.path, childName), path)) {
|
8585 | return true;
|
8586 | }
|
8587 | }
|
8588 | return false;
|
8589 | }
|
8590 | }
|
8591 |
|
8592 |
|
8593 |
|
8594 | function writeTreeResetTree_(writeTree) {
|
8595 | writeTree.visibleWrites = writeTreeLayerTree_(writeTree.allWrites, writeTreeDefaultFilter_, newEmptyPath());
|
8596 | if (writeTree.allWrites.length > 0) {
|
8597 | writeTree.lastWriteId =
|
8598 | writeTree.allWrites[writeTree.allWrites.length - 1].writeId;
|
8599 | }
|
8600 | else {
|
8601 | writeTree.lastWriteId = -1;
|
8602 | }
|
8603 | }
|
8604 |
|
8605 |
|
8606 |
|
8607 | function writeTreeDefaultFilter_(write) {
|
8608 | return write.visible;
|
8609 | }
|
8610 |
|
8611 |
|
8612 |
|
8613 |
|
8614 | function writeTreeLayerTree_(writes, filter, treeRoot) {
|
8615 | var compoundWrite = CompoundWrite.empty();
|
8616 | for (var i = 0; i < writes.length; ++i) {
|
8617 | var write = writes[i];
|
8618 |
|
8619 |
|
8620 |
|
8621 | if (filter(write)) {
|
8622 | var writePath = write.path;
|
8623 | var relativePath = void 0;
|
8624 | if (write.snap) {
|
8625 | if (pathContains(treeRoot, writePath)) {
|
8626 | relativePath = newRelativePath(treeRoot, writePath);
|
8627 | compoundWrite = compoundWriteAddWrite(compoundWrite, relativePath, write.snap);
|
8628 | }
|
8629 | else if (pathContains(writePath, treeRoot)) {
|
8630 | relativePath = newRelativePath(writePath, treeRoot);
|
8631 | compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), write.snap.getChild(relativePath));
|
8632 | }
|
8633 | else ;
|
8634 | }
|
8635 | else if (write.children) {
|
8636 | if (pathContains(treeRoot, writePath)) {
|
8637 | relativePath = newRelativePath(treeRoot, writePath);
|
8638 | compoundWrite = compoundWriteAddWrites(compoundWrite, relativePath, write.children);
|
8639 | }
|
8640 | else if (pathContains(writePath, treeRoot)) {
|
8641 | relativePath = newRelativePath(writePath, treeRoot);
|
8642 | if (pathIsEmpty(relativePath)) {
|
8643 | compoundWrite = compoundWriteAddWrites(compoundWrite, newEmptyPath(), write.children);
|
8644 | }
|
8645 | else {
|
8646 | var child = util.safeGet(write.children, pathGetFront(relativePath));
|
8647 | if (child) {
|
8648 |
|
8649 | var deepNode = child.getChild(pathPopFront(relativePath));
|
8650 | compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), deepNode);
|
8651 | }
|
8652 | }
|
8653 | }
|
8654 | else ;
|
8655 | }
|
8656 | else {
|
8657 | throw util.assertionError('WriteRecord should have .snap or .children');
|
8658 | }
|
8659 | }
|
8660 | }
|
8661 | return compoundWrite;
|
8662 | }
|
8663 |
|
8664 |
|
8665 |
|
8666 |
|
8667 |
|
8668 |
|
8669 |
|
8670 | function writeTreeCalcCompleteEventCache(writeTree, treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
|
8671 | if (!writeIdsToExclude && !includeHiddenWrites) {
|
8672 | var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
|
8673 | if (shadowingNode != null) {
|
8674 | return shadowingNode;
|
8675 | }
|
8676 | else {
|
8677 | var subMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8678 | if (compoundWriteIsEmpty(subMerge)) {
|
8679 | return completeServerCache;
|
8680 | }
|
8681 | else if (completeServerCache == null &&
|
8682 | !compoundWriteHasCompleteWrite(subMerge, newEmptyPath())) {
|
8683 |
|
8684 | return null;
|
8685 | }
|
8686 | else {
|
8687 | var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
|
8688 | return compoundWriteApply(subMerge, layeredCache);
|
8689 | }
|
8690 | }
|
8691 | }
|
8692 | else {
|
8693 | var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8694 | if (!includeHiddenWrites && compoundWriteIsEmpty(merge)) {
|
8695 | return completeServerCache;
|
8696 | }
|
8697 | else {
|
8698 |
|
8699 | if (!includeHiddenWrites &&
|
8700 | completeServerCache == null &&
|
8701 | !compoundWriteHasCompleteWrite(merge, newEmptyPath())) {
|
8702 | return null;
|
8703 | }
|
8704 | else {
|
8705 | var filter = function (write) {
|
8706 | return ((write.visible || includeHiddenWrites) &&
|
8707 | (!writeIdsToExclude ||
|
8708 | !~writeIdsToExclude.indexOf(write.writeId)) &&
|
8709 | (pathContains(write.path, treePath) ||
|
8710 | pathContains(treePath, write.path)));
|
8711 | };
|
8712 | var mergeAtPath = writeTreeLayerTree_(writeTree.allWrites, filter, treePath);
|
8713 | var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
|
8714 | return compoundWriteApply(mergeAtPath, layeredCache);
|
8715 | }
|
8716 | }
|
8717 | }
|
8718 | }
|
8719 |
|
8720 |
|
8721 |
|
8722 |
|
8723 | function writeTreeCalcCompleteEventChildren(writeTree, treePath, completeServerChildren) {
|
8724 | var completeChildren = ChildrenNode.EMPTY_NODE;
|
8725 | var topLevelSet = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
|
8726 | if (topLevelSet) {
|
8727 | if (!topLevelSet.isLeafNode()) {
|
8728 |
|
8729 | topLevelSet.forEachChild(PRIORITY_INDEX, function (childName, childSnap) {
|
8730 | completeChildren = completeChildren.updateImmediateChild(childName, childSnap);
|
8731 | });
|
8732 | }
|
8733 | return completeChildren;
|
8734 | }
|
8735 | else if (completeServerChildren) {
|
8736 |
|
8737 |
|
8738 | var merge_1 = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8739 | completeServerChildren.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
|
8740 | var node = compoundWriteApply(compoundWriteChildCompoundWrite(merge_1, new Path(childName)), childNode);
|
8741 | completeChildren = completeChildren.updateImmediateChild(childName, node);
|
8742 | });
|
8743 |
|
8744 | compoundWriteGetCompleteChildren(merge_1).forEach(function (namedNode) {
|
8745 | completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
|
8746 | });
|
8747 | return completeChildren;
|
8748 | }
|
8749 | else {
|
8750 |
|
8751 |
|
8752 | var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8753 | compoundWriteGetCompleteChildren(merge).forEach(function (namedNode) {
|
8754 | completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
|
8755 | });
|
8756 | return completeChildren;
|
8757 | }
|
8758 | }
|
8759 |
|
8760 |
|
8761 |
|
8762 |
|
8763 |
|
8764 |
|
8765 |
|
8766 |
|
8767 |
|
8768 |
|
8769 |
|
8770 |
|
8771 |
|
8772 |
|
8773 | function writeTreeCalcEventCacheAfterServerOverwrite(writeTree, treePath, childPath, existingEventSnap, existingServerSnap) {
|
8774 | util.assert(existingEventSnap || existingServerSnap, 'Either existingEventSnap or existingServerSnap must exist');
|
8775 | var path = pathChild(treePath, childPath);
|
8776 | if (compoundWriteHasCompleteWrite(writeTree.visibleWrites, path)) {
|
8777 |
|
8778 |
|
8779 | return null;
|
8780 | }
|
8781 | else {
|
8782 |
|
8783 | var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
|
8784 | if (compoundWriteIsEmpty(childMerge)) {
|
8785 |
|
8786 | return existingServerSnap.getChild(childPath);
|
8787 | }
|
8788 | else {
|
8789 |
|
8790 |
|
8791 |
|
8792 |
|
8793 |
|
8794 |
|
8795 | return compoundWriteApply(childMerge, existingServerSnap.getChild(childPath));
|
8796 | }
|
8797 | }
|
8798 | }
|
8799 |
|
8800 |
|
8801 |
|
8802 |
|
8803 | function writeTreeCalcCompleteChild(writeTree, treePath, childKey, existingServerSnap) {
|
8804 | var path = pathChild(treePath, childKey);
|
8805 | var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
|
8806 | if (shadowingNode != null) {
|
8807 | return shadowingNode;
|
8808 | }
|
8809 | else {
|
8810 | if (existingServerSnap.isCompleteForChild(childKey)) {
|
8811 | var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
|
8812 | return compoundWriteApply(childMerge, existingServerSnap.getNode().getImmediateChild(childKey));
|
8813 | }
|
8814 | else {
|
8815 | return null;
|
8816 | }
|
8817 | }
|
8818 | }
|
8819 |
|
8820 |
|
8821 |
|
8822 |
|
8823 |
|
8824 | function writeTreeShadowingWrite(writeTree, path) {
|
8825 | return compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
|
8826 | }
|
8827 |
|
8828 |
|
8829 |
|
8830 |
|
8831 | function writeTreeCalcIndexedSlice(writeTree, treePath, completeServerData, startPost, count, reverse, index) {
|
8832 | var toIterate;
|
8833 | var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
|
8834 | var shadowingNode = compoundWriteGetCompleteNode(merge, newEmptyPath());
|
8835 | if (shadowingNode != null) {
|
8836 | toIterate = shadowingNode;
|
8837 | }
|
8838 | else if (completeServerData != null) {
|
8839 | toIterate = compoundWriteApply(merge, completeServerData);
|
8840 | }
|
8841 | else {
|
8842 |
|
8843 | return [];
|
8844 | }
|
8845 | toIterate = toIterate.withIndex(index);
|
8846 | if (!toIterate.isEmpty() && !toIterate.isLeafNode()) {
|
8847 | var nodes = [];
|
8848 | var cmp = index.getCompare();
|
8849 | var iter = reverse
|
8850 | ? toIterate.getReverseIteratorFrom(startPost, index)
|
8851 | : toIterate.getIteratorFrom(startPost, index);
|
8852 | var next = iter.getNext();
|
8853 | while (next && nodes.length < count) {
|
8854 | if (cmp(next, startPost) !== 0) {
|
8855 | nodes.push(next);
|
8856 | }
|
8857 | next = iter.getNext();
|
8858 | }
|
8859 | return nodes;
|
8860 | }
|
8861 | else {
|
8862 | return [];
|
8863 | }
|
8864 | }
|
8865 | function newWriteTree() {
|
8866 | return {
|
8867 | visibleWrites: CompoundWrite.empty(),
|
8868 | allWrites: [],
|
8869 | lastWriteId: -1
|
8870 | };
|
8871 | }
|
8872 |
|
8873 |
|
8874 |
|
8875 |
|
8876 |
|
8877 |
|
8878 |
|
8879 |
|
8880 | function writeTreeRefCalcCompleteEventCache(writeTreeRef, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
|
8881 | return writeTreeCalcCompleteEventCache(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites);
|
8882 | }
|
8883 |
|
8884 |
|
8885 |
|
8886 |
|
8887 |
|
8888 | function writeTreeRefCalcCompleteEventChildren(writeTreeRef, completeServerChildren) {
|
8889 | return writeTreeCalcCompleteEventChildren(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerChildren);
|
8890 | }
|
8891 |
|
8892 |
|
8893 |
|
8894 |
|
8895 |
|
8896 |
|
8897 |
|
8898 |
|
8899 |
|
8900 |
|
8901 |
|
8902 |
|
8903 |
|
8904 |
|
8905 |
|
8906 |
|
8907 | function writeTreeRefCalcEventCacheAfterServerOverwrite(writeTreeRef, path, existingEventSnap, existingServerSnap) {
|
8908 | return writeTreeCalcEventCacheAfterServerOverwrite(writeTreeRef.writeTree, writeTreeRef.treePath, path, existingEventSnap, existingServerSnap);
|
8909 | }
|
8910 |
|
8911 |
|
8912 |
|
8913 |
|
8914 |
|
8915 |
|
8916 | function writeTreeRefShadowingWrite(writeTreeRef, path) {
|
8917 | return writeTreeShadowingWrite(writeTreeRef.writeTree, pathChild(writeTreeRef.treePath, path));
|
8918 | }
|
8919 |
|
8920 |
|
8921 |
|
8922 |
|
8923 | function writeTreeRefCalcIndexedSlice(writeTreeRef, completeServerData, startPost, count, reverse, index) {
|
8924 | return writeTreeCalcIndexedSlice(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerData, startPost, count, reverse, index);
|
8925 | }
|
8926 |
|
8927 |
|
8928 |
|
8929 |
|
8930 | function writeTreeRefCalcCompleteChild(writeTreeRef, childKey, existingServerCache) {
|
8931 | return writeTreeCalcCompleteChild(writeTreeRef.writeTree, writeTreeRef.treePath, childKey, existingServerCache);
|
8932 | }
|
8933 |
|
8934 |
|
8935 |
|
8936 | function writeTreeRefChild(writeTreeRef, childName) {
|
8937 | return newWriteTreeRef(pathChild(writeTreeRef.treePath, childName), writeTreeRef.writeTree);
|
8938 | }
|
8939 | function newWriteTreeRef(path, writeTree) {
|
8940 | return {
|
8941 | treePath: path,
|
8942 | writeTree: writeTree
|
8943 | };
|
8944 | }
|
8945 |
|
8946 |
|
8947 |
|
8948 |
|
8949 |
|
8950 |
|
8951 |
|
8952 |
|
8953 |
|
8954 |
|
8955 |
|
8956 |
|
8957 |
|
8958 |
|
8959 |
|
8960 |
|
8961 |
|
8962 | var ChildChangeAccumulator = (function () {
|
8963 | function ChildChangeAccumulator() {
|
8964 | this.changeMap = new Map();
|
8965 | }
|
8966 | ChildChangeAccumulator.prototype.trackChildChange = function (change) {
|
8967 | var type = change.type;
|
8968 | var childKey = change.childName;
|
8969 | util.assert(type === "child_added" ||
|
8970 | type === "child_changed" ||
|
8971 | type === "child_removed" , 'Only child changes supported for tracking');
|
8972 | util.assert(childKey !== '.priority', 'Only non-priority child changes can be tracked.');
|
8973 | var oldChange = this.changeMap.get(childKey);
|
8974 | if (oldChange) {
|
8975 | var oldType = oldChange.type;
|
8976 | if (type === "child_added" &&
|
8977 | oldType === "child_removed" ) {
|
8978 | this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.snapshotNode));
|
8979 | }
|
8980 | else if (type === "child_removed" &&
|
8981 | oldType === "child_added" ) {
|
8982 | this.changeMap.delete(childKey);
|
8983 | }
|
8984 | else if (type === "child_removed" &&
|
8985 | oldType === "child_changed" ) {
|
8986 | this.changeMap.set(childKey, changeChildRemoved(childKey, oldChange.oldSnap));
|
8987 | }
|
8988 | else if (type === "child_changed" &&
|
8989 | oldType === "child_added" ) {
|
8990 | this.changeMap.set(childKey, changeChildAdded(childKey, change.snapshotNode));
|
8991 | }
|
8992 | else if (type === "child_changed" &&
|
8993 | oldType === "child_changed" ) {
|
8994 | this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.oldSnap));
|
8995 | }
|
8996 | else {
|
8997 | throw util.assertionError('Illegal combination of changes: ' +
|
8998 | change +
|
8999 | ' occurred after ' +
|
9000 | oldChange);
|
9001 | }
|
9002 | }
|
9003 | else {
|
9004 | this.changeMap.set(childKey, change);
|
9005 | }
|
9006 | };
|
9007 | ChildChangeAccumulator.prototype.getChanges = function () {
|
9008 | return Array.from(this.changeMap.values());
|
9009 | };
|
9010 | return ChildChangeAccumulator;
|
9011 | }());
|
9012 |
|
9013 |
|
9014 |
|
9015 |
|
9016 |
|
9017 |
|
9018 |
|
9019 |
|
9020 |
|
9021 |
|
9022 |
|
9023 |
|
9024 |
|
9025 |
|
9026 |
|
9027 |
|
9028 |
|
9029 |
|
9030 |
|
9031 |
|
9032 |
|
9033 | var NoCompleteChildSource_ = (function () {
|
9034 | function NoCompleteChildSource_() {
|
9035 | }
|
9036 | NoCompleteChildSource_.prototype.getCompleteChild = function (childKey) {
|
9037 | return null;
|
9038 | };
|
9039 | NoCompleteChildSource_.prototype.getChildAfterChild = function (index, child, reverse) {
|
9040 | return null;
|
9041 | };
|
9042 | return NoCompleteChildSource_;
|
9043 | }());
|
9044 |
|
9045 |
|
9046 |
|
9047 | var NO_COMPLETE_CHILD_SOURCE = new NoCompleteChildSource_();
|
9048 |
|
9049 |
|
9050 |
|
9051 |
|
9052 | var WriteTreeCompleteChildSource = (function () {
|
9053 | function WriteTreeCompleteChildSource(writes_, viewCache_, optCompleteServerCache_) {
|
9054 | if (optCompleteServerCache_ === void 0) { optCompleteServerCache_ = null; }
|
9055 | this.writes_ = writes_;
|
9056 | this.viewCache_ = viewCache_;
|
9057 | this.optCompleteServerCache_ = optCompleteServerCache_;
|
9058 | }
|
9059 | WriteTreeCompleteChildSource.prototype.getCompleteChild = function (childKey) {
|
9060 | var node = this.viewCache_.eventCache;
|
9061 | if (node.isCompleteForChild(childKey)) {
|
9062 | return node.getNode().getImmediateChild(childKey);
|
9063 | }
|
9064 | else {
|
9065 | var serverNode = this.optCompleteServerCache_ != null
|
9066 | ? new CacheNode(this.optCompleteServerCache_, true, false)
|
9067 | : this.viewCache_.serverCache;
|
9068 | return writeTreeRefCalcCompleteChild(this.writes_, childKey, serverNode);
|
9069 | }
|
9070 | };
|
9071 | WriteTreeCompleteChildSource.prototype.getChildAfterChild = function (index, child, reverse) {
|
9072 | var completeServerData = this.optCompleteServerCache_ != null
|
9073 | ? this.optCompleteServerCache_
|
9074 | : viewCacheGetCompleteServerSnap(this.viewCache_);
|
9075 | var nodes = writeTreeRefCalcIndexedSlice(this.writes_, completeServerData, child, 1, reverse, index);
|
9076 | if (nodes.length === 0) {
|
9077 | return null;
|
9078 | }
|
9079 | else {
|
9080 | return nodes[0];
|
9081 | }
|
9082 | };
|
9083 | return WriteTreeCompleteChildSource;
|
9084 | }());
|
9085 |
|
9086 |
|
9087 |
|
9088 |
|
9089 |
|
9090 |
|
9091 |
|
9092 |
|
9093 |
|
9094 |
|
9095 |
|
9096 |
|
9097 |
|
9098 |
|
9099 |
|
9100 |
|
9101 |
|
9102 | function newViewProcessor(filter) {
|
9103 | return { filter: filter };
|
9104 | }
|
9105 | function viewProcessorAssertIndexed(viewProcessor, viewCache) {
|
9106 | util.assert(viewCache.eventCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Event snap not indexed');
|
9107 | util.assert(viewCache.serverCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Server snap not indexed');
|
9108 | }
|
9109 | function viewProcessorApplyOperation(viewProcessor, oldViewCache, operation, writesCache, completeCache) {
|
9110 | var accumulator = new ChildChangeAccumulator();
|
9111 | var newViewCache, filterServerNode;
|
9112 | if (operation.type === OperationType.OVERWRITE) {
|
9113 | var overwrite = operation;
|
9114 | if (overwrite.source.fromUser) {
|
9115 | newViewCache = viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, accumulator);
|
9116 | }
|
9117 | else {
|
9118 | util.assert(overwrite.source.fromServer, 'Unknown source.');
|
9119 |
|
9120 |
|
9121 |
|
9122 | filterServerNode =
|
9123 | overwrite.source.tagged ||
|
9124 | (oldViewCache.serverCache.isFiltered() && !pathIsEmpty(overwrite.path));
|
9125 | newViewCache = viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, filterServerNode, accumulator);
|
9126 | }
|
9127 | }
|
9128 | else if (operation.type === OperationType.MERGE) {
|
9129 | var merge = operation;
|
9130 | if (merge.source.fromUser) {
|
9131 | newViewCache = viewProcessorApplyUserMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, accumulator);
|
9132 | }
|
9133 | else {
|
9134 | util.assert(merge.source.fromServer, 'Unknown source.');
|
9135 |
|
9136 | filterServerNode =
|
9137 | merge.source.tagged || oldViewCache.serverCache.isFiltered();
|
9138 | newViewCache = viewProcessorApplyServerMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, filterServerNode, accumulator);
|
9139 | }
|
9140 | }
|
9141 | else if (operation.type === OperationType.ACK_USER_WRITE) {
|
9142 | var ackUserWrite = operation;
|
9143 | if (!ackUserWrite.revert) {
|
9144 | newViewCache = viewProcessorAckUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, ackUserWrite.affectedTree, writesCache, completeCache, accumulator);
|
9145 | }
|
9146 | else {
|
9147 | newViewCache = viewProcessorRevertUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, writesCache, completeCache, accumulator);
|
9148 | }
|
9149 | }
|
9150 | else if (operation.type === OperationType.LISTEN_COMPLETE) {
|
9151 | newViewCache = viewProcessorListenComplete(viewProcessor, oldViewCache, operation.path, writesCache, accumulator);
|
9152 | }
|
9153 | else {
|
9154 | throw util.assertionError('Unknown operation type: ' + operation.type);
|
9155 | }
|
9156 | var changes = accumulator.getChanges();
|
9157 | viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, changes);
|
9158 | return { viewCache: newViewCache, changes: changes };
|
9159 | }
|
9160 | function viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, accumulator) {
|
9161 | var eventSnap = newViewCache.eventCache;
|
9162 | if (eventSnap.isFullyInitialized()) {
|
9163 | var isLeafOrEmpty = eventSnap.getNode().isLeafNode() || eventSnap.getNode().isEmpty();
|
9164 | var oldCompleteSnap = viewCacheGetCompleteEventSnap(oldViewCache);
|
9165 | if (accumulator.length > 0 ||
|
9166 | !oldViewCache.eventCache.isFullyInitialized() ||
|
9167 | (isLeafOrEmpty && !eventSnap.getNode().equals(oldCompleteSnap)) ||
|
9168 | !eventSnap.getNode().getPriority().equals(oldCompleteSnap.getPriority())) {
|
9169 | accumulator.push(changeValue(viewCacheGetCompleteEventSnap(newViewCache)));
|
9170 | }
|
9171 | }
|
9172 | }
|
9173 | function viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, viewCache, changePath, writesCache, source, accumulator) {
|
9174 | var oldEventSnap = viewCache.eventCache;
|
9175 | if (writeTreeRefShadowingWrite(writesCache, changePath) != null) {
|
9176 |
|
9177 | return viewCache;
|
9178 | }
|
9179 | else {
|
9180 | var newEventCache = void 0, serverNode = void 0;
|
9181 | if (pathIsEmpty(changePath)) {
|
9182 |
|
9183 | util.assert(viewCache.serverCache.isFullyInitialized(), 'If change path is empty, we must have complete server data');
|
9184 | if (viewCache.serverCache.isFiltered()) {
|
9185 |
|
9186 |
|
9187 |
|
9188 | var serverCache = viewCacheGetCompleteServerSnap(viewCache);
|
9189 | var completeChildren = serverCache instanceof ChildrenNode
|
9190 | ? serverCache
|
9191 | : ChildrenNode.EMPTY_NODE;
|
9192 | var completeEventChildren = writeTreeRefCalcCompleteEventChildren(writesCache, completeChildren);
|
9193 | newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeEventChildren, accumulator);
|
9194 | }
|
9195 | else {
|
9196 | var completeNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
|
9197 | newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeNode, accumulator);
|
9198 | }
|
9199 | }
|
9200 | else {
|
9201 | var childKey = pathGetFront(changePath);
|
9202 | if (childKey === '.priority') {
|
9203 | util.assert(pathGetLength(changePath) === 1, "Can't have a priority with additional path components");
|
9204 | var oldEventNode = oldEventSnap.getNode();
|
9205 | serverNode = viewCache.serverCache.getNode();
|
9206 |
|
9207 | var updatedPriority = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventNode, serverNode);
|
9208 | if (updatedPriority != null) {
|
9209 | newEventCache = viewProcessor.filter.updatePriority(oldEventNode, updatedPriority);
|
9210 | }
|
9211 | else {
|
9212 |
|
9213 | newEventCache = oldEventSnap.getNode();
|
9214 | }
|
9215 | }
|
9216 | else {
|
9217 | var childChangePath = pathPopFront(changePath);
|
9218 |
|
9219 | var newEventChild = void 0;
|
9220 | if (oldEventSnap.isCompleteForChild(childKey)) {
|
9221 | serverNode = viewCache.serverCache.getNode();
|
9222 | var eventChildUpdate = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventSnap.getNode(), serverNode);
|
9223 | if (eventChildUpdate != null) {
|
9224 | newEventChild = oldEventSnap
|
9225 | .getNode()
|
9226 | .getImmediateChild(childKey)
|
9227 | .updateChild(childChangePath, eventChildUpdate);
|
9228 | }
|
9229 | else {
|
9230 |
|
9231 | newEventChild = oldEventSnap.getNode().getImmediateChild(childKey);
|
9232 | }
|
9233 | }
|
9234 | else {
|
9235 | newEventChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
|
9236 | }
|
9237 | if (newEventChild != null) {
|
9238 | newEventCache = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newEventChild, childChangePath, source, accumulator);
|
9239 | }
|
9240 | else {
|
9241 |
|
9242 | newEventCache = oldEventSnap.getNode();
|
9243 | }
|
9244 | }
|
9245 | }
|
9246 | return viewCacheUpdateEventSnap(viewCache, newEventCache, oldEventSnap.isFullyInitialized() || pathIsEmpty(changePath), viewProcessor.filter.filtersNodes());
|
9247 | }
|
9248 | }
|
9249 | function viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, filterServerNode, accumulator) {
|
9250 | var oldServerSnap = oldViewCache.serverCache;
|
9251 | var newServerCache;
|
9252 | var serverFilter = filterServerNode
|
9253 | ? viewProcessor.filter
|
9254 | : viewProcessor.filter.getIndexedFilter();
|
9255 | if (pathIsEmpty(changePath)) {
|
9256 | newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), changedSnap, null);
|
9257 | }
|
9258 | else if (serverFilter.filtersNodes() && !oldServerSnap.isFiltered()) {
|
9259 |
|
9260 | var newServerNode = oldServerSnap
|
9261 | .getNode()
|
9262 | .updateChild(changePath, changedSnap);
|
9263 | newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), newServerNode, null);
|
9264 | }
|
9265 | else {
|
9266 | var childKey = pathGetFront(changePath);
|
9267 | if (!oldServerSnap.isCompleteForPath(changePath) &&
|
9268 | pathGetLength(changePath) > 1) {
|
9269 |
|
9270 | return oldViewCache;
|
9271 | }
|
9272 | var childChangePath = pathPopFront(changePath);
|
9273 | var childNode = oldServerSnap.getNode().getImmediateChild(childKey);
|
9274 | var newChildNode = childNode.updateChild(childChangePath, changedSnap);
|
9275 | if (childKey === '.priority') {
|
9276 | newServerCache = serverFilter.updatePriority(oldServerSnap.getNode(), newChildNode);
|
9277 | }
|
9278 | else {
|
9279 | newServerCache = serverFilter.updateChild(oldServerSnap.getNode(), childKey, newChildNode, childChangePath, NO_COMPLETE_CHILD_SOURCE, null);
|
9280 | }
|
9281 | }
|
9282 | var newViewCache = viewCacheUpdateServerSnap(oldViewCache, newServerCache, oldServerSnap.isFullyInitialized() || pathIsEmpty(changePath), serverFilter.filtersNodes());
|
9283 | var source = new WriteTreeCompleteChildSource(writesCache, newViewCache, completeCache);
|
9284 | return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, changePath, writesCache, source, accumulator);
|
9285 | }
|
9286 | function viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, accumulator) {
|
9287 | var oldEventSnap = oldViewCache.eventCache;
|
9288 | var newViewCache, newEventCache;
|
9289 | var source = new WriteTreeCompleteChildSource(writesCache, oldViewCache, completeCache);
|
9290 | if (pathIsEmpty(changePath)) {
|
9291 | newEventCache = viewProcessor.filter.updateFullNode(oldViewCache.eventCache.getNode(), changedSnap, accumulator);
|
9292 | newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, true, viewProcessor.filter.filtersNodes());
|
9293 | }
|
9294 | else {
|
9295 | var childKey = pathGetFront(changePath);
|
9296 | if (childKey === '.priority') {
|
9297 | newEventCache = viewProcessor.filter.updatePriority(oldViewCache.eventCache.getNode(), changedSnap);
|
9298 | newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, oldEventSnap.isFullyInitialized(), oldEventSnap.isFiltered());
|
9299 | }
|
9300 | else {
|
9301 | var childChangePath = pathPopFront(changePath);
|
9302 | var oldChild = oldEventSnap.getNode().getImmediateChild(childKey);
|
9303 | var newChild = void 0;
|
9304 | if (pathIsEmpty(childChangePath)) {
|
9305 |
|
9306 | newChild = changedSnap;
|
9307 | }
|
9308 | else {
|
9309 | var childNode = source.getCompleteChild(childKey);
|
9310 | if (childNode != null) {
|
9311 | if (pathGetBack(childChangePath) === '.priority' &&
|
9312 | childNode.getChild(pathParent(childChangePath)).isEmpty()) {
|
9313 |
|
9314 |
|
9315 | newChild = childNode;
|
9316 | }
|
9317 | else {
|
9318 | newChild = childNode.updateChild(childChangePath, changedSnap);
|
9319 | }
|
9320 | }
|
9321 | else {
|
9322 |
|
9323 | newChild = ChildrenNode.EMPTY_NODE;
|
9324 | }
|
9325 | }
|
9326 | if (!oldChild.equals(newChild)) {
|
9327 | var newEventSnap = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newChild, childChangePath, source, accumulator);
|
9328 | newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventSnap, oldEventSnap.isFullyInitialized(), viewProcessor.filter.filtersNodes());
|
9329 | }
|
9330 | else {
|
9331 | newViewCache = oldViewCache;
|
9332 | }
|
9333 | }
|
9334 | }
|
9335 | return newViewCache;
|
9336 | }
|
9337 | function viewProcessorCacheHasChild(viewCache, childKey) {
|
9338 | return viewCache.eventCache.isCompleteForChild(childKey);
|
9339 | }
|
9340 | function viewProcessorApplyUserMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, accumulator) {
|
9341 |
|
9342 |
|
9343 |
|
9344 |
|
9345 |
|
9346 |
|
9347 | var curViewCache = viewCache;
|
9348 | changedChildren.foreach(function (relativePath, childNode) {
|
9349 | var writePath = pathChild(path, relativePath);
|
9350 | if (viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
|
9351 | curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
|
9352 | }
|
9353 | });
|
9354 | changedChildren.foreach(function (relativePath, childNode) {
|
9355 | var writePath = pathChild(path, relativePath);
|
9356 | if (!viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
|
9357 | curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
|
9358 | }
|
9359 | });
|
9360 | return curViewCache;
|
9361 | }
|
9362 | function viewProcessorApplyMerge(viewProcessor, node, merge) {
|
9363 | merge.foreach(function (relativePath, childNode) {
|
9364 | node = node.updateChild(relativePath, childNode);
|
9365 | });
|
9366 | return node;
|
9367 | }
|
9368 | function viewProcessorApplyServerMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, filterServerNode, accumulator) {
|
9369 |
|
9370 |
|
9371 | if (viewCache.serverCache.getNode().isEmpty() &&
|
9372 | !viewCache.serverCache.isFullyInitialized()) {
|
9373 | return viewCache;
|
9374 | }
|
9375 |
|
9376 |
|
9377 |
|
9378 |
|
9379 |
|
9380 |
|
9381 | var curViewCache = viewCache;
|
9382 | var viewMergeTree;
|
9383 | if (pathIsEmpty(path)) {
|
9384 | viewMergeTree = changedChildren;
|
9385 | }
|
9386 | else {
|
9387 | viewMergeTree = new ImmutableTree(null).setTree(path, changedChildren);
|
9388 | }
|
9389 | var serverNode = viewCache.serverCache.getNode();
|
9390 | viewMergeTree.children.inorderTraversal(function (childKey, childTree) {
|
9391 | if (serverNode.hasChild(childKey)) {
|
9392 | var serverChild = viewCache.serverCache
|
9393 | .getNode()
|
9394 | .getImmediateChild(childKey);
|
9395 | var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childTree);
|
9396 | curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
|
9397 | }
|
9398 | });
|
9399 | viewMergeTree.children.inorderTraversal(function (childKey, childMergeTree) {
|
9400 | var isUnknownDeepMerge = !viewCache.serverCache.isCompleteForChild(childKey) &&
|
9401 | childMergeTree.value === undefined;
|
9402 | if (!serverNode.hasChild(childKey) && !isUnknownDeepMerge) {
|
9403 | var serverChild = viewCache.serverCache
|
9404 | .getNode()
|
9405 | .getImmediateChild(childKey);
|
9406 | var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childMergeTree);
|
9407 | curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
|
9408 | }
|
9409 | });
|
9410 | return curViewCache;
|
9411 | }
|
9412 | function viewProcessorAckUserWrite(viewProcessor, viewCache, ackPath, affectedTree, writesCache, completeCache, accumulator) {
|
9413 | if (writeTreeRefShadowingWrite(writesCache, ackPath) != null) {
|
9414 | return viewCache;
|
9415 | }
|
9416 |
|
9417 | var filterServerNode = viewCache.serverCache.isFiltered();
|
9418 |
|
9419 |
|
9420 | var serverCache = viewCache.serverCache;
|
9421 | if (affectedTree.value != null) {
|
9422 |
|
9423 | if ((pathIsEmpty(ackPath) && serverCache.isFullyInitialized()) ||
|
9424 | serverCache.isCompleteForPath(ackPath)) {
|
9425 | return viewProcessorApplyServerOverwrite(viewProcessor, viewCache, ackPath, serverCache.getNode().getChild(ackPath), writesCache, completeCache, filterServerNode, accumulator);
|
9426 | }
|
9427 | else if (pathIsEmpty(ackPath)) {
|
9428 |
|
9429 |
|
9430 | var changedChildren_1 = new ImmutableTree(null);
|
9431 | serverCache.getNode().forEachChild(KEY_INDEX, function (name, node) {
|
9432 | changedChildren_1 = changedChildren_1.set(new Path(name), node);
|
9433 | });
|
9434 | return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_1, writesCache, completeCache, filterServerNode, accumulator);
|
9435 | }
|
9436 | else {
|
9437 | return viewCache;
|
9438 | }
|
9439 | }
|
9440 | else {
|
9441 |
|
9442 | var changedChildren_2 = new ImmutableTree(null);
|
9443 | affectedTree.foreach(function (mergePath, value) {
|
9444 | var serverCachePath = pathChild(ackPath, mergePath);
|
9445 | if (serverCache.isCompleteForPath(serverCachePath)) {
|
9446 | changedChildren_2 = changedChildren_2.set(mergePath, serverCache.getNode().getChild(serverCachePath));
|
9447 | }
|
9448 | });
|
9449 | return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_2, writesCache, completeCache, filterServerNode, accumulator);
|
9450 | }
|
9451 | }
|
9452 | function viewProcessorListenComplete(viewProcessor, viewCache, path, writesCache, accumulator) {
|
9453 | var oldServerNode = viewCache.serverCache;
|
9454 | var newViewCache = viewCacheUpdateServerSnap(viewCache, oldServerNode.getNode(), oldServerNode.isFullyInitialized() || pathIsEmpty(path), oldServerNode.isFiltered());
|
9455 | return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, path, writesCache, NO_COMPLETE_CHILD_SOURCE, accumulator);
|
9456 | }
|
9457 | function viewProcessorRevertUserWrite(viewProcessor, viewCache, path, writesCache, completeServerCache, accumulator) {
|
9458 | var complete;
|
9459 | if (writeTreeRefShadowingWrite(writesCache, path) != null) {
|
9460 | return viewCache;
|
9461 | }
|
9462 | else {
|
9463 | var source = new WriteTreeCompleteChildSource(writesCache, viewCache, completeServerCache);
|
9464 | var oldEventCache = viewCache.eventCache.getNode();
|
9465 | var newEventCache = void 0;
|
9466 | if (pathIsEmpty(path) || pathGetFront(path) === '.priority') {
|
9467 | var newNode = void 0;
|
9468 | if (viewCache.serverCache.isFullyInitialized()) {
|
9469 | newNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
|
9470 | }
|
9471 | else {
|
9472 | var serverChildren = viewCache.serverCache.getNode();
|
9473 | util.assert(serverChildren instanceof ChildrenNode, 'serverChildren would be complete if leaf node');
|
9474 | newNode = writeTreeRefCalcCompleteEventChildren(writesCache, serverChildren);
|
9475 | }
|
9476 | newNode = newNode;
|
9477 | newEventCache = viewProcessor.filter.updateFullNode(oldEventCache, newNode, accumulator);
|
9478 | }
|
9479 | else {
|
9480 | var childKey = pathGetFront(path);
|
9481 | var newChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
|
9482 | if (newChild == null &&
|
9483 | viewCache.serverCache.isCompleteForChild(childKey)) {
|
9484 | newChild = oldEventCache.getImmediateChild(childKey);
|
9485 | }
|
9486 | if (newChild != null) {
|
9487 | newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, newChild, pathPopFront(path), source, accumulator);
|
9488 | }
|
9489 | else if (viewCache.eventCache.getNode().hasChild(childKey)) {
|
9490 |
|
9491 | newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, ChildrenNode.EMPTY_NODE, pathPopFront(path), source, accumulator);
|
9492 | }
|
9493 | else {
|
9494 | newEventCache = oldEventCache;
|
9495 | }
|
9496 | if (newEventCache.isEmpty() &&
|
9497 | viewCache.serverCache.isFullyInitialized()) {
|
9498 |
|
9499 | complete = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
|
9500 | if (complete.isLeafNode()) {
|
9501 | newEventCache = viewProcessor.filter.updateFullNode(newEventCache, complete, accumulator);
|
9502 | }
|
9503 | }
|
9504 | }
|
9505 | complete =
|
9506 | viewCache.serverCache.isFullyInitialized() ||
|
9507 | writeTreeRefShadowingWrite(writesCache, newEmptyPath()) != null;
|
9508 | return viewCacheUpdateEventSnap(viewCache, newEventCache, complete, viewProcessor.filter.filtersNodes());
|
9509 | }
|
9510 | }
|
9511 |
|
9512 |
|
9513 |
|
9514 |
|
9515 |
|
9516 |
|
9517 |
|
9518 |
|
9519 |
|
9520 |
|
9521 |
|
9522 |
|
9523 |
|
9524 |
|
9525 |
|
9526 |
|
9527 |
|
9528 |
|
9529 |
|
9530 |
|
9531 |
|
9532 |
|
9533 |
|
9534 |
|
9535 |
|
9536 |
|
9537 | var View = (function () {
|
9538 | function View(query_, initialViewCache) {
|
9539 | this.query_ = query_;
|
9540 | this.eventRegistrations_ = [];
|
9541 | var params = this.query_._queryParams;
|
9542 | var indexFilter = new IndexedFilter(params.getIndex());
|
9543 | var filter = queryParamsGetNodeFilter(params);
|
9544 | this.processor_ = newViewProcessor(filter);
|
9545 | var initialServerCache = initialViewCache.serverCache;
|
9546 | var initialEventCache = initialViewCache.eventCache;
|
9547 |
|
9548 | var serverSnap = indexFilter.updateFullNode(ChildrenNode.EMPTY_NODE, initialServerCache.getNode(), null);
|
9549 | var eventSnap = filter.updateFullNode(ChildrenNode.EMPTY_NODE, initialEventCache.getNode(), null);
|
9550 | var newServerCache = new CacheNode(serverSnap, initialServerCache.isFullyInitialized(), indexFilter.filtersNodes());
|
9551 | var newEventCache = new CacheNode(eventSnap, initialEventCache.isFullyInitialized(), filter.filtersNodes());
|
9552 | this.viewCache_ = newViewCache(newEventCache, newServerCache);
|
9553 | this.eventGenerator_ = new EventGenerator(this.query_);
|
9554 | }
|
9555 | Object.defineProperty(View.prototype, "query", {
|
9556 | get: function () {
|
9557 | return this.query_;
|
9558 | },
|
9559 | enumerable: false,
|
9560 | configurable: true
|
9561 | });
|
9562 | return View;
|
9563 | }());
|
9564 | function viewGetServerCache(view) {
|
9565 | return view.viewCache_.serverCache.getNode();
|
9566 | }
|
9567 | function viewGetCompleteNode(view) {
|
9568 | return viewCacheGetCompleteEventSnap(view.viewCache_);
|
9569 | }
|
9570 | function viewGetCompleteServerCache(view, path) {
|
9571 | var cache = viewCacheGetCompleteServerSnap(view.viewCache_);
|
9572 | if (cache) {
|
9573 |
|
9574 |
|
9575 | if (view.query._queryParams.loadsAllData() ||
|
9576 | (!pathIsEmpty(path) &&
|
9577 | !cache.getImmediateChild(pathGetFront(path)).isEmpty())) {
|
9578 | return cache.getChild(path);
|
9579 | }
|
9580 | }
|
9581 | return null;
|
9582 | }
|
9583 | function viewIsEmpty(view) {
|
9584 | return view.eventRegistrations_.length === 0;
|
9585 | }
|
9586 | function viewAddEventRegistration(view, eventRegistration) {
|
9587 | view.eventRegistrations_.push(eventRegistration);
|
9588 | }
|
9589 |
|
9590 |
|
9591 |
|
9592 |
|
9593 |
|
9594 | function viewRemoveEventRegistration(view, eventRegistration, cancelError) {
|
9595 | var cancelEvents = [];
|
9596 | if (cancelError) {
|
9597 | util.assert(eventRegistration == null, 'A cancel should cancel all event registrations.');
|
9598 | var path_1 = view.query._path;
|
9599 | view.eventRegistrations_.forEach(function (registration) {
|
9600 | var maybeEvent = registration.createCancelEvent(cancelError, path_1);
|
9601 | if (maybeEvent) {
|
9602 | cancelEvents.push(maybeEvent);
|
9603 | }
|
9604 | });
|
9605 | }
|
9606 | if (eventRegistration) {
|
9607 | var remaining = [];
|
9608 | for (var i = 0; i < view.eventRegistrations_.length; ++i) {
|
9609 | var existing = view.eventRegistrations_[i];
|
9610 | if (!existing.matches(eventRegistration)) {
|
9611 | remaining.push(existing);
|
9612 | }
|
9613 | else if (eventRegistration.hasAnyCallback()) {
|
9614 |
|
9615 | remaining = remaining.concat(view.eventRegistrations_.slice(i + 1));
|
9616 | break;
|
9617 | }
|
9618 | }
|
9619 | view.eventRegistrations_ = remaining;
|
9620 | }
|
9621 | else {
|
9622 | view.eventRegistrations_ = [];
|
9623 | }
|
9624 | return cancelEvents;
|
9625 | }
|
9626 |
|
9627 |
|
9628 |
|
9629 | function viewApplyOperation(view, operation, writesCache, completeServerCache) {
|
9630 | if (operation.type === OperationType.MERGE &&
|
9631 | operation.source.queryId !== null) {
|
9632 | util.assert(viewCacheGetCompleteServerSnap(view.viewCache_), 'We should always have a full cache before handling merges');
|
9633 | util.assert(viewCacheGetCompleteEventSnap(view.viewCache_), 'Missing event cache, even though we have a server cache');
|
9634 | }
|
9635 | var oldViewCache = view.viewCache_;
|
9636 | var result = viewProcessorApplyOperation(view.processor_, oldViewCache, operation, writesCache, completeServerCache);
|
9637 | viewProcessorAssertIndexed(view.processor_, result.viewCache);
|
9638 | util.assert(result.viewCache.serverCache.isFullyInitialized() ||
|
9639 | !oldViewCache.serverCache.isFullyInitialized(), 'Once a server snap is complete, it should never go back');
|
9640 | view.viewCache_ = result.viewCache;
|
9641 | return viewGenerateEventsForChanges_(view, result.changes, result.viewCache.eventCache.getNode(), null);
|
9642 | }
|
9643 | function viewGetInitialEvents(view, registration) {
|
9644 | var eventSnap = view.viewCache_.eventCache;
|
9645 | var initialChanges = [];
|
9646 | if (!eventSnap.getNode().isLeafNode()) {
|
9647 | var eventNode = eventSnap.getNode();
|
9648 | eventNode.forEachChild(PRIORITY_INDEX, function (key, childNode) {
|
9649 | initialChanges.push(changeChildAdded(key, childNode));
|
9650 | });
|
9651 | }
|
9652 | if (eventSnap.isFullyInitialized()) {
|
9653 | initialChanges.push(changeValue(eventSnap.getNode()));
|
9654 | }
|
9655 | return viewGenerateEventsForChanges_(view, initialChanges, eventSnap.getNode(), registration);
|
9656 | }
|
9657 | function viewGenerateEventsForChanges_(view, changes, eventCache, eventRegistration) {
|
9658 | var registrations = eventRegistration
|
9659 | ? [eventRegistration]
|
9660 | : view.eventRegistrations_;
|
9661 | return eventGeneratorGenerateEventsForChanges(view.eventGenerator_, changes, eventCache, registrations);
|
9662 | }
|
9663 |
|
9664 |
|
9665 |
|
9666 |
|
9667 |
|
9668 |
|
9669 |
|
9670 |
|
9671 |
|
9672 |
|
9673 |
|
9674 |
|
9675 |
|
9676 |
|
9677 |
|
9678 |
|
9679 |
|
9680 | var referenceConstructor;
|
9681 |
|
9682 |
|
9683 |
|
9684 |
|
9685 |
|
9686 |
|
9687 |
|
9688 |
|
9689 |
|
9690 |
|
9691 | var SyncPoint = (function () {
|
9692 | function SyncPoint() {
|
9693 | |
9694 |
|
9695 |
|
9696 |
|
9697 |
|
9698 |
|
9699 | this.views = new Map();
|
9700 | }
|
9701 | return SyncPoint;
|
9702 | }());
|
9703 | function syncPointSetReferenceConstructor(val) {
|
9704 | util.assert(!referenceConstructor, '__referenceConstructor has already been defined');
|
9705 | referenceConstructor = val;
|
9706 | }
|
9707 | function syncPointGetReferenceConstructor() {
|
9708 | util.assert(referenceConstructor, 'Reference.ts has not been loaded');
|
9709 | return referenceConstructor;
|
9710 | }
|
9711 | function syncPointIsEmpty(syncPoint) {
|
9712 | return syncPoint.views.size === 0;
|
9713 | }
|
9714 | function syncPointApplyOperation(syncPoint, operation, writesCache, optCompleteServerCache) {
|
9715 | var e_1, _a;
|
9716 | var queryId = operation.source.queryId;
|
9717 | if (queryId !== null) {
|
9718 | var view = syncPoint.views.get(queryId);
|
9719 | util.assert(view != null, 'SyncTree gave us an op for an invalid query.');
|
9720 | return viewApplyOperation(view, operation, writesCache, optCompleteServerCache);
|
9721 | }
|
9722 | else {
|
9723 | var events = [];
|
9724 | try {
|
9725 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9726 | var view = _c.value;
|
9727 | events = events.concat(viewApplyOperation(view, operation, writesCache, optCompleteServerCache));
|
9728 | }
|
9729 | }
|
9730 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
9731 | finally {
|
9732 | try {
|
9733 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9734 | }
|
9735 | finally { if (e_1) throw e_1.error; }
|
9736 | }
|
9737 | return events;
|
9738 | }
|
9739 | }
|
9740 |
|
9741 |
|
9742 |
|
9743 |
|
9744 |
|
9745 |
|
9746 |
|
9747 |
|
9748 |
|
9749 | function syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete) {
|
9750 | var queryId = query._queryIdentifier;
|
9751 | var view = syncPoint.views.get(queryId);
|
9752 | if (!view) {
|
9753 |
|
9754 | var eventCache = writeTreeRefCalcCompleteEventCache(writesCache, serverCacheComplete ? serverCache : null);
|
9755 | var eventCacheComplete = false;
|
9756 | if (eventCache) {
|
9757 | eventCacheComplete = true;
|
9758 | }
|
9759 | else if (serverCache instanceof ChildrenNode) {
|
9760 | eventCache = writeTreeRefCalcCompleteEventChildren(writesCache, serverCache);
|
9761 | eventCacheComplete = false;
|
9762 | }
|
9763 | else {
|
9764 | eventCache = ChildrenNode.EMPTY_NODE;
|
9765 | eventCacheComplete = false;
|
9766 | }
|
9767 | var viewCache = newViewCache(new CacheNode(eventCache, eventCacheComplete, false), new CacheNode(serverCache, serverCacheComplete, false));
|
9768 | return new View(query, viewCache);
|
9769 | }
|
9770 | return view;
|
9771 | }
|
9772 |
|
9773 |
|
9774 |
|
9775 |
|
9776 |
|
9777 |
|
9778 |
|
9779 |
|
9780 |
|
9781 |
|
9782 | function syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete) {
|
9783 | var view = syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete);
|
9784 | if (!syncPoint.views.has(query._queryIdentifier)) {
|
9785 | syncPoint.views.set(query._queryIdentifier, view);
|
9786 | }
|
9787 |
|
9788 | viewAddEventRegistration(view, eventRegistration);
|
9789 | return viewGetInitialEvents(view, eventRegistration);
|
9790 | }
|
9791 |
|
9792 |
|
9793 |
|
9794 |
|
9795 |
|
9796 |
|
9797 |
|
9798 |
|
9799 |
|
9800 |
|
9801 | function syncPointRemoveEventRegistration(syncPoint, query, eventRegistration, cancelError) {
|
9802 | var e_2, _a;
|
9803 | var queryId = query._queryIdentifier;
|
9804 | var removed = [];
|
9805 | var cancelEvents = [];
|
9806 | var hadCompleteView = syncPointHasCompleteView(syncPoint);
|
9807 | if (queryId === 'default') {
|
9808 | try {
|
9809 |
|
9810 | for (var _b = tslib.__values(syncPoint.views.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9811 | var _d = tslib.__read(_c.value, 2), viewQueryId = _d[0], view = _d[1];
|
9812 | cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
|
9813 | if (viewIsEmpty(view)) {
|
9814 | syncPoint.views.delete(viewQueryId);
|
9815 |
|
9816 | if (!view.query._queryParams.loadsAllData()) {
|
9817 | removed.push(view.query);
|
9818 | }
|
9819 | }
|
9820 | }
|
9821 | }
|
9822 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
9823 | finally {
|
9824 | try {
|
9825 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9826 | }
|
9827 | finally { if (e_2) throw e_2.error; }
|
9828 | }
|
9829 | }
|
9830 | else {
|
9831 |
|
9832 | var view = syncPoint.views.get(queryId);
|
9833 | if (view) {
|
9834 | cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
|
9835 | if (viewIsEmpty(view)) {
|
9836 | syncPoint.views.delete(queryId);
|
9837 |
|
9838 | if (!view.query._queryParams.loadsAllData()) {
|
9839 | removed.push(view.query);
|
9840 | }
|
9841 | }
|
9842 | }
|
9843 | }
|
9844 | if (hadCompleteView && !syncPointHasCompleteView(syncPoint)) {
|
9845 |
|
9846 | removed.push(new (syncPointGetReferenceConstructor())(query._repo, query._path));
|
9847 | }
|
9848 | return { removed: removed, events: cancelEvents };
|
9849 | }
|
9850 | function syncPointGetQueryViews(syncPoint) {
|
9851 | var e_3, _a;
|
9852 | var result = [];
|
9853 | try {
|
9854 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9855 | var view = _c.value;
|
9856 | if (!view.query._queryParams.loadsAllData()) {
|
9857 | result.push(view);
|
9858 | }
|
9859 | }
|
9860 | }
|
9861 | catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
9862 | finally {
|
9863 | try {
|
9864 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9865 | }
|
9866 | finally { if (e_3) throw e_3.error; }
|
9867 | }
|
9868 | return result;
|
9869 | }
|
9870 |
|
9871 |
|
9872 |
|
9873 |
|
9874 | function syncPointGetCompleteServerCache(syncPoint, path) {
|
9875 | var e_4, _a;
|
9876 | var serverCache = null;
|
9877 | try {
|
9878 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9879 | var view = _c.value;
|
9880 | serverCache = serverCache || viewGetCompleteServerCache(view, path);
|
9881 | }
|
9882 | }
|
9883 | catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
9884 | finally {
|
9885 | try {
|
9886 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9887 | }
|
9888 | finally { if (e_4) throw e_4.error; }
|
9889 | }
|
9890 | return serverCache;
|
9891 | }
|
9892 | function syncPointViewForQuery(syncPoint, query) {
|
9893 | var params = query._queryParams;
|
9894 | if (params.loadsAllData()) {
|
9895 | return syncPointGetCompleteView(syncPoint);
|
9896 | }
|
9897 | else {
|
9898 | var queryId = query._queryIdentifier;
|
9899 | return syncPoint.views.get(queryId);
|
9900 | }
|
9901 | }
|
9902 | function syncPointViewExistsForQuery(syncPoint, query) {
|
9903 | return syncPointViewForQuery(syncPoint, query) != null;
|
9904 | }
|
9905 | function syncPointHasCompleteView(syncPoint) {
|
9906 | return syncPointGetCompleteView(syncPoint) != null;
|
9907 | }
|
9908 | function syncPointGetCompleteView(syncPoint) {
|
9909 | var e_5, _a;
|
9910 | try {
|
9911 | for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
9912 | var view = _c.value;
|
9913 | if (view.query._queryParams.loadsAllData()) {
|
9914 | return view;
|
9915 | }
|
9916 | }
|
9917 | }
|
9918 | catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
9919 | finally {
|
9920 | try {
|
9921 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
9922 | }
|
9923 | finally { if (e_5) throw e_5.error; }
|
9924 | }
|
9925 | return null;
|
9926 | }
|
9927 |
|
9928 |
|
9929 |
|
9930 |
|
9931 |
|
9932 |
|
9933 |
|
9934 |
|
9935 |
|
9936 |
|
9937 |
|
9938 |
|
9939 |
|
9940 |
|
9941 |
|
9942 |
|
9943 |
|
9944 | var referenceConstructor$1;
|
9945 | function syncTreeSetReferenceConstructor(val) {
|
9946 | util.assert(!referenceConstructor$1, '__referenceConstructor has already been defined');
|
9947 | referenceConstructor$1 = val;
|
9948 | }
|
9949 | function syncTreeGetReferenceConstructor() {
|
9950 | util.assert(referenceConstructor$1, 'Reference.ts has not been loaded');
|
9951 | return referenceConstructor$1;
|
9952 | }
|
9953 |
|
9954 |
|
9955 |
|
9956 | var syncTreeNextQueryTag_ = 1;
|
9957 |
|
9958 |
|
9959 |
|
9960 |
|
9961 |
|
9962 |
|
9963 |
|
9964 |
|
9965 |
|
9966 |
|
9967 |
|
9968 |
|
9969 |
|
9970 |
|
9971 |
|
9972 |
|
9973 |
|
9974 |
|
9975 |
|
9976 |
|
9977 |
|
9978 | var SyncTree = (function () {
|
9979 | |
9980 |
|
9981 |
|
9982 |
|
9983 | function SyncTree(listenProvider_) {
|
9984 | this.listenProvider_ = listenProvider_;
|
9985 | |
9986 |
|
9987 |
|
9988 | this.syncPointTree_ = new ImmutableTree(null);
|
9989 | |
9990 |
|
9991 |
|
9992 | this.pendingWriteTree_ = newWriteTree();
|
9993 | this.tagToQueryMap = new Map();
|
9994 | this.queryToTagMap = new Map();
|
9995 | }
|
9996 | return SyncTree;
|
9997 | }());
|
9998 |
|
9999 |
|
10000 |
|
10001 |
|
10002 |
|
10003 | function syncTreeApplyUserOverwrite(syncTree, path, newData, writeId, visible) {
|
10004 |
|
10005 | writeTreeAddOverwrite(syncTree.pendingWriteTree_, path, newData, writeId, visible);
|
10006 | if (!visible) {
|
10007 | return [];
|
10008 | }
|
10009 | else {
|
10010 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceUser(), path, newData));
|
10011 | }
|
10012 | }
|
10013 |
|
10014 |
|
10015 |
|
10016 |
|
10017 |
|
10018 | function syncTreeApplyUserMerge(syncTree, path, changedChildren, writeId) {
|
10019 |
|
10020 | writeTreeAddMerge(syncTree.pendingWriteTree_, path, changedChildren, writeId);
|
10021 | var changeTree = ImmutableTree.fromObject(changedChildren);
|
10022 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceUser(), path, changeTree));
|
10023 | }
|
10024 |
|
10025 |
|
10026 |
|
10027 |
|
10028 |
|
10029 |
|
10030 | function syncTreeAckUserWrite(syncTree, writeId, revert) {
|
10031 | if (revert === void 0) { revert = false; }
|
10032 | var write = writeTreeGetWrite(syncTree.pendingWriteTree_, writeId);
|
10033 | var needToReevaluate = writeTreeRemoveWrite(syncTree.pendingWriteTree_, writeId);
|
10034 | if (!needToReevaluate) {
|
10035 | return [];
|
10036 | }
|
10037 | else {
|
10038 | var affectedTree_1 = new ImmutableTree(null);
|
10039 | if (write.snap != null) {
|
10040 |
|
10041 | affectedTree_1 = affectedTree_1.set(newEmptyPath(), true);
|
10042 | }
|
10043 | else {
|
10044 | each(write.children, function (pathString) {
|
10045 | affectedTree_1 = affectedTree_1.set(new Path(pathString), true);
|
10046 | });
|
10047 | }
|
10048 | return syncTreeApplyOperationToSyncPoints_(syncTree, new AckUserWrite(write.path, affectedTree_1, revert));
|
10049 | }
|
10050 | }
|
10051 |
|
10052 |
|
10053 |
|
10054 |
|
10055 |
|
10056 | function syncTreeApplyServerOverwrite(syncTree, path, newData) {
|
10057 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceServer(), path, newData));
|
10058 | }
|
10059 |
|
10060 |
|
10061 |
|
10062 |
|
10063 |
|
10064 | function syncTreeApplyServerMerge(syncTree, path, changedChildren) {
|
10065 | var changeTree = ImmutableTree.fromObject(changedChildren);
|
10066 | return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceServer(), path, changeTree));
|
10067 | }
|
10068 |
|
10069 |
|
10070 |
|
10071 |
|
10072 |
|
10073 | function syncTreeApplyListenComplete(syncTree, path) {
|
10074 | return syncTreeApplyOperationToSyncPoints_(syncTree, new ListenComplete(newOperationSourceServer(), path));
|
10075 | }
|
10076 |
|
10077 |
|
10078 |
|
10079 |
|
10080 |
|
10081 | function syncTreeApplyTaggedListenComplete(syncTree, path, tag) {
|
10082 | var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
|
10083 | if (queryKey) {
|
10084 | var r = syncTreeParseQueryKey_(queryKey);
|
10085 | var queryPath = r.path, queryId = r.queryId;
|
10086 | var relativePath = newRelativePath(queryPath, path);
|
10087 | var op = new ListenComplete(newOperationSourceServerTaggedQuery(queryId), relativePath);
|
10088 | return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
|
10089 | }
|
10090 | else {
|
10091 |
|
10092 | return [];
|
10093 | }
|
10094 | }
|
10095 |
|
10096 |
|
10097 |
|
10098 |
|
10099 |
|
10100 |
|
10101 |
|
10102 |
|
10103 |
|
10104 |
|
10105 | function syncTreeRemoveEventRegistration(syncTree, query, eventRegistration, cancelError) {
|
10106 |
|
10107 | var path = query._path;
|
10108 | var maybeSyncPoint = syncTree.syncPointTree_.get(path);
|
10109 | var cancelEvents = [];
|
10110 |
|
10111 |
|
10112 |
|
10113 | if (maybeSyncPoint &&
|
10114 | (query._queryIdentifier === 'default' ||
|
10115 | syncPointViewExistsForQuery(maybeSyncPoint, query))) {
|
10116 | var removedAndEvents = syncPointRemoveEventRegistration(maybeSyncPoint, query, eventRegistration, cancelError);
|
10117 | if (syncPointIsEmpty(maybeSyncPoint)) {
|
10118 | syncTree.syncPointTree_ = syncTree.syncPointTree_.remove(path);
|
10119 | }
|
10120 | var removed = removedAndEvents.removed;
|
10121 | cancelEvents = removedAndEvents.events;
|
10122 |
|
10123 |
|
10124 |
|
10125 |
|
10126 |
|
10127 |
|
10128 | var removingDefault = -1 !==
|
10129 | removed.findIndex(function (query) {
|
10130 | return query._queryParams.loadsAllData();
|
10131 | });
|
10132 | var covered = syncTree.syncPointTree_.findOnPath(path, function (relativePath, parentSyncPoint) {
|
10133 | return syncPointHasCompleteView(parentSyncPoint);
|
10134 | });
|
10135 | if (removingDefault && !covered) {
|
10136 | var subtree = syncTree.syncPointTree_.subtree(path);
|
10137 |
|
10138 |
|
10139 | if (!subtree.isEmpty()) {
|
10140 |
|
10141 | var newViews = syncTreeCollectDistinctViewsForSubTree_(subtree);
|
10142 |
|
10143 | for (var i = 0; i < newViews.length; ++i) {
|
10144 | var view = newViews[i], newQuery = view.query;
|
10145 | var listener = syncTreeCreateListenerForView_(syncTree, view);
|
10146 | syncTree.listenProvider_.startListening(syncTreeQueryForListening_(newQuery), syncTreeTagForQuery_(syncTree, newQuery), listener.hashFn, listener.onComplete);
|
10147 | }
|
10148 | }
|
10149 | }
|
10150 |
|
10151 |
|
10152 |
|
10153 | if (!covered && removed.length > 0 && !cancelError) {
|
10154 |
|
10155 |
|
10156 | if (removingDefault) {
|
10157 |
|
10158 | var defaultTag = null;
|
10159 | syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(query), defaultTag);
|
10160 | }
|
10161 | else {
|
10162 | removed.forEach(function (queryToRemove) {
|
10163 | var tagToRemove = syncTree.queryToTagMap.get(syncTreeMakeQueryKey_(queryToRemove));
|
10164 | syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToRemove), tagToRemove);
|
10165 | });
|
10166 | }
|
10167 | }
|
10168 |
|
10169 | syncTreeRemoveTags_(syncTree, removed);
|
10170 | }
|
10171 | return cancelEvents;
|
10172 | }
|
10173 |
|
10174 |
|
10175 |
|
10176 |
|
10177 |
|
10178 | function syncTreeApplyTaggedQueryOverwrite(syncTree, path, snap, tag) {
|
10179 | var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
|
10180 | if (queryKey != null) {
|
10181 | var r = syncTreeParseQueryKey_(queryKey);
|
10182 | var queryPath = r.path, queryId = r.queryId;
|
10183 | var relativePath = newRelativePath(queryPath, path);
|
10184 | var op = new Overwrite(newOperationSourceServerTaggedQuery(queryId), relativePath, snap);
|
10185 | return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
|
10186 | }
|
10187 | else {
|
10188 |
|
10189 | return [];
|
10190 | }
|
10191 | }
|
10192 |
|
10193 |
|
10194 |
|
10195 |
|
10196 |
|
10197 | function syncTreeApplyTaggedQueryMerge(syncTree, path, changedChildren, tag) {
|
10198 | var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
|
10199 | if (queryKey) {
|
10200 | var r = syncTreeParseQueryKey_(queryKey);
|
10201 | var queryPath = r.path, queryId = r.queryId;
|
10202 | var relativePath = newRelativePath(queryPath, path);
|
10203 | var changeTree = ImmutableTree.fromObject(changedChildren);
|
10204 | var op = new Merge(newOperationSourceServerTaggedQuery(queryId), relativePath, changeTree);
|
10205 | return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
|
10206 | }
|
10207 | else {
|
10208 |
|
10209 | return [];
|
10210 | }
|
10211 | }
|
10212 |
|
10213 |
|
10214 |
|
10215 |
|
10216 |
|
10217 | function syncTreeAddEventRegistration(syncTree, query, eventRegistration) {
|
10218 | var path = query._path;
|
10219 | var serverCache = null;
|
10220 | var foundAncestorDefaultView = false;
|
10221 |
|
10222 |
|
10223 | syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
|
10224 | var relativePath = newRelativePath(pathToSyncPoint, path);
|
10225 | serverCache =
|
10226 | serverCache || syncPointGetCompleteServerCache(sp, relativePath);
|
10227 | foundAncestorDefaultView =
|
10228 | foundAncestorDefaultView || syncPointHasCompleteView(sp);
|
10229 | });
|
10230 | var syncPoint = syncTree.syncPointTree_.get(path);
|
10231 | if (!syncPoint) {
|
10232 | syncPoint = new SyncPoint();
|
10233 | syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
|
10234 | }
|
10235 | else {
|
10236 | foundAncestorDefaultView =
|
10237 | foundAncestorDefaultView || syncPointHasCompleteView(syncPoint);
|
10238 | serverCache =
|
10239 | serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10240 | }
|
10241 | var serverCacheComplete;
|
10242 | if (serverCache != null) {
|
10243 | serverCacheComplete = true;
|
10244 | }
|
10245 | else {
|
10246 | serverCacheComplete = false;
|
10247 | serverCache = ChildrenNode.EMPTY_NODE;
|
10248 | var subtree = syncTree.syncPointTree_.subtree(path);
|
10249 | subtree.foreachChild(function (childName, childSyncPoint) {
|
10250 | var completeCache = syncPointGetCompleteServerCache(childSyncPoint, newEmptyPath());
|
10251 | if (completeCache) {
|
10252 | serverCache = serverCache.updateImmediateChild(childName, completeCache);
|
10253 | }
|
10254 | });
|
10255 | }
|
10256 | var viewAlreadyExists = syncPointViewExistsForQuery(syncPoint, query);
|
10257 | if (!viewAlreadyExists && !query._queryParams.loadsAllData()) {
|
10258 |
|
10259 | var queryKey = syncTreeMakeQueryKey_(query);
|
10260 | util.assert(!syncTree.queryToTagMap.has(queryKey), 'View does not exist, but we have a tag');
|
10261 | var tag = syncTreeGetNextQueryTag_();
|
10262 | syncTree.queryToTagMap.set(queryKey, tag);
|
10263 | syncTree.tagToQueryMap.set(tag, queryKey);
|
10264 | }
|
10265 | var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
|
10266 | var events = syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete);
|
10267 | if (!viewAlreadyExists && !foundAncestorDefaultView) {
|
10268 | var view = syncPointViewForQuery(syncPoint, query);
|
10269 | events = events.concat(syncTreeSetupListener_(syncTree, query, view));
|
10270 | }
|
10271 | return events;
|
10272 | }
|
10273 |
|
10274 |
|
10275 |
|
10276 |
|
10277 |
|
10278 |
|
10279 |
|
10280 |
|
10281 |
|
10282 |
|
10283 |
|
10284 | function syncTreeCalcCompleteEventCache(syncTree, path, writeIdsToExclude) {
|
10285 | var includeHiddenSets = true;
|
10286 | var writeTree = syncTree.pendingWriteTree_;
|
10287 | var serverCache = syncTree.syncPointTree_.findOnPath(path, function (pathSoFar, syncPoint) {
|
10288 | var relativePath = newRelativePath(pathSoFar, path);
|
10289 | var serverCache = syncPointGetCompleteServerCache(syncPoint, relativePath);
|
10290 | if (serverCache) {
|
10291 | return serverCache;
|
10292 | }
|
10293 | });
|
10294 | return writeTreeCalcCompleteEventCache(writeTree, path, serverCache, writeIdsToExclude, includeHiddenSets);
|
10295 | }
|
10296 | function syncTreeGetServerValue(syncTree, query) {
|
10297 | var path = query._path;
|
10298 | var serverCache = null;
|
10299 |
|
10300 |
|
10301 | syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
|
10302 | var relativePath = newRelativePath(pathToSyncPoint, path);
|
10303 | serverCache =
|
10304 | serverCache || syncPointGetCompleteServerCache(sp, relativePath);
|
10305 | });
|
10306 | var syncPoint = syncTree.syncPointTree_.get(path);
|
10307 | if (!syncPoint) {
|
10308 | syncPoint = new SyncPoint();
|
10309 | syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
|
10310 | }
|
10311 | else {
|
10312 | serverCache =
|
10313 | serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10314 | }
|
10315 | var serverCacheComplete = serverCache != null;
|
10316 | var serverCacheNode = serverCacheComplete
|
10317 | ? new CacheNode(serverCache, true, false)
|
10318 | : null;
|
10319 | var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, query._path);
|
10320 | var view = syncPointGetView(syncPoint, query, writesCache, serverCacheComplete ? serverCacheNode.getNode() : ChildrenNode.EMPTY_NODE, serverCacheComplete);
|
10321 | return viewGetCompleteNode(view);
|
10322 | }
|
10323 |
|
10324 |
|
10325 |
|
10326 |
|
10327 |
|
10328 |
|
10329 |
|
10330 |
|
10331 |
|
10332 |
|
10333 |
|
10334 |
|
10335 |
|
10336 | function syncTreeApplyOperationToSyncPoints_(syncTree, operation) {
|
10337 | return syncTreeApplyOperationHelper_(operation, syncTree.syncPointTree_,
|
10338 | null, writeTreeChildWrites(syncTree.pendingWriteTree_, newEmptyPath()));
|
10339 | }
|
10340 |
|
10341 |
|
10342 |
|
10343 | function syncTreeApplyOperationHelper_(operation, syncPointTree, serverCache, writesCache) {
|
10344 | if (pathIsEmpty(operation.path)) {
|
10345 | return syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache);
|
10346 | }
|
10347 | else {
|
10348 | var syncPoint = syncPointTree.get(newEmptyPath());
|
10349 |
|
10350 | if (serverCache == null && syncPoint != null) {
|
10351 | serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10352 | }
|
10353 | var events = [];
|
10354 | var childName = pathGetFront(operation.path);
|
10355 | var childOperation = operation.operationForChild(childName);
|
10356 | var childTree = syncPointTree.children.get(childName);
|
10357 | if (childTree && childOperation) {
|
10358 | var childServerCache = serverCache
|
10359 | ? serverCache.getImmediateChild(childName)
|
10360 | : null;
|
10361 | var childWritesCache = writeTreeRefChild(writesCache, childName);
|
10362 | events = events.concat(syncTreeApplyOperationHelper_(childOperation, childTree, childServerCache, childWritesCache));
|
10363 | }
|
10364 | if (syncPoint) {
|
10365 | events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
|
10366 | }
|
10367 | return events;
|
10368 | }
|
10369 | }
|
10370 |
|
10371 |
|
10372 |
|
10373 | function syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache) {
|
10374 | var syncPoint = syncPointTree.get(newEmptyPath());
|
10375 |
|
10376 | if (serverCache == null && syncPoint != null) {
|
10377 | serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
|
10378 | }
|
10379 | var events = [];
|
10380 | syncPointTree.children.inorderTraversal(function (childName, childTree) {
|
10381 | var childServerCache = serverCache
|
10382 | ? serverCache.getImmediateChild(childName)
|
10383 | : null;
|
10384 | var childWritesCache = writeTreeRefChild(writesCache, childName);
|
10385 | var childOperation = operation.operationForChild(childName);
|
10386 | if (childOperation) {
|
10387 | events = events.concat(syncTreeApplyOperationDescendantsHelper_(childOperation, childTree, childServerCache, childWritesCache));
|
10388 | }
|
10389 | });
|
10390 | if (syncPoint) {
|
10391 | events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
|
10392 | }
|
10393 | return events;
|
10394 | }
|
10395 | function syncTreeCreateListenerForView_(syncTree, view) {
|
10396 | var query = view.query;
|
10397 | var tag = syncTreeTagForQuery_(syncTree, query);
|
10398 | return {
|
10399 | hashFn: function () {
|
10400 | var cache = viewGetServerCache(view) || ChildrenNode.EMPTY_NODE;
|
10401 | return cache.hash();
|
10402 | },
|
10403 | onComplete: function (status) {
|
10404 | if (status === 'ok') {
|
10405 | if (tag) {
|
10406 | return syncTreeApplyTaggedListenComplete(syncTree, query._path, tag);
|
10407 | }
|
10408 | else {
|
10409 | return syncTreeApplyListenComplete(syncTree, query._path);
|
10410 | }
|
10411 | }
|
10412 | else {
|
10413 |
|
10414 |
|
10415 | var error = errorForServerCode(status, query);
|
10416 | return syncTreeRemoveEventRegistration(syncTree, query,
|
10417 | null, error);
|
10418 | }
|
10419 | }
|
10420 | };
|
10421 | }
|
10422 |
|
10423 |
|
10424 |
|
10425 | function syncTreeTagForQuery_(syncTree, query) {
|
10426 | var queryKey = syncTreeMakeQueryKey_(query);
|
10427 | return syncTree.queryToTagMap.get(queryKey);
|
10428 | }
|
10429 |
|
10430 |
|
10431 |
|
10432 | function syncTreeMakeQueryKey_(query) {
|
10433 | return query._path.toString() + '$' + query._queryIdentifier;
|
10434 | }
|
10435 |
|
10436 |
|
10437 |
|
10438 | function syncTreeQueryKeyForTag_(syncTree, tag) {
|
10439 | return syncTree.tagToQueryMap.get(tag);
|
10440 | }
|
10441 |
|
10442 |
|
10443 |
|
10444 | function syncTreeParseQueryKey_(queryKey) {
|
10445 | var splitIndex = queryKey.indexOf('$');
|
10446 | util.assert(splitIndex !== -1 && splitIndex < queryKey.length - 1, 'Bad queryKey.');
|
10447 | return {
|
10448 | queryId: queryKey.substr(splitIndex + 1),
|
10449 | path: new Path(queryKey.substr(0, splitIndex))
|
10450 | };
|
10451 | }
|
10452 |
|
10453 |
|
10454 |
|
10455 | function syncTreeApplyTaggedOperation_(syncTree, queryPath, operation) {
|
10456 | var syncPoint = syncTree.syncPointTree_.get(queryPath);
|
10457 | util.assert(syncPoint, "Missing sync point for query tag that we're tracking");
|
10458 | var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, queryPath);
|
10459 | return syncPointApplyOperation(syncPoint, operation, writesCache, null);
|
10460 | }
|
10461 |
|
10462 |
|
10463 |
|
10464 |
|
10465 | function syncTreeCollectDistinctViewsForSubTree_(subtree) {
|
10466 | return subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
|
10467 | if (maybeChildSyncPoint && syncPointHasCompleteView(maybeChildSyncPoint)) {
|
10468 | var completeView = syncPointGetCompleteView(maybeChildSyncPoint);
|
10469 | return [completeView];
|
10470 | }
|
10471 | else {
|
10472 |
|
10473 | var views_1 = [];
|
10474 | if (maybeChildSyncPoint) {
|
10475 | views_1 = syncPointGetQueryViews(maybeChildSyncPoint);
|
10476 | }
|
10477 | each(childMap, function (_key, childViews) {
|
10478 | views_1 = views_1.concat(childViews);
|
10479 | });
|
10480 | return views_1;
|
10481 | }
|
10482 | });
|
10483 | }
|
10484 |
|
10485 |
|
10486 |
|
10487 |
|
10488 |
|
10489 | function syncTreeQueryForListening_(query) {
|
10490 | if (query._queryParams.loadsAllData() && !query._queryParams.isDefault()) {
|
10491 |
|
10492 |
|
10493 |
|
10494 | return new (syncTreeGetReferenceConstructor())(query._repo, query._path);
|
10495 | }
|
10496 | else {
|
10497 | return query;
|
10498 | }
|
10499 | }
|
10500 | function syncTreeRemoveTags_(syncTree, queries) {
|
10501 | for (var j = 0; j < queries.length; ++j) {
|
10502 | var removedQuery = queries[j];
|
10503 | if (!removedQuery._queryParams.loadsAllData()) {
|
10504 |
|
10505 | var removedQueryKey = syncTreeMakeQueryKey_(removedQuery);
|
10506 | var removedQueryTag = syncTree.queryToTagMap.get(removedQueryKey);
|
10507 | syncTree.queryToTagMap.delete(removedQueryKey);
|
10508 | syncTree.tagToQueryMap.delete(removedQueryTag);
|
10509 | }
|
10510 | }
|
10511 | }
|
10512 |
|
10513 |
|
10514 |
|
10515 | function syncTreeGetNextQueryTag_() {
|
10516 | return syncTreeNextQueryTag_++;
|
10517 | }
|
10518 |
|
10519 |
|
10520 |
|
10521 |
|
10522 |
|
10523 | function syncTreeSetupListener_(syncTree, query, view) {
|
10524 | var path = query._path;
|
10525 | var tag = syncTreeTagForQuery_(syncTree, query);
|
10526 | var listener = syncTreeCreateListenerForView_(syncTree, view);
|
10527 | var events = syncTree.listenProvider_.startListening(syncTreeQueryForListening_(query), tag, listener.hashFn, listener.onComplete);
|
10528 | var subtree = syncTree.syncPointTree_.subtree(path);
|
10529 |
|
10530 |
|
10531 | if (tag) {
|
10532 | util.assert(!syncPointHasCompleteView(subtree.value), "If we're adding a query, it shouldn't be shadowed");
|
10533 | }
|
10534 | else {
|
10535 |
|
10536 | var queriesToStop = subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
|
10537 | if (!pathIsEmpty(relativePath) &&
|
10538 | maybeChildSyncPoint &&
|
10539 | syncPointHasCompleteView(maybeChildSyncPoint)) {
|
10540 | return [syncPointGetCompleteView(maybeChildSyncPoint).query];
|
10541 | }
|
10542 | else {
|
10543 |
|
10544 | var queries_1 = [];
|
10545 | if (maybeChildSyncPoint) {
|
10546 | queries_1 = queries_1.concat(syncPointGetQueryViews(maybeChildSyncPoint).map(function (view) { return view.query; }));
|
10547 | }
|
10548 | each(childMap, function (_key, childQueries) {
|
10549 | queries_1 = queries_1.concat(childQueries);
|
10550 | });
|
10551 | return queries_1;
|
10552 | }
|
10553 | });
|
10554 | for (var i = 0; i < queriesToStop.length; ++i) {
|
10555 | var queryToStop = queriesToStop[i];
|
10556 | syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToStop), syncTreeTagForQuery_(syncTree, queryToStop));
|
10557 | }
|
10558 | }
|
10559 | return events;
|
10560 | }
|
10561 |
|
10562 |
|
10563 |
|
10564 |
|
10565 |
|
10566 |
|
10567 |
|
10568 |
|
10569 |
|
10570 |
|
10571 |
|
10572 |
|
10573 |
|
10574 |
|
10575 |
|
10576 |
|
10577 |
|
10578 | var ExistingValueProvider = (function () {
|
10579 | function ExistingValueProvider(node_) {
|
10580 | this.node_ = node_;
|
10581 | }
|
10582 | ExistingValueProvider.prototype.getImmediateChild = function (childName) {
|
10583 | var child = this.node_.getImmediateChild(childName);
|
10584 | return new ExistingValueProvider(child);
|
10585 | };
|
10586 | ExistingValueProvider.prototype.node = function () {
|
10587 | return this.node_;
|
10588 | };
|
10589 | return ExistingValueProvider;
|
10590 | }());
|
10591 | var DeferredValueProvider = (function () {
|
10592 | function DeferredValueProvider(syncTree, path) {
|
10593 | this.syncTree_ = syncTree;
|
10594 | this.path_ = path;
|
10595 | }
|
10596 | DeferredValueProvider.prototype.getImmediateChild = function (childName) {
|
10597 | var childPath = pathChild(this.path_, childName);
|
10598 | return new DeferredValueProvider(this.syncTree_, childPath);
|
10599 | };
|
10600 | DeferredValueProvider.prototype.node = function () {
|
10601 | return syncTreeCalcCompleteEventCache(this.syncTree_, this.path_);
|
10602 | };
|
10603 | return DeferredValueProvider;
|
10604 | }());
|
10605 |
|
10606 |
|
10607 |
|
10608 | var generateWithValues = function (values) {
|
10609 | values = values || {};
|
10610 | values['timestamp'] = values['timestamp'] || new Date().getTime();
|
10611 | return values;
|
10612 | };
|
10613 |
|
10614 |
|
10615 |
|
10616 |
|
10617 | var resolveDeferredLeafValue = function (value, existingVal, serverValues) {
|
10618 | if (!value || typeof value !== 'object') {
|
10619 | return value;
|
10620 | }
|
10621 | util.assert('.sv' in value, 'Unexpected leaf node or priority contents');
|
10622 | if (typeof value['.sv'] === 'string') {
|
10623 | return resolveScalarDeferredValue(value['.sv'], existingVal, serverValues);
|
10624 | }
|
10625 | else if (typeof value['.sv'] === 'object') {
|
10626 | return resolveComplexDeferredValue(value['.sv'], existingVal);
|
10627 | }
|
10628 | else {
|
10629 | util.assert(false, 'Unexpected server value: ' + JSON.stringify(value, null, 2));
|
10630 | }
|
10631 | };
|
10632 | var resolveScalarDeferredValue = function (op, existing, serverValues) {
|
10633 | switch (op) {
|
10634 | case 'timestamp':
|
10635 | return serverValues['timestamp'];
|
10636 | default:
|
10637 | util.assert(false, 'Unexpected server value: ' + op);
|
10638 | }
|
10639 | };
|
10640 | var resolveComplexDeferredValue = function (op, existing, unused) {
|
10641 | if (!op.hasOwnProperty('increment')) {
|
10642 | util.assert(false, 'Unexpected server value: ' + JSON.stringify(op, null, 2));
|
10643 | }
|
10644 | var delta = op['increment'];
|
10645 | if (typeof delta !== 'number') {
|
10646 | util.assert(false, 'Unexpected increment value: ' + delta);
|
10647 | }
|
10648 | var existingNode = existing.node();
|
10649 | util.assert(existingNode !== null && typeof existingNode !== 'undefined', 'Expected ChildrenNode.EMPTY_NODE for nulls');
|
10650 |
|
10651 | if (!existingNode.isLeafNode()) {
|
10652 | return delta;
|
10653 | }
|
10654 | var leaf = existingNode;
|
10655 | var existingVal = leaf.getValue();
|
10656 | if (typeof existingVal !== 'number') {
|
10657 | return delta;
|
10658 | }
|
10659 |
|
10660 | return existingVal + delta;
|
10661 | };
|
10662 |
|
10663 |
|
10664 |
|
10665 |
|
10666 |
|
10667 |
|
10668 |
|
10669 | var resolveDeferredValueTree = function (path, node, syncTree, serverValues) {
|
10670 | return resolveDeferredValue(node, new DeferredValueProvider(syncTree, path), serverValues);
|
10671 | };
|
10672 |
|
10673 |
|
10674 |
|
10675 |
|
10676 |
|
10677 | var resolveDeferredValueSnapshot = function (node, existing, serverValues) {
|
10678 | return resolveDeferredValue(node, new ExistingValueProvider(existing), serverValues);
|
10679 | };
|
10680 | function resolveDeferredValue(node, existingVal, serverValues) {
|
10681 | var rawPri = node.getPriority().val();
|
10682 | var priority = resolveDeferredLeafValue(rawPri, existingVal.getImmediateChild('.priority'), serverValues);
|
10683 | var newNode;
|
10684 | if (node.isLeafNode()) {
|
10685 | var leafNode = node;
|
10686 | var value = resolveDeferredLeafValue(leafNode.getValue(), existingVal, serverValues);
|
10687 | if (value !== leafNode.getValue() ||
|
10688 | priority !== leafNode.getPriority().val()) {
|
10689 | return new LeafNode(value, nodeFromJSON$1(priority));
|
10690 | }
|
10691 | else {
|
10692 | return node;
|
10693 | }
|
10694 | }
|
10695 | else {
|
10696 | var childrenNode = node;
|
10697 | newNode = childrenNode;
|
10698 | if (priority !== childrenNode.getPriority().val()) {
|
10699 | newNode = newNode.updatePriority(new LeafNode(priority));
|
10700 | }
|
10701 | childrenNode.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
|
10702 | var newChildNode = resolveDeferredValue(childNode, existingVal.getImmediateChild(childName), serverValues);
|
10703 | if (newChildNode !== childNode) {
|
10704 | newNode = newNode.updateImmediateChild(childName, newChildNode);
|
10705 | }
|
10706 | });
|
10707 | return newNode;
|
10708 | }
|
10709 | }
|
10710 |
|
10711 |
|
10712 |
|
10713 |
|
10714 |
|
10715 |
|
10716 |
|
10717 |
|
10718 |
|
10719 |
|
10720 |
|
10721 |
|
10722 |
|
10723 |
|
10724 |
|
10725 |
|
10726 |
|
10727 |
|
10728 |
|
10729 |
|
10730 |
|
10731 |
|
10732 | var Tree = (function () {
|
10733 | |
10734 |
|
10735 |
|
10736 |
|
10737 |
|
10738 | function Tree(name, parent, node) {
|
10739 | if (name === void 0) { name = ''; }
|
10740 | if (parent === void 0) { parent = null; }
|
10741 | if (node === void 0) { node = { children: {}, childCount: 0 }; }
|
10742 | this.name = name;
|
10743 | this.parent = parent;
|
10744 | this.node = node;
|
10745 | }
|
10746 | return Tree;
|
10747 | }());
|
10748 |
|
10749 |
|
10750 |
|
10751 |
|
10752 |
|
10753 |
|
10754 | function treeSubTree(tree, pathObj) {
|
10755 |
|
10756 | var path = pathObj instanceof Path ? pathObj : new Path(pathObj);
|
10757 | var child = tree, next = pathGetFront(path);
|
10758 | while (next !== null) {
|
10759 | var childNode = util.safeGet(child.node.children, next) || {
|
10760 | children: {},
|
10761 | childCount: 0
|
10762 | };
|
10763 | child = new Tree(next, child, childNode);
|
10764 | path = pathPopFront(path);
|
10765 | next = pathGetFront(path);
|
10766 | }
|
10767 | return child;
|
10768 | }
|
10769 |
|
10770 |
|
10771 |
|
10772 |
|
10773 |
|
10774 | function treeGetValue(tree) {
|
10775 | return tree.node.value;
|
10776 | }
|
10777 |
|
10778 |
|
10779 |
|
10780 |
|
10781 |
|
10782 | function treeSetValue(tree, value) {
|
10783 | tree.node.value = value;
|
10784 | treeUpdateParents(tree);
|
10785 | }
|
10786 |
|
10787 |
|
10788 |
|
10789 | function treeHasChildren(tree) {
|
10790 | return tree.node.childCount > 0;
|
10791 | }
|
10792 |
|
10793 |
|
10794 |
|
10795 | function treeIsEmpty(tree) {
|
10796 | return treeGetValue(tree) === undefined && !treeHasChildren(tree);
|
10797 | }
|
10798 |
|
10799 |
|
10800 |
|
10801 |
|
10802 |
|
10803 | function treeForEachChild(tree, action) {
|
10804 | each(tree.node.children, function (child, childTree) {
|
10805 | action(new Tree(child, tree, childTree));
|
10806 | });
|
10807 | }
|
10808 |
|
10809 |
|
10810 |
|
10811 |
|
10812 |
|
10813 |
|
10814 |
|
10815 |
|
10816 |
|
10817 | function treeForEachDescendant(tree, action, includeSelf, childrenFirst) {
|
10818 | if (includeSelf && !childrenFirst) {
|
10819 | action(tree);
|
10820 | }
|
10821 | treeForEachChild(tree, function (child) {
|
10822 | treeForEachDescendant(child, action, true, childrenFirst);
|
10823 | });
|
10824 | if (includeSelf && childrenFirst) {
|
10825 | action(tree);
|
10826 | }
|
10827 | }
|
10828 |
|
10829 |
|
10830 |
|
10831 |
|
10832 |
|
10833 |
|
10834 |
|
10835 |
|
10836 | function treeForEachAncestor(tree, action, includeSelf) {
|
10837 | var node = includeSelf ? tree : tree.parent;
|
10838 | while (node !== null) {
|
10839 | if (action(node)) {
|
10840 | return true;
|
10841 | }
|
10842 | node = node.parent;
|
10843 | }
|
10844 | return false;
|
10845 | }
|
10846 |
|
10847 |
|
10848 |
|
10849 | function treeGetPath(tree) {
|
10850 | return new Path(tree.parent === null
|
10851 | ? tree.name
|
10852 | : treeGetPath(tree.parent) + '/' + tree.name);
|
10853 | }
|
10854 |
|
10855 |
|
10856 |
|
10857 | function treeUpdateParents(tree) {
|
10858 | if (tree.parent !== null) {
|
10859 | treeUpdateChild(tree.parent, tree.name, tree);
|
10860 | }
|
10861 | }
|
10862 |
|
10863 |
|
10864 |
|
10865 |
|
10866 |
|
10867 |
|
10868 | function treeUpdateChild(tree, childName, child) {
|
10869 | var childEmpty = treeIsEmpty(child);
|
10870 | var childExists = util.contains(tree.node.children, childName);
|
10871 | if (childEmpty && childExists) {
|
10872 | delete tree.node.children[childName];
|
10873 | tree.node.childCount--;
|
10874 | treeUpdateParents(tree);
|
10875 | }
|
10876 | else if (!childEmpty && !childExists) {
|
10877 | tree.node.children[childName] = child.node;
|
10878 | tree.node.childCount++;
|
10879 | treeUpdateParents(tree);
|
10880 | }
|
10881 | }
|
10882 |
|
10883 |
|
10884 |
|
10885 |
|
10886 |
|
10887 |
|
10888 |
|
10889 |
|
10890 |
|
10891 |
|
10892 |
|
10893 |
|
10894 |
|
10895 |
|
10896 |
|
10897 |
|
10898 |
|
10899 |
|
10900 |
|
10901 |
|
10902 | var INVALID_KEY_REGEX_ = /[\[\].#$\/\u0000-\u001F\u007F]/;
|
10903 |
|
10904 |
|
10905 |
|
10906 |
|
10907 | var INVALID_PATH_REGEX_ = /[\[\].#$\u0000-\u001F\u007F]/;
|
10908 |
|
10909 |
|
10910 |
|
10911 | var MAX_LEAF_SIZE_ = 10 * 1024 * 1024;
|
10912 | var isValidKey = function (key) {
|
10913 | return (typeof key === 'string' && key.length !== 0 && !INVALID_KEY_REGEX_.test(key));
|
10914 | };
|
10915 | var isValidPathString = function (pathString) {
|
10916 | return (typeof pathString === 'string' &&
|
10917 | pathString.length !== 0 &&
|
10918 | !INVALID_PATH_REGEX_.test(pathString));
|
10919 | };
|
10920 | var isValidRootPathString = function (pathString) {
|
10921 | if (pathString) {
|
10922 |
|
10923 | pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
|
10924 | }
|
10925 | return isValidPathString(pathString);
|
10926 | };
|
10927 | var isValidPriority = function (priority) {
|
10928 | return (priority === null ||
|
10929 | typeof priority === 'string' ||
|
10930 | (typeof priority === 'number' && !isInvalidJSONNumber(priority)) ||
|
10931 | (priority &&
|
10932 | typeof priority === 'object' &&
|
10933 |
|
10934 | util.contains(priority, '.sv')));
|
10935 | };
|
10936 |
|
10937 |
|
10938 |
|
10939 | var validateFirebaseDataArg = function (fnName, value, path, optional) {
|
10940 | if (optional && value === undefined) {
|
10941 | return;
|
10942 | }
|
10943 | validateFirebaseData(util.errorPrefix(fnName, 'value'), value, path);
|
10944 | };
|
10945 |
|
10946 |
|
10947 |
|
10948 | var validateFirebaseData = function (errorPrefix, data, path_) {
|
10949 | var path = path_ instanceof Path ? new ValidationPath(path_, errorPrefix) : path_;
|
10950 | if (data === undefined) {
|
10951 | throw new Error(errorPrefix + 'contains undefined ' + validationPathToErrorString(path));
|
10952 | }
|
10953 | if (typeof data === 'function') {
|
10954 | throw new Error(errorPrefix +
|
10955 | 'contains a function ' +
|
10956 | validationPathToErrorString(path) +
|
10957 | ' with contents = ' +
|
10958 | data.toString());
|
10959 | }
|
10960 | if (isInvalidJSONNumber(data)) {
|
10961 | throw new Error(errorPrefix +
|
10962 | 'contains ' +
|
10963 | data.toString() +
|
10964 | ' ' +
|
10965 | validationPathToErrorString(path));
|
10966 | }
|
10967 |
|
10968 | if (typeof data === 'string' &&
|
10969 | data.length > MAX_LEAF_SIZE_ / 3 &&
|
10970 | util.stringLength(data) > MAX_LEAF_SIZE_) {
|
10971 | throw new Error(errorPrefix +
|
10972 | 'contains a string greater than ' +
|
10973 | MAX_LEAF_SIZE_ +
|
10974 | ' utf8 bytes ' +
|
10975 | validationPathToErrorString(path) +
|
10976 | " ('" +
|
10977 | data.substring(0, 50) +
|
10978 | "...')");
|
10979 | }
|
10980 |
|
10981 |
|
10982 | if (data && typeof data === 'object') {
|
10983 | var hasDotValue_1 = false;
|
10984 | var hasActualChild_1 = false;
|
10985 | each(data, function (key, value) {
|
10986 | if (key === '.value') {
|
10987 | hasDotValue_1 = true;
|
10988 | }
|
10989 | else if (key !== '.priority' && key !== '.sv') {
|
10990 | hasActualChild_1 = true;
|
10991 | if (!isValidKey(key)) {
|
10992 | throw new Error(errorPrefix +
|
10993 | ' contains an invalid key (' +
|
10994 | key +
|
10995 | ') ' +
|
10996 | validationPathToErrorString(path) +
|
10997 | '. Keys must be non-empty strings ' +
|
10998 | 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
|
10999 | }
|
11000 | }
|
11001 | validationPathPush(path, key);
|
11002 | validateFirebaseData(errorPrefix, value, path);
|
11003 | validationPathPop(path);
|
11004 | });
|
11005 | if (hasDotValue_1 && hasActualChild_1) {
|
11006 | throw new Error(errorPrefix +
|
11007 | ' contains ".value" child ' +
|
11008 | validationPathToErrorString(path) +
|
11009 | ' in addition to actual children.');
|
11010 | }
|
11011 | }
|
11012 | };
|
11013 |
|
11014 |
|
11015 |
|
11016 | var validateFirebaseMergePaths = function (errorPrefix, mergePaths) {
|
11017 | var i, curPath;
|
11018 | for (i = 0; i < mergePaths.length; i++) {
|
11019 | curPath = mergePaths[i];
|
11020 | var keys = pathSlice(curPath);
|
11021 | for (var j = 0; j < keys.length; j++) {
|
11022 | if (keys[j] === '.priority' && j === keys.length - 1) ;
|
11023 | else if (!isValidKey(keys[j])) {
|
11024 | throw new Error(errorPrefix +
|
11025 | 'contains an invalid key (' +
|
11026 | keys[j] +
|
11027 | ') in path ' +
|
11028 | curPath.toString() +
|
11029 | '. Keys must be non-empty strings ' +
|
11030 | 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
|
11031 | }
|
11032 | }
|
11033 | }
|
11034 |
|
11035 |
|
11036 |
|
11037 | mergePaths.sort(pathCompare);
|
11038 | var prevPath = null;
|
11039 | for (i = 0; i < mergePaths.length; i++) {
|
11040 | curPath = mergePaths[i];
|
11041 | if (prevPath !== null && pathContains(prevPath, curPath)) {
|
11042 | throw new Error(errorPrefix +
|
11043 | 'contains a path ' +
|
11044 | prevPath.toString() +
|
11045 | ' that is ancestor of another path ' +
|
11046 | curPath.toString());
|
11047 | }
|
11048 | prevPath = curPath;
|
11049 | }
|
11050 | };
|
11051 |
|
11052 |
|
11053 |
|
11054 |
|
11055 | var validateFirebaseMergeDataArg = function (fnName, data, path, optional) {
|
11056 | if (optional && data === undefined) {
|
11057 | return;
|
11058 | }
|
11059 | var errorPrefix = util.errorPrefix(fnName, 'values');
|
11060 | if (!(data && typeof data === 'object') || Array.isArray(data)) {
|
11061 | throw new Error(errorPrefix + ' must be an object containing the children to replace.');
|
11062 | }
|
11063 | var mergePaths = [];
|
11064 | each(data, function (key, value) {
|
11065 | var curPath = new Path(key);
|
11066 | validateFirebaseData(errorPrefix, value, pathChild(path, curPath));
|
11067 | if (pathGetBack(curPath) === '.priority') {
|
11068 | if (!isValidPriority(value)) {
|
11069 | throw new Error(errorPrefix +
|
11070 | "contains an invalid value for '" +
|
11071 | curPath.toString() +
|
11072 | "', which must be a valid " +
|
11073 | 'Firebase priority (a string, finite number, server value, or null).');
|
11074 | }
|
11075 | }
|
11076 | mergePaths.push(curPath);
|
11077 | });
|
11078 | validateFirebaseMergePaths(errorPrefix, mergePaths);
|
11079 | };
|
11080 | var validatePriority = function (fnName, priority, optional) {
|
11081 | if (optional && priority === undefined) {
|
11082 | return;
|
11083 | }
|
11084 | if (isInvalidJSONNumber(priority)) {
|
11085 | throw new Error(util.errorPrefix(fnName, 'priority') +
|
11086 | 'is ' +
|
11087 | priority.toString() +
|
11088 | ', but must be a valid Firebase priority (a string, finite number, ' +
|
11089 | 'server value, or null).');
|
11090 | }
|
11091 |
|
11092 | if (!isValidPriority(priority)) {
|
11093 | throw new Error(util.errorPrefix(fnName, 'priority') +
|
11094 | 'must be a valid Firebase priority ' +
|
11095 | '(a string, finite number, server value, or null).');
|
11096 | }
|
11097 | };
|
11098 | var validateEventType = function (fnName, eventType, optional) {
|
11099 | if (optional && eventType === undefined) {
|
11100 | return;
|
11101 | }
|
11102 | switch (eventType) {
|
11103 | case 'value':
|
11104 | case 'child_added':
|
11105 | case 'child_removed':
|
11106 | case 'child_changed':
|
11107 | case 'child_moved':
|
11108 | break;
|
11109 | default:
|
11110 | throw new Error(util.errorPrefix(fnName, 'eventType') +
|
11111 | 'must be a valid event type = "value", "child_added", "child_removed", ' +
|
11112 | '"child_changed", or "child_moved".');
|
11113 | }
|
11114 | };
|
11115 | var validateKey = function (fnName, argumentName, key, optional) {
|
11116 | if (optional && key === undefined) {
|
11117 | return;
|
11118 | }
|
11119 | if (!isValidKey(key)) {
|
11120 | throw new Error(util.errorPrefix(fnName, argumentName) +
|
11121 | 'was an invalid key = "' +
|
11122 | key +
|
11123 | '". Firebase keys must be non-empty strings and ' +
|
11124 | 'can\'t contain ".", "#", "$", "/", "[", or "]").');
|
11125 | }
|
11126 | };
|
11127 | var validatePathString = function (fnName, argumentName, pathString, optional) {
|
11128 | if (optional && pathString === undefined) {
|
11129 | return;
|
11130 | }
|
11131 | if (!isValidPathString(pathString)) {
|
11132 | throw new Error(util.errorPrefix(fnName, argumentName) +
|
11133 | 'was an invalid path = "' +
|
11134 | pathString +
|
11135 | '". Paths must be non-empty strings and ' +
|
11136 | 'can\'t contain ".", "#", "$", "[", or "]"');
|
11137 | }
|
11138 | };
|
11139 | var validateRootPathString = function (fnName, argumentName, pathString, optional) {
|
11140 | if (pathString) {
|
11141 |
|
11142 | pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
|
11143 | }
|
11144 | validatePathString(fnName, argumentName, pathString, optional);
|
11145 | };
|
11146 | var validateWritablePath = function (fnName, path) {
|
11147 | if (pathGetFront(path) === '.info') {
|
11148 | throw new Error(fnName + " failed = Can't modify data under /.info/");
|
11149 | }
|
11150 | };
|
11151 | var validateUrl = function (fnName, parsedUrl) {
|
11152 |
|
11153 | var pathString = parsedUrl.path.toString();
|
11154 | if (!(typeof parsedUrl.repoInfo.host === 'string') ||
|
11155 | parsedUrl.repoInfo.host.length === 0 ||
|
11156 | (!isValidKey(parsedUrl.repoInfo.namespace) &&
|
11157 | parsedUrl.repoInfo.host.split(':')[0] !== 'localhost') ||
|
11158 | (pathString.length !== 0 && !isValidRootPathString(pathString))) {
|
11159 | throw new Error(util.errorPrefix(fnName, 'url') +
|
11160 | 'must be a valid firebase URL and ' +
|
11161 | 'the path can\'t contain ".", "#", "$", "[", or "]".');
|
11162 | }
|
11163 | };
|
11164 | var validateBoolean = function (fnName, argumentName, bool, optional) {
|
11165 | if (optional && bool === undefined) {
|
11166 | return;
|
11167 | }
|
11168 | if (typeof bool !== 'boolean') {
|
11169 | throw new Error(util.errorPrefix(fnName, argumentName) + 'must be a boolean.');
|
11170 | }
|
11171 | };
|
11172 |
|
11173 |
|
11174 |
|
11175 |
|
11176 |
|
11177 |
|
11178 |
|
11179 |
|
11180 |
|
11181 |
|
11182 |
|
11183 |
|
11184 |
|
11185 |
|
11186 |
|
11187 |
|
11188 |
|
11189 |
|
11190 |
|
11191 |
|
11192 |
|
11193 |
|
11194 |
|
11195 |
|
11196 |
|
11197 |
|
11198 |
|
11199 |
|
11200 |
|
11201 |
|
11202 | var EventQueue = (function () {
|
11203 | function EventQueue() {
|
11204 | this.eventLists_ = [];
|
11205 | |
11206 |
|
11207 |
|
11208 | this.recursionDepth_ = 0;
|
11209 | }
|
11210 | return EventQueue;
|
11211 | }());
|
11212 |
|
11213 |
|
11214 |
|
11215 | function eventQueueQueueEvents(eventQueue, eventDataList) {
|
11216 |
|
11217 | var currList = null;
|
11218 | for (var i = 0; i < eventDataList.length; i++) {
|
11219 | var data = eventDataList[i];
|
11220 | var path = data.getPath();
|
11221 | if (currList !== null && !pathEquals(path, currList.path)) {
|
11222 | eventQueue.eventLists_.push(currList);
|
11223 | currList = null;
|
11224 | }
|
11225 | if (currList === null) {
|
11226 | currList = { events: [], path: path };
|
11227 | }
|
11228 | currList.events.push(data);
|
11229 | }
|
11230 | if (currList) {
|
11231 | eventQueue.eventLists_.push(currList);
|
11232 | }
|
11233 | }
|
11234 |
|
11235 |
|
11236 |
|
11237 |
|
11238 |
|
11239 |
|
11240 |
|
11241 |
|
11242 |
|
11243 | function eventQueueRaiseEventsAtPath(eventQueue, path, eventDataList) {
|
11244 | eventQueueQueueEvents(eventQueue, eventDataList);
|
11245 | eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
|
11246 | return pathEquals(eventPath, path);
|
11247 | });
|
11248 | }
|
11249 |
|
11250 |
|
11251 |
|
11252 |
|
11253 |
|
11254 |
|
11255 |
|
11256 |
|
11257 |
|
11258 | function eventQueueRaiseEventsForChangedPath(eventQueue, changedPath, eventDataList) {
|
11259 | eventQueueQueueEvents(eventQueue, eventDataList);
|
11260 | eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
|
11261 | return pathContains(eventPath, changedPath) ||
|
11262 | pathContains(changedPath, eventPath);
|
11263 | });
|
11264 | }
|
11265 | function eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, predicate) {
|
11266 | eventQueue.recursionDepth_++;
|
11267 | var sentAll = true;
|
11268 | for (var i = 0; i < eventQueue.eventLists_.length; i++) {
|
11269 | var eventList = eventQueue.eventLists_[i];
|
11270 | if (eventList) {
|
11271 | var eventPath = eventList.path;
|
11272 | if (predicate(eventPath)) {
|
11273 | eventListRaise(eventQueue.eventLists_[i]);
|
11274 | eventQueue.eventLists_[i] = null;
|
11275 | }
|
11276 | else {
|
11277 | sentAll = false;
|
11278 | }
|
11279 | }
|
11280 | }
|
11281 | if (sentAll) {
|
11282 | eventQueue.eventLists_ = [];
|
11283 | }
|
11284 | eventQueue.recursionDepth_--;
|
11285 | }
|
11286 |
|
11287 |
|
11288 |
|
11289 | function eventListRaise(eventList) {
|
11290 | for (var i = 0; i < eventList.events.length; i++) {
|
11291 | var eventData = eventList.events[i];
|
11292 | if (eventData !== null) {
|
11293 | eventList.events[i] = null;
|
11294 | var eventFn = eventData.getEventRunner();
|
11295 | if (logger) {
|
11296 | log('event: ' + eventData.toString());
|
11297 | }
|
11298 | exceptionGuard(eventFn);
|
11299 | }
|
11300 | }
|
11301 | }
|
11302 |
|
11303 |
|
11304 |
|
11305 |
|
11306 |
|
11307 |
|
11308 |
|
11309 |
|
11310 |
|
11311 |
|
11312 |
|
11313 |
|
11314 |
|
11315 |
|
11316 |
|
11317 |
|
11318 |
|
11319 | var INTERRUPT_REASON = 'repo_interrupt';
|
11320 |
|
11321 |
|
11322 |
|
11323 |
|
11324 |
|
11325 | var MAX_TRANSACTION_RETRIES = 25;
|
11326 |
|
11327 |
|
11328 |
|
11329 | var Repo = (function () {
|
11330 | function Repo(repoInfo_, forceRestClient_, authTokenProvider_, appCheckProvider_) {
|
11331 | this.repoInfo_ = repoInfo_;
|
11332 | this.forceRestClient_ = forceRestClient_;
|
11333 | this.authTokenProvider_ = authTokenProvider_;
|
11334 | this.appCheckProvider_ = appCheckProvider_;
|
11335 | this.dataUpdateCount = 0;
|
11336 | this.statsListener_ = null;
|
11337 | this.eventQueue_ = new EventQueue();
|
11338 | this.nextWriteId_ = 1;
|
11339 | this.interceptServerDataCallback_ = null;
|
11340 |
|
11341 | this.onDisconnect_ = newSparseSnapshotTree();
|
11342 |
|
11343 | this.transactionQueueTree_ = new Tree();
|
11344 |
|
11345 | this.persistentConnection_ = null;
|
11346 |
|
11347 | this.key = this.repoInfo_.toURLString();
|
11348 | }
|
11349 | |
11350 |
|
11351 |
|
11352 | Repo.prototype.toString = function () {
|
11353 | return ((this.repoInfo_.secure ? 'https://' : 'http://') + this.repoInfo_.host);
|
11354 | };
|
11355 | return Repo;
|
11356 | }());
|
11357 | function repoStart(repo, appId, authOverride) {
|
11358 | repo.stats_ = statsManagerGetCollection(repo.repoInfo_);
|
11359 | if (repo.forceRestClient_ || beingCrawled()) {
|
11360 | repo.server_ = new ReadonlyRestClient(repo.repoInfo_, function (pathString, data, isMerge, tag) {
|
11361 | repoOnDataUpdate(repo, pathString, data, isMerge, tag);
|
11362 | }, repo.authTokenProvider_, repo.appCheckProvider_);
|
11363 |
|
11364 | setTimeout(function () { return repoOnConnectStatus(repo, true); }, 0);
|
11365 | }
|
11366 | else {
|
11367 |
|
11368 | if (typeof authOverride !== 'undefined' && authOverride !== null) {
|
11369 | if (typeof authOverride !== 'object') {
|
11370 | throw new Error('Only objects are supported for option databaseAuthVariableOverride');
|
11371 | }
|
11372 | try {
|
11373 | util.stringify(authOverride);
|
11374 | }
|
11375 | catch (e) {
|
11376 | throw new Error('Invalid authOverride provided: ' + e);
|
11377 | }
|
11378 | }
|
11379 | repo.persistentConnection_ = new PersistentConnection(repo.repoInfo_, appId, function (pathString, data, isMerge, tag) {
|
11380 | repoOnDataUpdate(repo, pathString, data, isMerge, tag);
|
11381 | }, function (connectStatus) {
|
11382 | repoOnConnectStatus(repo, connectStatus);
|
11383 | }, function (updates) {
|
11384 | repoOnServerInfoUpdate(repo, updates);
|
11385 | }, repo.authTokenProvider_, repo.appCheckProvider_, authOverride);
|
11386 | repo.server_ = repo.persistentConnection_;
|
11387 | }
|
11388 | repo.authTokenProvider_.addTokenChangeListener(function (token) {
|
11389 | repo.server_.refreshAuthToken(token);
|
11390 | });
|
11391 | repo.appCheckProvider_.addTokenChangeListener(function (result) {
|
11392 | repo.server_.refreshAppCheckToken(result.token);
|
11393 | });
|
11394 |
|
11395 |
|
11396 | repo.statsReporter_ = statsManagerGetOrCreateReporter(repo.repoInfo_, function () { return new StatsReporter(repo.stats_, repo.server_); });
|
11397 |
|
11398 | repo.infoData_ = new SnapshotHolder();
|
11399 | repo.infoSyncTree_ = new SyncTree({
|
11400 | startListening: function (query, tag, currentHashFn, onComplete) {
|
11401 | var infoEvents = [];
|
11402 | var node = repo.infoData_.getNode(query._path);
|
11403 |
|
11404 |
|
11405 | if (!node.isEmpty()) {
|
11406 | infoEvents = syncTreeApplyServerOverwrite(repo.infoSyncTree_, query._path, node);
|
11407 | setTimeout(function () {
|
11408 | onComplete('ok');
|
11409 | }, 0);
|
11410 | }
|
11411 | return infoEvents;
|
11412 | },
|
11413 | stopListening: function () { }
|
11414 | });
|
11415 | repoUpdateInfo(repo, 'connected', false);
|
11416 | repo.serverSyncTree_ = new SyncTree({
|
11417 | startListening: function (query, tag, currentHashFn, onComplete) {
|
11418 | repo.server_.listen(query, currentHashFn, tag, function (status, data) {
|
11419 | var events = onComplete(status, data);
|
11420 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, query._path, events);
|
11421 | });
|
11422 |
|
11423 | return [];
|
11424 | },
|
11425 | stopListening: function (query, tag) {
|
11426 | repo.server_.unlisten(query, tag);
|
11427 | }
|
11428 | });
|
11429 | }
|
11430 |
|
11431 |
|
11432 |
|
11433 | function repoServerTime(repo) {
|
11434 | var offsetNode = repo.infoData_.getNode(new Path('.info/serverTimeOffset'));
|
11435 | var offset = offsetNode.val() || 0;
|
11436 | return new Date().getTime() + offset;
|
11437 | }
|
11438 |
|
11439 |
|
11440 |
|
11441 | function repoGenerateServerValues(repo) {
|
11442 | return generateWithValues({
|
11443 | timestamp: repoServerTime(repo)
|
11444 | });
|
11445 | }
|
11446 |
|
11447 |
|
11448 |
|
11449 | function repoOnDataUpdate(repo, pathString, data, isMerge, tag) {
|
11450 |
|
11451 | repo.dataUpdateCount++;
|
11452 | var path = new Path(pathString);
|
11453 | data = repo.interceptServerDataCallback_
|
11454 | ? repo.interceptServerDataCallback_(pathString, data)
|
11455 | : data;
|
11456 | var events = [];
|
11457 | if (tag) {
|
11458 | if (isMerge) {
|
11459 | var taggedChildren = util.map(data, function (raw) { return nodeFromJSON$1(raw); });
|
11460 | events = syncTreeApplyTaggedQueryMerge(repo.serverSyncTree_, path, taggedChildren, tag);
|
11461 | }
|
11462 | else {
|
11463 | var taggedSnap = nodeFromJSON$1(data);
|
11464 | events = syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, path, taggedSnap, tag);
|
11465 | }
|
11466 | }
|
11467 | else if (isMerge) {
|
11468 | var changedChildren = util.map(data, function (raw) { return nodeFromJSON$1(raw); });
|
11469 | events = syncTreeApplyServerMerge(repo.serverSyncTree_, path, changedChildren);
|
11470 | }
|
11471 | else {
|
11472 | var snap = nodeFromJSON$1(data);
|
11473 | events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap);
|
11474 | }
|
11475 | var affectedPath = path;
|
11476 | if (events.length > 0) {
|
11477 |
|
11478 |
|
11479 | affectedPath = repoRerunTransactions(repo, path);
|
11480 | }
|
11481 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, events);
|
11482 | }
|
11483 |
|
11484 | function repoInterceptServerData(repo, callback) {
|
11485 | repo.interceptServerDataCallback_ = callback;
|
11486 | }
|
11487 | function repoOnConnectStatus(repo, connectStatus) {
|
11488 | repoUpdateInfo(repo, 'connected', connectStatus);
|
11489 | if (connectStatus === false) {
|
11490 | repoRunOnDisconnectEvents(repo);
|
11491 | }
|
11492 | }
|
11493 | function repoOnServerInfoUpdate(repo, updates) {
|
11494 | each(updates, function (key, value) {
|
11495 | repoUpdateInfo(repo, key, value);
|
11496 | });
|
11497 | }
|
11498 | function repoUpdateInfo(repo, pathString, value) {
|
11499 | var path = new Path('/.info/' + pathString);
|
11500 | var newNode = nodeFromJSON$1(value);
|
11501 | repo.infoData_.updateSnapshot(path, newNode);
|
11502 | var events = syncTreeApplyServerOverwrite(repo.infoSyncTree_, path, newNode);
|
11503 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
11504 | }
|
11505 | function repoGetNextWriteId(repo) {
|
11506 | return repo.nextWriteId_++;
|
11507 | }
|
11508 |
|
11509 |
|
11510 |
|
11511 |
|
11512 |
|
11513 |
|
11514 |
|
11515 |
|
11516 |
|
11517 |
|
11518 |
|
11519 |
|
11520 |
|
11521 |
|
11522 |
|
11523 | function repoGetValue(repo, query) {
|
11524 |
|
11525 | var cached = syncTreeGetServerValue(repo.serverSyncTree_, query);
|
11526 | if (cached != null) {
|
11527 | return Promise.resolve(cached);
|
11528 | }
|
11529 | return repo.server_.get(query).then(function (payload) {
|
11530 | var node = nodeFromJSON$1(payload).withIndex(query._queryParams.getIndex());
|
11531 | var events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, query._path, node);
|
11532 | eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
|
11533 | return Promise.resolve(node);
|
11534 | }, function (err) {
|
11535 | repoLog(repo, 'get for query ' + util.stringify(query) + ' failed: ' + err);
|
11536 | return Promise.reject(new Error(err));
|
11537 | });
|
11538 | }
|
11539 | function repoSetWithPriority(repo, path, newVal, newPriority, onComplete) {
|
11540 | repoLog(repo, 'set', {
|
11541 | path: path.toString(),
|
11542 | value: newVal,
|
11543 | priority: newPriority
|
11544 | });
|
11545 |
|
11546 |
|
11547 | var serverValues = repoGenerateServerValues(repo);
|
11548 | var newNodeUnresolved = nodeFromJSON$1(newVal, newPriority);
|
11549 | var existing = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path);
|
11550 | var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, existing, serverValues);
|
11551 | var writeId = repoGetNextWriteId(repo);
|
11552 | var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, writeId, true);
|
11553 | eventQueueQueueEvents(repo.eventQueue_, events);
|
11554 | repo.server_.put(path.toString(), newNodeUnresolved.val( true), function (status, errorReason) {
|
11555 | var success = status === 'ok';
|
11556 | if (!success) {
|
11557 | warn('set at ' + path + ' failed: ' + status);
|
11558 | }
|
11559 | var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId, !success);
|
11560 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, clearEvents);
|
11561 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11562 | });
|
11563 | var affectedPath = repoAbortTransactions(repo, path);
|
11564 | repoRerunTransactions(repo, affectedPath);
|
11565 |
|
11566 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, []);
|
11567 | }
|
11568 | function repoUpdate(repo, path, childrenToMerge, onComplete) {
|
11569 | repoLog(repo, 'update', { path: path.toString(), value: childrenToMerge });
|
11570 |
|
11571 | var empty = true;
|
11572 | var serverValues = repoGenerateServerValues(repo);
|
11573 | var changedChildren = {};
|
11574 | each(childrenToMerge, function (changedKey, changedValue) {
|
11575 | empty = false;
|
11576 | changedChildren[changedKey] = resolveDeferredValueTree(pathChild(path, changedKey), nodeFromJSON$1(changedValue), repo.serverSyncTree_, serverValues);
|
11577 | });
|
11578 | if (!empty) {
|
11579 | var writeId_1 = repoGetNextWriteId(repo);
|
11580 | var events = syncTreeApplyUserMerge(repo.serverSyncTree_, path, changedChildren, writeId_1);
|
11581 | eventQueueQueueEvents(repo.eventQueue_, events);
|
11582 | repo.server_.merge(path.toString(), childrenToMerge, function (status, errorReason) {
|
11583 | var success = status === 'ok';
|
11584 | if (!success) {
|
11585 | warn('update at ' + path + ' failed: ' + status);
|
11586 | }
|
11587 | var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId_1, !success);
|
11588 | var affectedPath = clearEvents.length > 0 ? repoRerunTransactions(repo, path) : path;
|
11589 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, clearEvents);
|
11590 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11591 | });
|
11592 | each(childrenToMerge, function (changedPath) {
|
11593 | var affectedPath = repoAbortTransactions(repo, pathChild(path, changedPath));
|
11594 | repoRerunTransactions(repo, affectedPath);
|
11595 | });
|
11596 |
|
11597 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, []);
|
11598 | }
|
11599 | else {
|
11600 | log("update() called with empty data. Don't do anything.");
|
11601 | repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
|
11602 | }
|
11603 | }
|
11604 |
|
11605 |
|
11606 |
|
11607 | function repoRunOnDisconnectEvents(repo) {
|
11608 | repoLog(repo, 'onDisconnectEvents');
|
11609 | var serverValues = repoGenerateServerValues(repo);
|
11610 | var resolvedOnDisconnectTree = newSparseSnapshotTree();
|
11611 | sparseSnapshotTreeForEachTree(repo.onDisconnect_, newEmptyPath(), function (path, node) {
|
11612 | var resolved = resolveDeferredValueTree(path, node, repo.serverSyncTree_, serverValues);
|
11613 | sparseSnapshotTreeRemember(resolvedOnDisconnectTree, path, resolved);
|
11614 | });
|
11615 | var events = [];
|
11616 | sparseSnapshotTreeForEachTree(resolvedOnDisconnectTree, newEmptyPath(), function (path, snap) {
|
11617 | events = events.concat(syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap));
|
11618 | var affectedPath = repoAbortTransactions(repo, path);
|
11619 | repoRerunTransactions(repo, affectedPath);
|
11620 | });
|
11621 | repo.onDisconnect_ = newSparseSnapshotTree();
|
11622 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, newEmptyPath(), events);
|
11623 | }
|
11624 | function repoOnDisconnectCancel(repo, path, onComplete) {
|
11625 | repo.server_.onDisconnectCancel(path.toString(), function (status, errorReason) {
|
11626 | if (status === 'ok') {
|
11627 | sparseSnapshotTreeForget(repo.onDisconnect_, path);
|
11628 | }
|
11629 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11630 | });
|
11631 | }
|
11632 | function repoOnDisconnectSet(repo, path, value, onComplete) {
|
11633 | var newNode = nodeFromJSON$1(value);
|
11634 | repo.server_.onDisconnectPut(path.toString(), newNode.val( true), function (status, errorReason) {
|
11635 | if (status === 'ok') {
|
11636 | sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
|
11637 | }
|
11638 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11639 | });
|
11640 | }
|
11641 | function repoOnDisconnectSetWithPriority(repo, path, value, priority, onComplete) {
|
11642 | var newNode = nodeFromJSON$1(value, priority);
|
11643 | repo.server_.onDisconnectPut(path.toString(), newNode.val( true), function (status, errorReason) {
|
11644 | if (status === 'ok') {
|
11645 | sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
|
11646 | }
|
11647 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11648 | });
|
11649 | }
|
11650 | function repoOnDisconnectUpdate(repo, path, childrenToMerge, onComplete) {
|
11651 | if (util.isEmpty(childrenToMerge)) {
|
11652 | log("onDisconnect().update() called with empty data. Don't do anything.");
|
11653 | repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
|
11654 | return;
|
11655 | }
|
11656 | repo.server_.onDisconnectMerge(path.toString(), childrenToMerge, function (status, errorReason) {
|
11657 | if (status === 'ok') {
|
11658 | each(childrenToMerge, function (childName, childNode) {
|
11659 | var newChildNode = nodeFromJSON$1(childNode);
|
11660 | sparseSnapshotTreeRemember(repo.onDisconnect_, pathChild(path, childName), newChildNode);
|
11661 | });
|
11662 | }
|
11663 | repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
|
11664 | });
|
11665 | }
|
11666 | function repoAddEventCallbackForQuery(repo, query, eventRegistration) {
|
11667 | var events;
|
11668 | if (pathGetFront(query._path) === '.info') {
|
11669 | events = syncTreeAddEventRegistration(repo.infoSyncTree_, query, eventRegistration);
|
11670 | }
|
11671 | else {
|
11672 | events = syncTreeAddEventRegistration(repo.serverSyncTree_, query, eventRegistration);
|
11673 | }
|
11674 | eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
|
11675 | }
|
11676 | function repoRemoveEventCallbackForQuery(repo, query, eventRegistration) {
|
11677 |
|
11678 |
|
11679 | var events;
|
11680 | if (pathGetFront(query._path) === '.info') {
|
11681 | events = syncTreeRemoveEventRegistration(repo.infoSyncTree_, query, eventRegistration);
|
11682 | }
|
11683 | else {
|
11684 | events = syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, eventRegistration);
|
11685 | }
|
11686 | eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
|
11687 | }
|
11688 | function repoInterrupt(repo) {
|
11689 | if (repo.persistentConnection_) {
|
11690 | repo.persistentConnection_.interrupt(INTERRUPT_REASON);
|
11691 | }
|
11692 | }
|
11693 | function repoResume(repo) {
|
11694 | if (repo.persistentConnection_) {
|
11695 | repo.persistentConnection_.resume(INTERRUPT_REASON);
|
11696 | }
|
11697 | }
|
11698 | function repoStats(repo, showDelta) {
|
11699 | if (showDelta === void 0) { showDelta = false; }
|
11700 | if (typeof console === 'undefined') {
|
11701 | return;
|
11702 | }
|
11703 | var stats;
|
11704 | if (showDelta) {
|
11705 | if (!repo.statsListener_) {
|
11706 | repo.statsListener_ = new StatsListener(repo.stats_);
|
11707 | }
|
11708 | stats = repo.statsListener_.get();
|
11709 | }
|
11710 | else {
|
11711 | stats = repo.stats_.get();
|
11712 | }
|
11713 | var longestName = Object.keys(stats).reduce(function (previousValue, currentValue) {
|
11714 | return Math.max(currentValue.length, previousValue);
|
11715 | }, 0);
|
11716 | each(stats, function (stat, value) {
|
11717 | var paddedStat = stat;
|
11718 |
|
11719 | for (var i = stat.length; i < longestName + 2; i++) {
|
11720 | paddedStat += ' ';
|
11721 | }
|
11722 | console.log(paddedStat + value);
|
11723 | });
|
11724 | }
|
11725 | function repoStatsIncrementCounter(repo, metric) {
|
11726 | repo.stats_.incrementCounter(metric);
|
11727 | statsReporterIncludeStat(repo.statsReporter_, metric);
|
11728 | }
|
11729 | function repoLog(repo) {
|
11730 | var varArgs = [];
|
11731 | for (var _i = 1; _i < arguments.length; _i++) {
|
11732 | varArgs[_i - 1] = arguments[_i];
|
11733 | }
|
11734 | var prefix = '';
|
11735 | if (repo.persistentConnection_) {
|
11736 | prefix = repo.persistentConnection_.id + ':';
|
11737 | }
|
11738 | log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs)));
|
11739 | }
|
11740 | function repoCallOnCompleteCallback(repo, callback, status, errorReason) {
|
11741 | if (callback) {
|
11742 | exceptionGuard(function () {
|
11743 | if (status === 'ok') {
|
11744 | callback(null);
|
11745 | }
|
11746 | else {
|
11747 | var code = (status || 'error').toUpperCase();
|
11748 | var message = code;
|
11749 | if (errorReason) {
|
11750 | message += ': ' + errorReason;
|
11751 | }
|
11752 | var error = new Error(message);
|
11753 |
|
11754 | error.code = code;
|
11755 | callback(error);
|
11756 | }
|
11757 | });
|
11758 | }
|
11759 | }
|
11760 |
|
11761 |
|
11762 |
|
11763 |
|
11764 |
|
11765 |
|
11766 |
|
11767 |
|
11768 |
|
11769 |
|
11770 |
|
11771 | function repoStartTransaction(repo, path, transactionUpdate, onComplete, unwatcher, applyLocally) {
|
11772 | repoLog(repo, 'transaction on ' + path);
|
11773 |
|
11774 | var transaction = {
|
11775 | path: path,
|
11776 | update: transactionUpdate,
|
11777 | onComplete: onComplete,
|
11778 |
|
11779 | status: null,
|
11780 |
|
11781 |
|
11782 | order: LUIDGenerator(),
|
11783 |
|
11784 | applyLocally: applyLocally,
|
11785 |
|
11786 | retryCount: 0,
|
11787 |
|
11788 | unwatcher: unwatcher,
|
11789 |
|
11790 | abortReason: null,
|
11791 | currentWriteId: null,
|
11792 | currentInputSnapshot: null,
|
11793 | currentOutputSnapshotRaw: null,
|
11794 | currentOutputSnapshotResolved: null
|
11795 | };
|
11796 |
|
11797 | var currentState = repoGetLatestState(repo, path, undefined);
|
11798 | transaction.currentInputSnapshot = currentState;
|
11799 | var newVal = transaction.update(currentState.val());
|
11800 | if (newVal === undefined) {
|
11801 |
|
11802 | transaction.unwatcher();
|
11803 | transaction.currentOutputSnapshotRaw = null;
|
11804 | transaction.currentOutputSnapshotResolved = null;
|
11805 | if (transaction.onComplete) {
|
11806 | transaction.onComplete(null, false, transaction.currentInputSnapshot);
|
11807 | }
|
11808 | }
|
11809 | else {
|
11810 | validateFirebaseData('transaction failed: Data returned ', newVal, transaction.path);
|
11811 |
|
11812 | transaction.status = 0 ;
|
11813 | var queueNode = treeSubTree(repo.transactionQueueTree_, path);
|
11814 | var nodeQueue = treeGetValue(queueNode) || [];
|
11815 | nodeQueue.push(transaction);
|
11816 | treeSetValue(queueNode, nodeQueue);
|
11817 |
|
11818 |
|
11819 |
|
11820 |
|
11821 | var priorityForNode = void 0;
|
11822 | if (typeof newVal === 'object' &&
|
11823 | newVal !== null &&
|
11824 | util.contains(newVal, '.priority')) {
|
11825 |
|
11826 | priorityForNode = util.safeGet(newVal, '.priority');
|
11827 | util.assert(isValidPriority(priorityForNode), 'Invalid priority returned by transaction. ' +
|
11828 | 'Priority must be a valid string, finite number, server value, or null.');
|
11829 | }
|
11830 | else {
|
11831 | var currentNode = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path) ||
|
11832 | ChildrenNode.EMPTY_NODE;
|
11833 | priorityForNode = currentNode.getPriority().val();
|
11834 | }
|
11835 | var serverValues = repoGenerateServerValues(repo);
|
11836 | var newNodeUnresolved = nodeFromJSON$1(newVal, priorityForNode);
|
11837 | var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, currentState, serverValues);
|
11838 | transaction.currentOutputSnapshotRaw = newNodeUnresolved;
|
11839 | transaction.currentOutputSnapshotResolved = newNode;
|
11840 | transaction.currentWriteId = repoGetNextWriteId(repo);
|
11841 | var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, transaction.currentWriteId, transaction.applyLocally);
|
11842 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
11843 | repoSendReadyTransactions(repo, repo.transactionQueueTree_);
|
11844 | }
|
11845 | }
|
11846 |
|
11847 |
|
11848 |
|
11849 | function repoGetLatestState(repo, path, excludeSets) {
|
11850 | return (syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path, excludeSets) ||
|
11851 | ChildrenNode.EMPTY_NODE);
|
11852 | }
|
11853 |
|
11854 |
|
11855 |
|
11856 |
|
11857 |
|
11858 |
|
11859 |
|
11860 |
|
11861 |
|
11862 | function repoSendReadyTransactions(repo, node) {
|
11863 | if (node === void 0) { node = repo.transactionQueueTree_; }
|
11864 |
|
11865 | if (!node) {
|
11866 | repoPruneCompletedTransactionsBelowNode(repo, node);
|
11867 | }
|
11868 | if (treeGetValue(node)) {
|
11869 | var queue = repoBuildTransactionQueue(repo, node);
|
11870 | util.assert(queue.length > 0, 'Sending zero length transaction queue');
|
11871 | var allRun = queue.every(function (transaction) { return transaction.status === 0 ; });
|
11872 |
|
11873 | if (allRun) {
|
11874 | repoSendTransactionQueue(repo, treeGetPath(node), queue);
|
11875 | }
|
11876 | }
|
11877 | else if (treeHasChildren(node)) {
|
11878 | treeForEachChild(node, function (childNode) {
|
11879 | repoSendReadyTransactions(repo, childNode);
|
11880 | });
|
11881 | }
|
11882 | }
|
11883 |
|
11884 |
|
11885 |
|
11886 |
|
11887 |
|
11888 |
|
11889 |
|
11890 | function repoSendTransactionQueue(repo, path, queue) {
|
11891 |
|
11892 | var setsToIgnore = queue.map(function (txn) {
|
11893 | return txn.currentWriteId;
|
11894 | });
|
11895 | var latestState = repoGetLatestState(repo, path, setsToIgnore);
|
11896 | var snapToSend = latestState;
|
11897 | var latestHash = latestState.hash();
|
11898 | for (var i = 0; i < queue.length; i++) {
|
11899 | var txn = queue[i];
|
11900 | util.assert(txn.status === 0 , 'tryToSendTransactionQueue_: items in queue should all be run.');
|
11901 | txn.status = 1 ;
|
11902 | txn.retryCount++;
|
11903 | var relativePath = newRelativePath(path, txn.path);
|
11904 |
|
11905 | snapToSend = snapToSend.updateChild(relativePath , txn.currentOutputSnapshotRaw);
|
11906 | }
|
11907 | var dataToSend = snapToSend.val(true);
|
11908 | var pathToSend = path;
|
11909 |
|
11910 | repo.server_.put(pathToSend.toString(), dataToSend, function (status) {
|
11911 | repoLog(repo, 'transaction put response', {
|
11912 | path: pathToSend.toString(),
|
11913 | status: status
|
11914 | });
|
11915 | var events = [];
|
11916 | if (status === 'ok') {
|
11917 |
|
11918 |
|
11919 |
|
11920 | var callbacks = [];
|
11921 | var _loop_1 = function (i) {
|
11922 | queue[i].status = 2 ;
|
11923 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId));
|
11924 | if (queue[i].onComplete) {
|
11925 |
|
11926 |
|
11927 | callbacks.push(function () {
|
11928 | return queue[i].onComplete(null, true, queue[i].currentOutputSnapshotResolved);
|
11929 | });
|
11930 | }
|
11931 | queue[i].unwatcher();
|
11932 | };
|
11933 | for (var i = 0; i < queue.length; i++) {
|
11934 | _loop_1(i);
|
11935 | }
|
11936 |
|
11937 | repoPruneCompletedTransactionsBelowNode(repo, treeSubTree(repo.transactionQueueTree_, path));
|
11938 |
|
11939 | repoSendReadyTransactions(repo, repo.transactionQueueTree_);
|
11940 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
11941 |
|
11942 | for (var i = 0; i < callbacks.length; i++) {
|
11943 | exceptionGuard(callbacks[i]);
|
11944 | }
|
11945 | }
|
11946 | else {
|
11947 |
|
11948 | if (status === 'datastale') {
|
11949 | for (var i = 0; i < queue.length; i++) {
|
11950 | if (queue[i].status === 3 ) {
|
11951 | queue[i].status = 4 ;
|
11952 | }
|
11953 | else {
|
11954 | queue[i].status = 0 ;
|
11955 | }
|
11956 | }
|
11957 | }
|
11958 | else {
|
11959 | warn('transaction at ' + pathToSend.toString() + ' failed: ' + status);
|
11960 | for (var i = 0; i < queue.length; i++) {
|
11961 | queue[i].status = 4 ;
|
11962 | queue[i].abortReason = status;
|
11963 | }
|
11964 | }
|
11965 | repoRerunTransactions(repo, path);
|
11966 | }
|
11967 | }, latestHash);
|
11968 | }
|
11969 |
|
11970 |
|
11971 |
|
11972 |
|
11973 |
|
11974 |
|
11975 |
|
11976 |
|
11977 |
|
11978 |
|
11979 |
|
11980 | function repoRerunTransactions(repo, changedPath) {
|
11981 | var rootMostTransactionNode = repoGetAncestorTransactionNode(repo, changedPath);
|
11982 | var path = treeGetPath(rootMostTransactionNode);
|
11983 | var queue = repoBuildTransactionQueue(repo, rootMostTransactionNode);
|
11984 | repoRerunTransactionQueue(repo, queue, path);
|
11985 | return path;
|
11986 | }
|
11987 |
|
11988 |
|
11989 |
|
11990 |
|
11991 |
|
11992 |
|
11993 |
|
11994 | function repoRerunTransactionQueue(repo, queue, path) {
|
11995 | if (queue.length === 0) {
|
11996 | return;
|
11997 | }
|
11998 |
|
11999 |
|
12000 |
|
12001 | var callbacks = [];
|
12002 | var events = [];
|
12003 |
|
12004 | var txnsToRerun = queue.filter(function (q) {
|
12005 | return q.status === 0 ;
|
12006 | });
|
12007 | var setsToIgnore = txnsToRerun.map(function (q) {
|
12008 | return q.currentWriteId;
|
12009 | });
|
12010 | var _loop_2 = function (i) {
|
12011 | var transaction = queue[i];
|
12012 | var relativePath = newRelativePath(path, transaction.path);
|
12013 | var abortTransaction = false, abortReason;
|
12014 | util.assert(relativePath !== null, 'rerunTransactionsUnderNode_: relativePath should not be null.');
|
12015 | if (transaction.status === 4 ) {
|
12016 | abortTransaction = true;
|
12017 | abortReason = transaction.abortReason;
|
12018 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
|
12019 | }
|
12020 | else if (transaction.status === 0 ) {
|
12021 | if (transaction.retryCount >= MAX_TRANSACTION_RETRIES) {
|
12022 | abortTransaction = true;
|
12023 | abortReason = 'maxretry';
|
12024 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
|
12025 | }
|
12026 | else {
|
12027 |
|
12028 | var currentNode = repoGetLatestState(repo, transaction.path, setsToIgnore);
|
12029 | transaction.currentInputSnapshot = currentNode;
|
12030 | var newData = queue[i].update(currentNode.val());
|
12031 | if (newData !== undefined) {
|
12032 | validateFirebaseData('transaction failed: Data returned ', newData, transaction.path);
|
12033 | var newDataNode = nodeFromJSON$1(newData);
|
12034 | var hasExplicitPriority = typeof newData === 'object' &&
|
12035 | newData != null &&
|
12036 | util.contains(newData, '.priority');
|
12037 | if (!hasExplicitPriority) {
|
12038 |
|
12039 | newDataNode = newDataNode.updatePriority(currentNode.getPriority());
|
12040 | }
|
12041 | var oldWriteId = transaction.currentWriteId;
|
12042 | var serverValues = repoGenerateServerValues(repo);
|
12043 | var newNodeResolved = resolveDeferredValueSnapshot(newDataNode, currentNode, serverValues);
|
12044 | transaction.currentOutputSnapshotRaw = newDataNode;
|
12045 | transaction.currentOutputSnapshotResolved = newNodeResolved;
|
12046 | transaction.currentWriteId = repoGetNextWriteId(repo);
|
12047 |
|
12048 | setsToIgnore.splice(setsToIgnore.indexOf(oldWriteId), 1);
|
12049 | events = events.concat(syncTreeApplyUserOverwrite(repo.serverSyncTree_, transaction.path, newNodeResolved, transaction.currentWriteId, transaction.applyLocally));
|
12050 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, oldWriteId, true));
|
12051 | }
|
12052 | else {
|
12053 | abortTransaction = true;
|
12054 | abortReason = 'nodata';
|
12055 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
|
12056 | }
|
12057 | }
|
12058 | }
|
12059 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
|
12060 | events = [];
|
12061 | if (abortTransaction) {
|
12062 |
|
12063 | queue[i].status = 2 ;
|
12064 |
|
12065 |
|
12066 |
|
12067 | (function (unwatcher) {
|
12068 | setTimeout(unwatcher, Math.floor(0));
|
12069 | })(queue[i].unwatcher);
|
12070 | if (queue[i].onComplete) {
|
12071 | if (abortReason === 'nodata') {
|
12072 | callbacks.push(function () {
|
12073 | return queue[i].onComplete(null, false, queue[i].currentInputSnapshot);
|
12074 | });
|
12075 | }
|
12076 | else {
|
12077 | callbacks.push(function () {
|
12078 | return queue[i].onComplete(new Error(abortReason), false, null);
|
12079 | });
|
12080 | }
|
12081 | }
|
12082 | }
|
12083 | };
|
12084 | for (var i = 0; i < queue.length; i++) {
|
12085 | _loop_2(i);
|
12086 | }
|
12087 |
|
12088 | repoPruneCompletedTransactionsBelowNode(repo, repo.transactionQueueTree_);
|
12089 |
|
12090 | for (var i = 0; i < callbacks.length; i++) {
|
12091 | exceptionGuard(callbacks[i]);
|
12092 | }
|
12093 |
|
12094 | repoSendReadyTransactions(repo, repo.transactionQueueTree_);
|
12095 | }
|
12096 |
|
12097 |
|
12098 |
|
12099 |
|
12100 |
|
12101 |
|
12102 |
|
12103 |
|
12104 | function repoGetAncestorTransactionNode(repo, path) {
|
12105 | var front;
|
12106 |
|
12107 |
|
12108 | var transactionNode = repo.transactionQueueTree_;
|
12109 | front = pathGetFront(path);
|
12110 | while (front !== null && treeGetValue(transactionNode) === undefined) {
|
12111 | transactionNode = treeSubTree(transactionNode, front);
|
12112 | path = pathPopFront(path);
|
12113 | front = pathGetFront(path);
|
12114 | }
|
12115 | return transactionNode;
|
12116 | }
|
12117 |
|
12118 |
|
12119 |
|
12120 |
|
12121 |
|
12122 |
|
12123 |
|
12124 | function repoBuildTransactionQueue(repo, transactionNode) {
|
12125 |
|
12126 | var transactionQueue = [];
|
12127 | repoAggregateTransactionQueuesForNode(repo, transactionNode, transactionQueue);
|
12128 |
|
12129 | transactionQueue.sort(function (a, b) { return a.order - b.order; });
|
12130 | return transactionQueue;
|
12131 | }
|
12132 | function repoAggregateTransactionQueuesForNode(repo, node, queue) {
|
12133 | var nodeQueue = treeGetValue(node);
|
12134 | if (nodeQueue) {
|
12135 | for (var i = 0; i < nodeQueue.length; i++) {
|
12136 | queue.push(nodeQueue[i]);
|
12137 | }
|
12138 | }
|
12139 | treeForEachChild(node, function (child) {
|
12140 | repoAggregateTransactionQueuesForNode(repo, child, queue);
|
12141 | });
|
12142 | }
|
12143 |
|
12144 |
|
12145 |
|
12146 | function repoPruneCompletedTransactionsBelowNode(repo, node) {
|
12147 | var queue = treeGetValue(node);
|
12148 | if (queue) {
|
12149 | var to = 0;
|
12150 | for (var from = 0; from < queue.length; from++) {
|
12151 | if (queue[from].status !== 2 ) {
|
12152 | queue[to] = queue[from];
|
12153 | to++;
|
12154 | }
|
12155 | }
|
12156 | queue.length = to;
|
12157 | treeSetValue(node, queue.length > 0 ? queue : undefined);
|
12158 | }
|
12159 | treeForEachChild(node, function (childNode) {
|
12160 | repoPruneCompletedTransactionsBelowNode(repo, childNode);
|
12161 | });
|
12162 | }
|
12163 |
|
12164 |
|
12165 |
|
12166 |
|
12167 |
|
12168 |
|
12169 |
|
12170 | function repoAbortTransactions(repo, path) {
|
12171 | var affectedPath = treeGetPath(repoGetAncestorTransactionNode(repo, path));
|
12172 | var transactionNode = treeSubTree(repo.transactionQueueTree_, path);
|
12173 | treeForEachAncestor(transactionNode, function (node) {
|
12174 | repoAbortTransactionsOnNode(repo, node);
|
12175 | });
|
12176 | repoAbortTransactionsOnNode(repo, transactionNode);
|
12177 | treeForEachDescendant(transactionNode, function (node) {
|
12178 | repoAbortTransactionsOnNode(repo, node);
|
12179 | });
|
12180 | return affectedPath;
|
12181 | }
|
12182 |
|
12183 |
|
12184 |
|
12185 |
|
12186 |
|
12187 | function repoAbortTransactionsOnNode(repo, node) {
|
12188 | var queue = treeGetValue(node);
|
12189 | if (queue) {
|
12190 |
|
12191 |
|
12192 |
|
12193 | var callbacks = [];
|
12194 |
|
12195 |
|
12196 | var events = [];
|
12197 | var lastSent = -1;
|
12198 | for (var i = 0; i < queue.length; i++) {
|
12199 | if (queue[i].status === 3 ) ;
|
12200 | else if (queue[i].status === 1 ) {
|
12201 | util.assert(lastSent === i - 1, 'All SENT items should be at beginning of queue.');
|
12202 | lastSent = i;
|
12203 |
|
12204 | queue[i].status = 3 ;
|
12205 | queue[i].abortReason = 'set';
|
12206 | }
|
12207 | else {
|
12208 | util.assert(queue[i].status === 0 , 'Unexpected transaction status in abort');
|
12209 |
|
12210 | queue[i].unwatcher();
|
12211 | events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId, true));
|
12212 | if (queue[i].onComplete) {
|
12213 | callbacks.push(queue[i].onComplete.bind(null, new Error('set'), false, null));
|
12214 | }
|
12215 | }
|
12216 | }
|
12217 | if (lastSent === -1) {
|
12218 |
|
12219 | treeSetValue(node, undefined);
|
12220 | }
|
12221 | else {
|
12222 |
|
12223 | queue.length = lastSent + 1;
|
12224 | }
|
12225 |
|
12226 | eventQueueRaiseEventsForChangedPath(repo.eventQueue_, treeGetPath(node), events);
|
12227 | for (var i = 0; i < callbacks.length; i++) {
|
12228 | exceptionGuard(callbacks[i]);
|
12229 | }
|
12230 | }
|
12231 | }
|
12232 |
|
12233 |
|
12234 |
|
12235 |
|
12236 |
|
12237 |
|
12238 |
|
12239 |
|
12240 |
|
12241 |
|
12242 |
|
12243 |
|
12244 |
|
12245 |
|
12246 |
|
12247 |
|
12248 |
|
12249 | function decodePath(pathString) {
|
12250 | var pathStringDecoded = '';
|
12251 | var pieces = pathString.split('/');
|
12252 | for (var i = 0; i < pieces.length; i++) {
|
12253 | if (pieces[i].length > 0) {
|
12254 | var piece = pieces[i];
|
12255 | try {
|
12256 | piece = decodeURIComponent(piece.replace(/\+/g, ' '));
|
12257 | }
|
12258 | catch (e) { }
|
12259 | pathStringDecoded += '/' + piece;
|
12260 | }
|
12261 | }
|
12262 | return pathStringDecoded;
|
12263 | }
|
12264 |
|
12265 |
|
12266 |
|
12267 | function decodeQuery(queryString) {
|
12268 | var e_1, _a;
|
12269 | var results = {};
|
12270 | if (queryString.charAt(0) === '?') {
|
12271 | queryString = queryString.substring(1);
|
12272 | }
|
12273 | try {
|
12274 | for (var _b = tslib.__values(queryString.split('&')), _c = _b.next(); !_c.done; _c = _b.next()) {
|
12275 | var segment = _c.value;
|
12276 | if (segment.length === 0) {
|
12277 | continue;
|
12278 | }
|
12279 | var kv = segment.split('=');
|
12280 | if (kv.length === 2) {
|
12281 | results[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
|
12282 | }
|
12283 | else {
|
12284 | warn("Invalid query segment '" + segment + "' in query '" + queryString + "'");
|
12285 | }
|
12286 | }
|
12287 | }
|
12288 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
12289 | finally {
|
12290 | try {
|
12291 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
12292 | }
|
12293 | finally { if (e_1) throw e_1.error; }
|
12294 | }
|
12295 | return results;
|
12296 | }
|
12297 | var parseRepoInfo = function (dataURL, nodeAdmin) {
|
12298 | var parsedUrl = parseDatabaseURL(dataURL), namespace = parsedUrl.namespace;
|
12299 | if (parsedUrl.domain === 'firebase.com') {
|
12300 | fatal(parsedUrl.host +
|
12301 | ' is no longer supported. ' +
|
12302 | 'Please use <YOUR FIREBASE>.firebaseio.com instead');
|
12303 | }
|
12304 |
|
12305 | if ((!namespace || namespace === 'undefined') &&
|
12306 | parsedUrl.domain !== 'localhost') {
|
12307 | fatal('Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com');
|
12308 | }
|
12309 | if (!parsedUrl.secure) {
|
12310 | warnIfPageIsSecure();
|
12311 | }
|
12312 | var webSocketOnly = parsedUrl.scheme === 'ws' || parsedUrl.scheme === 'wss';
|
12313 | return {
|
12314 | repoInfo: new RepoInfo(parsedUrl.host, parsedUrl.secure, namespace, nodeAdmin, webSocketOnly,
|
12315 | '',
|
12316 | namespace !== parsedUrl.subdomain),
|
12317 | path: new Path(parsedUrl.pathString)
|
12318 | };
|
12319 | };
|
12320 | var parseDatabaseURL = function (dataURL) {
|
12321 |
|
12322 | var host = '', domain = '', subdomain = '', pathString = '', namespace = '';
|
12323 |
|
12324 | var secure = true, scheme = 'https', port = 443;
|
12325 |
|
12326 | if (typeof dataURL === 'string') {
|
12327 |
|
12328 | var colonInd = dataURL.indexOf('//');
|
12329 | if (colonInd >= 0) {
|
12330 | scheme = dataURL.substring(0, colonInd - 1);
|
12331 | dataURL = dataURL.substring(colonInd + 2);
|
12332 | }
|
12333 |
|
12334 | var slashInd = dataURL.indexOf('/');
|
12335 | if (slashInd === -1) {
|
12336 | slashInd = dataURL.length;
|
12337 | }
|
12338 | var questionMarkInd = dataURL.indexOf('?');
|
12339 | if (questionMarkInd === -1) {
|
12340 | questionMarkInd = dataURL.length;
|
12341 | }
|
12342 | host = dataURL.substring(0, Math.min(slashInd, questionMarkInd));
|
12343 | if (slashInd < questionMarkInd) {
|
12344 |
|
12345 | pathString = decodePath(dataURL.substring(slashInd, questionMarkInd));
|
12346 | }
|
12347 | var queryParams = decodeQuery(dataURL.substring(Math.min(dataURL.length, questionMarkInd)));
|
12348 |
|
12349 | colonInd = host.indexOf(':');
|
12350 | if (colonInd >= 0) {
|
12351 | secure = scheme === 'https' || scheme === 'wss';
|
12352 | port = parseInt(host.substring(colonInd + 1), 10);
|
12353 | }
|
12354 | else {
|
12355 | colonInd = host.length;
|
12356 | }
|
12357 | var hostWithoutPort = host.slice(0, colonInd);
|
12358 | if (hostWithoutPort.toLowerCase() === 'localhost') {
|
12359 | domain = 'localhost';
|
12360 | }
|
12361 | else if (hostWithoutPort.split('.').length <= 2) {
|
12362 | domain = hostWithoutPort;
|
12363 | }
|
12364 | else {
|
12365 |
|
12366 | var dotInd = host.indexOf('.');
|
12367 | subdomain = host.substring(0, dotInd).toLowerCase();
|
12368 | domain = host.substring(dotInd + 1);
|
12369 |
|
12370 | namespace = subdomain;
|
12371 | }
|
12372 |
|
12373 | if ('ns' in queryParams) {
|
12374 | namespace = queryParams['ns'];
|
12375 | }
|
12376 | }
|
12377 | return {
|
12378 | host: host,
|
12379 | port: port,
|
12380 | domain: domain,
|
12381 | subdomain: subdomain,
|
12382 | secure: secure,
|
12383 | scheme: scheme,
|
12384 | pathString: pathString,
|
12385 | namespace: namespace
|
12386 | };
|
12387 | };
|
12388 |
|
12389 |
|
12390 |
|
12391 |
|
12392 |
|
12393 |
|
12394 |
|
12395 |
|
12396 |
|
12397 |
|
12398 |
|
12399 |
|
12400 |
|
12401 |
|
12402 |
|
12403 |
|
12404 |
|
12405 |
|
12406 |
|
12407 |
|
12408 | var DataEvent = (function () {
|
12409 | |
12410 |
|
12411 |
|
12412 |
|
12413 |
|
12414 |
|
12415 | function DataEvent(eventType, eventRegistration, snapshot, prevName) {
|
12416 | this.eventType = eventType;
|
12417 | this.eventRegistration = eventRegistration;
|
12418 | this.snapshot = snapshot;
|
12419 | this.prevName = prevName;
|
12420 | }
|
12421 | DataEvent.prototype.getPath = function () {
|
12422 | var ref = this.snapshot.ref;
|
12423 | if (this.eventType === 'value') {
|
12424 | return ref._path;
|
12425 | }
|
12426 | else {
|
12427 | return ref.parent._path;
|
12428 | }
|
12429 | };
|
12430 | DataEvent.prototype.getEventType = function () {
|
12431 | return this.eventType;
|
12432 | };
|
12433 | DataEvent.prototype.getEventRunner = function () {
|
12434 | return this.eventRegistration.getEventRunner(this);
|
12435 | };
|
12436 | DataEvent.prototype.toString = function () {
|
12437 | return (this.getPath().toString() +
|
12438 | ':' +
|
12439 | this.eventType +
|
12440 | ':' +
|
12441 | util.stringify(this.snapshot.exportVal()));
|
12442 | };
|
12443 | return DataEvent;
|
12444 | }());
|
12445 | var CancelEvent = (function () {
|
12446 | function CancelEvent(eventRegistration, error, path) {
|
12447 | this.eventRegistration = eventRegistration;
|
12448 | this.error = error;
|
12449 | this.path = path;
|
12450 | }
|
12451 | CancelEvent.prototype.getPath = function () {
|
12452 | return this.path;
|
12453 | };
|
12454 | CancelEvent.prototype.getEventType = function () {
|
12455 | return 'cancel';
|
12456 | };
|
12457 | CancelEvent.prototype.getEventRunner = function () {
|
12458 | return this.eventRegistration.getEventRunner(this);
|
12459 | };
|
12460 | CancelEvent.prototype.toString = function () {
|
12461 | return this.path.toString() + ':cancel';
|
12462 | };
|
12463 | return CancelEvent;
|
12464 | }());
|
12465 |
|
12466 |
|
12467 |
|
12468 |
|
12469 |
|
12470 |
|
12471 |
|
12472 |
|
12473 |
|
12474 |
|
12475 |
|
12476 |
|
12477 |
|
12478 |
|
12479 |
|
12480 |
|
12481 |
|
12482 |
|
12483 |
|
12484 |
|
12485 |
|
12486 |
|
12487 |
|
12488 | var CallbackContext = (function () {
|
12489 | function CallbackContext(snapshotCallback, cancelCallback) {
|
12490 | this.snapshotCallback = snapshotCallback;
|
12491 | this.cancelCallback = cancelCallback;
|
12492 | }
|
12493 | CallbackContext.prototype.onValue = function (expDataSnapshot, previousChildName) {
|
12494 | this.snapshotCallback.call(null, expDataSnapshot, previousChildName);
|
12495 | };
|
12496 | CallbackContext.prototype.onCancel = function (error) {
|
12497 | util.assert(this.hasCancelCallback, 'Raising a cancel event on a listener with no cancel callback');
|
12498 | return this.cancelCallback.call(null, error);
|
12499 | };
|
12500 | Object.defineProperty(CallbackContext.prototype, "hasCancelCallback", {
|
12501 | get: function () {
|
12502 | return !!this.cancelCallback;
|
12503 | },
|
12504 | enumerable: false,
|
12505 | configurable: true
|
12506 | });
|
12507 | CallbackContext.prototype.matches = function (other) {
|
12508 | return (this.snapshotCallback === other.snapshotCallback ||
|
12509 | (this.snapshotCallback.userCallback ===
|
12510 | other.snapshotCallback.userCallback &&
|
12511 | this.snapshotCallback.context === other.snapshotCallback.context));
|
12512 | };
|
12513 | return CallbackContext;
|
12514 | }());
|
12515 |
|
12516 |
|
12517 |
|
12518 |
|
12519 |
|
12520 |
|
12521 |
|
12522 |
|
12523 |
|
12524 |
|
12525 |
|
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 | var OnDisconnect = (function () {
|
12553 |
|
12554 | function OnDisconnect(_repo, _path) {
|
12555 | this._repo = _repo;
|
12556 | this._path = _path;
|
12557 | }
|
12558 | |
12559 |
|
12560 |
|
12561 |
|
12562 |
|
12563 |
|
12564 |
|
12565 |
|
12566 |
|
12567 |
|
12568 | OnDisconnect.prototype.cancel = function () {
|
12569 | var deferred = new util.Deferred();
|
12570 | repoOnDisconnectCancel(this._repo, this._path, deferred.wrapCallback(function () { }));
|
12571 | return deferred.promise;
|
12572 | };
|
12573 | |
12574 |
|
12575 |
|
12576 |
|
12577 |
|
12578 |
|
12579 | OnDisconnect.prototype.remove = function () {
|
12580 | validateWritablePath('OnDisconnect.remove', this._path);
|
12581 | var deferred = new util.Deferred();
|
12582 | repoOnDisconnectSet(this._repo, this._path, null, deferred.wrapCallback(function () { }));
|
12583 | return deferred.promise;
|
12584 | };
|
12585 | |
12586 |
|
12587 |
|
12588 |
|
12589 |
|
12590 |
|
12591 |
|
12592 |
|
12593 |
|
12594 |
|
12595 |
|
12596 |
|
12597 |
|
12598 |
|
12599 |
|
12600 |
|
12601 |
|
12602 |
|
12603 |
|
12604 | OnDisconnect.prototype.set = function (value) {
|
12605 | validateWritablePath('OnDisconnect.set', this._path);
|
12606 | validateFirebaseDataArg('OnDisconnect.set', value, this._path, false);
|
12607 | var deferred = new util.Deferred();
|
12608 | repoOnDisconnectSet(this._repo, this._path, value, deferred.wrapCallback(function () { }));
|
12609 | return deferred.promise;
|
12610 | };
|
12611 | |
12612 |
|
12613 |
|
12614 |
|
12615 |
|
12616 |
|
12617 |
|
12618 |
|
12619 |
|
12620 |
|
12621 | OnDisconnect.prototype.setWithPriority = function (value, priority) {
|
12622 | validateWritablePath('OnDisconnect.setWithPriority', this._path);
|
12623 | validateFirebaseDataArg('OnDisconnect.setWithPriority', value, this._path, false);
|
12624 | validatePriority('OnDisconnect.setWithPriority', priority, false);
|
12625 | var deferred = new util.Deferred();
|
12626 | repoOnDisconnectSetWithPriority(this._repo, this._path, value, priority, deferred.wrapCallback(function () { }));
|
12627 | return deferred.promise;
|
12628 | };
|
12629 | |
12630 |
|
12631 |
|
12632 |
|
12633 |
|
12634 |
|
12635 |
|
12636 |
|
12637 |
|
12638 |
|
12639 |
|
12640 |
|
12641 |
|
12642 |
|
12643 |
|
12644 |
|
12645 | OnDisconnect.prototype.update = function (values) {
|
12646 | validateWritablePath('OnDisconnect.update', this._path);
|
12647 | validateFirebaseMergeDataArg('OnDisconnect.update', values, this._path, false);
|
12648 | var deferred = new util.Deferred();
|
12649 | repoOnDisconnectUpdate(this._repo, this._path, values, deferred.wrapCallback(function () { }));
|
12650 | return deferred.promise;
|
12651 | };
|
12652 | return OnDisconnect;
|
12653 | }());
|
12654 |
|
12655 |
|
12656 |
|
12657 |
|
12658 |
|
12659 |
|
12660 |
|
12661 |
|
12662 |
|
12663 |
|
12664 |
|
12665 |
|
12666 |
|
12667 |
|
12668 |
|
12669 |
|
12670 |
|
12671 |
|
12672 |
|
12673 |
|
12674 | var QueryImpl = (function () {
|
12675 | |
12676 |
|
12677 |
|
12678 | function QueryImpl(_repo, _path, _queryParams, _orderByCalled) {
|
12679 | this._repo = _repo;
|
12680 | this._path = _path;
|
12681 | this._queryParams = _queryParams;
|
12682 | this._orderByCalled = _orderByCalled;
|
12683 | }
|
12684 | Object.defineProperty(QueryImpl.prototype, "key", {
|
12685 | get: function () {
|
12686 | if (pathIsEmpty(this._path)) {
|
12687 | return null;
|
12688 | }
|
12689 | else {
|
12690 | return pathGetBack(this._path);
|
12691 | }
|
12692 | },
|
12693 | enumerable: false,
|
12694 | configurable: true
|
12695 | });
|
12696 | Object.defineProperty(QueryImpl.prototype, "ref", {
|
12697 | get: function () {
|
12698 | return new ReferenceImpl(this._repo, this._path);
|
12699 | },
|
12700 | enumerable: false,
|
12701 | configurable: true
|
12702 | });
|
12703 | Object.defineProperty(QueryImpl.prototype, "_queryIdentifier", {
|
12704 | get: function () {
|
12705 | var obj = queryParamsGetQueryObject(this._queryParams);
|
12706 | var id = ObjectToUniqueKey(obj);
|
12707 | return id === '{}' ? 'default' : id;
|
12708 | },
|
12709 | enumerable: false,
|
12710 | configurable: true
|
12711 | });
|
12712 | Object.defineProperty(QueryImpl.prototype, "_queryObject", {
|
12713 | |
12714 |
|
12715 |
|
12716 | get: function () {
|
12717 | return queryParamsGetQueryObject(this._queryParams);
|
12718 | },
|
12719 | enumerable: false,
|
12720 | configurable: true
|
12721 | });
|
12722 | QueryImpl.prototype.isEqual = function (other) {
|
12723 | other = util.getModularInstance(other);
|
12724 | if (!(other instanceof QueryImpl)) {
|
12725 | return false;
|
12726 | }
|
12727 | var sameRepo = this._repo === other._repo;
|
12728 | var samePath = pathEquals(this._path, other._path);
|
12729 | var sameQueryIdentifier = this._queryIdentifier === other._queryIdentifier;
|
12730 | return sameRepo && samePath && sameQueryIdentifier;
|
12731 | };
|
12732 | QueryImpl.prototype.toJSON = function () {
|
12733 | return this.toString();
|
12734 | };
|
12735 | QueryImpl.prototype.toString = function () {
|
12736 | return this._repo.toString() + pathToUrlEncodedString(this._path);
|
12737 | };
|
12738 | return QueryImpl;
|
12739 | }());
|
12740 |
|
12741 |
|
12742 |
|
12743 | function validateNoPreviousOrderByCall(query, fnName) {
|
12744 | if (query._orderByCalled === true) {
|
12745 | throw new Error(fnName + ": You can't combine multiple orderBy calls.");
|
12746 | }
|
12747 | }
|
12748 |
|
12749 |
|
12750 |
|
12751 | function validateQueryEndpoints(params) {
|
12752 | var startNode = null;
|
12753 | var endNode = null;
|
12754 | if (params.hasStart()) {
|
12755 | startNode = params.getIndexStartValue();
|
12756 | }
|
12757 | if (params.hasEnd()) {
|
12758 | endNode = params.getIndexEndValue();
|
12759 | }
|
12760 | if (params.getIndex() === KEY_INDEX) {
|
12761 | var tooManyArgsError = 'Query: When ordering by key, you may only pass one argument to ' +
|
12762 | 'startAt(), endAt(), or equalTo().';
|
12763 | var wrongArgTypeError = 'Query: When ordering by key, the argument passed to startAt(), startAfter(), ' +
|
12764 | 'endAt(), endBefore(), or equalTo() must be a string.';
|
12765 | if (params.hasStart()) {
|
12766 | var startName = params.getIndexStartName();
|
12767 | if (startName !== MIN_NAME) {
|
12768 | throw new Error(tooManyArgsError);
|
12769 | }
|
12770 | else if (typeof startNode !== 'string') {
|
12771 | throw new Error(wrongArgTypeError);
|
12772 | }
|
12773 | }
|
12774 | if (params.hasEnd()) {
|
12775 | var endName = params.getIndexEndName();
|
12776 | if (endName !== MAX_NAME) {
|
12777 | throw new Error(tooManyArgsError);
|
12778 | }
|
12779 | else if (typeof endNode !== 'string') {
|
12780 | throw new Error(wrongArgTypeError);
|
12781 | }
|
12782 | }
|
12783 | }
|
12784 | else if (params.getIndex() === PRIORITY_INDEX) {
|
12785 | if ((startNode != null && !isValidPriority(startNode)) ||
|
12786 | (endNode != null && !isValidPriority(endNode))) {
|
12787 | throw new Error('Query: When ordering by priority, the first argument passed to startAt(), ' +
|
12788 | 'startAfter() endAt(), endBefore(), or equalTo() must be a valid priority value ' +
|
12789 | '(null, a number, or a string).');
|
12790 | }
|
12791 | }
|
12792 | else {
|
12793 | util.assert(params.getIndex() instanceof PathIndex ||
|
12794 | params.getIndex() === VALUE_INDEX, 'unknown index type.');
|
12795 | if ((startNode != null && typeof startNode === 'object') ||
|
12796 | (endNode != null && typeof endNode === 'object')) {
|
12797 | throw new Error('Query: First argument passed to startAt(), startAfter(), endAt(), endBefore(), or ' +
|
12798 | 'equalTo() cannot be an object.');
|
12799 | }
|
12800 | }
|
12801 | }
|
12802 |
|
12803 |
|
12804 |
|
12805 | function validateLimit(params) {
|
12806 | if (params.hasStart() &&
|
12807 | params.hasEnd() &&
|
12808 | params.hasLimit() &&
|
12809 | !params.hasAnchoredLimit()) {
|
12810 | throw new Error("Query: Can't combine startAt(), startAfter(), endAt(), endBefore(), and limit(). Use " +
|
12811 | 'limitToFirst() or limitToLast() instead.');
|
12812 | }
|
12813 | }
|
12814 |
|
12815 |
|
12816 |
|
12817 | var ReferenceImpl = (function (_super) {
|
12818 | tslib.__extends(ReferenceImpl, _super);
|
12819 |
|
12820 | function ReferenceImpl(repo, path) {
|
12821 | return _super.call(this, repo, path, new QueryParams(), false) || this;
|
12822 | }
|
12823 | Object.defineProperty(ReferenceImpl.prototype, "parent", {
|
12824 | get: function () {
|
12825 | var parentPath = pathParent(this._path);
|
12826 | return parentPath === null
|
12827 | ? null
|
12828 | : new ReferenceImpl(this._repo, parentPath);
|
12829 | },
|
12830 | enumerable: false,
|
12831 | configurable: true
|
12832 | });
|
12833 | Object.defineProperty(ReferenceImpl.prototype, "root", {
|
12834 | get: function () {
|
12835 | var ref = this;
|
12836 | while (ref.parent !== null) {
|
12837 | ref = ref.parent;
|
12838 | }
|
12839 | return ref;
|
12840 | },
|
12841 | enumerable: false,
|
12842 | configurable: true
|
12843 | });
|
12844 | return ReferenceImpl;
|
12845 | }(QueryImpl));
|
12846 |
|
12847 |
|
12848 |
|
12849 |
|
12850 |
|
12851 |
|
12852 |
|
12853 |
|
12854 |
|
12855 |
|
12856 |
|
12857 |
|
12858 |
|
12859 |
|
12860 | var DataSnapshot = (function () {
|
12861 | |
12862 |
|
12863 |
|
12864 |
|
12865 |
|
12866 |
|
12867 | function DataSnapshot(_node,
|
12868 | /**
|
12869 | * The location of this DataSnapshot.
|
12870 | */
|
12871 | ref, _index) {
|
12872 | this._node = _node;
|
12873 | this.ref = ref;
|
12874 | this._index = _index;
|
12875 | }
|
12876 | Object.defineProperty(DataSnapshot.prototype, "priority", {
|
12877 | |
12878 |
|
12879 |
|
12880 |
|
12881 |
|
12882 |
|
12883 |
|
12884 |
|
12885 | get: function () {
|
12886 |
|
12887 | return this._node.getPriority().val();
|
12888 | },
|
12889 | enumerable: false,
|
12890 | configurable: true
|
12891 | });
|
12892 | Object.defineProperty(DataSnapshot.prototype, "key", {
|
12893 | |
12894 |
|
12895 |
|
12896 |
|
12897 |
|
12898 |
|
12899 |
|
12900 |
|
12901 |
|
12902 | get: function () {
|
12903 | return this.ref.key;
|
12904 | },
|
12905 | enumerable: false,
|
12906 | configurable: true
|
12907 | });
|
12908 | Object.defineProperty(DataSnapshot.prototype, "size", {
|
12909 |
|
12910 | get: function () {
|
12911 | return this._node.numChildren();
|
12912 | },
|
12913 | enumerable: false,
|
12914 | configurable: true
|
12915 | });
|
12916 | |
12917 |
|
12918 |
|
12919 |
|
12920 |
|
12921 |
|
12922 |
|
12923 |
|
12924 |
|
12925 |
|
12926 |
|
12927 |
|
12928 | DataSnapshot.prototype.child = function (path) {
|
12929 | var childPath = new Path(path);
|
12930 | var childRef = child(this.ref, path);
|
12931 | return new DataSnapshot(this._node.getChild(childPath), childRef, PRIORITY_INDEX);
|
12932 | };
|
12933 | |
12934 |
|
12935 |
|
12936 |
|
12937 | DataSnapshot.prototype.exists = function () {
|
12938 | return !this._node.isEmpty();
|
12939 | };
|
12940 | |
12941 |
|
12942 |
|
12943 |
|
12944 |
|
12945 |
|
12946 |
|
12947 |
|
12948 |
|
12949 |
|
12950 | DataSnapshot.prototype.exportVal = function () {
|
12951 | return this._node.val(true);
|
12952 | };
|
12953 | |
12954 |
|
12955 |
|
12956 |
|
12957 |
|
12958 |
|
12959 |
|
12960 |
|
12961 |
|
12962 |
|
12963 |
|
12964 |
|
12965 |
|
12966 |
|
12967 |
|
12968 |
|
12969 |
|
12970 |
|
12971 | DataSnapshot.prototype.forEach = function (action) {
|
12972 | var _this = this;
|
12973 | if (this._node.isLeafNode()) {
|
12974 | return false;
|
12975 | }
|
12976 | var childrenNode = this._node;
|
12977 |
|
12978 | return !!childrenNode.forEachChild(this._index, function (key, node) {
|
12979 | return action(new DataSnapshot(node, child(_this.ref, key), PRIORITY_INDEX));
|
12980 | });
|
12981 | };
|
12982 | |
12983 |
|
12984 |
|
12985 |
|
12986 |
|
12987 |
|
12988 |
|
12989 | DataSnapshot.prototype.hasChild = function (path) {
|
12990 | var childPath = new Path(path);
|
12991 | return !this._node.getChild(childPath).isEmpty();
|
12992 | };
|
12993 | |
12994 |
|
12995 |
|
12996 |
|
12997 |
|
12998 |
|
12999 |
|
13000 |
|
13001 |
|
13002 |
|
13003 |
|
13004 |
|
13005 | DataSnapshot.prototype.hasChildren = function () {
|
13006 | if (this._node.isLeafNode()) {
|
13007 | return false;
|
13008 | }
|
13009 | else {
|
13010 | return !this._node.isEmpty();
|
13011 | }
|
13012 | };
|
13013 | |
13014 |
|
13015 |
|
13016 | DataSnapshot.prototype.toJSON = function () {
|
13017 | return this.exportVal();
|
13018 | };
|
13019 | |
13020 |
|
13021 |
|
13022 |
|
13023 |
|
13024 |
|
13025 |
|
13026 |
|
13027 |
|
13028 |
|
13029 |
|
13030 |
|
13031 | DataSnapshot.prototype.val = function () {
|
13032 | return this._node.val();
|
13033 | };
|
13034 | return DataSnapshot;
|
13035 | }());
|
13036 |
|
13037 |
|
13038 |
|
13039 |
|
13040 |
|
13041 |
|
13042 |
|
13043 |
|
13044 |
|
13045 |
|
13046 |
|
13047 |
|
13048 |
|
13049 |
|
13050 | function ref(db, path) {
|
13051 | db = util.getModularInstance(db);
|
13052 | db._checkNotDeleted('ref');
|
13053 | return path !== undefined ? child(db._root, path) : db._root;
|
13054 | }
|
13055 |
|
13056 |
|
13057 |
|
13058 |
|
13059 |
|
13060 |
|
13061 |
|
13062 |
|
13063 |
|
13064 |
|
13065 |
|
13066 |
|
13067 |
|
13068 |
|
13069 |
|
13070 |
|
13071 | function refFromURL(db, url) {
|
13072 | db = util.getModularInstance(db);
|
13073 | db._checkNotDeleted('refFromURL');
|
13074 | var parsedURL = parseRepoInfo(url, db._repo.repoInfo_.nodeAdmin);
|
13075 | validateUrl('refFromURL', parsedURL);
|
13076 | var repoInfo = parsedURL.repoInfo;
|
13077 | if (!db._repo.repoInfo_.isCustomHost() &&
|
13078 | repoInfo.host !== db._repo.repoInfo_.host) {
|
13079 | fatal('refFromURL' +
|
13080 | ': Host name does not match the current database: ' +
|
13081 | '(found ' +
|
13082 | repoInfo.host +
|
13083 | ' but expected ' +
|
13084 | db._repo.repoInfo_.host +
|
13085 | ')');
|
13086 | }
|
13087 | return ref(db, parsedURL.path.toString());
|
13088 | }
|
13089 |
|
13090 |
|
13091 |
|
13092 |
|
13093 |
|
13094 |
|
13095 |
|
13096 |
|
13097 |
|
13098 |
|
13099 |
|
13100 | function child(parent, path) {
|
13101 | parent = util.getModularInstance(parent);
|
13102 | if (pathGetFront(parent._path) === null) {
|
13103 | validateRootPathString('child', 'path', path, false);
|
13104 | }
|
13105 | else {
|
13106 | validatePathString('child', 'path', path, false);
|
13107 | }
|
13108 | return new ReferenceImpl(parent._repo, pathChild(parent._path, path));
|
13109 | }
|
13110 |
|
13111 |
|
13112 |
|
13113 |
|
13114 |
|
13115 |
|
13116 |
|
13117 |
|
13118 |
|
13119 |
|
13120 |
|
13121 |
|
13122 |
|
13123 |
|
13124 |
|
13125 |
|
13126 |
|
13127 |
|
13128 |
|
13129 |
|
13130 |
|
13131 |
|
13132 |
|
13133 | function push(parent, value) {
|
13134 | parent = util.getModularInstance(parent);
|
13135 | validateWritablePath('push', parent._path);
|
13136 | validateFirebaseDataArg('push', value, parent._path, true);
|
13137 | var now = repoServerTime(parent._repo);
|
13138 | var name = nextPushId(now);
|
13139 |
|
13140 |
|
13141 |
|
13142 |
|
13143 |
|
13144 |
|
13145 | var thennablePushRef = child(parent, name);
|
13146 | var pushRef = child(parent, name);
|
13147 | var promise;
|
13148 | if (value != null) {
|
13149 | promise = set(pushRef, value).then(function () { return pushRef; });
|
13150 | }
|
13151 | else {
|
13152 | promise = Promise.resolve(pushRef);
|
13153 | }
|
13154 | thennablePushRef.then = promise.then.bind(promise);
|
13155 | thennablePushRef.catch = promise.then.bind(promise, undefined);
|
13156 | return thennablePushRef;
|
13157 | }
|
13158 |
|
13159 |
|
13160 |
|
13161 |
|
13162 |
|
13163 |
|
13164 |
|
13165 |
|
13166 |
|
13167 |
|
13168 |
|
13169 |
|
13170 |
|
13171 |
|
13172 | function remove(ref) {
|
13173 | validateWritablePath('remove', ref._path);
|
13174 | return set(ref, null);
|
13175 | }
|
13176 | /**
|
13177 | * Writes data to this Database location.
|
13178 | *
|
13179 | * This will overwrite any data at this location and all child locations.
|
13180 | *
|
13181 | * The effect of the write will be visible immediately, and the corresponding
|
13182 | * events ("value", "child_added", etc.) will be triggered. Synchronization of
|
13183 | * the data to the Firebase servers will also be started, and the returned
|
13184 | * Promise will resolve when complete. If provided, the `onComplete` callback
|
13185 | * will be called asynchronously after synchronization has finished.
|
13186 | *
|
13187 | * Passing `null` for the new value is equivalent to calling `remove()`; namely,
|
13188 | * all data at this location and all child locations will be deleted.
|
13189 | *
|
13190 | * `set()` will remove any priority stored at this location, so if priority is
|
13191 | * meant to be preserved, you need to use `setWithPriority()` instead.
|
13192 | *
|
13193 | * Note that modifying data with `set()` will cancel any pending transactions
|
13194 | * at that location, so extreme care should be taken if mixing `set()` and
|
13195 | * `transaction()` to modify the same data.
|
13196 | *
|
13197 | * A single `set()` will generate a single "value" event at the location where
|
13198 | * the `set()` was performed.
|
13199 | *
|
13200 | * @param ref - The location to write to.
|
13201 | * @param value - The value to be written (string, number, boolean, object,
|
13202 | * array, or null).
|
13203 | * @returns Resolves when write to server is complete.
|
13204 | */
|
13205 | function set(ref, value) {
|
13206 | ref = util.getModularInstance(ref);
|
13207 | validateWritablePath('set', ref._path);
|
13208 | validateFirebaseDataArg('set', value, ref._path, false);
|
13209 | var deferred = new util.Deferred();
|
13210 | repoSetWithPriority(ref._repo, ref._path, value,
|
13211 | null, deferred.wrapCallback(function () { }));
|
13212 | return deferred.promise;
|
13213 | }
|
13214 |
|
13215 |
|
13216 |
|
13217 |
|
13218 |
|
13219 |
|
13220 |
|
13221 |
|
13222 |
|
13223 |
|
13224 |
|
13225 |
|
13226 | function setPriority(ref, priority) {
|
13227 | ref = util.getModularInstance(ref);
|
13228 | validateWritablePath('setPriority', ref._path);
|
13229 | validatePriority('setPriority', priority, false);
|
13230 | var deferred = new util.Deferred();
|
13231 | repoSetWithPriority(ref._repo, pathChild(ref._path, '.priority'), priority, null, deferred.wrapCallback(function () { }));
|
13232 | return deferred.promise;
|
13233 | }
|
13234 |
|
13235 |
|
13236 |
|
13237 |
|
13238 |
|
13239 |
|
13240 |
|
13241 |
|
13242 |
|
13243 |
|
13244 |
|
13245 |
|
13246 |
|
13247 |
|
13248 |
|
13249 | function setWithPriority(ref, value, priority) {
|
13250 | validateWritablePath('setWithPriority', ref._path);
|
13251 | validateFirebaseDataArg('setWithPriority', value, ref._path, false);
|
13252 | validatePriority('setWithPriority', priority, false);
|
13253 | if (ref.key === '.length' || ref.key === '.keys') {
|
13254 | throw 'setWithPriority failed: ' + ref.key + ' is a read-only object.';
|
13255 | }
|
13256 | var deferred = new util.Deferred();
|
13257 | repoSetWithPriority(ref._repo, ref._path, value, priority, deferred.wrapCallback(function () { }));
|
13258 | return deferred.promise;
|
13259 | }
|
13260 |
|
13261 |
|
13262 |
|
13263 |
|
13264 |
|
13265 |
|
13266 |
|
13267 |
|
13268 |
|
13269 |
|
13270 |
|
13271 |
|
13272 |
|
13273 |
|
13274 |
|
13275 |
|
13276 |
|
13277 |
|
13278 |
|
13279 |
|
13280 |
|
13281 |
|
13282 |
|
13283 |
|
13284 |
|
13285 |
|
13286 |
|
13287 |
|
13288 |
|
13289 |
|
13290 |
|
13291 |
|
13292 |
|
13293 |
|
13294 |
|
13295 | function update(ref, values) {
|
13296 | validateFirebaseMergeDataArg('update', values, ref._path, false);
|
13297 | var deferred = new util.Deferred();
|
13298 | repoUpdate(ref._repo, ref._path, values, deferred.wrapCallback(function () { }));
|
13299 | return deferred.promise;
|
13300 | }
|
13301 |
|
13302 |
|
13303 |
|
13304 |
|
13305 |
|
13306 |
|
13307 |
|
13308 |
|
13309 | function get(query) {
|
13310 | query = util.getModularInstance(query);
|
13311 | return repoGetValue(query._repo, query).then(function (node) {
|
13312 | return new DataSnapshot(node, new ReferenceImpl(query._repo, query._path), query._queryParams.getIndex());
|
13313 | });
|
13314 | }
|
13315 |
|
13316 |
|
13317 |
|
13318 | var ValueEventRegistration = (function () {
|
13319 | function ValueEventRegistration(callbackContext) {
|
13320 | this.callbackContext = callbackContext;
|
13321 | }
|
13322 | ValueEventRegistration.prototype.respondsTo = function (eventType) {
|
13323 | return eventType === 'value';
|
13324 | };
|
13325 | ValueEventRegistration.prototype.createEvent = function (change, query) {
|
13326 | var index = query._queryParams.getIndex();
|
13327 | return new DataEvent('value', this, new DataSnapshot(change.snapshotNode, new ReferenceImpl(query._repo, query._path), index));
|
13328 | };
|
13329 | ValueEventRegistration.prototype.getEventRunner = function (eventData) {
|
13330 | var _this = this;
|
13331 | if (eventData.getEventType() === 'cancel') {
|
13332 | return function () {
|
13333 | return _this.callbackContext.onCancel(eventData.error);
|
13334 | };
|
13335 | }
|
13336 | else {
|
13337 | return function () {
|
13338 | return _this.callbackContext.onValue(eventData.snapshot, null);
|
13339 | };
|
13340 | }
|
13341 | };
|
13342 | ValueEventRegistration.prototype.createCancelEvent = function (error, path) {
|
13343 | if (this.callbackContext.hasCancelCallback) {
|
13344 | return new CancelEvent(this, error, path);
|
13345 | }
|
13346 | else {
|
13347 | return null;
|
13348 | }
|
13349 | };
|
13350 | ValueEventRegistration.prototype.matches = function (other) {
|
13351 | if (!(other instanceof ValueEventRegistration)) {
|
13352 | return false;
|
13353 | }
|
13354 | else if (!other.callbackContext || !this.callbackContext) {
|
13355 |
|
13356 | return true;
|
13357 | }
|
13358 | else {
|
13359 | return other.callbackContext.matches(this.callbackContext);
|
13360 | }
|
13361 | };
|
13362 | ValueEventRegistration.prototype.hasAnyCallback = function () {
|
13363 | return this.callbackContext !== null;
|
13364 | };
|
13365 | return ValueEventRegistration;
|
13366 | }());
|
13367 |
|
13368 |
|
13369 |
|
13370 | var ChildEventRegistration = (function () {
|
13371 | function ChildEventRegistration(eventType, callbackContext) {
|
13372 | this.eventType = eventType;
|
13373 | this.callbackContext = callbackContext;
|
13374 | }
|
13375 | ChildEventRegistration.prototype.respondsTo = function (eventType) {
|
13376 | var eventToCheck = eventType === 'children_added' ? 'child_added' : eventType;
|
13377 | eventToCheck =
|
13378 | eventToCheck === 'children_removed' ? 'child_removed' : eventToCheck;
|
13379 | return this.eventType === eventToCheck;
|
13380 | };
|
13381 | ChildEventRegistration.prototype.createCancelEvent = function (error, path) {
|
13382 | if (this.callbackContext.hasCancelCallback) {
|
13383 | return new CancelEvent(this, error, path);
|
13384 | }
|
13385 | else {
|
13386 | return null;
|
13387 | }
|
13388 | };
|
13389 | ChildEventRegistration.prototype.createEvent = function (change, query) {
|
13390 | util.assert(change.childName != null, 'Child events should have a childName.');
|
13391 | var childRef = child(new ReferenceImpl(query._repo, query._path), change.childName);
|
13392 | var index = query._queryParams.getIndex();
|
13393 | return new DataEvent(change.type, this, new DataSnapshot(change.snapshotNode, childRef, index), change.prevName);
|
13394 | };
|
13395 | ChildEventRegistration.prototype.getEventRunner = function (eventData) {
|
13396 | var _this = this;
|
13397 | if (eventData.getEventType() === 'cancel') {
|
13398 | return function () {
|
13399 | return _this.callbackContext.onCancel(eventData.error);
|
13400 | };
|
13401 | }
|
13402 | else {
|
13403 | return function () {
|
13404 | return _this.callbackContext.onValue(eventData.snapshot, eventData.prevName);
|
13405 | };
|
13406 | }
|
13407 | };
|
13408 | ChildEventRegistration.prototype.matches = function (other) {
|
13409 | if (other instanceof ChildEventRegistration) {
|
13410 | return (this.eventType === other.eventType &&
|
13411 | (!this.callbackContext ||
|
13412 | !other.callbackContext ||
|
13413 | this.callbackContext.matches(other.callbackContext)));
|
13414 | }
|
13415 | return false;
|
13416 | };
|
13417 | ChildEventRegistration.prototype.hasAnyCallback = function () {
|
13418 | return !!this.callbackContext;
|
13419 | };
|
13420 | return ChildEventRegistration;
|
13421 | }());
|
13422 | function addEventListener(query, eventType, callback, cancelCallbackOrListenOptions, options) {
|
13423 | var cancelCallback;
|
13424 | if (typeof cancelCallbackOrListenOptions === 'object') {
|
13425 | cancelCallback = undefined;
|
13426 | options = cancelCallbackOrListenOptions;
|
13427 | }
|
13428 | if (typeof cancelCallbackOrListenOptions === 'function') {
|
13429 | cancelCallback = cancelCallbackOrListenOptions;
|
13430 | }
|
13431 | if (options && options.onlyOnce) {
|
13432 | var userCallback_1 = callback;
|
13433 | var onceCallback = function (dataSnapshot, previousChildName) {
|
13434 | repoRemoveEventCallbackForQuery(query._repo, query, container);
|
13435 | userCallback_1(dataSnapshot, previousChildName);
|
13436 | };
|
13437 | onceCallback.userCallback = callback.userCallback;
|
13438 | onceCallback.context = callback.context;
|
13439 | callback = onceCallback;
|
13440 | }
|
13441 | var callbackContext = new CallbackContext(callback, cancelCallback || undefined);
|
13442 | var container = eventType === 'value'
|
13443 | ? new ValueEventRegistration(callbackContext)
|
13444 | : new ChildEventRegistration(eventType, callbackContext);
|
13445 | repoAddEventCallbackForQuery(query._repo, query, container);
|
13446 | return function () { return repoRemoveEventCallbackForQuery(query._repo, query, container); };
|
13447 | }
|
13448 | function onValue(query, callback, cancelCallbackOrListenOptions, options) {
|
13449 | return addEventListener(query, 'value', callback, cancelCallbackOrListenOptions, options);
|
13450 | }
|
13451 | function onChildAdded(query, callback, cancelCallbackOrListenOptions, options) {
|
13452 | return addEventListener(query, 'child_added', callback, cancelCallbackOrListenOptions, options);
|
13453 | }
|
13454 | function onChildChanged(query, callback, cancelCallbackOrListenOptions, options) {
|
13455 | return addEventListener(query, 'child_changed', callback, cancelCallbackOrListenOptions, options);
|
13456 | }
|
13457 | function onChildMoved(query, callback, cancelCallbackOrListenOptions, options) {
|
13458 | return addEventListener(query, 'child_moved', callback, cancelCallbackOrListenOptions, options);
|
13459 | }
|
13460 | function onChildRemoved(query, callback, cancelCallbackOrListenOptions, options) {
|
13461 | return addEventListener(query, 'child_removed', callback, cancelCallbackOrListenOptions, options);
|
13462 | }
|
13463 |
|
13464 |
|
13465 |
|
13466 |
|
13467 |
|
13468 |
|
13469 |
|
13470 |
|
13471 |
|
13472 |
|
13473 |
|
13474 |
|
13475 |
|
13476 |
|
13477 |
|
13478 |
|
13479 |
|
13480 |
|
13481 |
|
13482 |
|
13483 |
|
13484 |
|
13485 |
|
13486 |
|
13487 | function off(query, eventType, callback) {
|
13488 | var container = null;
|
13489 | var expCallback = callback ? new CallbackContext(callback) : null;
|
13490 | if (eventType === 'value') {
|
13491 | container = new ValueEventRegistration(expCallback);
|
13492 | }
|
13493 | else if (eventType) {
|
13494 | container = new ChildEventRegistration(eventType, expCallback);
|
13495 | }
|
13496 | repoRemoveEventCallbackForQuery(query._repo, query, container);
|
13497 | }
|
13498 |
|
13499 |
|
13500 |
|
13501 |
|
13502 |
|
13503 |
|
13504 |
|
13505 |
|
13506 |
|
13507 |
|
13508 | var QueryConstraint = (function () {
|
13509 | function QueryConstraint() {
|
13510 | }
|
13511 | return QueryConstraint;
|
13512 | }());
|
13513 | var QueryEndAtConstraint = (function (_super) {
|
13514 | tslib.__extends(QueryEndAtConstraint, _super);
|
13515 | function QueryEndAtConstraint(_value, _key) {
|
13516 | var _this = _super.call(this) || this;
|
13517 | _this._value = _value;
|
13518 | _this._key = _key;
|
13519 | return _this;
|
13520 | }
|
13521 | QueryEndAtConstraint.prototype._apply = function (query) {
|
13522 | validateFirebaseDataArg('endAt', this._value, query._path, true);
|
13523 | var newParams = queryParamsEndAt(query._queryParams, this._value, this._key);
|
13524 | validateLimit(newParams);
|
13525 | validateQueryEndpoints(newParams);
|
13526 | if (query._queryParams.hasEnd()) {
|
13527 | throw new Error('endAt: Starting point was already set (by another call to endAt, ' +
|
13528 | 'endBefore or equalTo).');
|
13529 | }
|
13530 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13531 | };
|
13532 | return QueryEndAtConstraint;
|
13533 | }(QueryConstraint));
|
13534 |
|
13535 |
|
13536 |
|
13537 |
|
13538 |
|
13539 |
|
13540 |
|
13541 |
|
13542 |
|
13543 |
|
13544 |
|
13545 |
|
13546 |
|
13547 |
|
13548 |
|
13549 |
|
13550 |
|
13551 |
|
13552 |
|
13553 |
|
13554 |
|
13555 |
|
13556 |
|
13557 | function endAt(value, key) {
|
13558 | validateKey('endAt', 'key', key, true);
|
13559 | return new QueryEndAtConstraint(value, key);
|
13560 | }
|
13561 | var QueryEndBeforeConstraint = (function (_super) {
|
13562 | tslib.__extends(QueryEndBeforeConstraint, _super);
|
13563 | function QueryEndBeforeConstraint(_value, _key) {
|
13564 | var _this = _super.call(this) || this;
|
13565 | _this._value = _value;
|
13566 | _this._key = _key;
|
13567 | return _this;
|
13568 | }
|
13569 | QueryEndBeforeConstraint.prototype._apply = function (query) {
|
13570 | validateFirebaseDataArg('endBefore', this._value, query._path, false);
|
13571 | var newParams = queryParamsEndBefore(query._queryParams, this._value, this._key);
|
13572 | validateLimit(newParams);
|
13573 | validateQueryEndpoints(newParams);
|
13574 | if (query._queryParams.hasEnd()) {
|
13575 | throw new Error('endBefore: Starting point was already set (by another call to endAt, ' +
|
13576 | 'endBefore or equalTo).');
|
13577 | }
|
13578 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13579 | };
|
13580 | return QueryEndBeforeConstraint;
|
13581 | }(QueryConstraint));
|
13582 |
|
13583 |
|
13584 |
|
13585 |
|
13586 |
|
13587 |
|
13588 |
|
13589 |
|
13590 |
|
13591 |
|
13592 |
|
13593 |
|
13594 |
|
13595 |
|
13596 |
|
13597 |
|
13598 |
|
13599 |
|
13600 |
|
13601 | function endBefore(value, key) {
|
13602 | validateKey('endBefore', 'key', key, true);
|
13603 | return new QueryEndBeforeConstraint(value, key);
|
13604 | }
|
13605 | var QueryStartAtConstraint = (function (_super) {
|
13606 | tslib.__extends(QueryStartAtConstraint, _super);
|
13607 | function QueryStartAtConstraint(_value, _key) {
|
13608 | var _this = _super.call(this) || this;
|
13609 | _this._value = _value;
|
13610 | _this._key = _key;
|
13611 | return _this;
|
13612 | }
|
13613 | QueryStartAtConstraint.prototype._apply = function (query) {
|
13614 | validateFirebaseDataArg('startAt', this._value, query._path, true);
|
13615 | var newParams = queryParamsStartAt(query._queryParams, this._value, this._key);
|
13616 | validateLimit(newParams);
|
13617 | validateQueryEndpoints(newParams);
|
13618 | if (query._queryParams.hasStart()) {
|
13619 | throw new Error('startAt: Starting point was already set (by another call to startAt, ' +
|
13620 | 'startBefore or equalTo).');
|
13621 | }
|
13622 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13623 | };
|
13624 | return QueryStartAtConstraint;
|
13625 | }(QueryConstraint));
|
13626 |
|
13627 |
|
13628 |
|
13629 |
|
13630 |
|
13631 |
|
13632 |
|
13633 |
|
13634 |
|
13635 |
|
13636 |
|
13637 |
|
13638 |
|
13639 |
|
13640 |
|
13641 |
|
13642 |
|
13643 |
|
13644 |
|
13645 |
|
13646 |
|
13647 |
|
13648 | function startAt(value, key) {
|
13649 | if (value === void 0) { value = null; }
|
13650 | validateKey('startAt', 'key', key, true);
|
13651 | return new QueryStartAtConstraint(value, key);
|
13652 | }
|
13653 | var QueryStartAfterConstraint = (function (_super) {
|
13654 | tslib.__extends(QueryStartAfterConstraint, _super);
|
13655 | function QueryStartAfterConstraint(_value, _key) {
|
13656 | var _this = _super.call(this) || this;
|
13657 | _this._value = _value;
|
13658 | _this._key = _key;
|
13659 | return _this;
|
13660 | }
|
13661 | QueryStartAfterConstraint.prototype._apply = function (query) {
|
13662 | validateFirebaseDataArg('startAfter', this._value, query._path, false);
|
13663 | var newParams = queryParamsStartAfter(query._queryParams, this._value, this._key);
|
13664 | validateLimit(newParams);
|
13665 | validateQueryEndpoints(newParams);
|
13666 | if (query._queryParams.hasStart()) {
|
13667 | throw new Error('startAfter: Starting point was already set (by another call to startAt, ' +
|
13668 | 'startAfter, or equalTo).');
|
13669 | }
|
13670 | return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
|
13671 | };
|
13672 | return QueryStartAfterConstraint;
|
13673 | }(QueryConstraint));
|
13674 |
|
13675 |
|
13676 |
|
13677 |
|
13678 |
|
13679 |
|
13680 |
|
13681 |
|
13682 |
|
13683 |
|
13684 |
|
13685 |
|
13686 |
|
13687 |
|
13688 |
|
13689 |
|
13690 |
|
13691 |
|
13692 | function startAfter(value, key) {
|
13693 | validateKey('startAfter', 'key', key, true);
|
13694 | return new QueryStartAfterConstraint(value, key);
|
13695 | }
|
13696 | var QueryLimitToFirstConstraint = (function (_super) {
|
13697 | tslib.__extends(QueryLimitToFirstConstraint, _super);
|
13698 | function QueryLimitToFirstConstraint(_limit) {
|
13699 | var _this = _super.call(this) || this;
|
13700 | _this._limit = _limit;
|
13701 | return _this;
|
13702 | }
|
13703 | QueryLimitToFirstConstraint.prototype._apply = function (query) {
|
13704 | if (query._queryParams.hasLimit()) {
|
13705 | throw new Error('limitToFirst: Limit was already set (by another call to limitToFirst ' +
|
13706 | 'or limitToLast).');
|
13707 | }
|
13708 | return new QueryImpl(query._repo, query._path, queryParamsLimitToFirst(query._queryParams, this._limit), query._orderByCalled);
|
13709 | };
|
13710 | return QueryLimitToFirstConstraint;
|
13711 | }(QueryConstraint));
|
13712 |
|
13713 |
|
13714 |
|
13715 |
|
13716 |
|
13717 |
|
13718 |
|
13719 |
|
13720 |
|
13721 |
|
13722 |
|
13723 |
|
13724 |
|
13725 |
|
13726 |
|
13727 |
|
13728 |
|
13729 |
|
13730 | function limitToFirst(limit) {
|
13731 | if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
|
13732 | throw new Error('limitToFirst: First argument must be a positive integer.');
|
13733 | }
|
13734 | return new QueryLimitToFirstConstraint(limit);
|
13735 | }
|
13736 | var QueryLimitToLastConstraint = (function (_super) {
|
13737 | tslib.__extends(QueryLimitToLastConstraint, _super);
|
13738 | function QueryLimitToLastConstraint(_limit) {
|
13739 | var _this = _super.call(this) || this;
|
13740 | _this._limit = _limit;
|
13741 | return _this;
|
13742 | }
|
13743 | QueryLimitToLastConstraint.prototype._apply = function (query) {
|
13744 | if (query._queryParams.hasLimit()) {
|
13745 | throw new Error('limitToLast: Limit was already set (by another call to limitToFirst ' +
|
13746 | 'or limitToLast).');
|
13747 | }
|
13748 | return new QueryImpl(query._repo, query._path, queryParamsLimitToLast(query._queryParams, this._limit), query._orderByCalled);
|
13749 | };
|
13750 | return QueryLimitToLastConstraint;
|
13751 | }(QueryConstraint));
|
13752 |
|
13753 |
|
13754 |
|
13755 |
|
13756 |
|
13757 |
|
13758 |
|
13759 |
|
13760 |
|
13761 |
|
13762 |
|
13763 |
|
13764 |
|
13765 |
|
13766 |
|
13767 |
|
13768 |
|
13769 |
|
13770 | function limitToLast(limit) {
|
13771 | if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
|
13772 | throw new Error('limitToLast: First argument must be a positive integer.');
|
13773 | }
|
13774 | return new QueryLimitToLastConstraint(limit);
|
13775 | }
|
13776 | var QueryOrderByChildConstraint = (function (_super) {
|
13777 | tslib.__extends(QueryOrderByChildConstraint, _super);
|
13778 | function QueryOrderByChildConstraint(_path) {
|
13779 | var _this = _super.call(this) || this;
|
13780 | _this._path = _path;
|
13781 | return _this;
|
13782 | }
|
13783 | QueryOrderByChildConstraint.prototype._apply = function (query) {
|
13784 | validateNoPreviousOrderByCall(query, 'orderByChild');
|
13785 | var parsedPath = new Path(this._path);
|
13786 | if (pathIsEmpty(parsedPath)) {
|
13787 | throw new Error('orderByChild: cannot pass in empty path. Use orderByValue() instead.');
|
13788 | }
|
13789 | var index = new PathIndex(parsedPath);
|
13790 | var newParams = queryParamsOrderBy(query._queryParams, index);
|
13791 | validateQueryEndpoints(newParams);
|
13792 | return new QueryImpl(query._repo, query._path, newParams,
|
13793 | true);
|
13794 | };
|
13795 | return QueryOrderByChildConstraint;
|
13796 | }(QueryConstraint));
|
13797 |
|
13798 |
|
13799 |
|
13800 |
|
13801 |
|
13802 |
|
13803 |
|
13804 |
|
13805 |
|
13806 |
|
13807 |
|
13808 |
|
13809 |
|
13810 |
|
13811 |
|
13812 |
|
13813 |
|
13814 | function orderByChild(path) {
|
13815 | if (path === '$key') {
|
13816 | throw new Error('orderByChild: "$key" is invalid. Use orderByKey() instead.');
|
13817 | }
|
13818 | else if (path === '$priority') {
|
13819 | throw new Error('orderByChild: "$priority" is invalid. Use orderByPriority() instead.');
|
13820 | }
|
13821 | else if (path === '$value') {
|
13822 | throw new Error('orderByChild: "$value" is invalid. Use orderByValue() instead.');
|
13823 | }
|
13824 | validatePathString('orderByChild', 'path', path, false);
|
13825 | return new QueryOrderByChildConstraint(path);
|
13826 | }
|
13827 | var QueryOrderByKeyConstraint = (function (_super) {
|
13828 | tslib.__extends(QueryOrderByKeyConstraint, _super);
|
13829 | function QueryOrderByKeyConstraint() {
|
13830 | return _super !== null && _super.apply(this, arguments) || this;
|
13831 | }
|
13832 | QueryOrderByKeyConstraint.prototype._apply = function (query) {
|
13833 | validateNoPreviousOrderByCall(query, 'orderByKey');
|
13834 | var newParams = queryParamsOrderBy(query._queryParams, KEY_INDEX);
|
13835 | validateQueryEndpoints(newParams);
|
13836 | return new QueryImpl(query._repo, query._path, newParams,
|
13837 | true);
|
13838 | };
|
13839 | return QueryOrderByKeyConstraint;
|
13840 | }(QueryConstraint));
|
13841 |
|
13842 |
|
13843 |
|
13844 |
|
13845 |
|
13846 |
|
13847 |
|
13848 |
|
13849 | function orderByKey() {
|
13850 | return new QueryOrderByKeyConstraint();
|
13851 | }
|
13852 | var QueryOrderByPriorityConstraint = (function (_super) {
|
13853 | tslib.__extends(QueryOrderByPriorityConstraint, _super);
|
13854 | function QueryOrderByPriorityConstraint() {
|
13855 | return _super !== null && _super.apply(this, arguments) || this;
|
13856 | }
|
13857 | QueryOrderByPriorityConstraint.prototype._apply = function (query) {
|
13858 | validateNoPreviousOrderByCall(query, 'orderByPriority');
|
13859 | var newParams = queryParamsOrderBy(query._queryParams, PRIORITY_INDEX);
|
13860 | validateQueryEndpoints(newParams);
|
13861 | return new QueryImpl(query._repo, query._path, newParams,
|
13862 | true);
|
13863 | };
|
13864 | return QueryOrderByPriorityConstraint;
|
13865 | }(QueryConstraint));
|
13866 |
|
13867 |
|
13868 |
|
13869 |
|
13870 |
|
13871 |
|
13872 |
|
13873 |
|
13874 | function orderByPriority() {
|
13875 | return new QueryOrderByPriorityConstraint();
|
13876 | }
|
13877 | var QueryOrderByValueConstraint = (function (_super) {
|
13878 | tslib.__extends(QueryOrderByValueConstraint, _super);
|
13879 | function QueryOrderByValueConstraint() {
|
13880 | return _super !== null && _super.apply(this, arguments) || this;
|
13881 | }
|
13882 | QueryOrderByValueConstraint.prototype._apply = function (query) {
|
13883 | validateNoPreviousOrderByCall(query, 'orderByValue');
|
13884 | var newParams = queryParamsOrderBy(query._queryParams, VALUE_INDEX);
|
13885 | validateQueryEndpoints(newParams);
|
13886 | return new QueryImpl(query._repo, query._path, newParams,
|
13887 | true);
|
13888 | };
|
13889 | return QueryOrderByValueConstraint;
|
13890 | }(QueryConstraint));
|
13891 |
|
13892 |
|
13893 |
|
13894 |
|
13895 |
|
13896 |
|
13897 |
|
13898 |
|
13899 |
|
13900 | function orderByValue() {
|
13901 | return new QueryOrderByValueConstraint();
|
13902 | }
|
13903 | var QueryEqualToValueConstraint = (function (_super) {
|
13904 | tslib.__extends(QueryEqualToValueConstraint, _super);
|
13905 | function QueryEqualToValueConstraint(_value, _key) {
|
13906 | var _this = _super.call(this) || this;
|
13907 | _this._value = _value;
|
13908 | _this._key = _key;
|
13909 | return _this;
|
13910 | }
|
13911 | QueryEqualToValueConstraint.prototype._apply = function (query) {
|
13912 | validateFirebaseDataArg('equalTo', this._value, query._path, false);
|
13913 | if (query._queryParams.hasStart()) {
|
13914 | throw new Error('equalTo: Starting point was already set (by another call to startAt/startAfter or ' +
|
13915 | 'equalTo).');
|
13916 | }
|
13917 | if (query._queryParams.hasEnd()) {
|
13918 | throw new Error('equalTo: Ending point was already set (by another call to endAt/endBefore or ' +
|
13919 | 'equalTo).');
|
13920 | }
|
13921 | return new QueryEndAtConstraint(this._value, this._key)._apply(new QueryStartAtConstraint(this._value, this._key)._apply(query));
|
13922 | };
|
13923 | return QueryEqualToValueConstraint;
|
13924 | }(QueryConstraint));
|
13925 |
|
13926 |
|
13927 |
|
13928 |
|
13929 |
|
13930 |
|
13931 |
|
13932 |
|
13933 |
|
13934 |
|
13935 |
|
13936 |
|
13937 |
|
13938 |
|
13939 |
|
13940 |
|
13941 |
|
13942 |
|
13943 |
|
13944 |
|
13945 |
|
13946 |
|
13947 |
|
13948 | function equalTo(value, key) {
|
13949 | validateKey('equalTo', 'key', key, true);
|
13950 | return new QueryEqualToValueConstraint(value, key);
|
13951 | }
|
13952 |
|
13953 |
|
13954 |
|
13955 |
|
13956 |
|
13957 |
|
13958 |
|
13959 |
|
13960 |
|
13961 | function query(query) {
|
13962 | var e_1, _a;
|
13963 | var queryConstraints = [];
|
13964 | for (var _i = 1; _i < arguments.length; _i++) {
|
13965 | queryConstraints[_i - 1] = arguments[_i];
|
13966 | }
|
13967 | var queryImpl = util.getModularInstance(query);
|
13968 | try {
|
13969 | for (var queryConstraints_1 = tslib.__values(queryConstraints), queryConstraints_1_1 = queryConstraints_1.next(); !queryConstraints_1_1.done; queryConstraints_1_1 = queryConstraints_1.next()) {
|
13970 | var constraint = queryConstraints_1_1.value;
|
13971 | queryImpl = constraint._apply(queryImpl);
|
13972 | }
|
13973 | }
|
13974 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
13975 | finally {
|
13976 | try {
|
13977 | if (queryConstraints_1_1 && !queryConstraints_1_1.done && (_a = queryConstraints_1.return)) _a.call(queryConstraints_1);
|
13978 | }
|
13979 | finally { if (e_1) throw e_1.error; }
|
13980 | }
|
13981 | return queryImpl;
|
13982 | }
|
13983 |
|
13984 |
|
13985 |
|
13986 |
|
13987 |
|
13988 |
|
13989 | syncPointSetReferenceConstructor(ReferenceImpl);
|
13990 | syncTreeSetReferenceConstructor(ReferenceImpl);
|
13991 |
|
13992 |
|
13993 |
|
13994 |
|
13995 |
|
13996 |
|
13997 |
|
13998 |
|
13999 |
|
14000 |
|
14001 |
|
14002 |
|
14003 |
|
14004 |
|
14005 |
|
14006 |
|
14007 |
|
14008 |
|
14009 |
|
14010 |
|
14011 |
|
14012 |
|
14013 |
|
14014 |
|
14015 |
|
14016 | var FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
|
14017 |
|
14018 |
|
14019 |
|
14020 | var repos = {};
|
14021 |
|
14022 |
|
14023 |
|
14024 | var useRestClient = false;
|
14025 |
|
14026 |
|
14027 |
|
14028 | function repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider) {
|
14029 | repo.repoInfo_ = new RepoInfo(host + ":" + port,
|
14030 | false, repo.repoInfo_.namespace, repo.repoInfo_.webSocketOnly, repo.repoInfo_.nodeAdmin, repo.repoInfo_.persistenceKey, repo.repoInfo_.includeNamespaceInQueryParams);
|
14031 | if (tokenProvider) {
|
14032 | repo.authTokenProvider_ = tokenProvider;
|
14033 | }
|
14034 | }
|
14035 |
|
14036 |
|
14037 |
|
14038 |
|
14039 | function repoManagerDatabaseFromApp(app, authProvider, appCheckProvider, url, nodeAdmin) {
|
14040 | var dbUrl = url || app.options.databaseURL;
|
14041 | if (dbUrl === undefined) {
|
14042 | if (!app.options.projectId) {
|
14043 | fatal("Can't determine Firebase Database URL. Be sure to include " +
|
14044 | ' a Project ID when calling firebase.initializeApp().');
|
14045 | }
|
14046 | log('Using default host for project ', app.options.projectId);
|
14047 | dbUrl = app.options.projectId + "-default-rtdb.firebaseio.com";
|
14048 | }
|
14049 | var parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
|
14050 | var repoInfo = parsedUrl.repoInfo;
|
14051 | var isEmulator;
|
14052 | var dbEmulatorHost = undefined;
|
14053 | if (typeof process !== 'undefined') {
|
14054 | dbEmulatorHost = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
|
14055 | }
|
14056 | if (dbEmulatorHost) {
|
14057 | isEmulator = true;
|
14058 | dbUrl = "http://" + dbEmulatorHost + "?ns=" + repoInfo.namespace;
|
14059 | parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
|
14060 | repoInfo = parsedUrl.repoInfo;
|
14061 | }
|
14062 | else {
|
14063 | isEmulator = !parsedUrl.repoInfo.secure;
|
14064 | }
|
14065 | var authTokenProvider = nodeAdmin && isEmulator
|
14066 | ? new EmulatorTokenProvider(EmulatorTokenProvider.OWNER)
|
14067 | : new FirebaseAuthTokenProvider(app.name, app.options, authProvider);
|
14068 | validateUrl('Invalid Firebase Database URL', parsedUrl);
|
14069 | if (!pathIsEmpty(parsedUrl.path)) {
|
14070 | fatal('Database URL must point to the root of a Firebase Database ' +
|
14071 | '(not including a child path).');
|
14072 | }
|
14073 | var repo = repoManagerCreateRepo(repoInfo, app, authTokenProvider, new AppCheckTokenProvider(app.name, appCheckProvider));
|
14074 | return new FirebaseDatabase(repo, app);
|
14075 | }
|
14076 |
|
14077 |
|
14078 |
|
14079 |
|
14080 | function repoManagerDeleteRepo(repo, appName) {
|
14081 | var appRepos = repos[appName];
|
14082 |
|
14083 | if (!appRepos || appRepos[repo.key] !== repo) {
|
14084 | fatal("Database " + appName + "(" + repo.repoInfo_ + ") has already been deleted.");
|
14085 | }
|
14086 | repoInterrupt(repo);
|
14087 | delete appRepos[repo.key];
|
14088 | }
|
14089 |
|
14090 |
|
14091 |
|
14092 |
|
14093 |
|
14094 |
|
14095 |
|
14096 | function repoManagerCreateRepo(repoInfo, app, authTokenProvider, appCheckProvider) {
|
14097 | var appRepos = repos[app.name];
|
14098 | if (!appRepos) {
|
14099 | appRepos = {};
|
14100 | repos[app.name] = appRepos;
|
14101 | }
|
14102 | var repo = appRepos[repoInfo.toURLString()];
|
14103 | if (repo) {
|
14104 | fatal('Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.');
|
14105 | }
|
14106 | repo = new Repo(repoInfo, useRestClient, authTokenProvider, appCheckProvider);
|
14107 | appRepos[repoInfo.toURLString()] = repo;
|
14108 | return repo;
|
14109 | }
|
14110 |
|
14111 |
|
14112 |
|
14113 | function repoManagerForceRestClient(forceRestClient) {
|
14114 | useRestClient = forceRestClient;
|
14115 | }
|
14116 |
|
14117 |
|
14118 |
|
14119 | var FirebaseDatabase = (function () {
|
14120 |
|
14121 | function FirebaseDatabase(_repoInternal,
|
14122 | /** The FirebaseApp associated with this Realtime Database instance. */
|
14123 | app) {
|
14124 | this._repoInternal = _repoInternal;
|
14125 | this.app = app;
|
14126 |
|
14127 | this['type'] = 'database';
|
14128 |
|
14129 | this._instanceStarted = false;
|
14130 | }
|
14131 | Object.defineProperty(FirebaseDatabase.prototype, "_repo", {
|
14132 | get: function () {
|
14133 | if (!this._instanceStarted) {
|
14134 | repoStart(this._repoInternal, this.app.options.appId, this.app.options['databaseAuthVariableOverride']);
|
14135 | this._instanceStarted = true;
|
14136 | }
|
14137 | return this._repoInternal;
|
14138 | },
|
14139 | enumerable: false,
|
14140 | configurable: true
|
14141 | });
|
14142 | Object.defineProperty(FirebaseDatabase.prototype, "_root", {
|
14143 | get: function () {
|
14144 | if (!this._rootInternal) {
|
14145 | this._rootInternal = new ReferenceImpl(this._repo, newEmptyPath());
|
14146 | }
|
14147 | return this._rootInternal;
|
14148 | },
|
14149 | enumerable: false,
|
14150 | configurable: true
|
14151 | });
|
14152 | FirebaseDatabase.prototype._delete = function () {
|
14153 | this._checkNotDeleted('delete');
|
14154 | repoManagerDeleteRepo(this._repo, this.app.name);
|
14155 | this._repoInternal = null;
|
14156 | this._rootInternal = null;
|
14157 | return Promise.resolve();
|
14158 | };
|
14159 | FirebaseDatabase.prototype._checkNotDeleted = function (apiName) {
|
14160 | if (this._rootInternal === null) {
|
14161 | fatal('Cannot call ' + apiName + ' on a deleted database.');
|
14162 | }
|
14163 | };
|
14164 | return FirebaseDatabase;
|
14165 | }());
|
14166 |
|
14167 |
|
14168 |
|
14169 |
|
14170 |
|
14171 |
|
14172 |
|
14173 |
|
14174 |
|
14175 |
|
14176 |
|
14177 | function useDatabaseEmulator(db, host, port, options) {
|
14178 | if (options === void 0) { options = {}; }
|
14179 | db = util.getModularInstance(db);
|
14180 | db._checkNotDeleted('useEmulator');
|
14181 | if (db._instanceStarted) {
|
14182 | fatal('Cannot call useEmulator() after instance has already been initialized.');
|
14183 | }
|
14184 | var repo = db._repoInternal;
|
14185 | var tokenProvider = undefined;
|
14186 | if (repo.repoInfo_.nodeAdmin) {
|
14187 | if (options.mockUserToken) {
|
14188 | fatal('mockUserToken is not supported by the Admin SDK. For client access with mock users, please use the "firebase" package instead of "firebase-admin".');
|
14189 | }
|
14190 | tokenProvider = new EmulatorTokenProvider(EmulatorTokenProvider.OWNER);
|
14191 | }
|
14192 | else if (options.mockUserToken) {
|
14193 | var token = util.createMockUserToken(options.mockUserToken, db.app.options.projectId);
|
14194 | tokenProvider = new EmulatorTokenProvider(token);
|
14195 | }
|
14196 |
|
14197 | repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
|
14198 | }
|
14199 |
|
14200 |
|
14201 |
|
14202 |
|
14203 |
|
14204 |
|
14205 |
|
14206 |
|
14207 |
|
14208 |
|
14209 |
|
14210 |
|
14211 |
|
14212 |
|
14213 |
|
14214 |
|
14215 |
|
14216 |
|
14217 |
|
14218 |
|
14219 |
|
14220 | function goOffline(db) {
|
14221 | db = util.getModularInstance(db);
|
14222 | db._checkNotDeleted('goOffline');
|
14223 | repoInterrupt(db._repo);
|
14224 | }
|
14225 |
|
14226 |
|
14227 |
|
14228 |
|
14229 |
|
14230 |
|
14231 |
|
14232 |
|
14233 |
|
14234 |
|
14235 |
|
14236 | function goOnline(db) {
|
14237 | db = util.getModularInstance(db);
|
14238 | db._checkNotDeleted('goOnline');
|
14239 | repoResume(db._repo);
|
14240 | }
|
14241 | function enableLogging$1(logger, persistent) {
|
14242 | enableLogging(logger, persistent);
|
14243 | }
|
14244 |
|
14245 |
|
14246 |
|
14247 |
|
14248 |
|
14249 |
|
14250 |
|
14251 |
|
14252 |
|
14253 |
|
14254 |
|
14255 |
|
14256 |
|
14257 |
|
14258 |
|
14259 |
|
14260 |
|
14261 | var SERVER_TIMESTAMP = {
|
14262 | '.sv': 'timestamp'
|
14263 | };
|
14264 |
|
14265 |
|
14266 |
|
14267 |
|
14268 |
|
14269 | function serverTimestamp() {
|
14270 | return SERVER_TIMESTAMP;
|
14271 | }
|
14272 |
|
14273 |
|
14274 |
|
14275 |
|
14276 |
|
14277 |
|
14278 |
|
14279 | function increment(delta) {
|
14280 | return {
|
14281 | '.sv': {
|
14282 | 'increment': delta
|
14283 | }
|
14284 | };
|
14285 | }
|
14286 |
|
14287 |
|
14288 |
|
14289 |
|
14290 |
|
14291 |
|
14292 |
|
14293 |
|
14294 |
|
14295 |
|
14296 |
|
14297 |
|
14298 |
|
14299 |
|
14300 |
|
14301 |
|
14302 |
|
14303 |
|
14304 |
|
14305 |
|
14306 | var TransactionResult = (function () {
|
14307 |
|
14308 | function TransactionResult(
|
14309 | /** Whether the transaction was successfully committed. */
|
14310 | committed,
|
14311 | /** The resulting data snapshot. */
|
14312 | snapshot) {
|
14313 | this.committed = committed;
|
14314 | this.snapshot = snapshot;
|
14315 | }
|
14316 |
|
14317 | TransactionResult.prototype.toJSON = function () {
|
14318 | return { committed: this.committed, snapshot: this.snapshot.toJSON() };
|
14319 | };
|
14320 | return TransactionResult;
|
14321 | }());
|
14322 |
|
14323 |
|
14324 |
|
14325 |
|
14326 |
|
14327 |
|
14328 |
|
14329 |
|
14330 |
|
14331 |
|
14332 |
|
14333 |
|
14334 |
|
14335 |
|
14336 |
|
14337 |
|
14338 |
|
14339 |
|
14340 |
|
14341 |
|
14342 |
|
14343 |
|
14344 |
|
14345 |
|
14346 |
|
14347 |
|
14348 |
|
14349 |
|
14350 |
|
14351 |
|
14352 |
|
14353 |
|
14354 |
|
14355 |
|
14356 |
|
14357 |
|
14358 |
|
14359 | function runTransaction(ref,
|
14360 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
14361 | transactionUpdate, options) {
|
14362 | var _a;
|
14363 | ref = util.getModularInstance(ref);
|
14364 | validateWritablePath('Reference.transaction', ref._path);
|
14365 | if (ref.key === '.length' || ref.key === '.keys') {
|
14366 | throw ('Reference.transaction failed: ' + ref.key + ' is a read-only object.');
|
14367 | }
|
14368 | var applyLocally = (_a = options === null || options === void 0 ? void 0 : options.applyLocally) !== null && _a !== void 0 ? _a : true;
|
14369 | var deferred = new util.Deferred();
|
14370 | var promiseComplete = function (error, committed, node) {
|
14371 | var dataSnapshot = null;
|
14372 | if (error) {
|
14373 | deferred.reject(error);
|
14374 | }
|
14375 | else {
|
14376 | dataSnapshot = new DataSnapshot(node, new ReferenceImpl(ref._repo, ref._path), PRIORITY_INDEX);
|
14377 | deferred.resolve(new TransactionResult(committed, dataSnapshot));
|
14378 | }
|
14379 | };
|
14380 |
|
14381 | var unwatcher = onValue(ref, function () { });
|
14382 | repoStartTransaction(ref._repo, ref._path, transactionUpdate, promiseComplete, unwatcher, applyLocally);
|
14383 | return deferred.promise;
|
14384 | }
|
14385 |
|
14386 |
|
14387 |
|
14388 |
|
14389 |
|
14390 |
|
14391 |
|
14392 |
|
14393 |
|
14394 |
|
14395 |
|
14396 |
|
14397 |
|
14398 |
|
14399 |
|
14400 |
|
14401 |
|
14402 | var OnDisconnect$1 = (function () {
|
14403 | function OnDisconnect(_delegate) {
|
14404 | this._delegate = _delegate;
|
14405 | }
|
14406 | OnDisconnect.prototype.cancel = function (onComplete) {
|
14407 | util.validateArgCount('OnDisconnect.cancel', 0, 1, arguments.length);
|
14408 | util.validateCallback('OnDisconnect.cancel', 'onComplete', onComplete, true);
|
14409 | var result = this._delegate.cancel();
|
14410 | if (onComplete) {
|
14411 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14412 | }
|
14413 | return result;
|
14414 | };
|
14415 | OnDisconnect.prototype.remove = function (onComplete) {
|
14416 | util.validateArgCount('OnDisconnect.remove', 0, 1, arguments.length);
|
14417 | util.validateCallback('OnDisconnect.remove', 'onComplete', onComplete, true);
|
14418 | var result = this._delegate.remove();
|
14419 | if (onComplete) {
|
14420 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14421 | }
|
14422 | return result;
|
14423 | };
|
14424 | OnDisconnect.prototype.set = function (value, onComplete) {
|
14425 | util.validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
|
14426 | util.validateCallback('OnDisconnect.set', 'onComplete', onComplete, true);
|
14427 | var result = this._delegate.set(value);
|
14428 | if (onComplete) {
|
14429 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14430 | }
|
14431 | return result;
|
14432 | };
|
14433 | OnDisconnect.prototype.setWithPriority = function (value, priority, onComplete) {
|
14434 | util.validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
|
14435 | util.validateCallback('OnDisconnect.setWithPriority', 'onComplete', onComplete, true);
|
14436 | var result = this._delegate.setWithPriority(value, priority);
|
14437 | if (onComplete) {
|
14438 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14439 | }
|
14440 | return result;
|
14441 | };
|
14442 | OnDisconnect.prototype.update = function (objectToMerge, onComplete) {
|
14443 | util.validateArgCount('OnDisconnect.update', 1, 2, arguments.length);
|
14444 | if (Array.isArray(objectToMerge)) {
|
14445 | var newObjectToMerge = {};
|
14446 | for (var i = 0; i < objectToMerge.length; ++i) {
|
14447 | newObjectToMerge['' + i] = objectToMerge[i];
|
14448 | }
|
14449 | objectToMerge = newObjectToMerge;
|
14450 | warn('Passing an Array to firebase.database.onDisconnect().update() is deprecated. Use set() if you want to overwrite the ' +
|
14451 | 'existing data, or an Object with integer keys if you really do want to only update some of the children.');
|
14452 | }
|
14453 | util.validateCallback('OnDisconnect.update', 'onComplete', onComplete, true);
|
14454 | var result = this._delegate.update(objectToMerge);
|
14455 | if (onComplete) {
|
14456 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14457 | }
|
14458 | return result;
|
14459 | };
|
14460 | return OnDisconnect;
|
14461 | }());
|
14462 |
|
14463 |
|
14464 |
|
14465 |
|
14466 |
|
14467 |
|
14468 |
|
14469 |
|
14470 |
|
14471 |
|
14472 |
|
14473 |
|
14474 |
|
14475 |
|
14476 |
|
14477 |
|
14478 |
|
14479 | var TransactionResult$1 = (function () {
|
14480 | |
14481 |
|
14482 |
|
14483 | function TransactionResult(committed, snapshot) {
|
14484 | this.committed = committed;
|
14485 | this.snapshot = snapshot;
|
14486 | }
|
14487 |
|
14488 |
|
14489 | TransactionResult.prototype.toJSON = function () {
|
14490 | util.validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
|
14491 | return { committed: this.committed, snapshot: this.snapshot.toJSON() };
|
14492 | };
|
14493 | return TransactionResult;
|
14494 | }());
|
14495 |
|
14496 |
|
14497 |
|
14498 |
|
14499 |
|
14500 |
|
14501 |
|
14502 |
|
14503 |
|
14504 |
|
14505 |
|
14506 |
|
14507 |
|
14508 |
|
14509 |
|
14510 |
|
14511 |
|
14512 |
|
14513 |
|
14514 |
|
14515 |
|
14516 |
|
14517 | var DataSnapshot$1 = (function () {
|
14518 | function DataSnapshot(_database, _delegate) {
|
14519 | this._database = _database;
|
14520 | this._delegate = _delegate;
|
14521 | }
|
14522 | |
14523 |
|
14524 |
|
14525 |
|
14526 |
|
14527 |
|
14528 | DataSnapshot.prototype.val = function () {
|
14529 | util.validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
|
14530 | return this._delegate.val();
|
14531 | };
|
14532 | |
14533 |
|
14534 |
|
14535 |
|
14536 |
|
14537 | DataSnapshot.prototype.exportVal = function () {
|
14538 | util.validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
|
14539 | return this._delegate.exportVal();
|
14540 | };
|
14541 |
|
14542 |
|
14543 | DataSnapshot.prototype.toJSON = function () {
|
14544 |
|
14545 | util.validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
|
14546 | return this._delegate.toJSON();
|
14547 | };
|
14548 | |
14549 |
|
14550 |
|
14551 |
|
14552 |
|
14553 | DataSnapshot.prototype.exists = function () {
|
14554 | util.validateArgCount('DataSnapshot.exists', 0, 0, arguments.length);
|
14555 | return this._delegate.exists();
|
14556 | };
|
14557 | |
14558 |
|
14559 |
|
14560 |
|
14561 |
|
14562 |
|
14563 | DataSnapshot.prototype.child = function (path) {
|
14564 | util.validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
|
14565 |
|
14566 | path = String(path);
|
14567 | validatePathString('DataSnapshot.child', 'path', path, false);
|
14568 | return new DataSnapshot(this._database, this._delegate.child(path));
|
14569 | };
|
14570 | |
14571 |
|
14572 |
|
14573 |
|
14574 |
|
14575 |
|
14576 | DataSnapshot.prototype.hasChild = function (path) {
|
14577 | util.validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
|
14578 | validatePathString('DataSnapshot.hasChild', 'path', path, false);
|
14579 | return this._delegate.hasChild(path);
|
14580 | };
|
14581 | |
14582 |
|
14583 |
|
14584 |
|
14585 |
|
14586 | DataSnapshot.prototype.getPriority = function () {
|
14587 | util.validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
|
14588 | return this._delegate.priority;
|
14589 | };
|
14590 | |
14591 |
|
14592 |
|
14593 |
|
14594 |
|
14595 |
|
14596 |
|
14597 |
|
14598 | DataSnapshot.prototype.forEach = function (action) {
|
14599 | var _this = this;
|
14600 | util.validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
|
14601 | util.validateCallback('DataSnapshot.forEach', 'action', action, false);
|
14602 | return this._delegate.forEach(function (expDataSnapshot) {
|
14603 | return action(new DataSnapshot(_this._database, expDataSnapshot));
|
14604 | });
|
14605 | };
|
14606 | |
14607 |
|
14608 |
|
14609 |
|
14610 | DataSnapshot.prototype.hasChildren = function () {
|
14611 | util.validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
|
14612 | return this._delegate.hasChildren();
|
14613 | };
|
14614 | Object.defineProperty(DataSnapshot.prototype, "key", {
|
14615 | get: function () {
|
14616 | return this._delegate.key;
|
14617 | },
|
14618 | enumerable: false,
|
14619 | configurable: true
|
14620 | });
|
14621 | |
14622 |
|
14623 |
|
14624 |
|
14625 | DataSnapshot.prototype.numChildren = function () {
|
14626 | util.validateArgCount('DataSnapshot.numChildren', 0, 0, arguments.length);
|
14627 | return this._delegate.size;
|
14628 | };
|
14629 | |
14630 |
|
14631 |
|
14632 |
|
14633 | DataSnapshot.prototype.getRef = function () {
|
14634 | util.validateArgCount('DataSnapshot.ref', 0, 0, arguments.length);
|
14635 | return new Reference(this._database, this._delegate.ref);
|
14636 | };
|
14637 | Object.defineProperty(DataSnapshot.prototype, "ref", {
|
14638 | get: function () {
|
14639 | return this.getRef();
|
14640 | },
|
14641 | enumerable: false,
|
14642 | configurable: true
|
14643 | });
|
14644 | return DataSnapshot;
|
14645 | }());
|
14646 |
|
14647 |
|
14648 |
|
14649 |
|
14650 |
|
14651 |
|
14652 | var Query = (function () {
|
14653 | function Query(database, _delegate) {
|
14654 | this.database = database;
|
14655 | this._delegate = _delegate;
|
14656 | }
|
14657 | Query.prototype.on = function (eventType, callback, cancelCallbackOrContext, context) {
|
14658 | var _this = this;
|
14659 | var _a;
|
14660 | util.validateArgCount('Query.on', 2, 4, arguments.length);
|
14661 | util.validateCallback('Query.on', 'callback', callback, false);
|
14662 | var ret = Query.getCancelAndContextArgs_('Query.on', cancelCallbackOrContext, context);
|
14663 | var valueCallback = function (expSnapshot, previousChildName) {
|
14664 | callback.call(ret.context, new DataSnapshot$1(_this.database, expSnapshot), previousChildName);
|
14665 | };
|
14666 | valueCallback.userCallback = callback;
|
14667 | valueCallback.context = ret.context;
|
14668 | var cancelCallback = (_a = ret.cancel) === null || _a === void 0 ? void 0 : _a.bind(ret.context);
|
14669 | switch (eventType) {
|
14670 | case 'value':
|
14671 | onValue(this._delegate, valueCallback, cancelCallback);
|
14672 | return callback;
|
14673 | case 'child_added':
|
14674 | onChildAdded(this._delegate, valueCallback, cancelCallback);
|
14675 | return callback;
|
14676 | case 'child_removed':
|
14677 | onChildRemoved(this._delegate, valueCallback, cancelCallback);
|
14678 | return callback;
|
14679 | case 'child_changed':
|
14680 | onChildChanged(this._delegate, valueCallback, cancelCallback);
|
14681 | return callback;
|
14682 | case 'child_moved':
|
14683 | onChildMoved(this._delegate, valueCallback, cancelCallback);
|
14684 | return callback;
|
14685 | default:
|
14686 | throw new Error(util.errorPrefix('Query.on', 'eventType') +
|
14687 | 'must be a valid event type = "value", "child_added", "child_removed", ' +
|
14688 | '"child_changed", or "child_moved".');
|
14689 | }
|
14690 | };
|
14691 | Query.prototype.off = function (eventType, callback, context) {
|
14692 | util.validateArgCount('Query.off', 0, 3, arguments.length);
|
14693 | validateEventType('Query.off', eventType, true);
|
14694 | util.validateCallback('Query.off', 'callback', callback, true);
|
14695 | util.validateContextObject('Query.off', 'context', context, true);
|
14696 | if (callback) {
|
14697 | var valueCallback = function () { };
|
14698 | valueCallback.userCallback = callback;
|
14699 | valueCallback.context = context;
|
14700 | off(this._delegate, eventType, valueCallback);
|
14701 | }
|
14702 | else {
|
14703 | off(this._delegate, eventType);
|
14704 | }
|
14705 | };
|
14706 | |
14707 |
|
14708 |
|
14709 | Query.prototype.get = function () {
|
14710 | var _this = this;
|
14711 | return get(this._delegate).then(function (expSnapshot) {
|
14712 | return new DataSnapshot$1(_this.database, expSnapshot);
|
14713 | });
|
14714 | };
|
14715 | |
14716 |
|
14717 |
|
14718 | Query.prototype.once = function (eventType, callback, failureCallbackOrContext, context) {
|
14719 | var _this = this;
|
14720 | util.validateArgCount('Query.once', 1, 4, arguments.length);
|
14721 | util.validateCallback('Query.once', 'callback', callback, true);
|
14722 | var ret = Query.getCancelAndContextArgs_('Query.once', failureCallbackOrContext, context);
|
14723 | var deferred = new util.Deferred();
|
14724 | var valueCallback = function (expSnapshot, previousChildName) {
|
14725 | var result = new DataSnapshot$1(_this.database, expSnapshot);
|
14726 | if (callback) {
|
14727 | callback.call(ret.context, result, previousChildName);
|
14728 | }
|
14729 | deferred.resolve(result);
|
14730 | };
|
14731 | valueCallback.userCallback = callback;
|
14732 | valueCallback.context = ret.context;
|
14733 | var cancelCallback = function (error) {
|
14734 | if (ret.cancel) {
|
14735 | ret.cancel.call(ret.context, error);
|
14736 | }
|
14737 | deferred.reject(error);
|
14738 | };
|
14739 | switch (eventType) {
|
14740 | case 'value':
|
14741 | onValue(this._delegate, valueCallback, cancelCallback, {
|
14742 | onlyOnce: true
|
14743 | });
|
14744 | break;
|
14745 | case 'child_added':
|
14746 | onChildAdded(this._delegate, valueCallback, cancelCallback, {
|
14747 | onlyOnce: true
|
14748 | });
|
14749 | break;
|
14750 | case 'child_removed':
|
14751 | onChildRemoved(this._delegate, valueCallback, cancelCallback, {
|
14752 | onlyOnce: true
|
14753 | });
|
14754 | break;
|
14755 | case 'child_changed':
|
14756 | onChildChanged(this._delegate, valueCallback, cancelCallback, {
|
14757 | onlyOnce: true
|
14758 | });
|
14759 | break;
|
14760 | case 'child_moved':
|
14761 | onChildMoved(this._delegate, valueCallback, cancelCallback, {
|
14762 | onlyOnce: true
|
14763 | });
|
14764 | break;
|
14765 | default:
|
14766 | throw new Error(util.errorPrefix('Query.once', 'eventType') +
|
14767 | 'must be a valid event type = "value", "child_added", "child_removed", ' +
|
14768 | '"child_changed", or "child_moved".');
|
14769 | }
|
14770 | return deferred.promise;
|
14771 | };
|
14772 | |
14773 |
|
14774 |
|
14775 | Query.prototype.limitToFirst = function (limit) {
|
14776 | util.validateArgCount('Query.limitToFirst', 1, 1, arguments.length);
|
14777 | return new Query(this.database, query(this._delegate, limitToFirst(limit)));
|
14778 | };
|
14779 | |
14780 |
|
14781 |
|
14782 | Query.prototype.limitToLast = function (limit) {
|
14783 | util.validateArgCount('Query.limitToLast', 1, 1, arguments.length);
|
14784 | return new Query(this.database, query(this._delegate, limitToLast(limit)));
|
14785 | };
|
14786 | |
14787 |
|
14788 |
|
14789 | Query.prototype.orderByChild = function (path) {
|
14790 | util.validateArgCount('Query.orderByChild', 1, 1, arguments.length);
|
14791 | return new Query(this.database, query(this._delegate, orderByChild(path)));
|
14792 | };
|
14793 | |
14794 |
|
14795 |
|
14796 | Query.prototype.orderByKey = function () {
|
14797 | util.validateArgCount('Query.orderByKey', 0, 0, arguments.length);
|
14798 | return new Query(this.database, query(this._delegate, orderByKey()));
|
14799 | };
|
14800 | |
14801 |
|
14802 |
|
14803 | Query.prototype.orderByPriority = function () {
|
14804 | util.validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
|
14805 | return new Query(this.database, query(this._delegate, orderByPriority()));
|
14806 | };
|
14807 | |
14808 |
|
14809 |
|
14810 | Query.prototype.orderByValue = function () {
|
14811 | util.validateArgCount('Query.orderByValue', 0, 0, arguments.length);
|
14812 | return new Query(this.database, query(this._delegate, orderByValue()));
|
14813 | };
|
14814 | Query.prototype.startAt = function (value, name) {
|
14815 | if (value === void 0) { value = null; }
|
14816 | util.validateArgCount('Query.startAt', 0, 2, arguments.length);
|
14817 | return new Query(this.database, query(this._delegate, startAt(value, name)));
|
14818 | };
|
14819 | Query.prototype.startAfter = function (value, name) {
|
14820 | if (value === void 0) { value = null; }
|
14821 | util.validateArgCount('Query.startAfter', 0, 2, arguments.length);
|
14822 | return new Query(this.database, query(this._delegate, startAfter(value, name)));
|
14823 | };
|
14824 | Query.prototype.endAt = function (value, name) {
|
14825 | if (value === void 0) { value = null; }
|
14826 | util.validateArgCount('Query.endAt', 0, 2, arguments.length);
|
14827 | return new Query(this.database, query(this._delegate, endAt(value, name)));
|
14828 | };
|
14829 | Query.prototype.endBefore = function (value, name) {
|
14830 | if (value === void 0) { value = null; }
|
14831 | util.validateArgCount('Query.endBefore', 0, 2, arguments.length);
|
14832 | return new Query(this.database, query(this._delegate, endBefore(value, name)));
|
14833 | };
|
14834 | |
14835 |
|
14836 |
|
14837 |
|
14838 | Query.prototype.equalTo = function (value, name) {
|
14839 | util.validateArgCount('Query.equalTo', 1, 2, arguments.length);
|
14840 | return new Query(this.database, query(this._delegate, equalTo(value, name)));
|
14841 | };
|
14842 | |
14843 |
|
14844 |
|
14845 | Query.prototype.toString = function () {
|
14846 | util.validateArgCount('Query.toString', 0, 0, arguments.length);
|
14847 | return this._delegate.toString();
|
14848 | };
|
14849 |
|
14850 |
|
14851 | Query.prototype.toJSON = function () {
|
14852 |
|
14853 | util.validateArgCount('Query.toJSON', 0, 1, arguments.length);
|
14854 | return this._delegate.toJSON();
|
14855 | };
|
14856 | |
14857 |
|
14858 |
|
14859 | Query.prototype.isEqual = function (other) {
|
14860 | util.validateArgCount('Query.isEqual', 1, 1, arguments.length);
|
14861 | if (!(other instanceof Query)) {
|
14862 | var error = 'Query.isEqual failed: First argument must be an instance of firebase.database.Query.';
|
14863 | throw new Error(error);
|
14864 | }
|
14865 | return this._delegate.isEqual(other._delegate);
|
14866 | };
|
14867 | |
14868 |
|
14869 |
|
14870 |
|
14871 |
|
14872 | Query.getCancelAndContextArgs_ = function (fnName, cancelOrContext, context) {
|
14873 | var ret = { cancel: undefined, context: undefined };
|
14874 | if (cancelOrContext && context) {
|
14875 | ret.cancel = cancelOrContext;
|
14876 | util.validateCallback(fnName, 'cancel', ret.cancel, true);
|
14877 | ret.context = context;
|
14878 | util.validateContextObject(fnName, 'context', ret.context, true);
|
14879 | }
|
14880 | else if (cancelOrContext) {
|
14881 |
|
14882 | if (typeof cancelOrContext === 'object' && cancelOrContext !== null) {
|
14883 |
|
14884 | ret.context = cancelOrContext;
|
14885 | }
|
14886 | else if (typeof cancelOrContext === 'function') {
|
14887 | ret.cancel = cancelOrContext;
|
14888 | }
|
14889 | else {
|
14890 | throw new Error(util.errorPrefix(fnName, 'cancelOrContext') +
|
14891 | ' must either be a cancel callback or a context object.');
|
14892 | }
|
14893 | }
|
14894 | return ret;
|
14895 | };
|
14896 | Object.defineProperty(Query.prototype, "ref", {
|
14897 | get: function () {
|
14898 | return new Reference(this.database, new ReferenceImpl(this._delegate._repo, this._delegate._path));
|
14899 | },
|
14900 | enumerable: false,
|
14901 | configurable: true
|
14902 | });
|
14903 | return Query;
|
14904 | }());
|
14905 | var Reference = (function (_super) {
|
14906 | tslib.__extends(Reference, _super);
|
14907 | |
14908 |
|
14909 |
|
14910 |
|
14911 |
|
14912 |
|
14913 |
|
14914 | function Reference(database, _delegate) {
|
14915 | var _this = _super.call(this, database, new QueryImpl(_delegate._repo, _delegate._path, new QueryParams(), false)) || this;
|
14916 | _this.database = database;
|
14917 | _this._delegate = _delegate;
|
14918 | return _this;
|
14919 | }
|
14920 |
|
14921 | Reference.prototype.getKey = function () {
|
14922 | util.validateArgCount('Reference.key', 0, 0, arguments.length);
|
14923 | return this._delegate.key;
|
14924 | };
|
14925 | Reference.prototype.child = function (pathString) {
|
14926 | util.validateArgCount('Reference.child', 1, 1, arguments.length);
|
14927 | if (typeof pathString === 'number') {
|
14928 | pathString = String(pathString);
|
14929 | }
|
14930 | return new Reference(this.database, child(this._delegate, pathString));
|
14931 | };
|
14932 |
|
14933 | Reference.prototype.getParent = function () {
|
14934 | util.validateArgCount('Reference.parent', 0, 0, arguments.length);
|
14935 | var parent = this._delegate.parent;
|
14936 | return parent ? new Reference(this.database, parent) : null;
|
14937 | };
|
14938 |
|
14939 | Reference.prototype.getRoot = function () {
|
14940 | util.validateArgCount('Reference.root', 0, 0, arguments.length);
|
14941 | return new Reference(this.database, this._delegate.root);
|
14942 | };
|
14943 | Reference.prototype.set = function (newVal, onComplete) {
|
14944 | util.validateArgCount('Reference.set', 1, 2, arguments.length);
|
14945 | util.validateCallback('Reference.set', 'onComplete', onComplete, true);
|
14946 | var result = set(this._delegate, newVal);
|
14947 | if (onComplete) {
|
14948 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14949 | }
|
14950 | return result;
|
14951 | };
|
14952 | Reference.prototype.update = function (values, onComplete) {
|
14953 | util.validateArgCount('Reference.update', 1, 2, arguments.length);
|
14954 | if (Array.isArray(values)) {
|
14955 | var newObjectToMerge = {};
|
14956 | for (var i = 0; i < values.length; ++i) {
|
14957 | newObjectToMerge['' + i] = values[i];
|
14958 | }
|
14959 | values = newObjectToMerge;
|
14960 | warn('Passing an Array to Firebase.update() is deprecated. ' +
|
14961 | 'Use set() if you want to overwrite the existing data, or ' +
|
14962 | 'an Object with integer keys if you really do want to ' +
|
14963 | 'only update some of the children.');
|
14964 | }
|
14965 | validateWritablePath('Reference.update', this._delegate._path);
|
14966 | util.validateCallback('Reference.update', 'onComplete', onComplete, true);
|
14967 | var result = update(this._delegate, values);
|
14968 | if (onComplete) {
|
14969 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14970 | }
|
14971 | return result;
|
14972 | };
|
14973 | Reference.prototype.setWithPriority = function (newVal, newPriority, onComplete) {
|
14974 | util.validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
|
14975 | util.validateCallback('Reference.setWithPriority', 'onComplete', onComplete, true);
|
14976 | var result = setWithPriority(this._delegate, newVal, newPriority);
|
14977 | if (onComplete) {
|
14978 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14979 | }
|
14980 | return result;
|
14981 | };
|
14982 | Reference.prototype.remove = function (onComplete) {
|
14983 | util.validateArgCount('Reference.remove', 0, 1, arguments.length);
|
14984 | util.validateCallback('Reference.remove', 'onComplete', onComplete, true);
|
14985 | var result = remove(this._delegate);
|
14986 | if (onComplete) {
|
14987 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
14988 | }
|
14989 | return result;
|
14990 | };
|
14991 | Reference.prototype.transaction = function (transactionUpdate, onComplete, applyLocally) {
|
14992 | var _this = this;
|
14993 | util.validateArgCount('Reference.transaction', 1, 3, arguments.length);
|
14994 | util.validateCallback('Reference.transaction', 'transactionUpdate', transactionUpdate, false);
|
14995 | util.validateCallback('Reference.transaction', 'onComplete', onComplete, true);
|
14996 | validateBoolean('Reference.transaction', 'applyLocally', applyLocally, true);
|
14997 | var result = runTransaction(this._delegate, transactionUpdate, {
|
14998 | applyLocally: applyLocally
|
14999 | }).then(function (transactionResult) {
|
15000 | return new TransactionResult$1(transactionResult.committed, new DataSnapshot$1(_this.database, transactionResult.snapshot));
|
15001 | });
|
15002 | if (onComplete) {
|
15003 | result.then(function (transactionResult) {
|
15004 | return onComplete(null, transactionResult.committed, transactionResult.snapshot);
|
15005 | }, function (error) { return onComplete(error, false, null); });
|
15006 | }
|
15007 | return result;
|
15008 | };
|
15009 | Reference.prototype.setPriority = function (priority, onComplete) {
|
15010 | util.validateArgCount('Reference.setPriority', 1, 2, arguments.length);
|
15011 | util.validateCallback('Reference.setPriority', 'onComplete', onComplete, true);
|
15012 | var result = setPriority(this._delegate, priority);
|
15013 | if (onComplete) {
|
15014 | result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
15015 | }
|
15016 | return result;
|
15017 | };
|
15018 | Reference.prototype.push = function (value, onComplete) {
|
15019 | var _this = this;
|
15020 | util.validateArgCount('Reference.push', 0, 2, arguments.length);
|
15021 | util.validateCallback('Reference.push', 'onComplete', onComplete, true);
|
15022 | var expPromise = push(this._delegate, value);
|
15023 | var promise = expPromise.then(function (expRef) { return new Reference(_this.database, expRef); });
|
15024 | if (onComplete) {
|
15025 | promise.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
|
15026 | }
|
15027 | var result = new Reference(this.database, expPromise);
|
15028 | result.then = promise.then.bind(promise);
|
15029 | result.catch = promise.catch.bind(promise, undefined);
|
15030 | return result;
|
15031 | };
|
15032 | Reference.prototype.onDisconnect = function () {
|
15033 | validateWritablePath('Reference.onDisconnect', this._delegate._path);
|
15034 | return new OnDisconnect$1(new OnDisconnect(this._delegate._repo, this._delegate._path));
|
15035 | };
|
15036 | Object.defineProperty(Reference.prototype, "key", {
|
15037 | get: function () {
|
15038 | return this.getKey();
|
15039 | },
|
15040 | enumerable: false,
|
15041 | configurable: true
|
15042 | });
|
15043 | Object.defineProperty(Reference.prototype, "parent", {
|
15044 | get: function () {
|
15045 | return this.getParent();
|
15046 | },
|
15047 | enumerable: false,
|
15048 | configurable: true
|
15049 | });
|
15050 | Object.defineProperty(Reference.prototype, "root", {
|
15051 | get: function () {
|
15052 | return this.getRoot();
|
15053 | },
|
15054 | enumerable: false,
|
15055 | configurable: true
|
15056 | });
|
15057 | return Reference;
|
15058 | }(Query));
|
15059 |
|
15060 |
|
15061 |
|
15062 |
|
15063 |
|
15064 |
|
15065 |
|
15066 |
|
15067 |
|
15068 |
|
15069 |
|
15070 |
|
15071 |
|
15072 |
|
15073 |
|
15074 |
|
15075 |
|
15076 |
|
15077 |
|
15078 |
|
15079 | var Database = (function () {
|
15080 | |
15081 |
|
15082 |
|
15083 | function Database(_delegate, app) {
|
15084 | var _this = this;
|
15085 | this._delegate = _delegate;
|
15086 | this.app = app;
|
15087 | this.INTERNAL = {
|
15088 | delete: function () { return _this._delegate._delete(); }
|
15089 | };
|
15090 | }
|
15091 | |
15092 |
|
15093 |
|
15094 |
|
15095 |
|
15096 |
|
15097 |
|
15098 |
|
15099 |
|
15100 | Database.prototype.useEmulator = function (host, port, options) {
|
15101 | if (options === void 0) { options = {}; }
|
15102 | useDatabaseEmulator(this._delegate, host, port, options);
|
15103 | };
|
15104 | Database.prototype.ref = function (path) {
|
15105 | util.validateArgCount('database.ref', 0, 1, arguments.length);
|
15106 | if (path instanceof Reference) {
|
15107 | var childRef = refFromURL(this._delegate, path.toString());
|
15108 | return new Reference(this, childRef);
|
15109 | }
|
15110 | else {
|
15111 | var childRef = ref(this._delegate, path);
|
15112 | return new Reference(this, childRef);
|
15113 | }
|
15114 | };
|
15115 | |
15116 |
|
15117 |
|
15118 |
|
15119 |
|
15120 |
|
15121 | Database.prototype.refFromURL = function (url) {
|
15122 | var apiName = 'database.refFromURL';
|
15123 | util.validateArgCount(apiName, 1, 1, arguments.length);
|
15124 | var childRef = refFromURL(this._delegate, url);
|
15125 | return new Reference(this, childRef);
|
15126 | };
|
15127 |
|
15128 | Database.prototype.goOffline = function () {
|
15129 | util.validateArgCount('database.goOffline', 0, 0, arguments.length);
|
15130 | return goOffline(this._delegate);
|
15131 | };
|
15132 | Database.prototype.goOnline = function () {
|
15133 | util.validateArgCount('database.goOnline', 0, 0, arguments.length);
|
15134 | return goOnline(this._delegate);
|
15135 | };
|
15136 | Database.ServerValue = {
|
15137 | TIMESTAMP: serverTimestamp(),
|
15138 | increment: function (delta) { return increment(delta); }
|
15139 | };
|
15140 | return Database;
|
15141 | }());
|
15142 |
|
15143 |
|
15144 |
|
15145 |
|
15146 |
|
15147 |
|
15148 |
|
15149 |
|
15150 |
|
15151 |
|
15152 |
|
15153 |
|
15154 |
|
15155 |
|
15156 |
|
15157 |
|
15158 |
|
15159 |
|
15160 |
|
15161 |
|
15162 |
|
15163 |
|
15164 | var forceLongPolling = function () {
|
15165 | WebSocketConnection.forceDisallow();
|
15166 | BrowserPollConnection.forceAllow();
|
15167 | };
|
15168 | var forceWebSockets = function () {
|
15169 | BrowserPollConnection.forceDisallow();
|
15170 | };
|
15171 |
|
15172 | var isWebSocketsAvailable = function () {
|
15173 | return WebSocketConnection['isAvailable']();
|
15174 | };
|
15175 | var setSecurityDebugCallback = function (ref, callback) {
|
15176 | var connection = ref._delegate._repo.persistentConnection_;
|
15177 |
|
15178 | connection.securityDebugCallback_ = callback;
|
15179 | };
|
15180 | var stats = function (ref, showDelta) {
|
15181 | repoStats(ref._delegate._repo, showDelta);
|
15182 | };
|
15183 | var statsIncrementCounter = function (ref, metric) {
|
15184 | repoStatsIncrementCounter(ref._delegate._repo, metric);
|
15185 | };
|
15186 | var dataUpdateCount = function (ref) {
|
15187 | return ref._delegate._repo.dataUpdateCount;
|
15188 | };
|
15189 | var interceptServerData = function (ref, callback) {
|
15190 | return repoInterceptServerData(ref._delegate._repo, callback);
|
15191 | };
|
15192 |
|
15193 |
|
15194 |
|
15195 |
|
15196 |
|
15197 |
|
15198 |
|
15199 |
|
15200 |
|
15201 | function initStandalone(_a) {
|
15202 | var app = _a.app, url = _a.url, version = _a.version, customAuthImpl = _a.customAuthImpl, namespace = _a.namespace, _b = _a.nodeAdmin, nodeAdmin = _b === void 0 ? false : _b;
|
15203 | setSDKVersion(version);
|
15204 | |
15205 |
|
15206 |
|
15207 |
|
15208 | var authProvider = new component.Provider('auth-internal', new component.ComponentContainer('database-standalone'));
|
15209 | authProvider.setComponent(new component.Component('auth-internal', function () { return customAuthImpl; }, "PRIVATE" ));
|
15210 | return {
|
15211 | instance: new Database(repoManagerDatabaseFromApp(app, authProvider,
|
15212 | undefined, url, nodeAdmin), app),
|
15213 | namespace: namespace
|
15214 | };
|
15215 | }
|
15216 |
|
15217 | var INTERNAL = Object.freeze({
|
15218 | __proto__: null,
|
15219 | forceLongPolling: forceLongPolling,
|
15220 | forceWebSockets: forceWebSockets,
|
15221 | isWebSocketsAvailable: isWebSocketsAvailable,
|
15222 | setSecurityDebugCallback: setSecurityDebugCallback,
|
15223 | stats: stats,
|
15224 | statsIncrementCounter: statsIncrementCounter,
|
15225 | dataUpdateCount: dataUpdateCount,
|
15226 | interceptServerData: interceptServerData,
|
15227 | initStandalone: initStandalone
|
15228 | });
|
15229 |
|
15230 |
|
15231 |
|
15232 |
|
15233 |
|
15234 |
|
15235 |
|
15236 |
|
15237 |
|
15238 |
|
15239 |
|
15240 |
|
15241 |
|
15242 |
|
15243 |
|
15244 |
|
15245 |
|
15246 | var DataConnection = PersistentConnection;
|
15247 |
|
15248 | PersistentConnection.prototype.simpleListen = function (pathString, onComplete) {
|
15249 | this.sendRequest('q', { p: pathString }, onComplete);
|
15250 | };
|
15251 |
|
15252 | PersistentConnection.prototype.echo = function (data, onEcho) {
|
15253 | this.sendRequest('echo', { d: data }, onEcho);
|
15254 | };
|
15255 |
|
15256 | var RealTimeConnection = Connection;
|
15257 | var hijackHash = function (newHash) {
|
15258 | var oldPut = PersistentConnection.prototype.put;
|
15259 | PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
|
15260 | if (hash !== undefined) {
|
15261 | hash = newHash();
|
15262 | }
|
15263 | oldPut.call(this, pathString, data, onComplete, hash);
|
15264 | };
|
15265 | return function () {
|
15266 | PersistentConnection.prototype.put = oldPut;
|
15267 | };
|
15268 | };
|
15269 | var ConnectionTarget = RepoInfo;
|
15270 | var queryIdentifier = function (query) {
|
15271 | return query._delegate._queryIdentifier;
|
15272 | };
|
15273 |
|
15274 |
|
15275 |
|
15276 | var forceRestClient = function (forceRestClient) {
|
15277 | repoManagerForceRestClient(forceRestClient);
|
15278 | };
|
15279 |
|
15280 | var TEST_ACCESS = Object.freeze({
|
15281 | __proto__: null,
|
15282 | DataConnection: DataConnection,
|
15283 | RealTimeConnection: RealTimeConnection,
|
15284 | hijackHash: hijackHash,
|
15285 | ConnectionTarget: ConnectionTarget,
|
15286 | queryIdentifier: queryIdentifier,
|
15287 | forceRestClient: forceRestClient
|
15288 | });
|
15289 |
|
15290 |
|
15291 |
|
15292 |
|
15293 |
|
15294 |
|
15295 |
|
15296 |
|
15297 |
|
15298 |
|
15299 |
|
15300 |
|
15301 |
|
15302 |
|
15303 |
|
15304 |
|
15305 |
|
15306 | setWebSocketImpl(fayeWebsocket.Client);
|
15307 | var ServerValue = Database.ServerValue;
|
15308 |
|
15309 |
|
15310 |
|
15311 |
|
15312 |
|
15313 |
|
15314 |
|
15315 |
|
15316 |
|
15317 | function initStandalone$1(app, url, version, nodeAdmin) {
|
15318 | if (nodeAdmin === void 0) { nodeAdmin = true; }
|
15319 | util.CONSTANTS.NODE_ADMIN = nodeAdmin;
|
15320 | return initStandalone({
|
15321 | app: app,
|
15322 | url: url,
|
15323 | version: version,
|
15324 |
|
15325 |
|
15326 | customAuthImpl: app.INTERNAL,
|
15327 | namespace: {
|
15328 | Reference: Reference,
|
15329 | Query: Query,
|
15330 | Database: Database,
|
15331 | DataSnapshot: DataSnapshot$1,
|
15332 | enableLogging: enableLogging$1,
|
15333 | INTERNAL: INTERNAL,
|
15334 | ServerValue: ServerValue,
|
15335 | TEST_ACCESS: TEST_ACCESS
|
15336 | },
|
15337 | nodeAdmin: nodeAdmin
|
15338 | });
|
15339 | }
|
15340 | function registerDatabase(instance) {
|
15341 |
|
15342 | setSDKVersion(instance.SDK_VERSION);
|
15343 |
|
15344 | var namespace = instance.INTERNAL.registerComponent(new component.Component('database', function (container, _a) {
|
15345 | var url = _a.instanceIdentifier;
|
15346 |
|
15347 |
|
15348 | var app = container.getProvider('app').getImmediate();
|
15349 | var authProvider = container.getProvider('auth-internal');
|
15350 | var appCheckProvider = container.getProvider('app-check-internal');
|
15351 | return new Database(repoManagerDatabaseFromApp(app, authProvider, appCheckProvider, url), app);
|
15352 | }, "PUBLIC" )
|
15353 | .setServiceProps(
|
15354 |
|
15355 | {
|
15356 | Reference: Reference,
|
15357 | Query: Query,
|
15358 | Database: Database,
|
15359 | DataSnapshot: DataSnapshot$1,
|
15360 | enableLogging: enableLogging$1,
|
15361 | INTERNAL: INTERNAL,
|
15362 | ServerValue: ServerValue,
|
15363 | TEST_ACCESS: TEST_ACCESS
|
15364 | })
|
15365 | .setMultipleInstances(true));
|
15366 | instance.registerVersion(name, version, 'node');
|
15367 | if (util.isNodeSdk()) {
|
15368 | module.exports = Object.assign({}, namespace, { initStandalone: initStandalone$1 });
|
15369 | }
|
15370 | }
|
15371 | try {
|
15372 |
|
15373 |
|
15374 |
|
15375 |
|
15376 |
|
15377 |
|
15378 | var firebase = require('@firebase/app').default;
|
15379 | if (firebase) {
|
15380 | registerDatabase(firebase);
|
15381 | }
|
15382 | }
|
15383 | catch (err) {
|
15384 |
|
15385 |
|
15386 | if (err.code !== 'MODULE_NOT_FOUND') {
|
15387 | throw err;
|
15388 | }
|
15389 | }
|
15390 |
|
15391 | exports.DataSnapshot = DataSnapshot$1;
|
15392 | exports.Database = Database;
|
15393 | exports.OnDisconnect = OnDisconnect$1;
|
15394 | exports.Query = Query;
|
15395 | exports.Reference = Reference;
|
15396 | exports.ServerValue = ServerValue;
|
15397 | exports.enableLogging = enableLogging$1;
|
15398 | exports.initStandalone = initStandalone$1;
|
15399 | exports.registerDatabase = registerDatabase;
|
15400 |
|