", t),
n = this.store.put(t)), Rt(n);
},
/**
* Adds a new value into an Object Store and returns the new key. Similar to
* IndexedDb's `add()`, this method will fail on primary key collisions.
*
* @param value - The object to write.
* @returns The key of the value to add.
*/
t.prototype.add = function(t) {
return V("SimpleDb", "ADD", this.store.name, t, t), Rt(this.store.add(t));
},
/**
* Gets the object with the specified key from the specified store, or null
* if no object exists with the specified key.
*
* @key The key of the object to get.
* @returns The object with the specified key or null if no object exists.
*/
t.prototype.get = function(t) {
var e = this;
// We're doing an unsafe cast to ValueType.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Rt(this.store.get(t)).next((function(n) {
// Normalize nonexistence to null.
return void 0 === n && (n = null), V("SimpleDb", "GET", e.store.name, t, n), n;
}));
}, t.prototype.delete = function(t) {
return V("SimpleDb", "DELETE", this.store.name, t), Rt(this.store.delete(t));
},
/**
* If we ever need more of the count variants, we can add overloads. For now,
* all we need is to count everything in a store.
*
* Returns the number of rows in the store.
*/
t.prototype.count = function() {
return V("SimpleDb", "COUNT", this.store.name), Rt(this.store.count());
}, t.prototype.W = function(t, e) {
var n = this.options(t, e);
// Use `getAll()` if the browser supports IndexedDB v3, as it is roughly
// 20% faster. Unfortunately, getAll() does not support custom indices.
if (n.index || "function" != typeof this.store.getAll) {
var r = this.cursor(n), i = [];
return this.H(r, (function(t, e) {
i.push(e);
})).next((function() {
return i;
}));
}
var o = this.store.getAll(n.range);
return new xt((function(t, e) {
o.onerror = function(t) {
e(t.target.error);
}, o.onsuccess = function(e) {
t(e.target.result);
};
}));
},
/**
* Loads the first `count` elements from the provided index range. Loads all
* elements if no limit is provided.
*/
t.prototype.J = function(t, e) {
var n = this.store.getAll(t, null === e ? void 0 : e);
return new xt((function(t, e) {
n.onerror = function(t) {
e(t.target.error);
}, n.onsuccess = function(e) {
t(e.target.result);
};
}));
}, t.prototype.Y = function(t, e) {
V("SimpleDb", "DELETE ALL", this.store.name);
var n = this.options(t, e);
n.X = !1;
var r = this.cursor(n);
return this.H(r, (function(t, e, n) {
return n.delete();
}));
}, t.prototype.Z = function(t, e) {
var n;
e ? n = t : (n = {}, e = t);
var r = this.cursor(n);
return this.H(r, e);
},
/**
* Iterates over a store, but waits for the given callback to complete for
* each entry before iterating the next entry. This allows the callback to do
* asynchronous work to determine if this iteration should continue.
*
* The provided callback should return `true` to continue iteration, and
* `false` otherwise.
*/
t.prototype.tt = function(t) {
var e = this.cursor({});
return new xt((function(n, r) {
e.onerror = function(t) {
var e = Mt(t.target.error);
r(e);
}, e.onsuccess = function(e) {
var r = e.target.result;
r ? t(r.primaryKey, r.value).next((function(t) {
t ? r.continue() : n();
})) : n();
};
}));
}, t.prototype.H = function(t, e) {
var n = [];
return new xt((function(r, i) {
t.onerror = function(t) {
i(t.target.error);
}, t.onsuccess = function(t) {
var i = t.target.result;
if (i) {
var o = new Nt(i), u = e(i.primaryKey, i.value, o);
if (u instanceof xt) {
var a = u.catch((function(t) {
return o.done(), xt.reject(t);
}));
n.push(a);
}
o.isDone ? r() : null === o.G ? i.continue() : i.continue(o.G);
} else r();
};
})).next((function() {
return xt.waitFor(n);
}));
}, t.prototype.options = function(t, e) {
var n;
return void 0 !== t && ("string" == typeof t ? n = t : e = t), {
index: n,
range: e
};
}, t.prototype.cursor = function(t) {
var e = "next";
if (t.reverse && (e = "prev"), t.index) {
var n = this.store.index(t.index);
return t.X ? n.openKeyCursor(t.range, e) : n.openCursor(t.range, e);
}
return this.store.openCursor(t.range, e);
}, t;
}();
/**
* Wraps an IDBRequest in a PersistencePromise, using the onsuccess / onerror
* handlers to resolve / reject the PersistencePromise as appropriate.
*/ function Rt(t) {
return new xt((function(e, n) {
t.onsuccess = function(t) {
var n = t.target.result;
e(n);
}, t.onerror = function(t) {
var e = Mt(t.target.error);
n(e);
};
}));
}
// Guard so we only report the error once.
var Vt = !1;
function Mt(t) {
var e = Ct.D(d());
if (e >= 12.2 && e < 13) {
var n = "An internal error was encountered in the Indexed Database server";
if (t.message.indexOf(n) >= 0) {
// Wrap error in a more descriptive one.
var r = new j("internal", "IOS_INDEXEDDB_BUG1: IndexedDb has thrown '".concat(n, "'. This is likely due to an unavoidable bug in iOS. See https://stackoverflow.com/q/56496296/110915 for details and a potential workaround."));
return Vt || (Vt = !0,
// Throw a global exception outside of this promise chain, for the user to
// potentially catch.
setTimeout((function() {
throw r;
}), 0)), r;
}
}
return t;
}
/** This class is responsible for the scheduling of Index Backfiller. */ var Lt = /** @class */ function() {
function t(t, e) {
this.asyncQueue = t, this.et = e, this.task = null;
}
return t.prototype.start = function() {
this.nt(15e3);
}, t.prototype.stop = function() {
this.task && (this.task.cancel(), this.task = null);
}, Object.defineProperty(t.prototype, "started", {
get: function() {
return null !== this.task;
},
enumerable: !1,
configurable: !0
}), t.prototype.nt = function(t) {
var r = this;
V("IndexBackiller", "Scheduled in ".concat(t, "ms")), this.task = this.asyncQueue.enqueueAfterDelay("index_backfill" /* TimerId.IndexBackfill */ , t, (function() {
return e(r, void 0, void 0, (function() {
var t, e, r, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
this.task = null, n.label = 1;
case 1:
return n.trys.push([ 1, 3, , 7 ]), t = V, e = [ "IndexBackiller" ], r = "Documents written: ".concat,
[ 4 /*yield*/ , this.et.st() ];
case 2:
return t.apply(void 0, e.concat([ r.apply("Documents written: ", [ n.sent() ]) ])),
[ 3 /*break*/ , 7 ];
case 3:
return Ft(i = n.sent()) ? (V("IndexBackiller", "Ignoring IndexedDB error during index backfill: ", i),
[ 3 /*break*/ , 6 ]) : [ 3 /*break*/ , 4 ];
case 4:
return [ 4 /*yield*/ , Dt(i) ];
case 5:
n.sent(), n.label = 6;
case 6:
return [ 3 /*break*/ , 7 ];
case 7:
return [ 4 /*yield*/ , this.nt(6e4) ];
case 8:
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}));
}, t;
}(), Pt = /** @class */ function() {
function t(
/**
* LocalStore provides access to IndexManager and LocalDocumentView.
* These properties will update when the user changes. Consequently,
* making a local copy of IndexManager and LocalDocumentView will require
* updates over time. The simpler solution is to rely on LocalStore to have
* an up-to-date references to IndexManager and LocalDocumentStore.
*/
t, e) {
this.localStore = t, this.persistence = e;
}
return t.prototype.st = function(t) {
return void 0 === t && (t = 50), e(this, void 0, void 0, (function() {
var e = this;
return n(this, (function(n) {
return [ 2 /*return*/ , this.persistence.runTransaction("Backfill Indexes", "readwrite-primary", (function(n) {
return e.it(n, t);
})) ];
}));
}));
},
/** Writes index entries until the cap is reached. Returns the number of documents processed. */ t.prototype.it = function(t, e) {
var n = this, r = new Set, i = e, o = !0;
return xt.doWhile((function() {
return !0 === o && i > 0;
}), (function() {
return n.localStore.indexManager.getNextCollectionGroupToUpdate(t).next((function(e) {
if (null !== e && !r.has(e)) return V("IndexBackiller", "Processing collection: ".concat(e)),
n.rt(t, e, i).next((function(t) {
i -= t, r.add(e);
}));
o = !1;
}));
})).next((function() {
return e - i;
}));
},
/**
* Writes entries for the provided collection group. Returns the number of documents processed.
*/
t.prototype.rt = function(t, e, n) {
var r = this;
// Use the earliest offset of all field indexes to query the local cache.
return this.localStore.indexManager.getMinOffsetFromCollectionGroup(t, e).next((function(i) {
return r.localStore.localDocuments.getNextDocuments(t, e, i, n).next((function(n) {
var o = n.changes;
return r.localStore.indexManager.updateIndexEntries(t, o).next((function() {
return r.ot(i, n);
})).next((function(n) {
return V("IndexBackiller", "Updating offset: ".concat(n)), r.localStore.indexManager.updateCollectionGroup(t, e, n);
})).next((function() {
return o.size;
}));
}));
}));
},
/** Returns the next offset based on the provided documents. */ t.prototype.ot = function(t, e) {
var n = t;
return e.changes.forEach((function(t, e) {
var r = It(e);
Et(r, n) > 0 && (n = r);
})), new Tt(n.readTime, n.documentKey, Math.max(e.batchId, t.largestBatchId));
}, t;
}(), qt = /** @class */ function() {
function t(t, e) {
var n = this;
this.previousValue = t, e && (e.sequenceNumberHandler = function(t) {
return n.ut(t);
}, this.ct = function(t) {
return e.writeSequenceNumber(t);
});
}
return t.prototype.ut = function(t) {
return this.previousValue = Math.max(t, this.previousValue), this.previousValue;
}, t.prototype.next = function() {
var t = ++this.previousValue;
return this.ct && this.ct(t), t;
}, t;
}();
/** Implements the steps for backfilling indexes. */ qt.at = -1;
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Ut =
/**
* Constructs a DatabaseInfo using the provided host, databaseId and
* persistenceKey.
*
* @param databaseId - The database to use.
* @param appId - The Firebase App Id.
* @param persistenceKey - A unique identifier for this Firestore's local
* storage (used in conjunction with the databaseId).
* @param host - The Firestore backend host to connect to.
* @param ssl - Whether to use SSL when connecting.
* @param forceLongPolling - Whether to use the forceLongPolling option
* when using WebChannel as the network transport.
* @param autoDetectLongPolling - Whether to use the detectBufferingProxy
* option when using WebChannel as the network transport.
* @param useFetchStreams Whether to use the Fetch API instead of
* XMLHTTPRequest
*/
function(t, e, n, r, i, o, u, a) {
this.databaseId = t, this.appId = e, this.persistenceKey = n, this.host = r, this.ssl = i,
this.forceLongPolling = o, this.autoDetectLongPolling = u, this.useFetchStreams = a;
}, Bt = /** @class */ function() {
function t(t, e) {
this.projectId = t, this.database = e || "(default)";
}
return t.empty = function() {
return new t("", "");
}, Object.defineProperty(t.prototype, "isDefaultDatabase", {
get: function() {
return "(default)" === this.database;
},
enumerable: !1,
configurable: !0
}), t.prototype.isEqual = function(e) {
return e instanceof t && e.projectId === this.projectId && e.database === this.database;
}, t;
}();
/** The default database name for a project. */
/**
* Represents the database ID a Firestore client is associated with.
* @internal
*/
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function Gt(t) {
var e = 0;
for (var n in t) Object.prototype.hasOwnProperty.call(t, n) && e++;
return e;
}
function Kt(t, e) {
for (var n in t) Object.prototype.hasOwnProperty.call(t, n) && e(n, t[n]);
}
function jt(t) {
for (var e in t) if (Object.prototype.hasOwnProperty.call(t, e)) return !1;
return !0;
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Sentinel value that sorts before any Mutation Batch ID. */
/**
* Returns whether a variable is either undefined or null.
*/ function Qt(t) {
return null == t;
}
/** Returns whether the value represents -0. */ function zt(t) {
// Detect if the value is -0.0. Based on polyfill from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
return 0 === t && 1 / t == -1 / 0;
}
/**
* Returns whether a value is an integer and in the safe integer range
* @param value - The value to test for being an integer and in the safe range
*/ function Wt(t) {
return "number" == typeof t && Number.isInteger(t) && !zt(t) && t <= Number.MAX_SAFE_INTEGER && t >= Number.MIN_SAFE_INTEGER;
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Converts a Base64 encoded string to a binary string. */
/** True if and only if the Base64 conversion functions are available. */ function Ht() {
return "undefined" != typeof atob;
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Immutable class that represents a "proto" byte string.
*
* Proto byte strings can either be Base64-encoded strings or Uint8Arrays when
* sent on the wire. This class abstracts away this differentiation by holding
* the proto byte string in a common class that must be converted into a string
* before being sent as a proto.
* @internal
*/ var Yt = /** @class */ function() {
function t(t) {
this.binaryString = t;
}
return t.fromBase64String = function(e) {
return new t(atob(e));
}, t.fromUint8Array = function(e) {
// TODO(indexing); Remove the copy of the byte string here as this method
// is frequently called during indexing.
var n =
/**
* Helper function to convert an Uint8array to a binary string.
*/
function(t) {
for (var e = "", n = 0; n < t.length; ++n) e += String.fromCharCode(t[n]);
return e;
}(e);
return new t(n);
}, t.prototype[Symbol.iterator] = function() {
var t = this, e = 0;
return {
next: function() {
return e < t.binaryString.length ? {
value: t.binaryString.charCodeAt(e++),
done: !1
} : {
value: void 0,
done: !0
};
}
};
}, t.prototype.toBase64 = function() {
return t = this.binaryString, btoa(t);
/** Converts a binary string to a Base64 encoded string. */ var t;
}, t.prototype.toUint8Array = function() {
return function(t) {
for (var e = new Uint8Array(t.length), n = 0; n < t.length; n++) e[n] = t.charCodeAt(n);
return e;
}(this.binaryString);
}, t.prototype.approximateByteSize = function() {
return 2 * this.binaryString.length;
}, t.prototype.compareTo = function(t) {
return rt(this.binaryString, t.binaryString);
}, t.prototype.isEqual = function(t) {
return this.binaryString === t.binaryString;
}, t;
}();
Yt.EMPTY_BYTE_STRING = new Yt("");
var Xt = new RegExp(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.(\d+))?Z$/);
/**
* Converts the possible Proto values for a timestamp value into a "seconds and
* nanos" representation.
*/ function Zt(t) {
// The json interface (for the browser) will return an iso timestamp string,
// while the proto js library (for node) will return a
// google.protobuf.Timestamp instance.
if (U(!!t), "string" == typeof t) {
// The date string can have higher precision (nanos) than the Date class
// (millis), so we do some custom parsing here.
// Parse the nanos right out of the string.
var e = 0, n = Xt.exec(t);
if (U(!!n), n[1]) {
// Pad the fraction out to 9 digits (nanos).
var r = n[1];
r = (r + "000000000").substr(0, 9), e = Number(r);
}
// Parse the date to get the seconds.
var i = new Date(t);
return {
seconds: Math.floor(i.getTime() / 1e3),
nanos: e
};
}
return {
seconds: Jt(t.seconds),
nanos: Jt(t.nanos)
};
}
/**
* Converts the possible Proto types for numbers into a JavaScript number.
* Returns 0 if the value is not numeric.
*/ function Jt(t) {
// TODO(bjornick): Handle int64 greater than 53 bits.
return "number" == typeof t ? t : "string" == typeof t ? Number(t) : 0;
}
/** Converts the possible Proto types for Blobs into a ByteString. */ function $t(t) {
return "string" == typeof t ? Yt.fromBase64String(t) : Yt.fromUint8Array(t);
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents a locally-applied ServerTimestamp.
*
* Server Timestamps are backed by MapValues that contain an internal field
* `__type__` with a value of `server_timestamp`. The previous value and local
* write time are stored in its `__previous_value__` and `__local_write_time__`
* fields respectively.
*
* Notes:
* - ServerTimestampValue instances are created as the result of applying a
* transform. They can only exist in the local view of a document. Therefore
* they do not need to be parsed or serialized.
* - When evaluated locally (e.g. for snapshot.data()), they by default
* evaluate to `null`. This behavior can be configured by passing custom
* FieldValueOptions to value().
* - With respect to other ServerTimestampValues, they sort by their
* localWriteTime.
*/ function te(t) {
var e, n;
return "server_timestamp" === (null === (n = ((null === (e = null == t ? void 0 : t.mapValue) || void 0 === e ? void 0 : e.fields) || {}).__type__) || void 0 === n ? void 0 : n.stringValue);
}
/**
* Creates a new ServerTimestamp proto value (using the internal format).
*/
/**
* Returns the value of the field before this ServerTimestamp was set.
*
* Preserving the previous values allows the user to display the last resoled
* value until the backend responds with the timestamp.
*/ function ee(t) {
var e = t.mapValue.fields.__previous_value__;
return te(e) ? ee(e) : e;
}
/**
* Returns the local time at which this timestamp was first set.
*/ function ne(t) {
var e = Zt(t.mapValue.fields.__local_write_time__.timestampValue);
return new ut(e.seconds, e.nanos);
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var re = {
mapValue: {
fields: {
__type__: {
stringValue: "__max__"
}
}
}
}, ie = {
nullValue: "NULL_VALUE"
};
/** Extracts the backend's type order for the provided value. */ function oe(t) {
return "nullValue" in t ? 0 /* TypeOrder.NullValue */ : "booleanValue" in t ? 1 /* TypeOrder.BooleanValue */ : "integerValue" in t || "doubleValue" in t ? 2 /* TypeOrder.NumberValue */ : "timestampValue" in t ? 3 /* TypeOrder.TimestampValue */ : "stringValue" in t ? 5 /* TypeOrder.StringValue */ : "bytesValue" in t ? 6 /* TypeOrder.BlobValue */ : "referenceValue" in t ? 7 /* TypeOrder.RefValue */ : "geoPointValue" in t ? 8 /* TypeOrder.GeoPointValue */ : "arrayValue" in t ? 9 /* TypeOrder.ArrayValue */ : "mapValue" in t ? te(t) ? 4 /* TypeOrder.ServerTimestampValue */ : we(t) ? 9007199254740991 /* TypeOrder.MaxValue */ : 10 /* TypeOrder.ObjectValue */ : q();
}
/** Tests `left` and `right` for equality based on the backend semantics. */ function ue(t, e) {
if (t === e) return !0;
var n = oe(t);
if (n !== oe(e)) return !1;
switch (n) {
case 0 /* TypeOrder.NullValue */ :
case 9007199254740991 /* TypeOrder.MaxValue */ :
return !0;
case 1 /* TypeOrder.BooleanValue */ :
return t.booleanValue === e.booleanValue;
case 4 /* TypeOrder.ServerTimestampValue */ :
return ne(t).isEqual(ne(e));
case 3 /* TypeOrder.TimestampValue */ :
return function(t, e) {
if ("string" == typeof t.timestampValue && "string" == typeof e.timestampValue && t.timestampValue.length === e.timestampValue.length)
// Use string equality for ISO 8601 timestamps
return t.timestampValue === e.timestampValue;
var n = Zt(t.timestampValue), r = Zt(e.timestampValue);
return n.seconds === r.seconds && n.nanos === r.nanos;
}(t, e);
case 5 /* TypeOrder.StringValue */ :
return t.stringValue === e.stringValue;
case 6 /* TypeOrder.BlobValue */ :
return function(t, e) {
return $t(t.bytesValue).isEqual($t(e.bytesValue));
}(t, e);
case 7 /* TypeOrder.RefValue */ :
return t.referenceValue === e.referenceValue;
case 8 /* TypeOrder.GeoPointValue */ :
return function(t, e) {
return Jt(t.geoPointValue.latitude) === Jt(e.geoPointValue.latitude) && Jt(t.geoPointValue.longitude) === Jt(e.geoPointValue.longitude);
}(t, e);
case 2 /* TypeOrder.NumberValue */ :
return function(t, e) {
if ("integerValue" in t && "integerValue" in e) return Jt(t.integerValue) === Jt(e.integerValue);
if ("doubleValue" in t && "doubleValue" in e) {
var n = Jt(t.doubleValue), r = Jt(e.doubleValue);
return n === r ? zt(n) === zt(r) : isNaN(n) && isNaN(r);
}
return !1;
}(t, e);
case 9 /* TypeOrder.ArrayValue */ :
return it(t.arrayValue.values || [], e.arrayValue.values || [], ue);
case 10 /* TypeOrder.ObjectValue */ :
return function(t, e) {
var n = t.mapValue.fields || {}, r = e.mapValue.fields || {};
if (Gt(n) !== Gt(r)) return !1;
for (var i in n) if (n.hasOwnProperty(i) && (void 0 === r[i] || !ue(n[i], r[i]))) return !1;
return !0;
}(t, e);
default:
return q();
}
}
function ae(t, e) {
return void 0 !== (t.values || []).find((function(t) {
return ue(t, e);
}));
}
function se(t, e) {
if (t === e) return 0;
var n = oe(t), r = oe(e);
if (n !== r) return rt(n, r);
switch (n) {
case 0 /* TypeOrder.NullValue */ :
case 9007199254740991 /* TypeOrder.MaxValue */ :
return 0;
case 1 /* TypeOrder.BooleanValue */ :
return rt(t.booleanValue, e.booleanValue);
case 2 /* TypeOrder.NumberValue */ :
return function(t, e) {
var n = Jt(t.integerValue || t.doubleValue), r = Jt(e.integerValue || e.doubleValue);
return n < r ? -1 : n > r ? 1 : n === r ? 0 :
// one or both are NaN.
isNaN(n) ? isNaN(r) ? 0 : -1 : 1;
}(t, e);
case 3 /* TypeOrder.TimestampValue */ :
return ce(t.timestampValue, e.timestampValue);
case 4 /* TypeOrder.ServerTimestampValue */ :
return ce(ne(t), ne(e));
case 5 /* TypeOrder.StringValue */ :
return rt(t.stringValue, e.stringValue);
case 6 /* TypeOrder.BlobValue */ :
return function(t, e) {
var n = $t(t), r = $t(e);
return n.compareTo(r);
}(t.bytesValue, e.bytesValue);
case 7 /* TypeOrder.RefValue */ :
return function(t, e) {
for (var n = t.split("/"), r = e.split("/"), i = 0; i < n.length && i < r.length; i++) {
var o = rt(n[i], r[i]);
if (0 !== o) return o;
}
return rt(n.length, r.length);
}(t.referenceValue, e.referenceValue);
case 8 /* TypeOrder.GeoPointValue */ :
return function(t, e) {
var n = rt(Jt(t.latitude), Jt(e.latitude));
return 0 !== n ? n : rt(Jt(t.longitude), Jt(e.longitude));
}(t.geoPointValue, e.geoPointValue);
case 9 /* TypeOrder.ArrayValue */ :
return function(t, e) {
for (var n = t.values || [], r = e.values || [], i = 0; i < n.length && i < r.length; ++i) {
var o = se(n[i], r[i]);
if (o) return o;
}
return rt(n.length, r.length);
}(t.arrayValue, e.arrayValue);
case 10 /* TypeOrder.ObjectValue */ :
return function(t, e) {
if (t === re.mapValue && e === re.mapValue) return 0;
if (t === re.mapValue) return 1;
if (e === re.mapValue) return -1;
var n = t.fields || {}, r = Object.keys(n), i = e.fields || {}, o = Object.keys(i);
// Even though MapValues are likely sorted correctly based on their insertion
// order (e.g. when received from the backend), local modifications can bring
// elements out of order. We need to re-sort the elements to ensure that
// canonical IDs are independent of insertion order.
r.sort(), o.sort();
for (var u = 0; u < r.length && u < o.length; ++u) {
var a = rt(r[u], o[u]);
if (0 !== a) return a;
var s = se(n[r[u]], i[o[u]]);
if (0 !== s) return s;
}
return rt(r.length, o.length);
}(t.mapValue, e.mapValue);
default:
throw q();
}
}
function ce(t, e) {
if ("string" == typeof t && "string" == typeof e && t.length === e.length) return rt(t, e);
var n = Zt(t), r = Zt(e), i = rt(n.seconds, r.seconds);
return 0 !== i ? i : rt(n.nanos, r.nanos);
}
function le(t) {
return he(t);
}
function he(t) {
return "nullValue" in t ? "null" : "booleanValue" in t ? "" + t.booleanValue : "integerValue" in t ? "" + t.integerValue : "doubleValue" in t ? "" + t.doubleValue : "timestampValue" in t ? function(t) {
var e = Zt(t);
return "time(".concat(e.seconds, ",").concat(e.nanos, ")");
}(t.timestampValue) : "stringValue" in t ? t.stringValue : "bytesValue" in t ? $t(t.bytesValue).toBase64() : "referenceValue" in t ? (n = t.referenceValue,
ft.fromName(n).toString()) : "geoPointValue" in t ? "geo(".concat((e = t.geoPointValue).latitude, ",").concat(e.longitude, ")") : "arrayValue" in t ? function(t) {
for (var e = "[", n = !0, r = 0, i = t.values || []; r < i.length; r++) {
n ? n = !1 : e += ",", e += he(i[r]);
}
return e + "]";
}(t.arrayValue) : "mapValue" in t ? function(t) {
for (
// Iteration order in JavaScript is not guaranteed. To ensure that we generate
// matching canonical IDs for identical maps, we need to sort the keys.
var e = "{", n = !0, r = 0, i = Object.keys(t.fields || {}).sort(); r < i.length; r++) {
var o = i[r];
n ? n = !1 : e += ",", e += "".concat(o, ":").concat(he(t.fields[o]));
}
return e + "}";
}(t.mapValue) : q();
var e, n;
}
function fe(t, e) {
return {
referenceValue: "projects/".concat(t.projectId, "/databases/").concat(t.database, "/documents/").concat(e.path.canonicalString())
};
}
/** Returns true if `value` is an IntegerValue . */ function de(t) {
return !!t && "integerValue" in t;
}
/** Returns true if `value` is a DoubleValue. */
/** Returns true if `value` is an ArrayValue. */ function pe(t) {
return !!t && "arrayValue" in t;
}
/** Returns true if `value` is a NullValue. */ function ye(t) {
return !!t && "nullValue" in t;
}
/** Returns true if `value` is NaN. */ function ve(t) {
return !!t && "doubleValue" in t && isNaN(Number(t.doubleValue));
}
/** Returns true if `value` is a MapValue. */ function me(t) {
return !!t && "mapValue" in t;
}
/** Creates a deep copy of `source`. */ function ge(t) {
if (t.geoPointValue) return {
geoPointValue: Object.assign({}, t.geoPointValue)
};
if (t.timestampValue && "object" == typeof t.timestampValue) return {
timestampValue: Object.assign({}, t.timestampValue)
};
if (t.mapValue) {
var e = {
mapValue: {
fields: {}
}
};
return Kt(t.mapValue.fields, (function(t, n) {
return e.mapValue.fields[t] = ge(n);
})), e;
}
if (t.arrayValue) {
for (var n = {
arrayValue: {
values: []
}
}, r = 0; r < (t.arrayValue.values || []).length; ++r) n.arrayValue.values[r] = ge(t.arrayValue.values[r]);
return n;
}
return Object.assign({}, t);
}
/** Returns true if the Value represents the canonical {@link #MAX_VALUE} . */ function we(t) {
return "__max__" === (((t.mapValue || {}).fields || {}).__type__ || {}).stringValue;
}
/** Returns the lowest value for the given value type (inclusive). */ function be(t) {
return "nullValue" in t ? ie : "booleanValue" in t ? {
booleanValue: !1
} : "integerValue" in t || "doubleValue" in t ? {
doubleValue: NaN
} : "timestampValue" in t ? {
timestampValue: {
seconds: Number.MIN_SAFE_INTEGER
}
} : "stringValue" in t ? {
stringValue: ""
} : "bytesValue" in t ? {
bytesValue: ""
} : "referenceValue" in t ? fe(Bt.empty(), ft.empty()) : "geoPointValue" in t ? {
geoPointValue: {
latitude: -90,
longitude: -180
}
} : "arrayValue" in t ? {
arrayValue: {}
} : "mapValue" in t ? {
mapValue: {}
} : q();
}
/** Returns the largest value for the given value type (exclusive). */ function Ie(t) {
return "nullValue" in t ? {
booleanValue: !1
} : "booleanValue" in t ? {
doubleValue: NaN
} : "integerValue" in t || "doubleValue" in t ? {
timestampValue: {
seconds: Number.MIN_SAFE_INTEGER
}
} : "timestampValue" in t ? {
stringValue: ""
} : "stringValue" in t ? {
bytesValue: ""
} : "bytesValue" in t ? fe(Bt.empty(), ft.empty()) : "referenceValue" in t ? {
geoPointValue: {
latitude: -90,
longitude: -180
}
} : "geoPointValue" in t ? {
arrayValue: {}
} : "arrayValue" in t ? {
mapValue: {}
} : "mapValue" in t ? re : q();
}
function Te(t, e) {
var n = se(t.value, e.value);
return 0 !== n ? n : t.inclusive && !e.inclusive ? -1 : !t.inclusive && e.inclusive ? 1 : 0;
}
function Ee(t, e) {
var n = se(t.value, e.value);
return 0 !== n ? n : t.inclusive && !e.inclusive ? 1 : !t.inclusive && e.inclusive ? -1 : 0;
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents a bound of a query.
*
* The bound is specified with the given components representing a position and
* whether it's just before or just after the position (relative to whatever the
* query order is).
*
* The position represents a logical index position for a query. It's a prefix
* of values for the (potentially implicit) order by clauses of a query.
*
* Bound provides a function to determine whether a document comes before or
* after a bound. This is influenced by whether the position is just before or
* just after the provided values.
*/ var Se = function(t, e) {
this.position = t, this.inclusive = e;
};
function _e(t, e, n) {
for (var r = 0, i = 0; i < t.position.length; i++) {
var o = e[i], u = t.position[i];
if (r = o.field.isKeyField() ? ft.comparator(ft.fromName(u.referenceValue), n.key) : se(u, n.data.field(o.field)),
"desc" /* Direction.DESCENDING */ === o.dir && (r *= -1), 0 !== r) break;
}
return r;
}
/**
* Returns true if a document sorts after a bound using the provided sort
* order.
*/ function De(t, e) {
if (null === t) return null === e;
if (null === e) return !1;
if (t.inclusive !== e.inclusive || t.position.length !== e.position.length) return !1;
for (var n = 0; n < t.position.length; n++) if (!ue(t.position[n], e.position[n])) return !1;
return !0;
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var xe = function() {}, Ae = /** @class */ function(e) {
function n(t, n, r) {
var i = this;
return (i = e.call(this) || this).field = t, i.op = n, i.value = r, i;
}
/**
* Creates a filter based on the provided arguments.
*/ return t(n, e), n.create = function(t, e, r) {
return t.isKeyField() ? "in" /* Operator.IN */ === e || "not-in" /* Operator.NOT_IN */ === e ? this.createKeyFieldInFilter(t, e, r) : new Pe(t, e, r) : "array-contains" /* Operator.ARRAY_CONTAINS */ === e ? new Ge(t, r) : "in" /* Operator.IN */ === e ? new Ke(t, r) : "not-in" /* Operator.NOT_IN */ === e ? new je(t, r) : "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ === e ? new Qe(t, r) : new n(t, e, r);
}, n.createKeyFieldInFilter = function(t, e, n) {
return "in" /* Operator.IN */ === e ? new qe(t, n) : new Ue(t, n);
}, n.prototype.matches = function(t) {
var e = t.data.field(this.field);
// Types do not have to match in NOT_EQUAL filters.
return "!=" /* Operator.NOT_EQUAL */ === this.op ? null !== e && this.matchesComparison(se(e, this.value)) : null !== e && oe(this.value) === oe(e) && this.matchesComparison(se(e, this.value));
// Only compare types with matching backend order (such as double and int).
}, n.prototype.matchesComparison = function(t) {
switch (this.op) {
case "<" /* Operator.LESS_THAN */ :
return t < 0;
case "<=" /* Operator.LESS_THAN_OR_EQUAL */ :
return t <= 0;
case "==" /* Operator.EQUAL */ :
return 0 === t;
case "!=" /* Operator.NOT_EQUAL */ :
return 0 !== t;
case ">" /* Operator.GREATER_THAN */ :
return t > 0;
case ">=" /* Operator.GREATER_THAN_OR_EQUAL */ :
return t >= 0;
default:
return q();
}
}, n.prototype.isInequality = function() {
return [ "<" /* Operator.LESS_THAN */ , "<=" /* Operator.LESS_THAN_OR_EQUAL */ , ">" /* Operator.GREATER_THAN */ , ">=" /* Operator.GREATER_THAN_OR_EQUAL */ , "!=" /* Operator.NOT_EQUAL */ , "not-in" /* Operator.NOT_IN */ ].indexOf(this.op) >= 0;
}, n.prototype.getFlattenedFilters = function() {
return [ this ];
}, n.prototype.getFilters = function() {
return [ this ];
}, n.prototype.getFirstInequalityField = function() {
return this.isInequality() ? this.field : null;
}, n;
}(xe), Ce = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this) || this).filters = t, r.op = n, r.ht = null, r;
}
/**
* Creates a filter based on the provided arguments.
*/ return t(n, e), n.create = function(t, e) {
return new n(t, e);
}, n.prototype.matches = function(t) {
return Ne(this) ? void 0 === this.filters.find((function(e) {
return !e.matches(t);
})) : void 0 !== this.filters.find((function(e) {
return e.matches(t);
}));
}, n.prototype.getFlattenedFilters = function() {
return null !== this.ht || (this.ht = this.filters.reduce((function(t, e) {
return t.concat(e.getFlattenedFilters());
}), [])), this.ht;
},
// Returns a mutable copy of `this.filters`
n.prototype.getFilters = function() {
return Object.assign([], this.filters);
}, n.prototype.getFirstInequalityField = function() {
var t = this.lt((function(t) {
return t.isInequality();
}));
return null !== t ? t.field : null;
},
// Performs a depth-first search to find and return the first FieldFilter in the composite filter
// that satisfies the predicate. Returns `null` if none of the FieldFilters satisfy the
// predicate.
n.prototype.lt = function(t) {
for (var e = 0, n = this.getFlattenedFilters(); e < n.length; e++) {
var r = n[e];
if (t(r)) return r;
}
return null;
}, n;
}(xe);
function Ne(t) {
return "and" /* CompositeOperator.AND */ === t.op;
}
function ke(t) {
return "or" /* CompositeOperator.OR */ === t.op;
}
/**
* Returns true if this filter is a conjunction of field filters only. Returns false otherwise.
*/ function Fe(t) {
return Oe(t) && Ne(t);
}
/**
* Returns true if this filter does not contain any composite filters. Returns false otherwise.
*/ function Oe(t) {
for (var e = 0, n = t.filters; e < n.length; e++) {
if (n[e] instanceof Ce) return !1;
}
return !0;
}
function Re(t) {
if (t instanceof Ae)
// TODO(b/29183165): Technically, this won't be unique if two values have
// the same description, such as the int 3 and the string "3". So we should
// add the types in here somehow, too.
return t.field.canonicalString() + t.op.toString() + le(t.value);
if (Fe(t))
// Older SDK versions use an implicit AND operation between their filters.
// In the new SDK versions, the developer may use an explicit AND filter.
// To stay consistent with the old usages, we add a special case to ensure
// the canonical ID for these two are the same. For example:
// `col.whereEquals("a", 1).whereEquals("b", 2)` should have the same
// canonical ID as `col.where(and(equals("a",1), equals("b",2)))`.
return t.filters.map((function(t) {
return Re(t);
})).join(",");
// filter instanceof CompositeFilter
var e = t.filters.map((function(t) {
return Re(t);
})).join(",");
return "".concat(t.op, "(").concat(e, ")");
}
function Ve(t, e) {
return t instanceof Ae ? function(t, e) {
return e instanceof Ae && t.op === e.op && t.field.isEqual(e.field) && ue(t.value, e.value);
}(t, e) : t instanceof Ce ? function(t, e) {
return e instanceof Ce && t.op === e.op && t.filters.length === e.filters.length && t.filters.reduce((function(t, n, r) {
return t && Ve(n, e.filters[r]);
}), !0);
}(t, e) : void q();
}
function Me(t, e) {
var n = t.filters.concat(e);
return Ce.create(n, t.op);
}
/** Returns a debug description for `filter`. */ function Le(t) {
return t instanceof Ae ? function(t) {
return "".concat(t.field.canonicalString(), " ").concat(t.op, " ").concat(le(t.value));
}(t) : t instanceof Ce ? function(t) {
return t.op.toString() + " {" + t.getFilters().map(Le).join(" ,") + "}";
}(t) : "Filter";
}
var Pe = /** @class */ function(e) {
function n(t, n, r) {
var i = this;
return (i = e.call(this, t, n, r) || this).key = ft.fromName(r.referenceValue),
i;
}
return t(n, e), n.prototype.matches = function(t) {
var e = ft.comparator(t.key, this.key);
return this.matchesComparison(e);
}, n;
}(Ae), qe = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this, t, "in" /* Operator.IN */ , n) || this).keys = Be("in" /* Operator.IN */ , n),
r;
}
return t(n, e), n.prototype.matches = function(t) {
return this.keys.some((function(e) {
return e.isEqual(t.key);
}));
}, n;
}(Ae), Ue = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this, t, "not-in" /* Operator.NOT_IN */ , n) || this).keys = Be("not-in" /* Operator.NOT_IN */ , n),
r;
}
return t(n, e), n.prototype.matches = function(t) {
return !this.keys.some((function(e) {
return e.isEqual(t.key);
}));
}, n;
}(Ae);
/** Filter that matches on key fields within an array. */ function Be(t, e) {
var n;
return ((null === (n = e.arrayValue) || void 0 === n ? void 0 : n.values) || []).map((function(t) {
return ft.fromName(t.referenceValue);
}));
}
/** A Filter that implements the array-contains operator. */ var Ge = /** @class */ function(e) {
function n(t, n) {
return e.call(this, t, "array-contains" /* Operator.ARRAY_CONTAINS */ , n) || this;
}
return t(n, e), n.prototype.matches = function(t) {
var e = t.data.field(this.field);
return pe(e) && ae(e.arrayValue, this.value);
}, n;
}(Ae), Ke = /** @class */ function(e) {
function n(t, n) {
return e.call(this, t, "in" /* Operator.IN */ , n) || this;
}
return t(n, e), n.prototype.matches = function(t) {
var e = t.data.field(this.field);
return null !== e && ae(this.value.arrayValue, e);
}, n;
}(Ae), je = /** @class */ function(e) {
function n(t, n) {
return e.call(this, t, "not-in" /* Operator.NOT_IN */ , n) || this;
}
return t(n, e), n.prototype.matches = function(t) {
if (ae(this.value.arrayValue, {
nullValue: "NULL_VALUE"
})) return !1;
var e = t.data.field(this.field);
return null !== e && !ae(this.value.arrayValue, e);
}, n;
}(Ae), Qe = /** @class */ function(e) {
function n(t, n) {
return e.call(this, t, "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ , n) || this;
}
return t(n, e), n.prototype.matches = function(t) {
var e = this, n = t.data.field(this.field);
return !(!pe(n) || !n.arrayValue.values) && n.arrayValue.values.some((function(t) {
return ae(e.value.arrayValue, t);
}));
}, n;
}(Ae), ze = function(t, e /* Direction.ASCENDING */) {
void 0 === e && (e = "asc"), this.field = t, this.dir = e;
};
/** A Filter that implements the IN operator. */ function We(t, e) {
return t.dir === e.dir && t.field.isEqual(e.field);
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// An immutable sorted map implementation, based on a Left-leaning Red-Black
// tree.
var He = /** @class */ function() {
function t(t, e) {
this.comparator = t, this.root = e || Xe.EMPTY;
}
// Returns a copy of the map, with the specified key/value added or replaced.
return t.prototype.insert = function(e, n) {
return new t(this.comparator, this.root.insert(e, n, this.comparator).copy(null, null, Xe.BLACK, null, null));
},
// Returns a copy of the map, with the specified key removed.
t.prototype.remove = function(e) {
return new t(this.comparator, this.root.remove(e, this.comparator).copy(null, null, Xe.BLACK, null, null));
},
// Returns the value of the node with the given key, or null.
t.prototype.get = function(t) {
for (var e = this.root; !e.isEmpty(); ) {
var n = this.comparator(t, e.key);
if (0 === n) return e.value;
n < 0 ? e = e.left : n > 0 && (e = e.right);
}
return null;
},
// Returns the index of the element in this sorted map, or -1 if it doesn't
// exist.
t.prototype.indexOf = function(t) {
for (
// Number of nodes that were pruned when descending right
var e = 0, n = this.root; !n.isEmpty(); ) {
var r = this.comparator(t, n.key);
if (0 === r) return e + n.left.size;
r < 0 ? n = n.left : (
// Count all nodes left of the node plus the node itself
e += n.left.size + 1, n = n.right);
}
// Node not found
return -1;
}, t.prototype.isEmpty = function() {
return this.root.isEmpty();
}, Object.defineProperty(t.prototype, "size", {
// Returns the total number of nodes in the map.
get: function() {
return this.root.size;
},
enumerable: !1,
configurable: !0
}),
// Returns the minimum key in the map.
t.prototype.minKey = function() {
return this.root.minKey();
},
// Returns the maximum key in the map.
t.prototype.maxKey = function() {
return this.root.maxKey();
},
// Traverses the map in key order and calls the specified action function
// for each key/value pair. If action returns true, traversal is aborted.
// Returns the first truthy value returned by action, or the last falsey
// value returned by action.
t.prototype.inorderTraversal = function(t) {
return this.root.inorderTraversal(t);
}, t.prototype.forEach = function(t) {
this.inorderTraversal((function(e, n) {
return t(e, n), !1;
}));
}, t.prototype.toString = function() {
var t = [];
return this.inorderTraversal((function(e, n) {
return t.push("".concat(e, ":").concat(n)), !1;
})), "{".concat(t.join(", "), "}");
},
// Traverses the map in reverse key order and calls the specified action
// function for each key/value pair. If action returns true, traversal is
// aborted.
// Returns the first truthy value returned by action, or the last falsey
// value returned by action.
t.prototype.reverseTraversal = function(t) {
return this.root.reverseTraversal(t);
},
// Returns an iterator over the SortedMap.
t.prototype.getIterator = function() {
return new Ye(this.root, null, this.comparator, !1);
}, t.prototype.getIteratorFrom = function(t) {
return new Ye(this.root, t, this.comparator, !1);
}, t.prototype.getReverseIterator = function() {
return new Ye(this.root, null, this.comparator, !0);
}, t.prototype.getReverseIteratorFrom = function(t) {
return new Ye(this.root, t, this.comparator, !0);
}, t;
}(), Ye = /** @class */ function() {
function t(t, e, n, r) {
this.isReverse = r, this.nodeStack = [];
for (var i = 1; !t.isEmpty(); ) if (i = e ? n(t.key, e) : 1,
// flip the comparison if we're going in reverse
e && r && (i *= -1), i < 0)
// This node is less than our start key. ignore it
t = this.isReverse ? t.left : t.right; else {
if (0 === i) {
// This node is exactly equal to our start key. Push it on the stack,
// but stop iterating;
this.nodeStack.push(t);
break;
}
// This node is greater than our start key, add it to the stack and move
// to the next one
this.nodeStack.push(t), t = this.isReverse ? t.right : t.left;
}
}
return t.prototype.getNext = function() {
var t = this.nodeStack.pop(), e = {
key: t.key,
value: t.value
};
if (this.isReverse) for (t = t.left; !t.isEmpty(); ) this.nodeStack.push(t), t = t.right; else for (t = t.right; !t.isEmpty(); ) this.nodeStack.push(t),
t = t.left;
return e;
}, t.prototype.hasNext = function() {
return this.nodeStack.length > 0;
}, t.prototype.peek = function() {
if (0 === this.nodeStack.length) return null;
var t = this.nodeStack[this.nodeStack.length - 1];
return {
key: t.key,
value: t.value
};
}, t;
}(), Xe = /** @class */ function() {
function t(e, n, r, i, o) {
this.key = e, this.value = n, this.color = null != r ? r : t.RED, this.left = null != i ? i : t.EMPTY,
this.right = null != o ? o : t.EMPTY, this.size = this.left.size + 1 + this.right.size;
}
// Returns a copy of the current node, optionally replacing pieces of it.
return t.prototype.copy = function(e, n, r, i, o) {
return new t(null != e ? e : this.key, null != n ? n : this.value, null != r ? r : this.color, null != i ? i : this.left, null != o ? o : this.right);
}, t.prototype.isEmpty = function() {
return !1;
},
// Traverses the tree in key order and calls the specified action function
// for each node. If action returns true, traversal is aborted.
// Returns the first truthy value returned by action, or the last falsey
// value returned by action.
t.prototype.inorderTraversal = function(t) {
return this.left.inorderTraversal(t) || t(this.key, this.value) || this.right.inorderTraversal(t);
},
// Traverses the tree in reverse key order and calls the specified action
// function for each node. If action returns true, traversal is aborted.
// Returns the first truthy value returned by action, or the last falsey
// value returned by action.
t.prototype.reverseTraversal = function(t) {
return this.right.reverseTraversal(t) || t(this.key, this.value) || this.left.reverseTraversal(t);
},
// Returns the minimum node in the tree.
t.prototype.min = function() {
return this.left.isEmpty() ? this : this.left.min();
},
// Returns the maximum key in the tree.
t.prototype.minKey = function() {
return this.min().key;
},
// Returns the maximum key in the tree.
t.prototype.maxKey = function() {
return this.right.isEmpty() ? this.key : this.right.maxKey();
},
// Returns new tree, with the key/value added.
t.prototype.insert = function(t, e, n) {
var r = this, i = n(t, r.key);
return (r = i < 0 ? r.copy(null, null, null, r.left.insert(t, e, n), null) : 0 === i ? r.copy(null, e, null, null, null) : r.copy(null, null, null, null, r.right.insert(t, e, n))).fixUp();
}, t.prototype.removeMin = function() {
if (this.left.isEmpty()) return t.EMPTY;
var e = this;
return e.left.isRed() || e.left.left.isRed() || (e = e.moveRedLeft()), (e = e.copy(null, null, null, e.left.removeMin(), null)).fixUp();
},
// Returns new tree, with the specified item removed.
t.prototype.remove = function(e, n) {
var r, i = this;
if (n(e, i.key) < 0) i.left.isEmpty() || i.left.isRed() || i.left.left.isRed() || (i = i.moveRedLeft()),
i = i.copy(null, null, null, i.left.remove(e, n), null); else {
if (i.left.isRed() && (i = i.rotateRight()), i.right.isEmpty() || i.right.isRed() || i.right.left.isRed() || (i = i.moveRedRight()),
0 === n(e, i.key)) {
if (i.right.isEmpty()) return t.EMPTY;
r = i.right.min(), i = i.copy(r.key, r.value, null, null, i.right.removeMin());
}
i = i.copy(null, null, null, null, i.right.remove(e, n));
}
return i.fixUp();
}, t.prototype.isRed = function() {
return this.color;
},
// Returns new tree after performing any needed rotations.
t.prototype.fixUp = function() {
var t = this;
return t.right.isRed() && !t.left.isRed() && (t = t.rotateLeft()), t.left.isRed() && t.left.left.isRed() && (t = t.rotateRight()),
t.left.isRed() && t.right.isRed() && (t = t.colorFlip()), t;
}, t.prototype.moveRedLeft = function() {
var t = this.colorFlip();
return t.right.left.isRed() && (t = (t = (t = t.copy(null, null, null, null, t.right.rotateRight())).rotateLeft()).colorFlip()),
t;
}, t.prototype.moveRedRight = function() {
var t = this.colorFlip();
return t.left.left.isRed() && (t = (t = t.rotateRight()).colorFlip()), t;
}, t.prototype.rotateLeft = function() {
var e = this.copy(null, null, t.RED, null, this.right.left);
return this.right.copy(null, null, this.color, e, null);
}, t.prototype.rotateRight = function() {
var e = this.copy(null, null, t.RED, this.left.right, null);
return this.left.copy(null, null, this.color, null, e);
}, t.prototype.colorFlip = function() {
var t = this.left.copy(null, null, !this.left.color, null, null), e = this.right.copy(null, null, !this.right.color, null, null);
return this.copy(null, null, !this.color, t, e);
},
// For testing.
t.prototype.checkMaxDepth = function() {
var t = this.check();
return Math.pow(2, t) <= this.size + 1;
},
// In a balanced RB tree, the black-depth (number of black nodes) from root to
// leaves is equal on both sides. This function verifies that or asserts.
t.prototype.check = function() {
if (this.isRed() && this.left.isRed()) throw q();
if (this.right.isRed()) throw q();
var t = this.left.check();
if (t !== this.right.check()) throw q();
return t + (this.isRed() ? 0 : 1);
}, t;
}();
// end SortedMap
// An iterator over an LLRBNode.
// end LLRBNode
// Empty node is shared between all LLRB trees.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Xe.EMPTY = null, Xe.RED = !0, Xe.BLACK = !1,
// end LLRBEmptyNode
Xe.EMPTY = new (/** @class */ function() {
function t() {
this.size = 0;
}
return Object.defineProperty(t.prototype, "key", {
get: function() {
throw q();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "value", {
get: function() {
throw q();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "color", {
get: function() {
throw q();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "left", {
get: function() {
throw q();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "right", {
get: function() {
throw q();
},
enumerable: !1,
configurable: !0
}),
// Returns a copy of the current node.
t.prototype.copy = function(t, e, n, r, i) {
return this;
},
// Returns a copy of the tree, with the specified key/value added.
t.prototype.insert = function(t, e, n) {
return new Xe(t, e);
},
// Returns a copy of the tree, with the specified key removed.
t.prototype.remove = function(t, e) {
return this;
}, t.prototype.isEmpty = function() {
return !0;
}, t.prototype.inorderTraversal = function(t) {
return !1;
}, t.prototype.reverseTraversal = function(t) {
return !1;
}, t.prototype.minKey = function() {
return null;
}, t.prototype.maxKey = function() {
return null;
}, t.prototype.isRed = function() {
return !1;
},
// For testing.
t.prototype.checkMaxDepth = function() {
return !0;
}, t.prototype.check = function() {
return 0;
}, t;
}());
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* SortedSet is an immutable (copy-on-write) collection that holds elements
* in order specified by the provided comparator.
*
* NOTE: if provided comparator returns 0 for two elements, we consider them to
* be equal!
*/
var Ze = /** @class */ function() {
function t(t) {
this.comparator = t, this.data = new He(this.comparator);
}
return t.prototype.has = function(t) {
return null !== this.data.get(t);
}, t.prototype.first = function() {
return this.data.minKey();
}, t.prototype.last = function() {
return this.data.maxKey();
}, Object.defineProperty(t.prototype, "size", {
get: function() {
return this.data.size;
},
enumerable: !1,
configurable: !0
}), t.prototype.indexOf = function(t) {
return this.data.indexOf(t);
},
/** Iterates elements in order defined by "comparator" */ t.prototype.forEach = function(t) {
this.data.inorderTraversal((function(e, n) {
return t(e), !1;
}));
},
/** Iterates over `elem`s such that: range[0] <= elem < range[1]. */ t.prototype.forEachInRange = function(t, e) {
for (var n = this.data.getIteratorFrom(t[0]); n.hasNext(); ) {
var r = n.getNext();
if (this.comparator(r.key, t[1]) >= 0) return;
e(r.key);
}
},
/**
* Iterates over `elem`s such that: start <= elem until false is returned.
*/
t.prototype.forEachWhile = function(t, e) {
var n;
for (n = void 0 !== e ? this.data.getIteratorFrom(e) : this.data.getIterator(); n.hasNext(); ) if (!t(n.getNext().key)) return;
},
/** Finds the least element greater than or equal to `elem`. */ t.prototype.firstAfterOrEqual = function(t) {
var e = this.data.getIteratorFrom(t);
return e.hasNext() ? e.getNext().key : null;
}, t.prototype.getIterator = function() {
return new Je(this.data.getIterator());
}, t.prototype.getIteratorFrom = function(t) {
return new Je(this.data.getIteratorFrom(t));
},
/** Inserts or updates an element */ t.prototype.add = function(t) {
return this.copy(this.data.remove(t).insert(t, !0));
},
/** Deletes an element */ t.prototype.delete = function(t) {
return this.has(t) ? this.copy(this.data.remove(t)) : this;
}, t.prototype.isEmpty = function() {
return this.data.isEmpty();
}, t.prototype.unionWith = function(t) {
var e = this;
// Make sure `result` always refers to the larger one of the two sets.
return e.size < t.size && (e = t, t = this), t.forEach((function(t) {
e = e.add(t);
})), e;
}, t.prototype.isEqual = function(e) {
if (!(e instanceof t)) return !1;
if (this.size !== e.size) return !1;
for (var n = this.data.getIterator(), r = e.data.getIterator(); n.hasNext(); ) {
var i = n.getNext().key, o = r.getNext().key;
if (0 !== this.comparator(i, o)) return !1;
}
return !0;
}, t.prototype.toArray = function() {
var t = [];
return this.forEach((function(e) {
t.push(e);
})), t;
}, t.prototype.toString = function() {
var t = [];
return this.forEach((function(e) {
return t.push(e);
})), "SortedSet(" + t.toString() + ")";
}, t.prototype.copy = function(e) {
var n = new t(this.comparator);
return n.data = e, n;
}, t;
}(), Je = /** @class */ function() {
function t(t) {
this.iter = t;
}
return t.prototype.getNext = function() {
return this.iter.getNext().key;
}, t.prototype.hasNext = function() {
return this.iter.hasNext();
}, t;
}();
/**
* Compares two sorted sets for equality using their natural ordering. The
* method computes the intersection and invokes `onAdd` for every element that
* is in `after` but not `before`. `onRemove` is invoked for every element in
* `before` but missing from `after`.
*
* The method creates a copy of both `before` and `after` and runs in O(n log
* n), where n is the size of the two lists.
*
* @param before - The elements that exist in the original set.
* @param after - The elements to diff against the original set.
* @param comparator - The comparator for the elements in before and after.
* @param onAdd - A function to invoke for every element that is part of `
* after` but not `before`.
* @param onRemove - A function to invoke for every element that is part of
* `before` but not `after`.
*/
/**
* Returns the next element from the iterator or `undefined` if none available.
*/
function $e(t) {
return t.hasNext() ? t.getNext() : void 0;
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Provides a set of fields that can be used to partially patch a document.
* FieldMask is used in conjunction with ObjectValue.
* Examples:
* foo - Overwrites foo entirely with the provided value. If foo is not
* present in the companion ObjectValue, the field is deleted.
* foo.bar - Overwrites only the field bar of the object foo.
* If foo is not an object, foo is replaced with an object
* containing foo
*/ var tn = /** @class */ function() {
function t(t) {
this.fields = t,
// TODO(dimond): validation of FieldMask
// Sort the field mask to support `FieldMask.isEqual()` and assert below.
t.sort(ht.comparator);
}
return t.empty = function() {
return new t([]);
},
/**
* Returns a new FieldMask object that is the result of adding all the given
* fields paths to this field mask.
*/
t.prototype.unionWith = function(e) {
for (var n = new Ze(ht.comparator), r = 0, i = this.fields; r < i.length; r++) {
var o = i[r];
n = n.add(o);
}
for (var u = 0, a = e; u < a.length; u++) {
var s = a[u];
n = n.add(s);
}
return new t(n.toArray());
},
/**
* Verifies that `fieldPath` is included by at least one field in this field
* mask.
*
* This is an O(n) operation, where `n` is the size of the field mask.
*/
t.prototype.covers = function(t) {
for (var e = 0, n = this.fields; e < n.length; e++) {
if (n[e].isPrefixOf(t)) return !0;
}
return !1;
}, t.prototype.isEqual = function(t) {
return it(this.fields, t.fields, (function(t, e) {
return t.isEqual(e);
}));
}, t;
}(), en = /** @class */ function() {
function t(t) {
this.value = t;
}
return t.empty = function() {
return new t({
mapValue: {}
});
},
/**
* Returns the value at the given path or null.
*
* @param path - the path to search
* @returns The value at the path or null if the path is not set.
*/
t.prototype.field = function(t) {
if (t.isEmpty()) return this.value;
for (var e = this.value, n = 0; n < t.length - 1; ++n) if (!me(e = (e.mapValue.fields || {})[t.get(n)])) return null;
return (e = (e.mapValue.fields || {})[t.lastSegment()]) || null;
},
/**
* Sets the field to the provided value.
*
* @param path - The field path to set.
* @param value - The value to set.
*/
t.prototype.set = function(t, e) {
this.getFieldsMap(t.popLast())[t.lastSegment()] = ge(e);
},
/**
* Sets the provided fields to the provided values.
*
* @param data - A map of fields to values (or null for deletes).
*/
t.prototype.setAll = function(t) {
var e = this, n = ht.emptyPath(), r = {}, i = [];
t.forEach((function(t, o) {
if (!n.isImmediateParentOf(o)) {
// Insert the accumulated changes at this parent location
var u = e.getFieldsMap(n);
e.applyChanges(u, r, i), r = {}, i = [], n = o.popLast();
}
t ? r[o.lastSegment()] = ge(t) : i.push(o.lastSegment());
}));
var o = this.getFieldsMap(n);
this.applyChanges(o, r, i);
},
/**
* Removes the field at the specified path. If there is no field at the
* specified path, nothing is changed.
*
* @param path - The field path to remove.
*/
t.prototype.delete = function(t) {
var e = this.field(t.popLast());
me(e) && e.mapValue.fields && delete e.mapValue.fields[t.lastSegment()];
}, t.prototype.isEqual = function(t) {
return ue(this.value, t.value);
},
/**
* Returns the map that contains the leaf element of `path`. If the parent
* entry does not yet exist, or if it is not a map, a new map will be created.
*/
t.prototype.getFieldsMap = function(t) {
var e = this.value;
e.mapValue.fields || (e.mapValue = {
fields: {}
});
for (var n = 0; n < t.length; ++n) {
var r = e.mapValue.fields[t.get(n)];
me(r) && r.mapValue.fields || (r = {
mapValue: {
fields: {}
}
}, e.mapValue.fields[t.get(n)] = r), e = r;
}
return e.mapValue.fields;
},
/**
* Modifies `fieldsMap` by adding, replacing or deleting the specified
* entries.
*/
t.prototype.applyChanges = function(t, e, n) {
Kt(e, (function(e, n) {
return t[e] = n;
}));
for (var r = 0, i = n; r < i.length; r++) {
var o = i[r];
delete t[o];
}
}, t.prototype.clone = function() {
return new t(ge(this.value));
}, t;
}();
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* An ObjectValue represents a MapValue in the Firestore Proto and offers the
* ability to add and remove fields (via the ObjectValueBuilder).
*/
/**
* Returns a FieldMask built from all fields in a MapValue.
*/
function nn(t) {
var e = [];
return Kt(t.fields, (function(t, n) {
var r = new ht([ t ]);
if (me(n)) {
var i = nn(n.mapValue).fields;
if (0 === i.length)
// Preserve the empty map by adding it to the FieldMask.
e.push(r); else
// For nested and non-empty ObjectValues, add the FieldPath of the
// leaf nodes.
for (var o = 0, u = i; o < u.length; o++) {
var a = u[o];
e.push(r.child(a));
}
} else
// For nested and non-empty ObjectValues, add the FieldPath of the leaf
// nodes.
e.push(r);
})), new tn(e)
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents a document in Firestore with a key, version, data and whether it
* has local mutations applied to it.
*
* Documents can transition between states via `convertToFoundDocument()`,
* `convertToNoDocument()` and `convertToUnknownDocument()`. If a document does
* not transition to one of these states even after all mutations have been
* applied, `isValidDocument()` returns false and the document should be removed
* from all views.
*/;
}
var rn = /** @class */ function() {
function t(t, e, n, r, i, o, u) {
this.key = t, this.documentType = e, this.version = n, this.readTime = r, this.createTime = i,
this.data = o, this.documentState = u
/**
* Creates a document with no known version or data, but which can serve as
* base document for mutations.
*/;
}
return t.newInvalidDocument = function(e) {
return new t(e, 0 /* DocumentType.INVALID */ ,
/* version */ at.min(),
/* readTime */ at.min(),
/* createTime */ at.min(), en.empty(), 0 /* DocumentState.SYNCED */);
},
/**
* Creates a new document that is known to exist with the given data at the
* given version.
*/
t.newFoundDocument = function(e, n, r, i) {
return new t(e, 1 /* DocumentType.FOUND_DOCUMENT */ ,
/* version */ n,
/* readTime */ at.min(),
/* createTime */ r, i, 0 /* DocumentState.SYNCED */);
},
/** Creates a new document that is known to not exist at the given version. */ t.newNoDocument = function(e, n) {
return new t(e, 2 /* DocumentType.NO_DOCUMENT */ ,
/* version */ n,
/* readTime */ at.min(),
/* createTime */ at.min(), en.empty(), 0 /* DocumentState.SYNCED */);
},
/**
* Creates a new document that is known to exist at the given version but
* whose data is not known (e.g. a document that was updated without a known
* base document).
*/
t.newUnknownDocument = function(e, n) {
return new t(e, 3 /* DocumentType.UNKNOWN_DOCUMENT */ ,
/* version */ n,
/* readTime */ at.min(),
/* createTime */ at.min(), en.empty(), 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */);
},
/**
* Changes the document type to indicate that it exists and that its version
* and data are known.
*/
t.prototype.convertToFoundDocument = function(t, e) {
// If a document is switching state from being an invalid or deleted
// document to a valid (FOUND_DOCUMENT) document, either due to receiving an
// update from Watch or due to applying a local set mutation on top
// of a deleted document, our best guess about its createTime would be the
// version at which the document transitioned to a FOUND_DOCUMENT.
return !this.createTime.isEqual(at.min()) || 2 /* DocumentType.NO_DOCUMENT */ !== this.documentType && 0 /* DocumentType.INVALID */ !== this.documentType || (this.createTime = t),
this.version = t, this.documentType = 1 /* DocumentType.FOUND_DOCUMENT */ , this.data = e,
this.documentState = 0 /* DocumentState.SYNCED */ , this;
},
/**
* Changes the document type to indicate that it doesn't exist at the given
* version.
*/
t.prototype.convertToNoDocument = function(t) {
return this.version = t, this.documentType = 2 /* DocumentType.NO_DOCUMENT */ ,
this.data = en.empty(), this.documentState = 0 /* DocumentState.SYNCED */ , this;
},
/**
* Changes the document type to indicate that it exists at a given version but
* that its data is not known (e.g. a document that was updated without a known
* base document).
*/
t.prototype.convertToUnknownDocument = function(t) {
return this.version = t, this.documentType = 3 /* DocumentType.UNKNOWN_DOCUMENT */ ,
this.data = en.empty(), this.documentState = 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */ ,
this;
}, t.prototype.setHasCommittedMutations = function() {
return this.documentState = 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */ , this;
}, t.prototype.setHasLocalMutations = function() {
return this.documentState = 1 /* DocumentState.HAS_LOCAL_MUTATIONS */ , this.version = at.min(),
this;
}, t.prototype.setReadTime = function(t) {
return this.readTime = t, this;
}, Object.defineProperty(t.prototype, "hasLocalMutations", {
get: function() {
return 1 /* DocumentState.HAS_LOCAL_MUTATIONS */ === this.documentState;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "hasCommittedMutations", {
get: function() {
return 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */ === this.documentState;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "hasPendingWrites", {
get: function() {
return this.hasLocalMutations || this.hasCommittedMutations;
},
enumerable: !1,
configurable: !0
}), t.prototype.isValidDocument = function() {
return 0 /* DocumentType.INVALID */ !== this.documentType;
}, t.prototype.isFoundDocument = function() {
return 1 /* DocumentType.FOUND_DOCUMENT */ === this.documentType;
}, t.prototype.isNoDocument = function() {
return 2 /* DocumentType.NO_DOCUMENT */ === this.documentType;
}, t.prototype.isUnknownDocument = function() {
return 3 /* DocumentType.UNKNOWN_DOCUMENT */ === this.documentType;
}, t.prototype.isEqual = function(e) {
return e instanceof t && this.key.isEqual(e.key) && this.version.isEqual(e.version) && this.documentType === e.documentType && this.documentState === e.documentState && this.data.isEqual(e.data);
}, t.prototype.mutableCopy = function() {
return new t(this.key, this.documentType, this.version, this.readTime, this.createTime, this.data.clone(), this.documentState);
}, t.prototype.toString = function() {
return "Document(".concat(this.key, ", ").concat(this.version, ", ").concat(JSON.stringify(this.data.value), ", {createTime: ").concat(this.createTime, "}), {documentType: ").concat(this.documentType, "}), {documentState: ").concat(this.documentState, "})");
}, t;
}(), on = function(t, e, n, r, i, o, u) {
void 0 === e && (e = null), void 0 === n && (n = []), void 0 === r && (r = []),
void 0 === i && (i = null), void 0 === o && (o = null), void 0 === u && (u = null),
this.path = t, this.collectionGroup = e, this.orderBy = n, this.filters = r, this.limit = i,
this.startAt = o, this.endAt = u, this.ft = null;
};
/**
* Compares the value for field `field` in the provided documents. Throws if
* the field does not exist in both documents.
*/
/**
* @license
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Visible for testing
/**
* Initializes a Target with a path and optional additional query constraints.
* Path must currently be empty if this is a collection group query.
*
* NOTE: you should always construct `Target` from `Query.toTarget` instead of
* using this factory method, because `Query` provides an implicit `orderBy`
* property.
*/
function un(t, e, n, r, i, o, u) {
return void 0 === e && (e = null), void 0 === n && (n = []), void 0 === r && (r = []),
void 0 === i && (i = null), void 0 === o && (o = null), void 0 === u && (u = null),
new on(t, e, n, r, i, o, u);
}
function an(t) {
var e = G(t);
if (null === e.ft) {
var n = e.path.canonicalString();
null !== e.collectionGroup && (n += "|cg:" + e.collectionGroup), n += "|f:", n += e.filters.map((function(t) {
return Re(t);
})).join(","), n += "|ob:", n += e.orderBy.map((function(t) {
return function(t) {
// TODO(b/29183165): Make this collision robust.
return t.field.canonicalString() + t.dir;
}(t);
})).join(","), Qt(e.limit) || (n += "|l:", n += e.limit), e.startAt && (n += "|lb:",
n += e.startAt.inclusive ? "b:" : "a:", n += e.startAt.position.map((function(t) {
return le(t);
})).join(",")), e.endAt && (n += "|ub:", n += e.endAt.inclusive ? "a:" : "b:", n += e.endAt.position.map((function(t) {
return le(t);
})).join(",")), e.ft = n;
}
return e.ft;
}
function sn(t, e) {
if (t.limit !== e.limit) return !1;
if (t.orderBy.length !== e.orderBy.length) return !1;
for (var n = 0; n < t.orderBy.length; n++) if (!We(t.orderBy[n], e.orderBy[n])) return !1;
if (t.filters.length !== e.filters.length) return !1;
for (var r = 0; r < t.filters.length; r++) if (!Ve(t.filters[r], e.filters[r])) return !1;
return t.collectionGroup === e.collectionGroup && !!t.path.isEqual(e.path) && !!De(t.startAt, e.startAt) && De(t.endAt, e.endAt);
}
function cn(t) {
return ft.isDocumentKey(t.path) && null === t.collectionGroup && 0 === t.filters.length;
}
/** Returns the field filters that target the given field path. */ function ln(t, e) {
return t.filters.filter((function(t) {
return t instanceof Ae && t.field.isEqual(e);
}));
}
/**
* Returns the values that are used in ARRAY_CONTAINS or ARRAY_CONTAINS_ANY
* filters. Returns `null` if there are no such filters.
*/
/**
* Returns the value to use as the lower bound for ascending index segment at
* the provided `fieldPath` (or the upper bound for an descending segment).
*/ function hn(t, e, n) {
// Process all filters to find a value for the current field segment
for (var r = ie, i = !0, o = 0, u = ln(t, e); o < u.length; o++) {
var a = u[o], s = ie, c = !0;
switch (a.op) {
case "<" /* Operator.LESS_THAN */ :
case "<=" /* Operator.LESS_THAN_OR_EQUAL */ :
s = be(a.value);
break;
case "==" /* Operator.EQUAL */ :
case "in" /* Operator.IN */ :
case ">=" /* Operator.GREATER_THAN_OR_EQUAL */ :
s = a.value;
break;
case ">" /* Operator.GREATER_THAN */ :
s = a.value, c = !1;
break;
case "!=" /* Operator.NOT_EQUAL */ :
case "not-in" /* Operator.NOT_IN */ :
s = ie;
// Remaining filters cannot be used as lower bounds.
}
Te({
value: r,
inclusive: i
}, {
value: s,
inclusive: c
}) < 0 && (r = s, i = c);
}
// If there is an additional bound, compare the values against the existing
// range to see if we can narrow the scope.
if (null !== n) for (var l = 0; l < t.orderBy.length; ++l) if (t.orderBy[l].field.isEqual(e)) {
var h = n.position[l];
Te({
value: r,
inclusive: i
}, {
value: h,
inclusive: n.inclusive
}) < 0 && (r = h, i = n.inclusive);
break;
}
return {
value: r,
inclusive: i
};
}
/**
* Returns the value to use as the upper bound for ascending index segment at
* the provided `fieldPath` (or the lower bound for a descending segment).
*/ function fn(t, e, n) {
// Process all filters to find a value for the current field segment
for (var r = re, i = !0, o = 0, u = ln(t, e); o < u.length; o++) {
var a = u[o], s = re, c = !0;
switch (a.op) {
case ">=" /* Operator.GREATER_THAN_OR_EQUAL */ :
case ">" /* Operator.GREATER_THAN */ :
s = Ie(a.value), c = !1;
break;
case "==" /* Operator.EQUAL */ :
case "in" /* Operator.IN */ :
case "<=" /* Operator.LESS_THAN_OR_EQUAL */ :
s = a.value;
break;
case "<" /* Operator.LESS_THAN */ :
s = a.value, c = !1;
break;
case "!=" /* Operator.NOT_EQUAL */ :
case "not-in" /* Operator.NOT_IN */ :
s = re;
// Remaining filters cannot be used as upper bounds.
}
Ee({
value: r,
inclusive: i
}, {
value: s,
inclusive: c
}) > 0 && (r = s, i = c);
}
// If there is an additional bound, compare the values against the existing
// range to see if we can narrow the scope.
if (null !== n) for (var l = 0; l < t.orderBy.length; ++l) if (t.orderBy[l].field.isEqual(e)) {
var h = n.position[l];
Ee({
value: r,
inclusive: i
}, {
value: h,
inclusive: n.inclusive
}) > 0 && (r = h, i = n.inclusive);
break;
}
return {
value: r,
inclusive: i
};
}
/** Returns the number of segments of a perfect index for this target. */
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Query encapsulates all the query attributes we support in the SDK. It can
* be run against the LocalStore, as well as be converted to a `Target` to
* query the RemoteStore results.
*
* Visible for testing.
*/ var dn =
/**
* Initializes a Query with a path and optional additional query constraints.
* Path must currently be empty if this is a collection group query.
*/
function(t, e, n, r, i, o /* LimitType.First */ , u, a) {
void 0 === e && (e = null), void 0 === n && (n = []), void 0 === r && (r = []),
void 0 === i && (i = null), void 0 === o && (o = "F"), void 0 === u && (u = null),
void 0 === a && (a = null), this.path = t, this.collectionGroup = e, this.explicitOrderBy = n,
this.filters = r, this.limit = i, this.limitType = o, this.startAt = u, this.endAt = a,
this.dt = null,
// The corresponding `Target` of this `Query` instance.
this._t = null, this.startAt, this.endAt;
};
/** Creates a new Query instance with the options provided. */ function pn(t, e, n, r, i, o, u, a) {
return new dn(t, e, n, r, i, o, u, a);
}
/** Creates a new Query for a query that matches all documents at `path` */ function yn(t) {
return new dn(t);
}
/**
* Helper to convert a collection group query into a collection query at a
* specific path. This is used when executing collection group queries, since
* we have to split the query into a set of collection queries at multiple
* paths.
*/
/**
* Returns true if this query does not specify any query constraints that
* could remove results.
*/ function vn(t) {
return 0 === t.filters.length && null === t.limit && null == t.startAt && null == t.endAt && (0 === t.explicitOrderBy.length || 1 === t.explicitOrderBy.length && t.explicitOrderBy[0].field.isKeyField());
}
function mn(t) {
return t.explicitOrderBy.length > 0 ? t.explicitOrderBy[0].field : null;
}
function gn(t) {
for (var e = 0, n = t.filters; e < n.length; e++) {
var r = n[e].getFirstInequalityField();
if (null !== r) return r;
}
return null;
}
/**
* Creates a new Query for a collection group query that matches all documents
* within the provided collection group.
*/
/**
* Returns whether the query matches a collection group rather than a specific
* collection.
*/ function wn(t) {
return null !== t.collectionGroup;
}
/**
* Returns the implicit order by constraint that is used to execute the Query,
* which can be different from the order by constraints the user provided (e.g.
* the SDK and backend always orders by `__name__`).
*/ function bn(t) {
var e = G(t);
if (null === e.dt) {
e.dt = [];
var n = gn(e), r = mn(e);
if (null !== n && null === r)
// In order to implicitly add key ordering, we must also add the
// inequality filter field for it to be a valid query.
// Note that the default inequality field and key ordering is ascending.
n.isKeyField() || e.dt.push(new ze(n)), e.dt.push(new ze(ht.keyField(), "asc" /* Direction.ASCENDING */)); else {
for (var i = !1, o = 0, u = e.explicitOrderBy; o < u.length; o++) {
var a = u[o];
e.dt.push(a), a.field.isKeyField() && (i = !0);
}
if (!i) {
// The order of the implicit key ordering always matches the last
// explicit order by
var s = e.explicitOrderBy.length > 0 ? e.explicitOrderBy[e.explicitOrderBy.length - 1].dir : "asc" /* Direction.ASCENDING */;
e.dt.push(new ze(ht.keyField(), s));
}
}
}
return e.dt;
}
/**
* Converts this `Query` instance to it's corresponding `Target` representation.
*/ function In(t) {
var e = G(t);
if (!e._t) if ("F" /* LimitType.First */ === e.limitType) e._t = un(e.path, e.collectionGroup, bn(e), e.filters, e.limit, e.startAt, e.endAt); else {
for (
// Flip the orderBy directions since we want the last results
var n = [], r = 0, i = bn(e); r < i.length; r++) {
var o = i[r], u = "desc" /* Direction.DESCENDING */ === o.dir ? "asc" /* Direction.ASCENDING */ : "desc" /* Direction.DESCENDING */;
n.push(new ze(o.field, u));
}
// We need to swap the cursors to match the now-flipped query ordering.
var a = e.endAt ? new Se(e.endAt.position, e.endAt.inclusive) : null, s = e.startAt ? new Se(e.startAt.position, e.startAt.inclusive) : null;
// Now return as a LimitType.First query.
e._t = un(e.path, e.collectionGroup, n, e.filters, e.limit, a, s);
}
return e._t;
}
function Tn(t, e) {
e.getFirstInequalityField(), gn(t);
var n = t.filters.concat([ e ]);
return new dn(t.path, t.collectionGroup, t.explicitOrderBy.slice(), n, t.limit, t.limitType, t.startAt, t.endAt);
}
function En(t, e, n) {
return new dn(t.path, t.collectionGroup, t.explicitOrderBy.slice(), t.filters.slice(), e, n, t.startAt, t.endAt);
}
function Sn(t, e) {
return sn(In(t), In(e)) && t.limitType === e.limitType;
}
// TODO(b/29183165): This is used to get a unique string from a query to, for
// example, use as a dictionary key, but the implementation is subject to
// collisions. Make it collision-free.
function _n(t) {
return "".concat(an(In(t)), "|lt:").concat(t.limitType);
}
function Dn(t) {
return "Query(target=".concat(function(t) {
var e = t.path.canonicalString();
return null !== t.collectionGroup && (e += " collectionGroup=" + t.collectionGroup),
t.filters.length > 0 && (e += ", filters: [".concat(t.filters.map((function(t) {
return Le(t);
})).join(", "), "]")), Qt(t.limit) || (e += ", limit: " + t.limit), t.orderBy.length > 0 && (e += ", orderBy: [".concat(t.orderBy.map((function(t) {
return function(t) {
return "".concat(t.field.canonicalString(), " (").concat(t.dir, ")");
}(t);
})).join(", "), "]")), t.startAt && (e += ", startAt: ", e += t.startAt.inclusive ? "b:" : "a:",
e += t.startAt.position.map((function(t) {
return le(t);
})).join(",")), t.endAt && (e += ", endAt: ", e += t.endAt.inclusive ? "a:" : "b:",
e += t.endAt.position.map((function(t) {
return le(t);
})).join(",")), "Target(".concat(e, ")");
}(In(t)), "; limitType=").concat(t.limitType, ")");
}
/** Returns whether `doc` matches the constraints of `query`. */ function xn(t, e) {
return e.isFoundDocument() && function(t, e) {
var n = e.key.path;
return null !== t.collectionGroup ? e.key.hasCollectionId(t.collectionGroup) && t.path.isPrefixOf(n) : ft.isDocumentKey(t.path) ? t.path.isEqual(n) : t.path.isImmediateParentOf(n);
}(t, e) && function(t, e) {
// We must use `queryOrderBy()` to get the list of all orderBys (both implicit and explicit).
// Note that for OR queries, orderBy applies to all disjunction terms and implicit orderBys must
// be taken into account. For example, the query "a > 1 || b==1" has an implicit "orderBy a" due
// to the inequality, and is evaluated as "a > 1 orderBy a || b==1 orderBy a".
// A document with content of {b:1} matches the filters, but does not match the orderBy because
// it's missing the field 'a'.
for (var n = 0, r = bn(t); n < r.length; n++) {
var i = r[n];
// order by key always matches
if (!i.field.isKeyField() && null === e.data.field(i.field)) return !1;
}
return !0;
}(t, e) && function(t, e) {
for (var n = 0, r = t.filters; n < r.length; n++) {
if (!r[n].matches(e)) return !1;
}
return !0;
}(t, e) && function(t, e) {
return !(t.startAt &&
/**
* Returns true if a document sorts before a bound using the provided sort
* order.
*/
!function(t, e, n) {
var r = _e(t, e, n);
return t.inclusive ? r <= 0 : r < 0;
}(t.startAt, bn(t), e)) && !(t.endAt && !function(t, e, n) {
var r = _e(t, e, n);
return t.inclusive ? r >= 0 : r > 0;
}(t.endAt, bn(t), e));
}(t, e);
}
function An(t) {
return t.collectionGroup || (t.path.length % 2 == 1 ? t.path.lastSegment() : t.path.get(t.path.length - 2));
}
/**
* Returns a new comparator function that can be used to compare two documents
* based on the Query's ordering constraint.
*/ function Cn(t) {
return function(e, n) {
for (var r = !1, i = 0, o = bn(t); i < o.length; i++) {
var u = o[i], a = Nn(u, e, n);
if (0 !== a) return a;
r = r || u.field.isKeyField();
}
return 0;
};
}
function Nn(t, e, n) {
var r = t.field.isKeyField() ? ft.comparator(e.key, n.key) : function(t, e, n) {
var r = e.data.field(t), i = n.data.field(t);
return null !== r && null !== i ? se(r, i) : q();
}(t.field, e, n);
switch (t.dir) {
case "asc" /* Direction.ASCENDING */ :
return r;
case "desc" /* Direction.DESCENDING */ :
return -1 * r;
default:
return q();
}
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Returns an DoubleValue for `value` that is encoded based the serializer's
* `useProto3Json` setting.
*/ function kn(t, e) {
if (t.wt) {
if (isNaN(e)) return {
doubleValue: "NaN"
};
if (e === 1 / 0) return {
doubleValue: "Infinity"
};
if (e === -1 / 0) return {
doubleValue: "-Infinity"
};
}
return {
doubleValue: zt(e) ? "-0" : e
};
}
/**
* Returns an IntegerValue for `value`.
*/ function Fn(t) {
return {
integerValue: "" + t
};
}
/**
* Returns a value for a number that's appropriate to put into a proto.
* The return value is an IntegerValue if it can safely represent the value,
* otherwise a DoubleValue is returned.
*/ function On(t, e) {
return Wt(e) ? Fn(e) : kn(t, e);
}
/**
* @license
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Used to represent a field transform on a mutation. */ var Rn = function() {
// Make sure that the structural type of `TransformOperation` is unique.
// See https://github.com/microsoft/TypeScript/issues/5451
this._ = void 0;
};
/**
* Computes the local transform result against the provided `previousValue`,
* optionally using the provided localWriteTime.
*/ function Vn(t, e, n) {
return t instanceof Pn ? function(t, e) {
var n = {
fields: {
__type__: {
stringValue: "server_timestamp"
},
__local_write_time__: {
timestampValue: {
seconds: t.seconds,
nanos: t.nanoseconds
}
}
}
};
return e && (n.fields.__previous_value__ = e), {
mapValue: n
};
}(n, e) : t instanceof qn ? Un(t, e) : t instanceof Bn ? Gn(t, e) : function(t, e) {
// PORTING NOTE: Since JavaScript's integer arithmetic is limited to 53 bit
// precision and resolves overflows by reducing precision, we do not
// manually cap overflows at 2^63.
var n = Ln(t, e), r = jn(n) + jn(t.gt);
return de(n) && de(t.gt) ? Fn(r) : kn(t.yt, r);
}(t, e);
}
/**
* Computes a final transform result after the transform has been acknowledged
* by the server, potentially using the server-provided transformResult.
*/ function Mn(t, e, n) {
// The server just sends null as the transform result for array operations,
// so we have to calculate a result the same as we do for local
// applications.
return t instanceof qn ? Un(t, e) : t instanceof Bn ? Gn(t, e) : n;
}
/**
* If this transform operation is not idempotent, returns the base value to
* persist for this transform. If a base value is returned, the transform
* operation is always applied to this base value, even if document has
* already been updated.
*
* Base values provide consistent behavior for non-idempotent transforms and
* allow us to return the same latency-compensated value even if the backend
* has already applied the transform operation. The base value is null for
* idempotent transforms, as they can be re-played even if the backend has
* already applied them.
*
* @returns a base value to store along with the mutation, or null for
* idempotent transforms.
*/ function Ln(t, e) {
return t instanceof Kn ? de(n = e) || function(t) {
return !!t && "doubleValue" in t;
}(n) ? e : {
integerValue: 0
} : null;
var n;
}
/** Transforms a value into a server-generated timestamp. */ var Pn = /** @class */ function(e) {
function n() {
return null !== e && e.apply(this, arguments) || this;
}
return t(n, e), n;
}(Rn), qn = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this) || this).elements = t, n;
}
return t(n, e), n;
}(Rn);
/** Transforms an array value via a union operation. */ function Un(t, e) {
for (var n = Qn(e), r = function(t) {
n.some((function(e) {
return ue(e, t);
})) || n.push(t);
}, i = 0, o = t.elements; i < o.length; i++) {
r(o[i]);
}
return {
arrayValue: {
values: n
}
};
}
/** Transforms an array value via a remove operation. */ var Bn = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this) || this).elements = t, n;
}
return t(n, e), n;
}(Rn);
function Gn(t, e) {
for (var n = Qn(e), r = function(t) {
n = n.filter((function(e) {
return !ue(e, t);
}));
}, i = 0, o = t.elements; i < o.length; i++) {
r(o[i]);
}
return {
arrayValue: {
values: n
}
};
}
/**
* Implements the backend semantics for locally computed NUMERIC_ADD (increment)
* transforms. Converts all field values to integers or doubles, but unlike the
* backend does not cap integer values at 2^63. Instead, JavaScript number
* arithmetic is used and precision loss can occur for values greater than 2^53.
*/ var Kn = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this) || this).yt = t, r.gt = n, r;
}
return t(n, e), n;
}(Rn);
function jn(t) {
return Jt(t.integerValue || t.doubleValue);
}
function Qn(t) {
return pe(t) && t.arrayValue.values ? t.arrayValue.values.slice() : [];
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** A field path and the TransformOperation to perform upon it. */ var zn = function(t, e) {
this.field = t, this.transform = e;
};
/** The result of successfully applying a mutation to the backend. */
var Wn = function(
/**
* The version at which the mutation was committed:
*
* - For most operations, this is the updateTime in the WriteResult.
* - For deletes, the commitTime of the WriteResponse (because deletes are
* not stored and have no updateTime).
*
* Note that these versions can be different: No-op writes will not change
* the updateTime even though the commitTime advances.
*/
t,
/**
* The resulting fields returned from the backend after a mutation
* containing field transforms has been committed. Contains one FieldValue
* for each FieldTransform that was in the mutation.
*
* Will be empty if the mutation did not contain any field transforms.
*/
e) {
this.version = t, this.transformResults = e;
}, Hn = /** @class */ function() {
function t(t, e) {
this.updateTime = t, this.exists = e
/** Creates a new empty Precondition. */;
}
return t.none = function() {
return new t;
},
/** Creates a new Precondition with an exists flag. */ t.exists = function(e) {
return new t(void 0, e);
},
/** Creates a new Precondition based on a version a document exists at. */ t.updateTime = function(e) {
return new t(e);
}, Object.defineProperty(t.prototype, "isNone", {
/** Returns whether this Precondition is empty. */ get: function() {
return void 0 === this.updateTime && void 0 === this.exists;
},
enumerable: !1,
configurable: !0
}), t.prototype.isEqual = function(t) {
return this.exists === t.exists && (this.updateTime ? !!t.updateTime && this.updateTime.isEqual(t.updateTime) : !t.updateTime);
}, t;
}();
/**
* Encodes a precondition for a mutation. This follows the model that the
* backend accepts with the special case of an explicit "empty" precondition
* (meaning no precondition).
*/
/** Returns true if the preconditions is valid for the given document. */ function Yn(t, e) {
return void 0 !== t.updateTime ? e.isFoundDocument() && e.version.isEqual(t.updateTime) : void 0 === t.exists || t.exists === e.isFoundDocument();
}
/**
* A mutation describes a self-contained change to a document. Mutations can
* create, replace, delete, and update subsets of documents.
*
* Mutations not only act on the value of the document but also its version.
*
* For local mutations (mutations that haven't been committed yet), we preserve
* the existing version for Set and Patch mutations. For Delete mutations, we
* reset the version to 0.
*
* Here's the expected transition table.
*
* MUTATION APPLIED TO RESULTS IN
*
* SetMutation Document(v3) Document(v3)
* SetMutation NoDocument(v3) Document(v0)
* SetMutation InvalidDocument(v0) Document(v0)
* PatchMutation Document(v3) Document(v3)
* PatchMutation NoDocument(v3) NoDocument(v3)
* PatchMutation InvalidDocument(v0) UnknownDocument(v3)
* DeleteMutation Document(v3) NoDocument(v0)
* DeleteMutation NoDocument(v3) NoDocument(v0)
* DeleteMutation InvalidDocument(v0) NoDocument(v0)
*
* For acknowledged mutations, we use the updateTime of the WriteResponse as
* the resulting version for Set and Patch mutations. As deletes have no
* explicit update time, we use the commitTime of the WriteResponse for
* Delete mutations.
*
* If a mutation is acknowledged by the backend but fails the precondition check
* locally, we transition to an `UnknownDocument` and rely on Watch to send us
* the updated version.
*
* Field transforms are used only with Patch and Set Mutations. We use the
* `updateTransforms` message to store transforms, rather than the `transforms`s
* messages.
*
* ## Subclassing Notes
*
* Every type of mutation needs to implement its own applyToRemoteDocument() and
* applyToLocalView() to implement the actual behavior of applying the mutation
* to some source document (see `setMutationApplyToRemoteDocument()` for an
* example).
*/ var Xn = function() {};
/**
* A utility method to calculate a `Mutation` representing the overlay from the
* final state of the document, and a `FieldMask` representing the fields that
* are mutated by the local mutations.
*/ function Zn(t, e) {
if (!t.hasLocalMutations || e && 0 === e.fields.length) return null;
// mask is null when sets or deletes are applied to the current document.
if (null === e) return t.isNoDocument() ? new cr(t.key, Hn.none()) : new nr(t.key, t.data, Hn.none());
for (var n = t.data, r = en.empty(), i = new Ze(ht.comparator), o = 0, u = e.fields; o < u.length; o++) {
var a = u[o];
if (!i.has(a)) {
var s = n.field(a);
// If we are deleting a nested field, we take the immediate parent as
// the mask used to construct the resulting mutation.
// Justification: Nested fields can create parent fields implicitly. If
// only a leaf entry is deleted in later mutations, the parent field
// should still remain, but we may have lost this information.
// Consider mutation (foo.bar 1), then mutation (foo.bar delete()).
// This leaves the final result (foo, {}). Despite the fact that `doc`
// has the correct result, `foo` is not in `mask`, and the resulting
// mutation would miss `foo`.
null === s && a.length > 1 && (a = a.popLast(), s = n.field(a)), null === s ? r.delete(a) : r.set(a, s),
i = i.add(a);
}
}
return new rr(t.key, r, new tn(i.toArray()), Hn.none());
}
/**
* Applies this mutation to the given document for the purposes of computing a
* new remote document. If the input document doesn't match the expected state
* (e.g. it is invalid or outdated), the document type may transition to
* unknown.
*
* @param mutation - The mutation to apply.
* @param document - The document to mutate. The input document can be an
* invalid document if the client has no knowledge of the pre-mutation state
* of the document.
* @param mutationResult - The result of applying the mutation from the backend.
*/ function Jn(t, e, n) {
t instanceof nr ? function(t, e, n) {
// Unlike setMutationApplyToLocalView, if we're applying a mutation to a
// remote document the server has accepted the mutation so the precondition
// must have held.
var r = t.value.clone(), i = or(t.fieldTransforms, e, n.transformResults);
r.setAll(i), e.convertToFoundDocument(n.version, r).setHasCommittedMutations();
}(t, e, n) : t instanceof rr ? function(t, e, n) {
if (Yn(t.precondition, e)) {
var r = or(t.fieldTransforms, e, n.transformResults), i = e.data;
i.setAll(ir(t)), i.setAll(r), e.convertToFoundDocument(n.version, i).setHasCommittedMutations();
} else e.convertToUnknownDocument(n.version);
}(t, e, n) : function(t, e, n) {
// Unlike applyToLocalView, if we're applying a mutation to a remote
// document the server has accepted the mutation so the precondition must
// have held.
e.convertToNoDocument(n.version).setHasCommittedMutations();
}(0, e, n);
}
/**
* Applies this mutation to the given document for the purposes of computing
* the new local view of a document. If the input document doesn't match the
* expected state, the document is not modified.
*
* @param mutation - The mutation to apply.
* @param document - The document to mutate. The input document can be an
* invalid document if the client has no knowledge of the pre-mutation state
* of the document.
* @param previousMask - The fields that have been updated before applying this mutation.
* @param localWriteTime - A timestamp indicating the local write time of the
* batch this mutation is a part of.
* @returns A `FieldMask` representing the fields that are changed by applying this mutation.
*/ function $n(t, e, n, r) {
return t instanceof nr ? function(t, e, n, r) {
if (!Yn(t.precondition, e))
// The mutation failed to apply (e.g. a document ID created with add()
// caused a name collision).
return n;
var i = t.value.clone(), o = ur(t.fieldTransforms, r, e);
return i.setAll(o), e.convertToFoundDocument(e.version, i).setHasLocalMutations(),
null;
// SetMutation overwrites all fields.
}(t, e, n, r) : t instanceof rr ? function(t, e, n, r) {
if (!Yn(t.precondition, e)) return n;
var i = ur(t.fieldTransforms, r, e), o = e.data;
return o.setAll(ir(t)), o.setAll(i), e.convertToFoundDocument(e.version, o).setHasLocalMutations(),
null === n ? null : n.unionWith(t.fieldMask.fields).unionWith(t.fieldTransforms.map((function(t) {
return t.field;
})));
}(t, e, n, r) : function(t, e, n) {
return Yn(t.precondition, e) ? (e.convertToNoDocument(e.version).setHasLocalMutations(),
null) : n;
}(t, e, n);
}
/**
* If this mutation is not idempotent, returns the base value to persist with
* this mutation. If a base value is returned, the mutation is always applied
* to this base value, even if document has already been updated.
*
* The base value is a sparse object that consists of only the document
* fields for which this mutation contains a non-idempotent transformation
* (e.g. a numeric increment). The provided value guarantees consistent
* behavior for non-idempotent transforms and allow us to return the same
* latency-compensated value even if the backend has already applied the
* mutation. The base value is null for idempotent mutations, as they can be
* re-played even if the backend has already applied them.
*
* @returns a base value to store along with the mutation, or null for
* idempotent mutations.
*/ function tr(t, e) {
for (var n = null, r = 0, i = t.fieldTransforms; r < i.length; r++) {
var o = i[r], u = e.data.field(o.field), a = Ln(o.transform, u || null);
null != a && (null === n && (n = en.empty()), n.set(o.field, a));
}
return n || null;
}
function er(t, e) {
return t.type === e.type && !!t.key.isEqual(e.key) && !!t.precondition.isEqual(e.precondition) && !!function(t, e) {
return void 0 === t && void 0 === e || !(!t || !e) && it(t, e, (function(t, e) {
return function(t, e) {
return t.field.isEqual(e.field) && function(t, e) {
return t instanceof qn && e instanceof qn || t instanceof Bn && e instanceof Bn ? it(t.elements, e.elements, ue) : t instanceof Kn && e instanceof Kn ? ue(t.gt, e.gt) : t instanceof Pn && e instanceof Pn;
}(t.transform, e.transform);
}(t, e);
}));
}(t.fieldTransforms, e.fieldTransforms) && (0 /* MutationType.Set */ === t.type ? t.value.isEqual(e.value) : 1 /* MutationType.Patch */ !== t.type || t.data.isEqual(e.data) && t.fieldMask.isEqual(e.fieldMask));
}
/**
* A mutation that creates or replaces the document at the given key with the
* object value contents.
*/ var nr = /** @class */ function(e) {
function n(t, n, r, i) {
void 0 === i && (i = []);
var o = this;
return (o = e.call(this) || this).key = t, o.value = n, o.precondition = r, o.fieldTransforms = i,
o.type = 0 /* MutationType.Set */ , o;
}
return t(n, e), n.prototype.getFieldMask = function() {
return null;
}, n;
}(Xn), rr = /** @class */ function(e) {
function n(t, n, r, i, o) {
void 0 === o && (o = []);
var u = this;
return (u = e.call(this) || this).key = t, u.data = n, u.fieldMask = r, u.precondition = i,
u.fieldTransforms = o, u.type = 1 /* MutationType.Patch */ , u;
}
return t(n, e), n.prototype.getFieldMask = function() {
return this.fieldMask;
}, n;
}(Xn);
function ir(t) {
var e = new Map;
return t.fieldMask.fields.forEach((function(n) {
if (!n.isEmpty()) {
var r = t.data.field(n);
e.set(n, r);
}
})), e
/**
* Creates a list of "transform results" (a transform result is a field value
* representing the result of applying a transform) for use after a mutation
* containing transforms has been acknowledged by the server.
*
* @param fieldTransforms - The field transforms to apply the result to.
* @param mutableDocument - The current state of the document after applying all
* previous mutations.
* @param serverTransformResults - The transform results received by the server.
* @returns The transform results list.
*/;
}
function or(t, e, n) {
var r = new Map;
U(t.length === n.length);
for (var i = 0; i < n.length; i++) {
var o = t[i], u = o.transform, a = e.data.field(o.field);
r.set(o.field, Mn(u, a, n[i]));
}
return r;
}
/**
* Creates a list of "transform results" (a transform result is a field value
* representing the result of applying a transform) for use when applying a
* transform locally.
*
* @param fieldTransforms - The field transforms to apply the result to.
* @param localWriteTime - The local time of the mutation (used to
* generate ServerTimestampValues).
* @param mutableDocument - The document to apply transforms on.
* @returns The transform results list.
*/ function ur(t, e, n) {
for (var r = new Map, i = 0, o = t; i < o.length; i++) {
var u = o[i], a = u.transform, s = n.data.field(u.field);
r.set(u.field, Vn(a, s, e));
}
return r;
}
/** A mutation that deletes the document at the given key. */ var ar, sr, cr = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this) || this).key = t, r.precondition = n, r.type = 2 /* MutationType.Delete */ ,
r.fieldTransforms = [], r;
}
return t(n, e), n.prototype.getFieldMask = function() {
return null;
}, n;
}(Xn), lr = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this) || this).key = t, r.precondition = n, r.type = 3 /* MutationType.Verify */ ,
r.fieldTransforms = [], r;
}
return t(n, e), n.prototype.getFieldMask = function() {
return null;
}, n;
}(Xn), hr =
// TODO(b/33078163): just use simplest form of existence filter for now
function(t) {
this.count = t;
};
/**
* Determines whether an error code represents a permanent error when received
* in response to a non-write operation.
*
* See isPermanentWriteError for classifying write errors.
*/
function fr(t) {
switch (t) {
default:
return q();
case K.CANCELLED:
case K.UNKNOWN:
case K.DEADLINE_EXCEEDED:
case K.RESOURCE_EXHAUSTED:
case K.INTERNAL:
case K.UNAVAILABLE:
// Unauthenticated means something went wrong with our token and we need
// to retry with new credentials which will happen automatically.
case K.UNAUTHENTICATED:
return !1;
case K.INVALID_ARGUMENT:
case K.NOT_FOUND:
case K.ALREADY_EXISTS:
case K.PERMISSION_DENIED:
case K.FAILED_PRECONDITION:
// Aborted might be retried in some scenarios, but that is dependant on
// the context and should handled individually by the calling code.
// See https://cloud.google.com/apis/design/errors.
case K.ABORTED:
case K.OUT_OF_RANGE:
case K.UNIMPLEMENTED:
case K.DATA_LOSS:
return !0;
}
}
/**
* Determines whether an error code represents a permanent error when received
* in response to a write operation.
*
* Write operations must be handled specially because as of b/119437764, ABORTED
* errors on the write stream should be retried too (even though ABORTED errors
* are not generally retryable).
*
* Note that during the initial handshake on the write stream an ABORTED error
* signals that we should discard our stream token (i.e. it is permanent). This
* means a handshake error should be classified with isPermanentError, above.
*/
/**
* Maps an error Code from GRPC status code number, like 0, 1, or 14. These
* are not the same as HTTP status codes.
*
* @returns The Code equivalent to the given GRPC status code. Fails if there
* is no match.
*/ function dr(t) {
if (void 0 === t)
// This shouldn't normally happen, but in certain error cases (like trying
// to send invalid proto messages) we may get an error with no GRPC code.
return M("GRPC error has no .code"), K.UNKNOWN;
switch (t) {
case ar.OK:
return K.OK;
case ar.CANCELLED:
return K.CANCELLED;
case ar.UNKNOWN:
return K.UNKNOWN;
case ar.DEADLINE_EXCEEDED:
return K.DEADLINE_EXCEEDED;
case ar.RESOURCE_EXHAUSTED:
return K.RESOURCE_EXHAUSTED;
case ar.INTERNAL:
return K.INTERNAL;
case ar.UNAVAILABLE:
return K.UNAVAILABLE;
case ar.UNAUTHENTICATED:
return K.UNAUTHENTICATED;
case ar.INVALID_ARGUMENT:
return K.INVALID_ARGUMENT;
case ar.NOT_FOUND:
return K.NOT_FOUND;
case ar.ALREADY_EXISTS:
return K.ALREADY_EXISTS;
case ar.PERMISSION_DENIED:
return K.PERMISSION_DENIED;
case ar.FAILED_PRECONDITION:
return K.FAILED_PRECONDITION;
case ar.ABORTED:
return K.ABORTED;
case ar.OUT_OF_RANGE:
return K.OUT_OF_RANGE;
case ar.UNIMPLEMENTED:
return K.UNIMPLEMENTED;
case ar.DATA_LOSS:
return K.DATA_LOSS;
default:
return q();
}
}
/**
* Converts an HTTP response's error status to the equivalent error code.
*
* @param status - An HTTP error response status ("FAILED_PRECONDITION",
* "UNKNOWN", etc.)
* @returns The equivalent Code. Non-matching responses are mapped to
* Code.UNKNOWN.
*/ (sr = ar || (ar = {}))[sr.OK = 0] = "OK", sr[sr.CANCELLED = 1] = "CANCELLED",
sr[sr.UNKNOWN = 2] = "UNKNOWN", sr[sr.INVALID_ARGUMENT = 3] = "INVALID_ARGUMENT",
sr[sr.DEADLINE_EXCEEDED = 4] = "DEADLINE_EXCEEDED", sr[sr.NOT_FOUND = 5] = "NOT_FOUND",
sr[sr.ALREADY_EXISTS = 6] = "ALREADY_EXISTS", sr[sr.PERMISSION_DENIED = 7] = "PERMISSION_DENIED",
sr[sr.UNAUTHENTICATED = 16] = "UNAUTHENTICATED", sr[sr.RESOURCE_EXHAUSTED = 8] = "RESOURCE_EXHAUSTED",
sr[sr.FAILED_PRECONDITION = 9] = "FAILED_PRECONDITION", sr[sr.ABORTED = 10] = "ABORTED",
sr[sr.OUT_OF_RANGE = 11] = "OUT_OF_RANGE", sr[sr.UNIMPLEMENTED = 12] = "UNIMPLEMENTED",
sr[sr.INTERNAL = 13] = "INTERNAL", sr[sr.UNAVAILABLE = 14] = "UNAVAILABLE", sr[sr.DATA_LOSS = 15] = "DATA_LOSS";
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A map implementation that uses objects as keys. Objects must have an
* associated equals function and must be immutable. Entries in the map are
* stored together with the key being produced from the mapKeyFn. This map
* automatically handles collisions of keys.
*/
var pr = /** @class */ function() {
function t(t, e) {
this.mapKeyFn = t, this.equalsFn = e,
/**
* The inner map for a key/value pair. Due to the possibility of collisions we
* keep a list of entries that we do a linear search through to find an actual
* match. Note that collisions should be rare, so we still expect near
* constant time lookups in practice.
*/
this.inner = {},
/** The number of entries stored in the map */
this.innerSize = 0
/** Get a value for this key, or undefined if it does not exist. */;
}
return t.prototype.get = function(t) {
var e = this.mapKeyFn(t), n = this.inner[e];
if (void 0 !== n) for (var r = 0, i = n; r < i.length; r++) {
var o = i[r], u = o[0], a = o[1];
if (this.equalsFn(u, t)) return a;
}
}, t.prototype.has = function(t) {
return void 0 !== this.get(t);
},
/** Put this key and value in the map. */ t.prototype.set = function(t, e) {
var n = this.mapKeyFn(t), r = this.inner[n];
if (void 0 === r) return this.inner[n] = [ [ t, e ] ], void this.innerSize++;
for (var i = 0; i < r.length; i++) if (this.equalsFn(r[i][0], t))
// This is updating an existing entry and does not increase `innerSize`.
return void (r[i] = [ t, e ]);
r.push([ t, e ]), this.innerSize++;
},
/**
* Remove this key from the map. Returns a boolean if anything was deleted.
*/
t.prototype.delete = function(t) {
var e = this.mapKeyFn(t), n = this.inner[e];
if (void 0 === n) return !1;
for (var r = 0; r < n.length; r++) if (this.equalsFn(n[r][0], t)) return 1 === n.length ? delete this.inner[e] : n.splice(r, 1),
this.innerSize--, !0;
return !1;
}, t.prototype.forEach = function(t) {
Kt(this.inner, (function(e, n) {
for (var r = 0, i = n; r < i.length; r++) {
var o = i[r], u = o[0], a = o[1];
t(u, a);
}
}));
}, t.prototype.isEmpty = function() {
return jt(this.inner);
}, t.prototype.size = function() {
return this.innerSize;
}, t;
}(), yr = new He(ft.comparator);
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ function vr() {
return yr;
}
var mr = new He(ft.comparator);
function gr() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
for (var n = mr, r = 0, i = t; r < i.length; r++) {
var o = i[r];
n = n.insert(o.key, o);
}
return n;
}
function wr(t) {
var e = mr;
return t.forEach((function(t, n) {
return e = e.insert(t, n.overlayedDocument);
})), e;
}
function br() {
return Tr();
}
function Ir() {
return Tr();
}
function Tr() {
return new pr((function(t) {
return t.toString();
}), (function(t, e) {
return t.isEqual(e);
}));
}
var Er = new He(ft.comparator), Sr = new Ze(ft.comparator);
function _r() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
for (var n = Sr, r = 0, i = t; r < i.length; r++) {
var o = i[r];
n = n.add(o);
}
return n;
}
var Dr = new Ze(rt);
function xr() {
return Dr;
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* An event from the RemoteStore. It is split into targetChanges (changes to the
* state or the set of documents in our watched targets) and documentUpdates
* (changes to the actual documents).
*/ var Ar = /** @class */ function() {
function t(
/**
* The snapshot version this event brings us up to, or MIN if not set.
*/
t,
/**
* A map from target to changes to the target. See TargetChange.
*/
e,
/**
* A set of targets that is known to be inconsistent. Listens for these
* targets should be re-established without resume tokens.
*/
n,
/**
* A set of which documents have changed or been deleted, along with the
* doc's new values (if not deleted).
*/
r,
/**
* A set of which document updates are due only to limbo resolution targets.
*/
i) {
this.snapshotVersion = t, this.targetChanges = e, this.targetMismatches = n, this.documentUpdates = r,
this.resolvedLimboDocuments = i;
}
/**
* HACK: Views require RemoteEvents in order to determine whether the view is
* CURRENT, but secondary tabs don't receive remote events. So this method is
* used to create a synthesized RemoteEvent that can be used to apply a
* CURRENT status change to a View, for queries executed in a different tab.
*/
// PORTING NOTE: Multi-tab only
return t.createSynthesizedRemoteEventForCurrentChange = function(e, n, r) {
var i = new Map;
return i.set(e, Cr.createSynthesizedTargetChangeForCurrentChange(e, n, r)), new t(at.min(), i, xr(), vr(), _r());
}, t;
}(), Cr = /** @class */ function() {
function t(
/**
* An opaque, server-assigned token that allows watching a query to be resumed
* after disconnecting without retransmitting all the data that matches the
* query. The resume token essentially identifies a point in time from which
* the server should resume sending results.
*/
t,
/**
* The "current" (synced) status of this target. Note that "current"
* has special meaning in the RPC protocol that implies that a target is
* both up-to-date and consistent with the rest of the watch stream.
*/
e,
/**
* The set of documents that were newly assigned to this target as part of
* this remote event.
*/
n,
/**
* The set of documents that were already assigned to this target but received
* an update during this remote event.
*/
r,
/**
* The set of documents that were removed from this target as part of this
* remote event.
*/
i) {
this.resumeToken = t, this.current = e, this.addedDocuments = n, this.modifiedDocuments = r,
this.removedDocuments = i
/**
* This method is used to create a synthesized TargetChanges that can be used to
* apply a CURRENT status change to a View (for queries executed in a different
* tab) or for new queries (to raise snapshots with correct CURRENT status).
*/;
}
return t.createSynthesizedTargetChangeForCurrentChange = function(e, n, r) {
return new t(r, n, _r(), _r(), _r());
}, t;
}(), Nr = function(
/** The new document applies to all of these targets. */
t,
/** The new document is removed from all of these targets. */
e,
/** The key of the document for this change. */
n,
/**
* The new document or NoDocument if it was deleted. Is null if the
* document went out of view without the server sending a new document.
*/
r) {
this.It = t, this.removedTargetIds = e, this.key = n, this.Tt = r;
}, kr = function(t, e) {
this.targetId = t, this.Et = e;
}, Fr = function(
/** What kind of change occurred to the watch target. */
t,
/** The target IDs that were added/removed/set. */
e,
/**
* An opaque, server-assigned token that allows watching a target to be
* resumed after disconnecting without retransmitting all the data that
* matches the target. The resume token essentially identifies a point in
* time from which the server should resume sending results.
*/
n
/** An RPC error indicating why the watch failed. */ , r) {
void 0 === n && (n = Yt.EMPTY_BYTE_STRING), void 0 === r && (r = null), this.state = t,
this.targetIds = e, this.resumeToken = n, this.cause = r;
}, Or = /** @class */ function() {
function t() {
/**
* The number of pending responses (adds or removes) that we are waiting on.
* We only consider targets active that have no pending responses.
*/
this.At = 0,
/**
* Keeps track of the document changes since the last raised snapshot.
*
* These changes are continuously updated as we receive document updates and
* always reflect the current set of changes against the last issued snapshot.
*/
this.Rt = Mr(),
/** See public getters for explanations of these fields. */
this.bt = Yt.EMPTY_BYTE_STRING, this.Pt = !1,
/**
* Whether this target state should be included in the next snapshot. We
* initialize to true so that newly-added targets are included in the next
* RemoteEvent.
*/
this.vt = !0;
}
return Object.defineProperty(t.prototype, "current", {
/**
* Whether this target has been marked 'current'.
*
* 'Current' has special meaning in the RPC protocol: It implies that the
* Watch backend has sent us all changes up to the point at which the target
* was added and that the target is consistent with the rest of the watch
* stream.
*/
get: function() {
return this.Pt;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "resumeToken", {
/** The last resume token sent to us for this target. */ get: function() {
return this.bt;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "Vt", {
/** Whether this target has pending target adds or target removes. */ get: function() {
return 0 !== this.At;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "St", {
/** Whether we have modified any state that should trigger a snapshot. */ get: function() {
return this.vt;
},
enumerable: !1,
configurable: !0
}),
/**
* Applies the resume token to the TargetChange, but only when it has a new
* value. Empty resumeTokens are discarded.
*/
t.prototype.Dt = function(t) {
t.approximateByteSize() > 0 && (this.vt = !0, this.bt = t);
},
/**
* Creates a target change from the current set of changes.
*
* To reset the document changes after raising this snapshot, call
* `clearPendingChanges()`.
*/
t.prototype.Ct = function() {
var t = _r(), e = _r(), n = _r();
return this.Rt.forEach((function(r, i) {
switch (i) {
case 0 /* ChangeType.Added */ :
t = t.add(r);
break;
case 2 /* ChangeType.Modified */ :
e = e.add(r);
break;
case 1 /* ChangeType.Removed */ :
n = n.add(r);
break;
default:
q();
}
})), new Cr(this.bt, this.Pt, t, e, n);
},
/**
* Resets the document changes and sets `hasPendingChanges` to false.
*/
t.prototype.xt = function() {
this.vt = !1, this.Rt = Mr();
}, t.prototype.Nt = function(t, e) {
this.vt = !0, this.Rt = this.Rt.insert(t, e);
}, t.prototype.kt = function(t) {
this.vt = !0, this.Rt = this.Rt.remove(t);
}, t.prototype.Ot = function() {
this.At += 1;
}, t.prototype.Mt = function() {
this.At -= 1;
}, t.prototype.Ft = function() {
this.vt = !0, this.Pt = !0;
}, t;
}(), Rr = /** @class */ function() {
function t(t) {
this.$t = t,
/** The internal state of all tracked targets. */
this.Bt = new Map,
/** Keeps track of the documents to update since the last raised snapshot. */
this.Lt = vr(),
/** A mapping of document keys to their set of target IDs. */
this.qt = Vr(),
/**
* A list of targets with existence filter mismatches. These targets are
* known to be inconsistent and their listens needs to be re-established by
* RemoteStore.
*/
this.Ut = new Ze(rt)
/**
* Processes and adds the DocumentWatchChange to the current set of changes.
*/;
}
return t.prototype.Kt = function(t) {
for (var e = 0, n = t.It; e < n.length; e++) {
var r = n[e];
t.Tt && t.Tt.isFoundDocument() ? this.Gt(r, t.Tt) : this.Qt(r, t.key, t.Tt);
}
for (var i = 0, o = t.removedTargetIds; i < o.length; i++) {
var u = o[i];
this.Qt(u, t.key, t.Tt);
}
},
/** Processes and adds the WatchTargetChange to the current set of changes. */ t.prototype.jt = function(t) {
var e = this;
this.forEachTarget(t, (function(n) {
var r = e.Wt(n);
switch (t.state) {
case 0 /* WatchTargetChangeState.NoChange */ :
e.zt(n) && r.Dt(t.resumeToken);
break;
case 1 /* WatchTargetChangeState.Added */ :
// We need to decrement the number of pending acks needed from watch
// for this targetId.
r.Mt(), r.Vt ||
// We have a freshly added target, so we need to reset any state
// that we had previously. This can happen e.g. when remove and add
// back a target for existence filter mismatches.
r.xt(), r.Dt(t.resumeToken);
break;
case 2 /* WatchTargetChangeState.Removed */ :
// We need to keep track of removed targets to we can post-filter and
// remove any target changes.
// We need to decrement the number of pending acks needed from watch
// for this targetId.
r.Mt(), r.Vt || e.removeTarget(n);
break;
case 3 /* WatchTargetChangeState.Current */ :
e.zt(n) && (r.Ft(), r.Dt(t.resumeToken));
break;
case 4 /* WatchTargetChangeState.Reset */ :
e.zt(n) && (
// Reset the target and synthesizes removes for all existing
// documents. The backend will re-add any documents that still
// match the target before it sends the next global snapshot.
e.Ht(n), r.Dt(t.resumeToken));
break;
default:
q();
}
}));
},
/**
* Iterates over all targetIds that the watch change applies to: either the
* targetIds explicitly listed in the change or the targetIds of all currently
* active targets.
*/
t.prototype.forEachTarget = function(t, e) {
var n = this;
t.targetIds.length > 0 ? t.targetIds.forEach(e) : this.Bt.forEach((function(t, r) {
n.zt(r) && e(r);
}));
},
/**
* Handles existence filters and synthesizes deletes for filter mismatches.
* Targets that are invalidated by filter mismatches are added to
* `pendingTargetResets`.
*/
t.prototype.Jt = function(t) {
var e = t.targetId, n = t.Et.count, r = this.Yt(e);
if (r) {
var i = r.target;
if (cn(i)) if (0 === n) {
// The existence filter told us the document does not exist. We deduce
// that this document does not exist and apply a deleted document to
// our updates. Without applying this deleted document there might be
// another query that will raise this document as part of a snapshot
// until it is resolved, essentially exposing inconsistency between
// queries.
var o = new ft(i.path);
this.Qt(e, o, rn.newNoDocument(o, at.min()));
} else U(1 === n); else this.Xt(e) !== n && (
// Existence filter mismatch: We reset the mapping and raise a new
// snapshot with `isFromCache:true`.
this.Ht(e), this.Ut = this.Ut.add(e));
}
},
/**
* Converts the currently accumulated state into a remote event at the
* provided snapshot version. Resets the accumulated changes before returning.
*/
t.prototype.Zt = function(t) {
var e = this, n = new Map;
this.Bt.forEach((function(r, i) {
var o = e.Yt(i);
if (o) {
if (r.current && cn(o.target)) {
// Document queries for document that don't exist can produce an empty
// result set. To update our local cache, we synthesize a document
// delete if we have not previously received the document. This
// resolves the limbo state of the document, removing it from
// limboDocumentRefs.
// TODO(dimond): Ideally we would have an explicit lookup target
// instead resulting in an explicit delete message and we could
// remove this special logic.
var u = new ft(o.target.path);
null !== e.Lt.get(u) || e.te(i, u) || e.Qt(i, u, rn.newNoDocument(u, t));
}
r.St && (n.set(i, r.Ct()), r.xt());
}
}));
var r = _r();
// We extract the set of limbo-only document updates as the GC logic
// special-cases documents that do not appear in the target cache.
// TODO(gsoltis): Expand on this comment once GC is available in the JS
// client.
this.qt.forEach((function(t, n) {
var i = !0;
n.forEachWhile((function(t) {
var n = e.Yt(t);
return !n || 2 /* TargetPurpose.LimboResolution */ === n.purpose || (i = !1, !1);
})), i && (r = r.add(t));
})), this.Lt.forEach((function(e, n) {
return n.setReadTime(t);
}));
var i = new Ar(t, n, this.Ut, this.Lt, r);
return this.Lt = vr(), this.qt = Vr(), this.Ut = new Ze(rt), i;
},
/**
* Adds the provided document to the internal list of document updates and
* its document key to the given target's mapping.
*/
// Visible for testing.
t.prototype.Gt = function(t, e) {
if (this.zt(t)) {
var n = this.te(t, e.key) ? 2 /* ChangeType.Modified */ : 0 /* ChangeType.Added */;
this.Wt(t).Nt(e.key, n), this.Lt = this.Lt.insert(e.key, e), this.qt = this.qt.insert(e.key, this.ee(e.key).add(t));
}
},
/**
* Removes the provided document from the target mapping. If the
* document no longer matches the target, but the document's state is still
* known (e.g. we know that the document was deleted or we received the change
* that caused the filter mismatch), the new document can be provided
* to update the remote document cache.
*/
// Visible for testing.
t.prototype.Qt = function(t, e, n) {
if (this.zt(t)) {
var r = this.Wt(t);
this.te(t, e) ? r.Nt(e, 1 /* ChangeType.Removed */) :
// The document may have entered and left the target before we raised a
// snapshot, so we can just ignore the change.
r.kt(e), this.qt = this.qt.insert(e, this.ee(e).delete(t)), n && (this.Lt = this.Lt.insert(e, n));
}
}, t.prototype.removeTarget = function(t) {
this.Bt.delete(t);
},
/**
* Returns the current count of documents in the target. This includes both
* the number of documents that the LocalStore considers to be part of the
* target as well as any accumulated changes.
*/
t.prototype.Xt = function(t) {
var e = this.Wt(t).Ct();
return this.$t.getRemoteKeysForTarget(t).size + e.addedDocuments.size - e.removedDocuments.size;
},
/**
* Increment the number of acks needed from watch before we can consider the
* server to be 'in-sync' with the client's active targets.
*/
t.prototype.Ot = function(t) {
this.Wt(t).Ot();
}, t.prototype.Wt = function(t) {
var e = this.Bt.get(t);
return e || (e = new Or, this.Bt.set(t, e)), e;
}, t.prototype.ee = function(t) {
var e = this.qt.get(t);
return e || (e = new Ze(rt), this.qt = this.qt.insert(t, e)), e;
},
/**
* Verifies that the user is still interested in this target (by calling
* `getTargetDataForTarget()`) and that we are not waiting for pending ADDs
* from watch.
*/
t.prototype.zt = function(t) {
var e = null !== this.Yt(t);
return e || V("WatchChangeAggregator", "Detected inactive target", t), e;
},
/**
* Returns the TargetData for an active target (i.e. a target that the user
* is still interested in that has no outstanding target change requests).
*/
t.prototype.Yt = function(t) {
var e = this.Bt.get(t);
return e && e.Vt ? null : this.$t.ne(t);
},
/**
* Resets the state of a Watch target to its initial state (e.g. sets
* 'current' to false, clears the resume token and removes its target mapping
* from all documents).
*/
t.prototype.Ht = function(t) {
var e = this;
this.Bt.set(t, new Or), this.$t.getRemoteKeysForTarget(t).forEach((function(n) {
e.Qt(t, n, /*updatedDocument=*/ null);
}));
},
/**
* Returns whether the LocalStore considers the document to be part of the
* specified target.
*/
t.prototype.te = function(t, e) {
return this.$t.getRemoteKeysForTarget(t).has(e);
}, t;
}();
/**
* A TargetChange specifies the set of changes for a specific target as part of
* a RemoteEvent. These changes track which documents are added, modified or
* removed, as well as the target's resume token and whether the target is
* marked CURRENT.
* The actual changes *to* documents are not part of the TargetChange since
* documents may be part of multiple targets.
*/ function Vr() {
return new He(ft.comparator);
}
function Mr() {
return new He(ft.comparator);
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var Lr = {
asc: "ASCENDING",
desc: "DESCENDING"
}, Pr = {
"<": "LESS_THAN",
"<=": "LESS_THAN_OR_EQUAL",
">": "GREATER_THAN",
">=": "GREATER_THAN_OR_EQUAL",
"==": "EQUAL",
"!=": "NOT_EQUAL",
"array-contains": "ARRAY_CONTAINS",
in: "IN",
"not-in": "NOT_IN",
"array-contains-any": "ARRAY_CONTAINS_ANY"
}, qr = {
and: "AND",
or: "OR"
}, Ur = function(t, e) {
this.databaseId = t, this.wt = e;
};
/**
* This class generates JsonObject values for the Datastore API suitable for
* sending to either GRPC stub methods or via the JSON/HTTP REST API.
*
* The serializer supports both Protobuf.js and Proto3 JSON formats. By
* setting `useProto3Json` to true, the serializer will use the Proto3 JSON
* format.
*
* For a description of the Proto3 JSON format check
* https://developers.google.com/protocol-buffers/docs/proto3#json
*
* TODO(klimt): We can remove the databaseId argument if we keep the full
* resource name in documents.
*/
/**
* Returns a value for a Date that's appropriate to put into a proto.
*/
function Br(t, e) {
return t.wt ? "".concat(new Date(1e3 * e.seconds).toISOString().replace(/\.\d*/, "").replace("Z", ""), ".").concat(("000000000" + e.nanoseconds).slice(-9), "Z") : {
seconds: "" + e.seconds,
nanos: e.nanoseconds
};
}
/**
* Returns a value for bytes that's appropriate to put in a proto.
*
* Visible for testing.
*/ function Gr(t, e) {
return t.wt ? e.toBase64() : e.toUint8Array();
}
/**
* Returns a ByteString based on the proto string value.
*/ function Kr(t, e) {
return Br(t, e.toTimestamp());
}
function jr(t) {
return U(!!t), at.fromTimestamp(function(t) {
var e = Zt(t);
return new ut(e.seconds, e.nanos);
}(t));
}
function Qr(t, e) {
return function(t) {
return new ct([ "projects", t.projectId, "databases", t.database ]);
}(t).child("documents").child(e).canonicalString();
}
function zr(t) {
var e = ct.fromString(t);
return U(pi(e)), e;
}
function Wr(t, e) {
return Qr(t.databaseId, e.path);
}
function Hr(t, e) {
var n = zr(e);
if (n.get(1) !== t.databaseId.projectId) throw new j(K.INVALID_ARGUMENT, "Tried to deserialize key from different project: " + n.get(1) + " vs " + t.databaseId.projectId);
if (n.get(3) !== t.databaseId.database) throw new j(K.INVALID_ARGUMENT, "Tried to deserialize key from different database: " + n.get(3) + " vs " + t.databaseId.database);
return new ft(Jr(n));
}
function Yr(t, e) {
return Qr(t.databaseId, e);
}
function Xr(t) {
var e = zr(t);
// In v1beta1 queries for collections at the root did not have a trailing
// "/documents". In v1 all resource paths contain "/documents". Preserve the
// ability to read the v1beta1 form for compatibility with queries persisted
// in the local target cache.
return 4 === e.length ? ct.emptyPath() : Jr(e);
}
function Zr(t) {
return new ct([ "projects", t.databaseId.projectId, "databases", t.databaseId.database ]).canonicalString();
}
function Jr(t) {
return U(t.length > 4 && "documents" === t.get(4)), t.popFirst(5)
/** Creates a Document proto from key and fields (but no create/update time) */;
}
function $r(t, e, n) {
return {
name: Wr(t, e),
fields: n.value.mapValue.fields
};
}
function ti(t, e, n) {
var r = Hr(t, e.name), i = jr(e.updateTime), o = e.createTime ? jr(e.createTime) : at.min(), u = new en({
mapValue: {
fields: e.fields
}
}), a = rn.newFoundDocument(r, i, o, u);
return n && a.setHasCommittedMutations(), n ? a.setHasCommittedMutations() : a;
}
function ei(t, e) {
var n;
if (e instanceof nr) n = {
update: $r(t, e.key, e.value)
}; else if (e instanceof cr) n = {
delete: Wr(t, e.key)
}; else if (e instanceof rr) n = {
update: $r(t, e.key, e.data),
updateMask: di(e.fieldMask)
}; else {
if (!(e instanceof lr)) return q();
n = {
verify: Wr(t, e.key)
};
}
return e.fieldTransforms.length > 0 && (n.updateTransforms = e.fieldTransforms.map((function(t) {
return function(t, e) {
var n = e.transform;
if (n instanceof Pn) return {
fieldPath: e.field.canonicalString(),
setToServerValue: "REQUEST_TIME"
};
if (n instanceof qn) return {
fieldPath: e.field.canonicalString(),
appendMissingElements: {
values: n.elements
}
};
if (n instanceof Bn) return {
fieldPath: e.field.canonicalString(),
removeAllFromArray: {
values: n.elements
}
};
if (n instanceof Kn) return {
fieldPath: e.field.canonicalString(),
increment: n.gt
};
throw q();
}(0, t);
}))), e.precondition.isNone || (n.currentDocument = function(t, e) {
return void 0 !== e.updateTime ? {
updateTime: Kr(t, e.updateTime)
} : void 0 !== e.exists ? {
exists: e.exists
} : q();
}(t, e.precondition)), n;
}
function ni(t, e) {
var n = e.currentDocument ? function(t) {
return void 0 !== t.updateTime ? Hn.updateTime(jr(t.updateTime)) : void 0 !== t.exists ? Hn.exists(t.exists) : Hn.none();
}(e.currentDocument) : Hn.none(), r = e.updateTransforms ? e.updateTransforms.map((function(e) {
return function(t, e) {
var n = null;
if ("setToServerValue" in e) U("REQUEST_TIME" === e.setToServerValue), n = new Pn; else if ("appendMissingElements" in e) {
var r = e.appendMissingElements.values || [];
n = new qn(r);
} else if ("removeAllFromArray" in e) {
var i = e.removeAllFromArray.values || [];
n = new Bn(i);
} else "increment" in e ? n = new Kn(t, e.increment) : q();
var o = ht.fromServerFormat(e.fieldPath);
return new zn(o, n);
}(t, e);
})) : [];
if (e.update) {
e.update.name;
var i = Hr(t, e.update.name), o = new en({
mapValue: {
fields: e.update.fields
}
});
if (e.updateMask) {
var u = function(t) {
var e = t.fieldPaths || [];
return new tn(e.map((function(t) {
return ht.fromServerFormat(t);
})));
}(e.updateMask);
return new rr(i, o, u, n, r);
}
return new nr(i, o, n, r);
}
if (e.delete) {
var a = Hr(t, e.delete);
return new cr(a, n);
}
if (e.verify) {
var s = Hr(t, e.verify);
return new lr(s, n);
}
return q();
}
function ri(t, e) {
return {
documents: [ Yr(t, e.path) ]
};
}
function ii(t, e) {
// Dissect the path into parent, collectionId, and optional key filter.
var n = {
structuredQuery: {}
}, r = e.path;
null !== e.collectionGroup ? (n.parent = Yr(t, r), n.structuredQuery.from = [ {
collectionId: e.collectionGroup,
allDescendants: !0
} ]) : (n.parent = Yr(t, r.popLast()), n.structuredQuery.from = [ {
collectionId: r.lastSegment()
} ]);
var i = function(t) {
if (0 !== t.length) return fi(Ce.create(t, "and" /* CompositeOperator.AND */));
}(e.filters);
i && (n.structuredQuery.where = i);
var o = function(t) {
if (0 !== t.length) return t.map((function(t) {
// visible for testing
return function(t) {
return {
field: li(t.field),
direction: ai(t.dir)
};
}(t);
}));
}(e.orderBy);
o && (n.structuredQuery.orderBy = o);
var u, a = function(t, e) {
return t.wt || Qt(e) ? e : {
value: e
};
}(t, e.limit);
return null !== a && (n.structuredQuery.limit = a), e.startAt && (n.structuredQuery.startAt = {
before: (u = e.startAt).inclusive,
values: u.position
}), e.endAt && (n.structuredQuery.endAt = function(t) {
return {
before: !t.inclusive,
values: t.position
};
}(e.endAt)), n;
}
function oi(t) {
var e = Xr(t.parent), n = t.structuredQuery, r = n.from ? n.from.length : 0, i = null;
if (r > 0) {
U(1 === r);
var o = n.from[0];
o.allDescendants ? i = o.collectionId : e = e.child(o.collectionId);
}
var u = [];
n.where && (u = function(t) {
var e = ui(t);
return e instanceof Ce && Fe(e) ? e.getFilters() : [ e ];
}(n.where));
var a = [];
n.orderBy && (a = n.orderBy.map((function(t) {
return function(t) {
return new ze(hi(t.field),
// visible for testing
function(t) {
switch (t) {
case "ASCENDING":
return "asc" /* Direction.ASCENDING */;
case "DESCENDING":
return "desc" /* Direction.DESCENDING */;
default:
return;
}
}(t.direction));
}(t);
})));
var s = null;
n.limit && (s = function(t) {
var e;
return Qt(e = "object" == typeof t ? t.value : t) ? null : e;
}(n.limit));
var c = null;
n.startAt && (c = function(t) {
var e = !!t.before, n = t.values || [];
return new Se(n, e);
}(n.startAt));
var l = null;
return n.endAt && (l = function(t) {
var e = !t.before, n = t.values || [];
return new Se(n, e);
}(n.endAt)), pn(e, i, a, u, s, "F" /* LimitType.First */ , c, l);
}
function ui(t) {
return void 0 !== t.unaryFilter ? function(t) {
switch (t.unaryFilter.op) {
case "IS_NAN":
var e = hi(t.unaryFilter.field);
return Ae.create(e, "==" /* Operator.EQUAL */ , {
doubleValue: NaN
});
case "IS_NULL":
var n = hi(t.unaryFilter.field);
return Ae.create(n, "==" /* Operator.EQUAL */ , {
nullValue: "NULL_VALUE"
});
case "IS_NOT_NAN":
var r = hi(t.unaryFilter.field);
return Ae.create(r, "!=" /* Operator.NOT_EQUAL */ , {
doubleValue: NaN
});
case "IS_NOT_NULL":
var i = hi(t.unaryFilter.field);
return Ae.create(i, "!=" /* Operator.NOT_EQUAL */ , {
nullValue: "NULL_VALUE"
});
default:
return q();
}
}(t) : void 0 !== t.fieldFilter ? function(t) {
return Ae.create(hi(t.fieldFilter.field), function(t) {
switch (t) {
case "EQUAL":
return "==" /* Operator.EQUAL */;
case "NOT_EQUAL":
return "!=" /* Operator.NOT_EQUAL */;
case "GREATER_THAN":
return ">" /* Operator.GREATER_THAN */;
case "GREATER_THAN_OR_EQUAL":
return ">=" /* Operator.GREATER_THAN_OR_EQUAL */;
case "LESS_THAN":
return "<" /* Operator.LESS_THAN */;
case "LESS_THAN_OR_EQUAL":
return "<=" /* Operator.LESS_THAN_OR_EQUAL */;
case "ARRAY_CONTAINS":
return "array-contains" /* Operator.ARRAY_CONTAINS */;
case "IN":
return "in" /* Operator.IN */;
case "NOT_IN":
return "not-in" /* Operator.NOT_IN */;
case "ARRAY_CONTAINS_ANY":
return "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */;
default:
return q();
}
}(t.fieldFilter.op), t.fieldFilter.value);
}(t) : void 0 !== t.compositeFilter ? function(t) {
return Ce.create(t.compositeFilter.filters.map((function(t) {
return ui(t);
})), function(t) {
switch (t) {
case "AND":
return "and" /* CompositeOperator.AND */;
case "OR":
return "or" /* CompositeOperator.OR */;
default:
return q();
}
}(t.compositeFilter.op));
}(t) : q();
}
function ai(t) {
return Lr[t];
}
function si(t) {
return Pr[t];
}
function ci(t) {
return qr[t];
}
function li(t) {
return {
fieldPath: t.canonicalString()
};
}
function hi(t) {
return ht.fromServerFormat(t.fieldPath);
}
function fi(t) {
return t instanceof Ae ? function(t) {
if ("==" /* Operator.EQUAL */ === t.op) {
if (ve(t.value)) return {
unaryFilter: {
field: li(t.field),
op: "IS_NAN"
}
};
if (ye(t.value)) return {
unaryFilter: {
field: li(t.field),
op: "IS_NULL"
}
};
} else if ("!=" /* Operator.NOT_EQUAL */ === t.op) {
if (ve(t.value)) return {
unaryFilter: {
field: li(t.field),
op: "IS_NOT_NAN"
}
};
if (ye(t.value)) return {
unaryFilter: {
field: li(t.field),
op: "IS_NOT_NULL"
}
};
}
return {
fieldFilter: {
field: li(t.field),
op: si(t.op),
value: t.value
}
};
}(t) : t instanceof Ce ? function(t) {
var e = t.getFilters().map((function(t) {
return fi(t);
}));
return 1 === e.length ? e[0] : {
compositeFilter: {
op: ci(t.op),
filters: e
}
};
}(t) : q();
}
function di(t) {
var e = [];
return t.fields.forEach((function(t) {
return e.push(t.canonicalString());
})), {
fieldPaths: e
};
}
function pi(t) {
// Resource names have at least 4 components (project ID, database ID)
return t.length >= 4 && "projects" === t.get(0) && "databases" === t.get(2);
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Encodes a resource path into a IndexedDb-compatible string form.
*/ function yi(t) {
for (var e = "", n = 0; n < t.length; n++) e.length > 0 && (e = mi(e)), e = vi(t.get(n), e);
return mi(e);
}
/** Encodes a single segment of a resource path into the given result */ function vi(t, e) {
for (var n = e, r = t.length, i = 0; i < r; i++) {
var o = t.charAt(i);
switch (o) {
case "\0":
n += "";
break;
case "":
n += "";
break;
default:
n += o;
}
}
return n;
}
/** Encodes a path separator into the given result */ function mi(t) {
return t + "";
}
/**
* Decodes the given IndexedDb-compatible string form of a resource path into
* a ResourcePath instance. Note that this method is not suitable for use with
* decoding resource names from the server; those are One Platform format
* strings.
*/ function gi(t) {
// Event the empty path must encode as a path of at least length 2. A path
// with exactly 2 must be the empty path.
var e = t.length;
if (U(e >= 2), 2 === e) return U("" === t.charAt(0) && "" === t.charAt(1)), ct.emptyPath();
// Escape characters cannot exist past the second-to-last position in the
// source value.
for (var n = e - 2, r = [], i = "", o = 0; o < e; ) {
// The last two characters of a valid encoded path must be a separator, so
// there must be an end to this segment.
var u = t.indexOf("", o);
switch ((u < 0 || u > n) && q(), t.charAt(u + 1)) {
case "":
var a = t.substring(o, u), s = void 0;
0 === i.length ?
// Avoid copying for the common case of a segment that excludes \0
// and \001
s = a : (s = i += a, i = ""), r.push(s);
break;
case "":
i += t.substring(o, u), i += "\0";
break;
case "":
// The escape character can be used in the output to encode itself.
i += t.substring(o, u + 1);
break;
default:
q();
}
o = u + 2;
}
return new ct(r);
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var wi = [ "userId", "batchId" ];
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Name of the IndexedDb object store.
*
* Note that the name 'owner' is chosen to ensure backwards compatibility with
* older clients that only supported single locked access to the persistence
* layer.
*/
/**
* Creates a [userId, encodedPath] key for use in the DbDocumentMutations
* index to iterate over all at document mutations for a given path or lower.
*/ function bi(t, e) {
return [ t, yi(e) ];
}
/**
* Creates a full index key of [userId, encodedPath, batchId] for inserting
* and deleting into the DbDocumentMutations index.
*/ function Ii(t, e, n) {
return [ t, yi(e), n ];
}
/**
* Because we store all the useful information for this store in the key,
* there is no useful information to store as the value. The raw (unencoded)
* path cannot be stored because IndexedDb doesn't store prototype
* information.
*/ var Ti = {}, Ei = [ "prefixPath", "collectionGroup", "readTime", "documentId" ], Si = [ "prefixPath", "collectionGroup", "documentId" ], _i = [ "collectionGroup", "readTime", "prefixPath", "documentId" ], Di = [ "canonicalId", "targetId" ], xi = [ "targetId", "path" ], Ai = [ "path", "targetId" ], Ci = [ "collectionId", "parent" ], Ni = [ "indexId", "uid" ], ki = [ "uid", "sequenceNumber" ], Fi = [ "indexId", "uid", "arrayValue", "directionalValue", "orderedDocumentKey", "documentKey" ], Oi = [ "indexId", "uid", "orderedDocumentKey" ], Ri = [ "userId", "collectionPath", "documentId" ], Vi = [ "userId", "collectionPath", "largestBatchId" ], Mi = [ "userId", "collectionGroup", "largestBatchId" ], Li = r(r([], r(r([], r(r([], r(r([], [ "mutationQueues", "mutations", "documentMutations", "remoteDocuments", "targets", "owner", "targetGlobal", "targetDocuments" ], !1), [ "clientMetadata" ], !1), !0), [ "remoteDocumentGlobal" ], !1), !0), [ "collectionParents" ], !1), !0), [ "bundles", "namedQueries" ], !1), Pi = r(r([], Li, !0), [ "documentOverlays" ], !1), qi = [ "mutationQueues", "mutations", "documentMutations", "remoteDocumentsV14", "targets", "owner", "targetGlobal", "targetDocuments", "clientMetadata", "remoteDocumentGlobal", "collectionParents", "bundles", "namedQueries", "documentOverlays" ], Ui = qi, Bi = r(r([], Ui, !0), [ "indexConfiguration", "indexState", "indexEntries" ], !1), Gi = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this) || this).se = t, r.currentSequenceNumber = n, r;
}
return t(n, e), n;
}(_t);
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ function Ki(t, e) {
var n = G(t);
return Ct.M(n.se, e);
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A batch of mutations that will be sent as one unit to the backend.
*/ var ji = /** @class */ function() {
/**
* @param batchId - The unique ID of this mutation batch.
* @param localWriteTime - The original write time of this mutation.
* @param baseMutations - Mutations that are used to populate the base
* values when this mutation is applied locally. This can be used to locally
* overwrite values that are persisted in the remote document cache. Base
* mutations are never sent to the backend.
* @param mutations - The user-provided mutations in this mutation batch.
* User-provided mutations are applied both locally and remotely on the
* backend.
*/
function t(t, e, n, r) {
this.batchId = t, this.localWriteTime = e, this.baseMutations = n, this.mutations = r
/**
* Applies all the mutations in this MutationBatch to the specified document
* to compute the state of the remote document
*
* @param document - The document to apply mutations to.
* @param batchResult - The result of applying the MutationBatch to the
* backend.
*/;
}
return t.prototype.applyToRemoteDocument = function(t, e) {
for (var n = e.mutationResults, r = 0; r < this.mutations.length; r++) {
var i = this.mutations[r];
i.key.isEqual(t.key) && Jn(i, t, n[r]);
}
},
/**
* Computes the local view of a document given all the mutations in this
* batch.
*
* @param document - The document to apply mutations to.
* @param mutatedFields - Fields that have been updated before applying this mutation batch.
* @returns A `FieldMask` representing all the fields that are mutated.
*/
t.prototype.applyToLocalView = function(t, e) {
// First, apply the base state. This allows us to apply non-idempotent
// transform against a consistent set of values.
for (var n = 0, r = this.baseMutations; n < r.length; n++) {
var i = r[n];
i.key.isEqual(t.key) && (e = $n(i, t, e, this.localWriteTime));
}
// Second, apply all user-provided mutations.
for (var o = 0, u = this.mutations; o < u.length; o++) {
var a = u[o];
a.key.isEqual(t.key) && (e = $n(a, t, e, this.localWriteTime));
}
return e;
},
/**
* Computes the local view for all provided documents given the mutations in
* this batch. Returns a `DocumentKey` to `Mutation` map which can be used to
* replace all the mutation applications.
*/
t.prototype.applyToLocalDocumentSet = function(t, e) {
var n = this, r = Ir();
// TODO(mrschmidt): This implementation is O(n^2). If we apply the mutations
// directly (as done in `applyToLocalView()`), we can reduce the complexity
// to O(n).
return this.mutations.forEach((function(i) {
var o = t.get(i.key), u = o.overlayedDocument, a = n.applyToLocalView(u, o.mutatedFields), s = Zn(u,
// Set mutatedFields to null if the document is only from local mutations.
// This creates a Set or Delete mutation, instead of trying to create a
// patch mutation as the overlay.
a = e.has(i.key) ? null : a);
// TODO(mutabledocuments): This method should take a MutableDocumentMap
// and we should remove this cast.
null !== s && r.set(i.key, s), u.isValidDocument() || u.convertToNoDocument(at.min());
})), r;
}, t.prototype.keys = function() {
return this.mutations.reduce((function(t, e) {
return t.add(e.key);
}), _r());
}, t.prototype.isEqual = function(t) {
return this.batchId === t.batchId && it(this.mutations, t.mutations, (function(t, e) {
return er(t, e);
})) && it(this.baseMutations, t.baseMutations, (function(t, e) {
return er(t, e);
}));
}, t;
}(), Qi = /** @class */ function() {
function t(t, e, n,
/**
* A pre-computed mapping from each mutated document to the resulting
* version.
*/
r) {
this.batch = t, this.commitVersion = e, this.mutationResults = n, this.docVersions = r
/**
* Creates a new MutationBatchResult for the given batch and results. There
* must be one result for each mutation in the batch. This static factory
* caches a document=>version mapping (docVersions).
*/;
}
return t.from = function(e, n, r) {
U(e.mutations.length === r.length);
for (var i = Er, o = e.mutations, u = 0; u < o.length; u++) i = i.insert(o[u].key, r[u].version);
return new t(e, n, r, i);
}, t;
}(), zi = /** @class */ function() {
function t(t, e) {
this.largestBatchId = t, this.mutation = e;
}
return t.prototype.getKey = function() {
return this.mutation.key;
}, t.prototype.isEqual = function(t) {
return null !== t && this.mutation === t.mutation;
}, t.prototype.toString = function() {
return "Overlay{\n largestBatchId: ".concat(this.largestBatchId, ",\n mutation: ").concat(this.mutation.toString(), "\n }");
}, t;
}(), Wi = /** @class */ function() {
function t(
/** The target being listened to. */
t,
/**
* The target ID to which the target corresponds; Assigned by the
* LocalStore for user listens and by the SyncEngine for limbo watches.
*/
e,
/** The purpose of the target. */
n,
/**
* The sequence number of the last transaction during which this target data
* was modified.
*/
r,
/** The latest snapshot version seen for this target. */
i
/**
* The maximum snapshot version at which the associated view
* contained no limbo documents.
*/ , o
/**
* An opaque, server-assigned token that allows watching a target to be
* resumed after disconnecting without retransmitting all the data that
* matches the target. The resume token essentially identifies a point in
* time from which the server should resume sending results.
*/ , u) {
void 0 === i && (i = at.min()), void 0 === o && (o = at.min()), void 0 === u && (u = Yt.EMPTY_BYTE_STRING),
this.target = t, this.targetId = e, this.purpose = n, this.sequenceNumber = r, this.snapshotVersion = i,
this.lastLimboFreeSnapshotVersion = o, this.resumeToken = u;
}
/** Creates a new target data instance with an updated sequence number. */ return t.prototype.withSequenceNumber = function(e) {
return new t(this.target, this.targetId, this.purpose, e, this.snapshotVersion, this.lastLimboFreeSnapshotVersion, this.resumeToken);
},
/**
* Creates a new target data instance with an updated resume token and
* snapshot version.
*/
t.prototype.withResumeToken = function(e, n) {
return new t(this.target, this.targetId, this.purpose, this.sequenceNumber, n, this.lastLimboFreeSnapshotVersion, e);
},
/**
* Creates a new target data instance with an updated last limbo free
* snapshot version number.
*/
t.prototype.withLastLimboFreeSnapshotVersion = function(e) {
return new t(this.target, this.targetId, this.purpose, this.sequenceNumber, this.snapshotVersion, e, this.resumeToken);
}, t;
}(), Hi = function(t) {
this.ie = t;
};
/** The result of applying a mutation batch to the backend. */
/** Encodes a document for storage locally. */ function Yi(t, e) {
var n = e.key, r = {
prefixPath: n.getCollectionPath().popLast().toArray(),
collectionGroup: n.collectionGroup,
documentId: n.path.lastSegment(),
readTime: Xi(e.readTime),
hasCommittedMutations: e.hasCommittedMutations
};
if (e.isFoundDocument()) r.document = function(t, e) {
return {
name: Wr(t, e.key),
fields: e.data.value.mapValue.fields,
updateTime: Br(t, e.version.toTimestamp()),
createTime: Br(t, e.createTime.toTimestamp())
};
}(t.ie, e); else if (e.isNoDocument()) r.noDocument = {
path: n.path.toArray(),
readTime: Zi(e.version)
}; else {
if (!e.isUnknownDocument()) return q();
r.unknownDocument = {
path: n.path.toArray(),
version: Zi(e.version)
};
}
return r;
}
function Xi(t) {
var e = t.toTimestamp();
return [ e.seconds, e.nanoseconds ];
}
function Zi(t) {
var e = t.toTimestamp();
return {
seconds: e.seconds,
nanoseconds: e.nanoseconds
};
}
function Ji(t) {
var e = new ut(t.seconds, t.nanoseconds);
return at.fromTimestamp(e);
}
/** Encodes a batch of mutations into a DbMutationBatch for local storage. */
/** Decodes a DbMutationBatch into a MutationBatch */ function $i(t, e) {
// Squash old transform mutations into existing patch or set mutations.
// The replacement of representing `transforms` with `update_transforms`
// on the SDK means that old `transform` mutations stored in IndexedDB need
// to be updated to `update_transforms`.
// TODO(b/174608374): Remove this code once we perform a schema migration.
for (var n = (e.baseMutations || []).map((function(e) {
return ni(t.ie, e);
})), r = 0; r < e.mutations.length - 1; ++r) {
var i = e.mutations[r];
if (r + 1 < e.mutations.length && void 0 !== e.mutations[r + 1].transform) {
var o = e.mutations[r + 1];
i.updateTransforms = o.transform.fieldTransforms, e.mutations.splice(r + 1, 1),
++r;
}
}
var u = e.mutations.map((function(e) {
return ni(t.ie, e);
})), a = ut.fromMillis(e.localWriteTimeMs);
return new ji(e.batchId, a, n, u);
}
/** Decodes a DbTarget into TargetData */ function to(t) {
var e, n, r = Ji(t.readTime), i = void 0 !== t.lastLimboFreeSnapshotVersion ? Ji(t.lastLimboFreeSnapshotVersion) : at.min();
return void 0 !== t.query.documents ? (U(1 === (n = t.query).documents.length),
e = In(yn(Xr(n.documents[0])))) : e = function(t) {
return In(oi(t));
}(t.query), new Wi(e, t.targetId, 0 /* TargetPurpose.Listen */ , t.lastListenSequenceNumber, r, i, Yt.fromBase64String(t.resumeToken))
/** Encodes TargetData into a DbTarget for storage locally. */;
}
function eo(t, e) {
var n, r = Zi(e.snapshotVersion), i = Zi(e.lastLimboFreeSnapshotVersion);
n = cn(e.target) ? ri(t.ie, e.target) : ii(t.ie, e.target);
// We can't store the resumeToken as a ByteString in IndexedDb, so we
// convert it to a base64 string for storage.
var o = e.resumeToken.toBase64();
// lastListenSequenceNumber is always 0 until we do real GC.
return {
targetId: e.targetId,
canonicalId: an(e.target),
readTime: r,
resumeToken: o,
lastListenSequenceNumber: e.sequenceNumber,
lastLimboFreeSnapshotVersion: i,
query: n
};
}
/**
* A helper function for figuring out what kind of query has been stored.
*/
/**
* Encodes a `BundledQuery` from bundle proto to a Query object.
*
* This reconstructs the original query used to build the bundle being loaded,
* including features exists only in SDKs (for example: limit-to-last).
*/ function no(t) {
var e = oi({
parent: t.parent,
structuredQuery: t.structuredQuery
});
return "LAST" === t.limitType ? En(e, e.limit, "L" /* LimitType.Last */) : e;
}
/** Encodes a NamedQuery proto object to a NamedQuery model object. */
/** Encodes a DbDocumentOverlay object to an Overlay model object. */ function ro(t, e) {
return new zi(e.largestBatchId, ni(t.ie, e.overlayMutation));
}
/** Decodes an Overlay model object into a DbDocumentOverlay object. */
/**
* Returns the DbDocumentOverlayKey corresponding to the given user and
* document key.
*/ function io(t, e) {
var n = e.path.lastSegment();
return [ t, yi(e.path.popLast()), n ];
}
function oo(t, e, n, r) {
return {
indexId: t,
uid: e.uid || "",
sequenceNumber: n,
readTime: Zi(r.readTime),
documentKey: yi(r.documentKey.path),
largestBatchId: r.largestBatchId
};
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var uo = /** @class */ function() {
function t() {}
return t.prototype.getBundleMetadata = function(t, e) {
return ao(t).get(e).next((function(t) {
if (t) return {
id: (e = t).bundleId,
createTime: Ji(e.createTime),
version: e.version
};
/** Encodes a DbBundle to a BundleMetadata object. */ var e;
/** Encodes a BundleMetadata to a DbBundle. */ }));
}, t.prototype.saveBundleMetadata = function(t, e) {
return ao(t).put({
bundleId: (n = e).id,
createTime: Zi(jr(n.createTime)),
version: n.version
});
var n;
/** Encodes a DbNamedQuery to a NamedQuery. */ }, t.prototype.getNamedQuery = function(t, e) {
return so(t).get(e).next((function(t) {
if (t) return {
name: (e = t).name,
query: no(e.bundledQuery),
readTime: Ji(e.readTime)
};
var e;
/** Encodes a NamedQuery from a bundle proto to a DbNamedQuery. */ }));
}, t.prototype.saveNamedQuery = function(t, e) {
return so(t).put(function(t) {
return {
name: t.name,
readTime: Zi(jr(t.readTime)),
bundledQuery: t.bundledQuery
};
}(e));
}, t;
}();
/**
* Helper to get a typed SimpleDbStore for the bundles object store.
*/ function ao(t) {
return Ki(t, "bundles");
}
/**
* Helper to get a typed SimpleDbStore for the namedQueries object store.
*/ function so(t) {
return Ki(t, "namedQueries");
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implementation of DocumentOverlayCache using IndexedDb.
*/ var co = /** @class */ function() {
/**
* @param serializer - The document serializer.
* @param userId - The userId for which we are accessing overlays.
*/
function t(t, e) {
this.yt = t, this.userId = e;
}
return t.re = function(e, n) {
return new t(e, n.uid || "");
}, t.prototype.getOverlay = function(t, e) {
var n = this;
return lo(t).get(io(this.userId, e)).next((function(t) {
return t ? ro(n.yt, t) : null;
}));
}, t.prototype.getOverlays = function(t, e) {
var n = this, r = br();
return xt.forEach(e, (function(e) {
return n.getOverlay(t, e).next((function(t) {
null !== t && r.set(e, t);
}));
})).next((function() {
return r;
}));
}, t.prototype.saveOverlays = function(t, e, n) {
var r = this, i = [];
return n.forEach((function(n, o) {
var u = new zi(e, o);
i.push(r.oe(t, u));
})), xt.waitFor(i);
}, t.prototype.removeOverlaysForBatchId = function(t, e, n) {
var r = this, i = new Set;
// Get the set of unique collection paths.
e.forEach((function(t) {
return i.add(yi(t.getCollectionPath()));
}));
var o = [];
return i.forEach((function(e) {
var i = IDBKeyRange.bound([ r.userId, e, n ], [ r.userId, e, n + 1 ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0);
o.push(lo(t).Y("collectionPathOverlayIndex", i));
})), xt.waitFor(o);
}, t.prototype.getOverlaysForCollection = function(t, e, n) {
var r = this, i = br(), o = yi(e), u = IDBKeyRange.bound([ this.userId, o, n ], [ this.userId, o, Number.POSITIVE_INFINITY ],
/*lowerOpen=*/ !0);
return lo(t).W("collectionPathOverlayIndex", u).next((function(t) {
for (var e = 0, n = t; e < n.length; e++) {
var o = n[e], u = ro(r.yt, o);
i.set(u.getKey(), u);
}
return i;
}));
}, t.prototype.getOverlaysForCollectionGroup = function(t, e, n, r) {
var i, o = this, u = br(), a = IDBKeyRange.bound([ this.userId, e, n ], [ this.userId, e, Number.POSITIVE_INFINITY ],
/*lowerOpen=*/ !0);
return lo(t).Z({
index: "collectionGroupOverlayIndex",
range: a
}, (function(t, e, n) {
// We do not want to return partial batch overlays, even if the size
// of the result set exceeds the given `count` argument. Therefore, we
// continue to aggregate results even after the result size exceeds
// `count` if there are more overlays from the `currentBatchId`.
var a = ro(o.yt, e);
u.size() < r || a.largestBatchId === i ? (u.set(a.getKey(), a), i = a.largestBatchId) : n.done();
})).next((function() {
return u;
}));
}, t.prototype.oe = function(t, e) {
return lo(t).put(function(t, e, n) {
var r = io(e, n.mutation.key);
return r[0], {
userId: e,
collectionPath: r[1],
documentId: r[2],
collectionGroup: n.mutation.key.getCollectionGroup(),
largestBatchId: n.largestBatchId,
overlayMutation: ei(t.ie, n.mutation)
};
}(this.yt, this.userId, e));
}, t;
}();
/**
* Helper to get a typed SimpleDbStore for the document overlay object store.
*/ function lo(t) {
return Ki(t, "documentOverlays");
}
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Note: This code is copied from the backend. Code that is not used by
// Firestore was removed.
/** Firestore index value writer. */ var ho = /** @class */ function() {
function t() {}
// The write methods below short-circuit writing terminators for values
// containing a (terminating) truncated value.
// As an example, consider the resulting encoding for:
// ["bar", [2, "foo"]] -> (STRING, "bar", TERM, ARRAY, NUMBER, 2, STRING, "foo", TERM, TERM, TERM)
// ["bar", [2, truncated("foo")]] -> (STRING, "bar", TERM, ARRAY, NUMBER, 2, STRING, "foo", TRUNC)
// ["bar", truncated(["foo"])] -> (STRING, "bar", TERM, ARRAY. STRING, "foo", TERM, TRUNC)
/** Writes an index value. */ return t.prototype.ue = function(t, e) {
this.ce(t, e),
// Write separator to split index values
// (see go/firestore-storage-format#encodings).
e.ae();
}, t.prototype.ce = function(t, e) {
if ("nullValue" in t) this.he(e, 5); else if ("booleanValue" in t) this.he(e, 10),
e.le(t.booleanValue ? 1 : 0); else if ("integerValue" in t) this.he(e, 15), e.le(Jt(t.integerValue)); else if ("doubleValue" in t) {
var n = Jt(t.doubleValue);
isNaN(n) ? this.he(e, 13) : (this.he(e, 15), zt(n) ?
// -0.0, 0 and 0.0 are all considered the same
e.le(0) : e.le(n));
} else if ("timestampValue" in t) {
var r = t.timestampValue;
this.he(e, 20), "string" == typeof r ? e.fe(r) : (e.fe("".concat(r.seconds || "")),
e.le(r.nanos || 0));
} else if ("stringValue" in t) this.de(t.stringValue, e), this._e(e); else if ("bytesValue" in t) this.he(e, 30),
e.we($t(t.bytesValue)), this._e(e); else if ("referenceValue" in t) this.me(t.referenceValue, e); else if ("geoPointValue" in t) {
var i = t.geoPointValue;
this.he(e, 45), e.le(i.latitude || 0), e.le(i.longitude || 0);
} else "mapValue" in t ? we(t) ? this.he(e, Number.MAX_SAFE_INTEGER) : (this.ge(t.mapValue, e),
this._e(e)) : "arrayValue" in t ? (this.ye(t.arrayValue, e), this._e(e)) : q();
}, t.prototype.de = function(t, e) {
this.he(e, 25), this.pe(t, e);
}, t.prototype.pe = function(t, e) {
e.fe(t);
}, t.prototype.ge = function(t, e) {
var n = t.fields || {};
this.he(e, 55);
for (var r = 0, i = Object.keys(n); r < i.length; r++) {
var o = i[r];
this.de(o, e), this.ce(n[o], e);
}
}, t.prototype.ye = function(t, e) {
var n = t.values || [];
this.he(e, 50);
for (var r = 0, i = n; r < i.length; r++) {
var o = i[r];
this.ce(o, e);
}
}, t.prototype.me = function(t, e) {
var n = this;
this.he(e, 37), ft.fromName(t).path.forEach((function(t) {
n.he(e, 60), n.pe(t, e);
}));
}, t.prototype.he = function(t, e) {
t.le(e);
}, t.prototype._e = function(t) {
// While the SDK does not implement truncation, the truncation marker is
// used to terminate all variable length values (which are strings, bytes,
// references, arrays and maps).
t.le(2);
}, t;
}();
/**
* Counts the number of zeros in a byte.
*
* Visible for testing.
*/
function fo(t) {
if (0 === t) return 8;
var e = 0;
return t >> 4 == 0 && (
// Test if the first four bits are zero.
e += 4, t <<= 4), t >> 6 == 0 && (
// Test if the first two (or next two) bits are zero.
e += 2, t <<= 2), t >> 7 == 0 && (
// Test if the remaining bit is zero.
e += 1), e
/** Counts the number of leading zeros in the given byte array. */
/**
* Returns the number of bytes required to store "value". Leading zero bytes
* are skipped.
*/;
}
function po(t) {
// This is just the number of bytes for the unsigned representation of the number.
var e = 64 - function(t) {
for (var e = 0, n = 0; n < 8; ++n) {
var r = fo(255 & t[n]);
if (e += r, 8 !== r) break;
}
return e;
}(t);
return Math.ceil(e / 8);
}
/**
* OrderedCodeWriter is a minimal-allocation implementation of the writing
* behavior defined by the backend.
*
* The code is ported from its Java counterpart.
*/ ho.Ie = new ho;
var yo = /** @class */ function() {
function t() {
this.buffer = new Uint8Array(1024), this.position = 0;
}
return t.prototype.Te = function(t) {
for (var e = t[Symbol.iterator](), n = e.next(); !n.done; ) this.Ee(n.value), n = e.next();
this.Ae();
}, t.prototype.Re = function(t) {
for (var e = t[Symbol.iterator](), n = e.next(); !n.done; ) this.be(n.value), n = e.next();
this.Pe();
},
/** Writes utf8 bytes into this byte sequence, ascending. */ t.prototype.ve = function(t) {
for (var e = 0, n = t; e < n.length; e++) {
var r = n[e], i = r.charCodeAt(0);
if (i < 128) this.Ee(i); else if (i < 2048) this.Ee(960 | i >>> 6), this.Ee(128 | 63 & i); else if (r < "\ud800" || "\udbff" < r) this.Ee(480 | i >>> 12),
this.Ee(128 | 63 & i >>> 6), this.Ee(128 | 63 & i); else {
var o = r.codePointAt(0);
this.Ee(240 | o >>> 18), this.Ee(128 | 63 & o >>> 12), this.Ee(128 | 63 & o >>> 6),
this.Ee(128 | 63 & o);
}
}
this.Ae();
},
/** Writes utf8 bytes into this byte sequence, descending */ t.prototype.Ve = function(t) {
for (var e = 0, n = t; e < n.length; e++) {
var r = n[e], i = r.charCodeAt(0);
if (i < 128) this.be(i); else if (i < 2048) this.be(960 | i >>> 6), this.be(128 | 63 & i); else if (r < "\ud800" || "\udbff" < r) this.be(480 | i >>> 12),
this.be(128 | 63 & i >>> 6), this.be(128 | 63 & i); else {
var o = r.codePointAt(0);
this.be(240 | o >>> 18), this.be(128 | 63 & o >>> 12), this.be(128 | 63 & o >>> 6),
this.be(128 | 63 & o);
}
}
this.Pe();
}, t.prototype.Se = function(t) {
// Values are encoded with a single byte length prefix, followed by the
// actual value in big-endian format with leading 0 bytes dropped.
var e = this.De(t), n = po(e);
this.Ce(1 + n), this.buffer[this.position++] = 255 & n;
// Write the length
for (var r = e.length - n; r < e.length; ++r) this.buffer[this.position++] = 255 & e[r];
}, t.prototype.xe = function(t) {
// Values are encoded with a single byte length prefix, followed by the
// inverted value in big-endian format with leading 0 bytes dropped.
var e = this.De(t), n = po(e);
this.Ce(1 + n), this.buffer[this.position++] = ~(255 & n);
// Write the length
for (var r = e.length - n; r < e.length; ++r) this.buffer[this.position++] = ~(255 & e[r]);
},
/**
* Writes the "infinity" byte sequence that sorts after all other byte
* sequences written in ascending order.
*/
t.prototype.Ne = function() {
this.ke(255), this.ke(255);
},
/**
* Writes the "infinity" byte sequence that sorts before all other byte
* sequences written in descending order.
*/
t.prototype.Oe = function() {
this.Me(255), this.Me(255);
},
/**
* Resets the buffer such that it is the same as when it was newly
* constructed.
*/
t.prototype.reset = function() {
this.position = 0;
}, t.prototype.seed = function(t) {
this.Ce(t.length), this.buffer.set(t, this.position), this.position += t.length;
},
/** Makes a copy of the encoded bytes in this buffer. */ t.prototype.Fe = function() {
return this.buffer.slice(0, this.position);
},
/**
* Encodes `val` into an encoding so that the order matches the IEEE 754
* floating-point comparison results with the following exceptions:
* -0.0 < 0.0
* all non-NaN < NaN
* NaN = NaN
*/
t.prototype.De = function(t) {
var e =
/** Converts a JavaScript number to a byte array (using big endian encoding). */
function(t) {
var e = new DataView(new ArrayBuffer(8));
return e.setFloat64(0, t, /* littleEndian= */ !1), new Uint8Array(e.buffer);
}(t), n = 0 != (128 & e[0]);
// Check if the first bit is set. We use a bit mask since value[0] is
// encoded as a number from 0 to 255.
// Revert the two complement to get natural ordering
e[0] ^= n ? 255 : 128;
for (var r = 1; r < e.length; ++r) e[r] ^= n ? 255 : 0;
return e;
},
/** Writes a single byte ascending to the buffer. */ t.prototype.Ee = function(t) {
var e = 255 & t;
0 === e ? (this.ke(0), this.ke(255)) : 255 === e ? (this.ke(255), this.ke(0)) : this.ke(e);
},
/** Writes a single byte descending to the buffer. */ t.prototype.be = function(t) {
var e = 255 & t;
0 === e ? (this.Me(0), this.Me(255)) : 255 === e ? (this.Me(255), this.Me(0)) : this.Me(t);
}, t.prototype.Ae = function() {
this.ke(0), this.ke(1);
}, t.prototype.Pe = function() {
this.Me(0), this.Me(1);
}, t.prototype.ke = function(t) {
this.Ce(1), this.buffer[this.position++] = t;
}, t.prototype.Me = function(t) {
this.Ce(1), this.buffer[this.position++] = ~t;
}, t.prototype.Ce = function(t) {
var e = t + this.position;
if (!(e <= this.buffer.length)) {
// Try doubling.
var n = 2 * this.buffer.length;
// Still not big enough? Just allocate the right size.
n < e && (n = e);
// Create the new buffer.
var r = new Uint8Array(n);
r.set(this.buffer), // copy old data
this.buffer = r;
}
}, t;
}(), vo = /** @class */ function() {
function t(t) {
this.$e = t;
}
return t.prototype.we = function(t) {
this.$e.Te(t);
}, t.prototype.fe = function(t) {
this.$e.ve(t);
}, t.prototype.le = function(t) {
this.$e.Se(t);
}, t.prototype.ae = function() {
this.$e.Ne();
}, t;
}(), mo = /** @class */ function() {
function t(t) {
this.$e = t;
}
return t.prototype.we = function(t) {
this.$e.Re(t);
}, t.prototype.fe = function(t) {
this.$e.Ve(t);
}, t.prototype.le = function(t) {
this.$e.xe(t);
}, t.prototype.ae = function() {
this.$e.Oe();
}, t;
}(), go = /** @class */ function() {
function t() {
this.$e = new yo, this.Be = new vo(this.$e), this.Le = new mo(this.$e);
}
return t.prototype.seed = function(t) {
this.$e.seed(t);
}, t.prototype.qe = function(t) {
return 0 /* IndexKind.ASCENDING */ === t ? this.Be : this.Le;
}, t.prototype.Fe = function() {
return this.$e.Fe();
}, t.prototype.reset = function() {
this.$e.reset();
}, t;
}(), wo = /** @class */ function() {
function t(t, e, n, r) {
this.indexId = t, this.documentKey = e, this.arrayValue = n, this.directionalValue = r
/**
* Returns an IndexEntry entry that sorts immediately after the current
* directional value.
*/;
}
return t.prototype.Ue = function() {
var e = this.directionalValue.length, n = 0 === e || 255 === this.directionalValue[e - 1] ? e + 1 : e, r = new Uint8Array(n);
return r.set(this.directionalValue, 0), n !== e ? r.set([ 0 ], this.directionalValue.length) : ++r[r.length - 1],
new t(this.indexId, this.documentKey, this.arrayValue, r);
}, t;
}();
function bo(t, e) {
var n = t.indexId - e.indexId;
return 0 !== n ? n : 0 !== (n = Io(t.arrayValue, e.arrayValue)) ? n : 0 !== (n = Io(t.directionalValue, e.directionalValue)) ? n : ft.comparator(t.documentKey, e.documentKey);
}
function Io(t, e) {
for (var n = 0; n < t.length && n < e.length; ++n) {
var r = t[n] - e[n];
if (0 !== r) return r;
}
return t.length - e.length;
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A light query planner for Firestore.
*
* This class matches a `FieldIndex` against a Firestore Query `Target`. It
* determines whether a given index can be used to serve the specified target.
*
* The following table showcases some possible index configurations:
*
* Query | Index
* -----------------------------------------------------------------------------
* where('a', '==', 'a').where('b', '==', 'b') | a ASC, b DESC
* where('a', '==', 'a').where('b', '==', 'b') | a ASC
* where('a', '==', 'a').where('b', '==', 'b') | b DESC
* where('a', '>=', 'a').orderBy('a') | a ASC
* where('a', '>=', 'a').orderBy('a', 'desc') | a DESC
* where('a', '>=', 'a').orderBy('a').orderBy('b') | a ASC, b ASC
* where('a', '>=', 'a').orderBy('a').orderBy('b') | a ASC
* where('a', 'array-contains', 'a').orderBy('b') | a CONTAINS, b ASCENDING
* where('a', 'array-contains', 'a').orderBy('b') | a CONTAINS
*/ var To = /** @class */ function() {
function t(t) {
this.collectionId = null != t.collectionGroup ? t.collectionGroup : t.path.lastSegment(),
this.Ke = t.orderBy, this.Ge = [];
for (var e = 0, n = t.filters; e < n.length; e++) {
var r = n[e];
r.isInequality() ? this.Qe = r : this.Ge.push(r);
}
}
/**
* Returns whether the index can be used to serve the TargetIndexMatcher's
* target.
*
* An index is considered capable of serving the target when:
* - The target uses all index segments for its filters and orderBy clauses.
* The target can have additional filter and orderBy clauses, but not
* fewer.
* - If an ArrayContains/ArrayContainsAnyfilter is used, the index must also
* have a corresponding `CONTAINS` segment.
* - All directional index segments can be mapped to the target as a series of
* equality filters, a single inequality filter and a series of orderBy
* clauses.
* - The segments that represent the equality filters may appear out of order.
* - The optional segment for the inequality filter must appear after all
* equality segments.
* - The segments that represent that orderBy clause of the target must appear
* in order after all equality and inequality segments. Single orderBy
* clauses cannot be skipped, but a continuous orderBy suffix may be
* omitted.
*/ return t.prototype.je = function(t) {
U(t.collectionGroup === this.collectionId);
// If there is an array element, find a matching filter.
var e = pt(t);
if (void 0 !== e && !this.We(e)) return !1;
// Process all equalities first. Equalities can appear out of order.
for (var n = yt(t), r = 0, i = 0; r < n.length && this.We(n[r]); ++r) ;
// If we already have processed all segments, all segments are used to serve
// the equality filters and we do not need to map any segments to the
// target's inequality and orderBy clauses.
if (r === n.length) return !0;
// If there is an inequality filter, the next segment must match both the
// filter and the first orderBy clause.
if (void 0 !== this.Qe) {
var o = n[r];
if (!this.ze(this.Qe, o) || !this.He(this.Ke[i++], o)) return !1;
++r;
}
// All remaining segments need to represent the prefix of the target's
// orderBy.
for (;r < n.length; ++r) {
var u = n[r];
if (i >= this.Ke.length || !this.He(this.Ke[i++], u)) return !1;
}
return !0;
}, t.prototype.We = function(t) {
for (var e = 0, n = this.Ge; e < n.length; e++) {
var r = n[e];
if (this.ze(r, t)) return !0;
}
return !1;
}, t.prototype.ze = function(t, e) {
if (void 0 === t || !t.field.isEqual(e.fieldPath)) return !1;
var n = "array-contains" /* Operator.ARRAY_CONTAINS */ === t.op || "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ === t.op;
return 2 /* IndexKind.CONTAINS */ === e.kind === n;
}, t.prototype.He = function(t, e) {
return !!t.field.isEqual(e.fieldPath) && (0 /* IndexKind.ASCENDING */ === e.kind && "asc" /* Direction.ASCENDING */ === t.dir || 1 /* IndexKind.DESCENDING */ === e.kind && "desc" /* Direction.DESCENDING */ === t.dir);
}, t;
}();
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Provides utility functions that help with boolean logic transformations needed for handling
* complex filters used in queries.
*/
/**
* The `in` filter is only a syntactic sugar over a disjunction of equalities. For instance: `a in
* [1,2,3]` is in fact `a==1 || a==2 || a==3`. This method expands any `in` filter in the given
* input into a disjunction of equality filters and returns the expanded filter.
*/ function Eo(t) {
var e, n;
if (U(t instanceof Ae || t instanceof Ce), t instanceof Ae) {
if (t instanceof Ke) {
var r = (null === (n = null === (e = t.value.arrayValue) || void 0 === e ? void 0 : e.values) || void 0 === n ? void 0 : n.map((function(e) {
return Ae.create(t.field, "==" /* Operator.EQUAL */ , e);
}))) || [];
return Ce.create(r, "or" /* CompositeOperator.OR */);
}
// We have reached other kinds of field filters.
return t;
}
// We have a composite filter.
var i = t.filters.map((function(t) {
return Eo(t);
}));
return Ce.create(i, t.op);
}
/**
* Given a composite filter, returns the list of terms in its disjunctive normal form.
*
* Each element in the return value is one term of the resulting DNF. For instance: For the
* input: (A || B) && C, the DNF form is: (A && C) || (B && C), and the return value is a list
* with two elements: a composite filter that performs (A && C), and a composite filter that
* performs (B && C).
*
* @param filter the composite filter to calculate DNF transform for.
* @return the terms in the DNF transform.
*/ function So(t) {
if (0 === t.getFilters().length) return [];
var e = Ao(Eo(t));
return U(xo(e)), _o(e) || Do(e) ? [ e ] : e.getFilters()
/** Returns true if the given filter is a single field filter. e.g. (a == 10). */;
}
function _o(t) {
return t instanceof Ae;
}
/**
* Returns true if the given filter is the conjunction of one or more field filters. e.g. (a == 10
* && b == 20)
*/ function Do(t) {
return t instanceof Ce && Fe(t);
}
/**
* Returns whether or not the given filter is in disjunctive normal form (DNF).
*
*
In boolean logic, a disjunctive normal form (DNF) is a canonical normal form of a logical
* formula consisting of a disjunction of conjunctions; it can also be described as an OR of ANDs.
*
*
For more info, visit: https://en.wikipedia.org/wiki/Disjunctive_normal_form
*/ function xo(t) {
return _o(t) || Do(t) ||
/**
* Returns true if the given filter is the disjunction of one or more "flat conjunctions" and
* field filters. e.g. (a == 10) || (b==20 && c==30)
*/
function(t) {
if (t instanceof Ce && ke(t)) {
for (var e = 0, n = t.getFilters(); e < n.length; e++) {
var r = n[e];
if (!_o(r) && !Do(r)) return !1;
}
return !0;
}
return !1;
}(t);
}
function Ao(t) {
if (U(t instanceof Ae || t instanceof Ce), t instanceof Ae) return t;
if (1 === t.filters.length) return Ao(t.filters[0]);
// Compute DNF for each of the subfilters first
var e = t.filters.map((function(t) {
return Ao(t);
})), n = Ce.create(e, t.op);
return xo(n = ko(n)) ? n : (U(n instanceof Ce), U(Ne(n)), U(n.filters.length > 1),
n.filters.reduce((function(t, e) {
return Co(t, e);
})));
}
function Co(t, e) {
var n;
return U(t instanceof Ae || t instanceof Ce), U(e instanceof Ae || e instanceof Ce),
// FieldFilter FieldFilter
n = t instanceof Ae ? e instanceof Ae ? function(t, e) {
// Conjunction distribution for two field filters is the conjunction of them.
return Ce.create([ t, e ], "and" /* CompositeOperator.AND */);
}(t, e) : No(t, e) : e instanceof Ae ? No(e, t) : function(t, e) {
// There are four cases:
// (A & B) & (C & D) --> (A & B & C & D)
// (A & B) & (C | D) --> (A & B & C) | (A & B & D)
// (A | B) & (C & D) --> (C & D & A) | (C & D & B)
// (A | B) & (C | D) --> (A & C) | (A & D) | (B & C) | (B & D)
// Case 1 is a merge.
if (U(t.filters.length > 0 && e.filters.length > 0), Ne(t) && Ne(e)) return Me(t, e.getFilters());
// Case 2,3,4 all have at least one side (lhs or rhs) that is a disjunction. In all three cases
// we should take each element of the disjunction and distribute it over the other side, and
// return the disjunction of the distribution results.
var n = ke(t) ? t : e, r = ke(t) ? e : t, i = n.filters.map((function(t) {
return Co(t, r);
}));
return Ce.create(i, "or" /* CompositeOperator.OR */);
}(t, e), ko(n);
}
function No(t, e) {
// There are two cases:
// A & (B & C) --> (A & B & C)
// A & (B | C) --> (A & B) | (A & C)
if (Ne(e))
// Case 1
return Me(e, t.getFilters());
// Case 2
var n = e.filters.map((function(e) {
return Co(t, e);
}));
return Ce.create(n, "or" /* CompositeOperator.OR */);
}
/**
* Applies the associativity property to the given filter and returns the resulting filter.
*
*
* - A | (B | C) == (A | B) | C == (A | B | C)
*
- A & (B & C) == (A & B) & C == (A & B & C)
*
*
* For more info, visit: https://en.wikipedia.org/wiki/Associative_property#Propositional_logic
*/ function ko(t) {
if (U(t instanceof Ae || t instanceof Ce), t instanceof Ae) return t;
var e = t.getFilters();
// If the composite filter only contains 1 filter, apply associativity to it.
if (1 === e.length) return ko(e[0]);
// Associativity applied to a flat composite filter results is itself.
if (Oe(t)) return t;
// First apply associativity to all subfilters. This will in turn recursively apply
// associativity to all nested composite filters and field filters.
var n = e.map((function(t) {
return ko(t);
})), r = [];
// For composite subfilters that perform the same kind of logical operation as `compositeFilter`
// take out their filters and add them to `compositeFilter`. For example:
// compositeFilter = (A | (B | C | D))
// compositeSubfilter = (B | C | D)
// Result: (A | B | C | D)
// Note that the `compositeSubfilter` has been eliminated, and its filters (B, C, D) have been
// added to the top-level "compositeFilter".
return n.forEach((function(e) {
e instanceof Ae ? r.push(e) : e instanceof Ce && (e.op === t.op ?
// compositeFilter: (A | (B | C))
// compositeSubfilter: (B | C)
// Result: (A | B | C)
r.push.apply(
// compositeFilter: (A | (B | C))
// compositeSubfilter: (B | C)
// Result: (A | B | C)
r, e.filters) :
// compositeFilter: (A | (B & C))
// compositeSubfilter: (B & C)
// Result: (A | (B & C))
r.push(e));
})), 1 === r.length ? r[0] : Ce.create(r, t.op)
/**
* @license
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* An in-memory implementation of IndexManager.
*/;
}
var Fo = /** @class */ function() {
function t() {
this.Je = new Oo;
}
return t.prototype.addToCollectionParentIndex = function(t, e) {
return this.Je.add(e), xt.resolve();
}, t.prototype.getCollectionParents = function(t, e) {
return xt.resolve(this.Je.getEntries(e));
}, t.prototype.addFieldIndex = function(t, e) {
// Field indices are not supported with memory persistence.
return xt.resolve();
}, t.prototype.deleteFieldIndex = function(t, e) {
// Field indices are not supported with memory persistence.
return xt.resolve();
}, t.prototype.getDocumentsMatchingTarget = function(t, e) {
// Field indices are not supported with memory persistence.
return xt.resolve(null);
}, t.prototype.getIndexType = function(t, e) {
// Field indices are not supported with memory persistence.
return xt.resolve(0 /* IndexType.NONE */);
}, t.prototype.getFieldIndexes = function(t, e) {
// Field indices are not supported with memory persistence.
return xt.resolve([]);
}, t.prototype.getNextCollectionGroupToUpdate = function(t) {
// Field indices are not supported with memory persistence.
return xt.resolve(null);
}, t.prototype.getMinOffset = function(t, e) {
return xt.resolve(Tt.min());
}, t.prototype.getMinOffsetFromCollectionGroup = function(t, e) {
return xt.resolve(Tt.min());
}, t.prototype.updateCollectionGroup = function(t, e, n) {
// Field indices are not supported with memory persistence.
return xt.resolve();
}, t.prototype.updateIndexEntries = function(t, e) {
// Field indices are not supported with memory persistence.
return xt.resolve();
}, t;
}(), Oo = /** @class */ function() {
function t() {
this.index = {};
}
// Returns false if the entry already existed.
return t.prototype.add = function(t) {
var e = t.lastSegment(), n = t.popLast(), r = this.index[e] || new Ze(ct.comparator), i = !r.has(n);
return this.index[e] = r.add(n), i;
}, t.prototype.has = function(t) {
var e = t.lastSegment(), n = t.popLast(), r = this.index[e];
return r && r.has(n);
}, t.prototype.getEntries = function(t) {
return (this.index[t] || new Ze(ct.comparator)).toArray();
}, t;
}(), Ro = new Uint8Array(0), Vo = /** @class */ function() {
function t(t, e) {
this.user = t, this.databaseId = e,
/**
* An in-memory copy of the index entries we've already written since the SDK
* launched. Used to avoid re-writing the same entry repeatedly.
*
* This is *NOT* a complete cache of what's in persistence and so can never be
* used to satisfy reads.
*/
this.Ye = new Oo,
/**
* Maps from a target to its equivalent list of sub-targets. Each sub-target
* contains only one term from the target's disjunctive normal form (DNF).
*/
this.Xe = new pr((function(t) {
return an(t);
}), (function(t, e) {
return sn(t, e);
})), this.uid = t.uid || ""
/**
* Adds a new entry to the collection parent index.
*
* Repeated calls for the same collectionPath should be avoided within a
* transaction as IndexedDbIndexManager only caches writes once a transaction
* has been committed.
*/;
}
return t.prototype.addToCollectionParentIndex = function(t, e) {
var n = this;
if (!this.Ye.has(e)) {
var r = e.lastSegment(), i = e.popLast();
t.addOnCommittedListener((function() {
// Add the collection to the in memory cache only if the transaction was
// successfully committed.
n.Ye.add(e);
}));
var o = {
collectionId: r,
parent: yi(i)
};
return Mo(t).put(o);
}
return xt.resolve();
}, t.prototype.getCollectionParents = function(t, e) {
var n = [], r = IDBKeyRange.bound([ e, "" ], [ ot(e), "" ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0);
return Mo(t).W(r).next((function(t) {
for (var r = 0, i = t; r < i.length; r++) {
var o = i[r];
// This collectionId guard shouldn't be necessary (and isn't as long
// as we're running in a real browser), but there's a bug in
// indexeddbshim that breaks our range in our tests running in node:
// https://github.com/axemclion/IndexedDBShim/issues/334
if (o.collectionId !== e) break;
n.push(gi(o.parent));
}
return n;
}));
}, t.prototype.addFieldIndex = function(t, e) {
var n = this, r = Po(t), i = function(t) {
return {
indexId: t.indexId,
collectionGroup: t.collectionGroup,
fields: t.fields.map((function(t) {
return [ t.fieldPath.canonicalString(), t.kind ];
}))
};
}(e);
// TODO(indexing): Verify that the auto-incrementing index ID works in
// Safari & Firefox.
delete i.indexId;
// `indexId` is auto-populated by IndexedDb
var o = r.add(i);
if (e.indexState) {
var u = qo(t);
return o.next((function(t) {
u.put(oo(t, n.user, e.indexState.sequenceNumber, e.indexState.offset));
}));
}
return o.next();
}, t.prototype.deleteFieldIndex = function(t, e) {
var n = Po(t), r = qo(t), i = Lo(t);
return n.delete(e.indexId).next((function() {
return r.delete(IDBKeyRange.bound([ e.indexId ], [ e.indexId + 1 ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0));
})).next((function() {
return i.delete(IDBKeyRange.bound([ e.indexId ], [ e.indexId + 1 ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0));
}));
}, t.prototype.getDocumentsMatchingTarget = function(t, e) {
var n = this, r = Lo(t), i = !0, o = new Map;
return xt.forEach(this.Ze(e), (function(e) {
return n.tn(t, e).next((function(t) {
i && (i = !!t), o.set(e, t);
}));
})).next((function() {
if (i) {
var t = _r(), u = [];
return xt.forEach(o, (function(i, o) {
var a;
V("IndexedDbIndexManager", "Using index ".concat((a = i, "id=".concat(a.indexId, "|cg=").concat(a.collectionGroup, "|f=").concat(a.fields.map((function(t) {
return "".concat(t.fieldPath, ":").concat(t.kind);
})).join(","))), " to execute ").concat(an(e)));
var s = function(t, e) {
var n = pt(e);
if (void 0 === n) return null;
for (var r = 0, i = ln(t, n.fieldPath); r < i.length; r++) {
var o = i[r];
switch (o.op) {
case "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ :
return o.value.arrayValue.values || [];
case "array-contains" /* Operator.ARRAY_CONTAINS */ :
return [ o.value ];
// Remaining filters are not array filters.
}
}
return null;
}(o, i), c = function(t, e) {
for (var n = new Map, r = 0, i = yt(e); r < i.length; r++) for (var o = i[r], u = 0, a = ln(t, o.fieldPath); u < a.length; u++) {
var s = a[u];
switch (s.op) {
case "==" /* Operator.EQUAL */ :
case "in" /* Operator.IN */ :
// Encode equality prefix, which is encoded in the index value before
// the inequality (e.g. `a == 'a' && b != 'b'` is encoded to
// `value != 'ab'`).
n.set(o.fieldPath.canonicalString(), s.value);
break;
case "not-in" /* Operator.NOT_IN */ :
case "!=" /* Operator.NOT_EQUAL */ :
// NotIn/NotEqual is always a suffix. There cannot be any remaining
// segments and hence we can return early here.
return n.set(o.fieldPath.canonicalString(), s.value), Array.from(n.values());
// Remaining filters cannot be used as notIn bounds.
}
}
return null;
}(o, i), l = function(t, e) {
// For each segment, retrieve a lower bound if there is a suitable filter or
// startAt.
for (var n = [], r = !0, i = 0, o = yt(e); i < o.length; i++) {
var u = o[i], a = 0 /* IndexKind.ASCENDING */ === u.kind ? hn(t, u.fieldPath, t.startAt) : fn(t, u.fieldPath, t.startAt);
n.push(a.value), r && (r = a.inclusive);
}
return new Se(n, r);
}(o, i), h = function(t, e) {
// For each segment, retrieve an upper bound if there is a suitable filter or
// endAt.
for (var n = [], r = !0, i = 0, o = yt(e); i < o.length; i++) {
var u = o[i], a = 0 /* IndexKind.ASCENDING */ === u.kind ? fn(t, u.fieldPath, t.endAt) : hn(t, u.fieldPath, t.endAt);
n.push(a.value), r && (r = a.inclusive);
}
return new Se(n, r);
}(o, i), f = n.en(i, o, l), d = n.en(i, o, h), p = n.nn(i, o, c), y = n.sn(i.indexId, s, f, l.inclusive, d, h.inclusive, p);
return xt.forEach(y, (function(n) {
return r.J(n, e.limit).next((function(e) {
e.forEach((function(e) {
var n = ft.fromSegments(e.documentKey);
t.has(n) || (t = t.add(n), u.push(n));
}));
}));
}));
})).next((function() {
return u;
}));
}
return xt.resolve(null);
}));
}, t.prototype.Ze = function(t) {
var e = this.Xe.get(t);
return e || (e = 0 === t.filters.length ? [ t ] : So(Ce.create(t.filters, "and" /* CompositeOperator.AND */)).map((function(e) {
return un(t.path, t.collectionGroup, t.orderBy, e.getFilters(), t.limit, t.startAt, t.endAt);
})), this.Xe.set(t, e), e);
},
/**
* Constructs a key range query on `DbIndexEntryStore` that unions all
* bounds.
*/
t.prototype.sn = function(t, e, n, r, i, o, u) {
for (var a = this, s = (null != e ? e.length : 1) * Math.max(n.length, i.length), c = s / (null != e ? e.length : 1), l = [], h = function(s) {
var h = e ? f.rn(e[s / c]) : Ro, d = f.on(t, h, n[s % c], r), p = f.un(t, h, i[s % c], o), y = u.map((function(e) {
return a.on(t, h, e,
/* inclusive= */ !0);
}));
l.push.apply(l, f.createRange(d, p, y));
}, f = this, d = 0
// The number of total index scans we union together. This is similar to a
// distributed normal form, but adapted for array values. We create a single
// index range per value in an ARRAY_CONTAINS or ARRAY_CONTAINS_ANY filter
// combined with the values from the query bounds.
; d < s; ++d) h(d);
return l;
},
/** Generates the lower bound for `arrayValue` and `directionalValue`. */ t.prototype.on = function(t, e, n, r) {
var i = new wo(t, ft.empty(), e, n);
return r ? i : i.Ue();
},
/** Generates the upper bound for `arrayValue` and `directionalValue`. */ t.prototype.un = function(t, e, n, r) {
var i = new wo(t, ft.empty(), e, n);
return r ? i.Ue() : i;
}, t.prototype.tn = function(t, e) {
var n = new To(e), r = null != e.collectionGroup ? e.collectionGroup : e.path.lastSegment();
return this.getFieldIndexes(t, r).next((function(t) {
for (
// Return the index with the most number of segments.
var e = null, r = 0, i = t; r < i.length; r++) {
var o = i[r];
n.je(o) && (!e || o.fields.length > e.fields.length) && (e = o);
}
return e;
}));
}, t.prototype.getIndexType = function(t, e) {
var n = this, r = 2 /* IndexType.FULL */ , i = this.Ze(e);
return xt.forEach(i, (function(e) {
return n.tn(t, e).next((function(t) {
t ? 0 /* IndexType.NONE */ !== r && t.fields.length < function(t) {
for (var e = new Ze(ht.comparator), n = !1, r = 0, i = t.filters; r < i.length; r++) for (var o = 0, u = i[r].getFlattenedFilters(); o < u.length; o++) {
var a = u[o];
// __name__ is not an explicit segment of any index, so we don't need to
// count it.
a.field.isKeyField() || (
// ARRAY_CONTAINS or ARRAY_CONTAINS_ANY filters must be counted separately.
// For instance, it is possible to have an index for "a ARRAY a ASC". Even
// though these are on the same field, they should be counted as two
// separate segments in an index.
"array-contains" /* Operator.ARRAY_CONTAINS */ === a.op || "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ === a.op ? n = !0 : e = e.add(a.field));
}
for (var s = 0, c = t.orderBy; s < c.length; s++) {
var l = c[s];
// __name__ is not an explicit segment of any index, so we don't need to
// count it.
l.field.isKeyField() || (e = e.add(l.field));
}
return e.size + (n ? 1 : 0);
}(e) && (r = 1 /* IndexType.PARTIAL */) : r = 0 /* IndexType.NONE */;
}));
})).next((function() {
// OR queries have more than one sub-target (one sub-target per DNF term). We currently consider
// OR queries that have a `limit` to have a partial index. For such queries we perform sorting
// and apply the limit in memory as a post-processing step.
return function(t) {
return null !== t.limit;
}(e) && i.length > 1 && 2 /* IndexType.FULL */ === r ? 1 /* IndexType.PARTIAL */ : r;
}));
},
/**
* Returns the byte encoded form of the directional values in the field index.
* Returns `null` if the document does not have all fields specified in the
* index.
*/
t.prototype.cn = function(t, e) {
for (var n = new go, r = 0, i = yt(t); r < i.length; r++) {
var o = i[r], u = e.data.field(o.fieldPath);
if (null == u) return null;
var a = n.qe(o.kind);
ho.Ie.ue(u, a);
}
return n.Fe();
},
/** Encodes a single value to the ascending index format. */ t.prototype.rn = function(t) {
var e = new go;
return ho.Ie.ue(t, e.qe(0 /* IndexKind.ASCENDING */)), e.Fe();
},
/**
* Returns an encoded form of the document key that sorts based on the key
* ordering of the field index.
*/
t.prototype.an = function(t, e) {
var n = new go;
return ho.Ie.ue(fe(this.databaseId, e), n.qe(function(t) {
var e = yt(t);
return 0 === e.length ? 0 /* IndexKind.ASCENDING */ : e[e.length - 1].kind;
}(t))), n.Fe();
},
/**
* Encodes the given field values according to the specification in `target`.
* For IN queries, a list of possible values is returned.
*/
t.prototype.nn = function(t, e, n) {
if (null === n) return [];
var r = [];
r.push(new go);
for (var i = 0, o = 0, u = yt(t); o < u.length; o++) for (var a = u[o], s = n[i++], c = 0, l = r; c < l.length; c++) {
var h = l[c];
if (this.hn(e, a.fieldPath) && pe(s)) r = this.ln(r, a, s); else {
var f = h.qe(a.kind);
ho.Ie.ue(s, f);
}
}
return this.fn(r);
},
/**
* Encodes the given bounds according to the specification in `target`. For IN
* queries, a list of possible values is returned.
*/
t.prototype.en = function(t, e, n) {
return this.nn(t, e, n.position);
},
/** Returns the byte representation for the provided encoders. */ t.prototype.fn = function(t) {
for (var e = [], n = 0; n < t.length; ++n) e[n] = t[n].Fe();
return e;
},
/**
* Creates a separate encoder for each element of an array.
*
* The method appends each value to all existing encoders (e.g. filter("a",
* "==", "a1").filter("b", "in", ["b1", "b2"]) becomes ["a1,b1", "a1,b2"]). A
* list of new encoders is returned.
*/
t.prototype.ln = function(t, e, n) {
for (var i = r([], t, !0), o = [], u = 0, a = n.arrayValue.values || []; u < a.length; u++) for (var s = a[u], c = 0, l = i; c < l.length; c++) {
var h = l[c], f = new go;
f.seed(h.Fe()), ho.Ie.ue(s, f.qe(e.kind)), o.push(f);
}
return o;
}, t.prototype.hn = function(t, e) {
return !!t.filters.find((function(t) {
return t instanceof Ae && t.field.isEqual(e) && ("in" /* Operator.IN */ === t.op || "not-in" /* Operator.NOT_IN */ === t.op);
}));
}, t.prototype.getFieldIndexes = function(t, e) {
var n = this, r = Po(t), i = qo(t);
return (e ? r.W("collectionGroupIndex", IDBKeyRange.bound(e, e)) : r.W()).next((function(t) {
var e = [];
return xt.forEach(t, (function(t) {
return i.get([ t.indexId, n.uid ]).next((function(n) {
e.push(function(t, e) {
var n = e ? new wt(e.sequenceNumber, new Tt(Ji(e.readTime), new ft(gi(e.documentKey)), e.largestBatchId)) : wt.empty(), r = t.fields.map((function(t) {
var e = t[0], n = t[1];
return new mt(ht.fromServerFormat(e), n);
}));
return new dt(t.indexId, t.collectionGroup, r, n);
}(t, n));
}));
})).next((function() {
return e;
}));
}));
}, t.prototype.getNextCollectionGroupToUpdate = function(t) {
return this.getFieldIndexes(t).next((function(t) {
return 0 === t.length ? null : (t.sort((function(t, e) {
var n = t.indexState.sequenceNumber - e.indexState.sequenceNumber;
return 0 !== n ? n : rt(t.collectionGroup, e.collectionGroup);
})), t[0].collectionGroup);
}));
}, t.prototype.updateCollectionGroup = function(t, e, n) {
var r = this, i = Po(t), o = qo(t);
return this.dn(t).next((function(t) {
return i.W("collectionGroupIndex", IDBKeyRange.bound(e, e)).next((function(e) {
return xt.forEach(e, (function(e) {
return o.put(oo(e.indexId, r.user, t, n));
}));
}));
}));
}, t.prototype.updateIndexEntries = function(t, e) {
var n = this, r = new Map;
// Porting Note: `getFieldIndexes()` on Web does not cache index lookups as
// it could be used across different IndexedDB transactions. As any cached
// data might be invalidated by other multi-tab clients, we can only trust
// data within a single IndexedDB transaction. We therefore add a cache
// here.
return xt.forEach(e, (function(e, i) {
var o = r.get(e.collectionGroup);
return (o ? xt.resolve(o) : n.getFieldIndexes(t, e.collectionGroup)).next((function(o) {
return r.set(e.collectionGroup, o), xt.forEach(o, (function(r) {
return n._n(t, e, r).next((function(e) {
var o = n.wn(i, r);
return e.isEqual(o) ? xt.resolve() : n.mn(t, i, r, e, o);
}));
}));
}));
}));
}, t.prototype.gn = function(t, e, n, r) {
return Lo(t).put({
indexId: r.indexId,
uid: this.uid,
arrayValue: r.arrayValue,
directionalValue: r.directionalValue,
orderedDocumentKey: this.an(n, e.key),
documentKey: e.key.path.toArray()
});
}, t.prototype.yn = function(t, e, n, r) {
return Lo(t).delete([ r.indexId, this.uid, r.arrayValue, r.directionalValue, this.an(n, e.key), e.key.path.toArray() ]);
}, t.prototype._n = function(t, e, n) {
var r = Lo(t), i = new Ze(bo);
return r.Z({
index: "documentKeyIndex",
range: IDBKeyRange.only([ n.indexId, this.uid, this.an(n, e) ])
}, (function(t, r) {
i = i.add(new wo(n.indexId, e, r.arrayValue, r.directionalValue));
})).next((function() {
return i;
}));
},
/** Creates the index entries for the given document. */ t.prototype.wn = function(t, e) {
var n = new Ze(bo), r = this.cn(e, t);
if (null == r) return n;
var i = pt(e);
if (null != i) {
var o = t.data.field(i.fieldPath);
if (pe(o)) for (var u = 0, a = o.arrayValue.values || []; u < a.length; u++) {
var s = a[u];
n = n.add(new wo(e.indexId, t.key, this.rn(s), r));
}
} else n = n.add(new wo(e.indexId, t.key, Ro, r));
return n;
},
/**
* Updates the index entries for the provided document by deleting entries
* that are no longer referenced in `newEntries` and adding all newly added
* entries.
*/
t.prototype.mn = function(t, e, n, r, i) {
var o = this;
V("IndexedDbIndexManager", "Updating index entries for document '%s'", e.key);
var u = [];
return function(t, e, n, r, i) {
// Walk through the two sets at the same time, using the ordering defined by
// `comparator`.
for (var o = t.getIterator(), u = e.getIterator(), a = $e(o), s = $e(u); a || s; ) {
var c = !1, l = !1;
if (a && s) {
var h = n(a, s);
h < 0 ?
// The element was removed if the next element in our ordered
// walkthrough is only in `before`.
l = !0 : h > 0 && (
// The element was added if the next element in our ordered walkthrough
// is only in `after`.
c = !0);
} else null != a ? l = !0 : c = !0;
c ? (r(s), s = $e(u)) : l ? (i(a), a = $e(o)) : (a = $e(o), s = $e(u));
}
}(r, i, bo, (
/* onAdd= */ function(r) {
u.push(o.gn(t, e, n, r));
}), (
/* onRemove= */ function(r) {
u.push(o.yn(t, e, n, r));
})), xt.waitFor(u);
}, t.prototype.dn = function(t) {
var e = 1;
return qo(t).Z({
index: "sequenceNumberIndex",
reverse: !0,
range: IDBKeyRange.upperBound([ this.uid, Number.MAX_SAFE_INTEGER ])
}, (function(t, n, r) {
r.done(), e = n.sequenceNumber + 1;
})).next((function() {
return e;
}));
},
/**
* Returns a new set of IDB ranges that splits the existing range and excludes
* any values that match the `notInValue` from these ranges. As an example,
* '[foo > 2 && foo != 3]` becomes `[foo > 2 && < 3, foo > 3]`.
*/
t.prototype.createRange = function(t, e, n) {
// The notIn values need to be sorted and unique so that we can return a
// sorted set of non-overlapping ranges.
n = n.sort((function(t, e) {
return bo(t, e);
})).filter((function(t, e, n) {
return !e || 0 !== bo(t, n[e - 1]);
}));
var r = [];
r.push(t);
for (var i = 0, o = n; i < o.length; i++) {
var u = o[i], a = bo(u, t), s = bo(u, e);
if (0 === a)
// `notInValue` is the lower bound. We therefore need to raise the bound
// to the next value.
r[0] = t.Ue(); else if (a > 0 && s < 0)
// `notInValue` is in the middle of the range
r.push(u), r.push(u.Ue()); else if (s > 0)
// `notInValue` (and all following values) are out of the range
break;
}
r.push(e);
for (var c = [], l = 0; l < r.length; l += 2) {
// If we encounter two bounds that will create an unmatchable key range,
// then we return an empty set of key ranges.
if (this.pn(r[l], r[l + 1])) return [];
var h = [ r[l].indexId, this.uid, r[l].arrayValue, r[l].directionalValue, Ro, [] ], f = [ r[l + 1].indexId, this.uid, r[l + 1].arrayValue, r[l + 1].directionalValue, Ro, [] ];
c.push(IDBKeyRange.bound(h, f));
}
return c;
}, t.prototype.pn = function(t, e) {
// If lower bound is greater than the upper bound, then the key
// range can never be matched.
return bo(t, e) > 0;
}, t.prototype.getMinOffsetFromCollectionGroup = function(t, e) {
return this.getFieldIndexes(t, e).next(Uo);
}, t.prototype.getMinOffset = function(t, e) {
var n = this;
return xt.mapArray(this.Ze(e), (function(e) {
return n.tn(t, e).next((function(t) {
return t || q();
}));
})).next(Uo);
}, t;
}();
/**
* Internal implementation of the collection-parent index exposed by MemoryIndexManager.
* Also used for in-memory caching by IndexedDbIndexManager and initial index population
* in indexeddb_schema.ts
*/
/**
* Helper to get a typed SimpleDbStore for the collectionParents
* document store.
*/
function Mo(t) {
return Ki(t, "collectionParents");
}
/**
* Helper to get a typed SimpleDbStore for the index entry object store.
*/ function Lo(t) {
return Ki(t, "indexEntries");
}
/**
* Helper to get a typed SimpleDbStore for the index configuration object store.
*/ function Po(t) {
return Ki(t, "indexConfiguration");
}
/**
* Helper to get a typed SimpleDbStore for the index state object store.
*/ function qo(t) {
return Ki(t, "indexState");
}
function Uo(t) {
U(0 !== t.length);
for (var e = t[0].indexState.offset, n = e.largestBatchId, r = 1; r < t.length; r++) {
var i = t[r].indexState.offset;
Et(i, e) < 0 && (e = i), n < i.largestBatchId && (n = i.largestBatchId);
}
return new Tt(e.readTime, e.documentKey, n);
}
/**
* @license
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var Bo = {
didRun: !1,
sequenceNumbersCollected: 0,
targetsRemoved: 0,
documentsRemoved: 0
}, Go = /** @class */ function() {
function t(
// When we attempt to collect, we will only do so if the cache size is greater than this
// threshold. Passing `COLLECTION_DISABLED` here will cause collection to always be skipped.
t,
// The percentage of sequence numbers that we will attempt to collect
e,
// A cap on the total number of sequence numbers that will be collected. This prevents
// us from collecting a huge number of sequence numbers if the cache has grown very large.
n) {
this.cacheSizeCollectionThreshold = t, this.percentileToCollect = e, this.maximumSequenceNumbersToCollect = n;
}
return t.withCacheSize = function(e) {
return new t(e, t.DEFAULT_COLLECTION_PERCENTILE, t.DEFAULT_MAX_SEQUENCE_NUMBERS_TO_COLLECT);
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Delete a mutation batch and the associated document mutations.
* @returns A PersistencePromise of the document mutations that were removed.
*/
function Ko(t, e, n) {
var r = t.store("mutations"), i = t.store("documentMutations"), o = [], u = IDBKeyRange.only(n.batchId), a = 0, s = r.Z({
range: u
}, (function(t, e, n) {
return a++, n.delete();
}));
o.push(s.next((function() {
U(1 === a);
})));
for (var c = [], l = 0, h = n.mutations; l < h.length; l++) {
var f = h[l], d = Ii(e, f.key.path, n.batchId);
o.push(i.delete(d)), c.push(f.key);
}
return xt.waitFor(o).next((function() {
return c;
}));
}
/**
* Returns an approximate size for the given document.
*/ function jo(t) {
if (!t) return 0;
var e;
if (t.document) e = t.document; else if (t.unknownDocument) e = t.unknownDocument; else {
if (!t.noDocument) throw q();
e = t.noDocument;
}
return JSON.stringify(e).length;
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** A mutation queue for a specific user, backed by IndexedDB. */ Go.DEFAULT_COLLECTION_PERCENTILE = 10,
Go.DEFAULT_MAX_SEQUENCE_NUMBERS_TO_COLLECT = 1e3, Go.DEFAULT = new Go(41943040, Go.DEFAULT_COLLECTION_PERCENTILE, Go.DEFAULT_MAX_SEQUENCE_NUMBERS_TO_COLLECT),
Go.DISABLED = new Go(-1, 0, 0);
var Qo = /** @class */ function() {
function t(
/**
* The normalized userId (e.g. null UID => "" userId) used to store /
* retrieve mutations.
*/
t, e, n, r) {
this.userId = t, this.yt = e, this.indexManager = n, this.referenceDelegate = r,
/**
* Caches the document keys for pending mutation batches. If the mutation
* has been removed from IndexedDb, the cached value may continue to
* be used to retrieve the batch's document keys. To remove a cached value
* locally, `removeCachedMutationKeys()` should be invoked either directly
* or through `removeMutationBatches()`.
*
* With multi-tab, when the primary client acknowledges or rejects a mutation,
* this cache is used by secondary clients to invalidate the local
* view of the documents that were previously affected by the mutation.
*/
// PORTING NOTE: Multi-tab only.
this.In = {}
/**
* Creates a new mutation queue for the given user.
* @param user - The user for which to create a mutation queue.
* @param serializer - The serializer to use when persisting to IndexedDb.
*/;
}
return t.re = function(e, n, r, i) {
// TODO(mcg): Figure out what constraints there are on userIDs
// In particular, are there any reserved characters? are empty ids allowed?
// For the moment store these together in the same mutations table assuming
// that empty userIDs aren't allowed.
return U("" !== e.uid), new t(e.isAuthenticated() ? e.uid : "", n, r, i);
}, t.prototype.checkEmpty = function(t) {
var e = !0, n = IDBKeyRange.bound([ this.userId, Number.NEGATIVE_INFINITY ], [ this.userId, Number.POSITIVE_INFINITY ]);
return Wo(t).Z({
index: "userMutationsIndex",
range: n
}, (function(t, n, r) {
e = !1, r.done();
})).next((function() {
return e;
}));
}, t.prototype.addMutationBatch = function(t, e, n, r) {
var i = this, o = Ho(t), u = Wo(t);
// The IndexedDb implementation in Chrome (and Firefox) does not handle
// compound indices that include auto-generated keys correctly. To ensure
// that the index entry is added correctly in all browsers, we perform two
// writes: The first write is used to retrieve the next auto-generated Batch
// ID, and the second write populates the index and stores the actual
// mutation batch.
// See: https://bugs.chromium.org/p/chromium/issues/detail?id=701972
// We write an empty object to obtain key
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return u.add({}).next((function(a) {
U("number" == typeof a);
for (var s = new ji(a, e, n, r), c = function(t, e, n) {
var r = n.baseMutations.map((function(e) {
return ei(t.ie, e);
})), i = n.mutations.map((function(e) {
return ei(t.ie, e);
}));
return {
userId: e,
batchId: n.batchId,
localWriteTimeMs: n.localWriteTime.toMillis(),
baseMutations: r,
mutations: i
};
}(i.yt, i.userId, s), l = [], h = new Ze((function(t, e) {
return rt(t.canonicalString(), e.canonicalString());
})), f = 0, d = r; f < d.length; f++) {
var p = d[f], y = Ii(i.userId, p.key.path, a);
h = h.add(p.key.path.popLast()), l.push(u.put(c)), l.push(o.put(y, Ti));
}
return h.forEach((function(e) {
l.push(i.indexManager.addToCollectionParentIndex(t, e));
})), t.addOnCommittedListener((function() {
i.In[a] = s.keys();
})), xt.waitFor(l).next((function() {
return s;
}));
}));
}, t.prototype.lookupMutationBatch = function(t, e) {
var n = this;
return Wo(t).get(e).next((function(t) {
return t ? (U(t.userId === n.userId), $i(n.yt, t)) : null;
}));
},
/**
* Returns the document keys for the mutation batch with the given batchId.
* For primary clients, this method returns `null` after
* `removeMutationBatches()` has been called. Secondary clients return a
* cached result until `removeCachedMutationKeys()` is invoked.
*/
// PORTING NOTE: Multi-tab only.
t.prototype.Tn = function(t, e) {
var n = this;
return this.In[e] ? xt.resolve(this.In[e]) : this.lookupMutationBatch(t, e).next((function(t) {
if (t) {
var r = t.keys();
return n.In[e] = r, r;
}
return null;
}));
}, t.prototype.getNextMutationBatchAfterBatchId = function(t, e) {
var n = this, r = e + 1, i = IDBKeyRange.lowerBound([ this.userId, r ]), o = null;
return Wo(t).Z({
index: "userMutationsIndex",
range: i
}, (function(t, e, i) {
e.userId === n.userId && (U(e.batchId >= r), o = $i(n.yt, e)), i.done();
})).next((function() {
return o;
}));
}, t.prototype.getHighestUnacknowledgedBatchId = function(t) {
var e = IDBKeyRange.upperBound([ this.userId, Number.POSITIVE_INFINITY ]), n = -1;
return Wo(t).Z({
index: "userMutationsIndex",
range: e,
reverse: !0
}, (function(t, e, r) {
n = e.batchId, r.done();
})).next((function() {
return n;
}));
}, t.prototype.getAllMutationBatches = function(t) {
var e = this, n = IDBKeyRange.bound([ this.userId, -1 ], [ this.userId, Number.POSITIVE_INFINITY ]);
return Wo(t).W("userMutationsIndex", n).next((function(t) {
return t.map((function(t) {
return $i(e.yt, t);
}));
}));
}, t.prototype.getAllMutationBatchesAffectingDocumentKey = function(t, e) {
var n = this, r = bi(this.userId, e.path), i = IDBKeyRange.lowerBound(r), o = [];
// Scan the document-mutation index starting with a prefix starting with
// the given documentKey.
return Ho(t).Z({
range: i
}, (function(r, i, u) {
var a = r[0], s = r[1], c = r[2], l = gi(s);
// Only consider rows matching exactly the specific key of
// interest. Note that because we order by path first, and we
// order terminators before path separators, we'll encounter all
// the index rows for documentKey contiguously. In particular, all
// the rows for documentKey will occur before any rows for
// documents nested in a subcollection beneath documentKey so we
// can stop as soon as we hit any such row.
if (a === n.userId && e.path.isEqual(l))
// Look up the mutation batch in the store.
return Wo(t).get(c).next((function(t) {
if (!t) throw q();
U(t.userId === n.userId), o.push($i(n.yt, t));
}));
u.done();
})).next((function() {
return o;
}));
}, t.prototype.getAllMutationBatchesAffectingDocumentKeys = function(t, e) {
var n = this, r = new Ze(rt), i = [];
return e.forEach((function(e) {
var o = bi(n.userId, e.path), u = IDBKeyRange.lowerBound(o), a = Ho(t).Z({
range: u
}, (function(t, i, o) {
var u = t[0], a = t[1], s = t[2], c = gi(a);
// Only consider rows matching exactly the specific key of
// interest. Note that because we order by path first, and we
// order terminators before path separators, we'll encounter all
// the index rows for documentKey contiguously. In particular, all
// the rows for documentKey will occur before any rows for
// documents nested in a subcollection beneath documentKey so we
// can stop as soon as we hit any such row.
u === n.userId && e.path.isEqual(c) ? r = r.add(s) : o.done();
}));
i.push(a);
})), xt.waitFor(i).next((function() {
return n.En(t, r);
}));
}, t.prototype.getAllMutationBatchesAffectingQuery = function(t, e) {
var n = this, r = e.path, i = r.length + 1, o = bi(this.userId, r), u = IDBKeyRange.lowerBound(o), a = new Ze(rt);
return Ho(t).Z({
range: u
}, (function(t, e, o) {
var u = t[0], s = t[1], c = t[2], l = gi(s);
u === n.userId && r.isPrefixOf(l) ?
// Rows with document keys more than one segment longer than the
// query path can't be matches. For example, a query on 'rooms'
// can't match the document /rooms/abc/messages/xyx.
// TODO(mcg): we'll need a different scanner when we implement
// ancestor queries.
l.length === i && (a = a.add(c)) : o.done();
})).next((function() {
return n.En(t, a);
}));
}, t.prototype.En = function(t, e) {
var n = this, r = [], i = [];
// TODO(rockwood): Implement this using iterate.
return e.forEach((function(e) {
i.push(Wo(t).get(e).next((function(t) {
if (null === t) throw q();
U(t.userId === n.userId), r.push($i(n.yt, t));
})));
})), xt.waitFor(i).next((function() {
return r;
}));
}, t.prototype.removeMutationBatch = function(t, e) {
var n = this;
return Ko(t.se, this.userId, e).next((function(r) {
return t.addOnCommittedListener((function() {
n.An(e.batchId);
})), xt.forEach(r, (function(e) {
return n.referenceDelegate.markPotentiallyOrphaned(t, e);
}));
}));
},
/**
* Clears the cached keys for a mutation batch. This method should be
* called by secondary clients after they process mutation updates.
*
* Note that this method does not have to be called from primary clients as
* the corresponding cache entries are cleared when an acknowledged or
* rejected batch is removed from the mutation queue.
*/
// PORTING NOTE: Multi-tab only
t.prototype.An = function(t) {
delete this.In[t];
}, t.prototype.performConsistencyCheck = function(t) {
var e = this;
return this.checkEmpty(t).next((function(n) {
if (!n) return xt.resolve();
// Verify that there are no entries in the documentMutations index if
// the queue is empty.
var r = IDBKeyRange.lowerBound([ e.userId ]), i = [];
return Ho(t).Z({
range: r
}, (function(t, n, r) {
if (t[0] === e.userId) {
var o = gi(t[1]);
i.push(o);
} else r.done();
})).next((function() {
U(0 === i.length);
}));
}));
}, t.prototype.containsKey = function(t, e) {
return zo(t, this.userId, e);
},
// PORTING NOTE: Multi-tab only (state is held in memory in other clients).
/** Returns the mutation queue's metadata from IndexedDb. */
t.prototype.Rn = function(t) {
var e = this;
return Yo(t).get(this.userId).next((function(t) {
return t || {
userId: e.userId,
lastAcknowledgedBatchId: -1,
lastStreamToken: ""
};
}));
}, t;
}();
/**
* @returns true if the mutation queue for the given user contains a pending
* mutation for the given key.
*/ function zo(t, e, n) {
var r = bi(e, n.path), i = r[1], o = IDBKeyRange.lowerBound(r), u = !1;
return Ho(t).Z({
range: o,
X: !0
}, (function(t, n, r) {
var o = t[0], a = t[1];
/*batchID*/ t[2], o === e && a === i && (u = !0),
r.done();
})).next((function() {
return u;
}));
}
/** Returns true if any mutation queue contains the given document. */
/**
* Helper to get a typed SimpleDbStore for the mutations object store.
*/ function Wo(t) {
return Ki(t, "mutations");
}
/**
* Helper to get a typed SimpleDbStore for the mutationQueues object store.
*/ function Ho(t) {
return Ki(t, "documentMutations");
}
/**
* Helper to get a typed SimpleDbStore for the mutationQueues object store.
*/ function Yo(t) {
return Ki(t, "mutationQueues");
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Offset to ensure non-overlapping target ids. */
/**
* Generates monotonically increasing target IDs for sending targets to the
* watch stream.
*
* The client constructs two generators, one for the target cache, and one for
* for the sync engine (to generate limbo documents targets). These
* generators produce non-overlapping IDs (by using even and odd IDs
* respectively).
*
* By separating the target ID space, the query cache can generate target IDs
* that persist across client restarts, while sync engine can independently
* generate in-memory target IDs that are transient and can be reused after a
* restart.
*/ var Xo = /** @class */ function() {
function t(t) {
this.bn = t;
}
return t.prototype.next = function() {
return this.bn += 2, this.bn;
}, t.Pn = function() {
// The target cache generator must return '2' in its first call to `next()`
// as there is no differentiation in the protocol layer between an unset
// number and the number '0'. If we were to sent a target with target ID
// '0', the backend would consider it unset and replace it with its own ID.
return new t(0);
}, t.vn = function() {
// Sync engine assigns target IDs for limbo document detection.
return new t(-1);
}, t;
}(), Zo = /** @class */ function() {
function t(t, e) {
this.referenceDelegate = t, this.yt = e;
}
// PORTING NOTE: We don't cache global metadata for the target cache, since
// some of it (in particular `highestTargetId`) can be modified by secondary
// tabs. We could perhaps be more granular (and e.g. still cache
// `lastRemoteSnapshotVersion` in memory) but for simplicity we currently go
// to IndexedDb whenever we need to read metadata. We can revisit if it turns
// out to have a meaningful performance impact.
return t.prototype.allocateTargetId = function(t) {
var e = this;
return this.Vn(t).next((function(n) {
var r = new Xo(n.highestTargetId);
return n.highestTargetId = r.next(), e.Sn(t, n).next((function() {
return n.highestTargetId;
}));
}));
}, t.prototype.getLastRemoteSnapshotVersion = function(t) {
return this.Vn(t).next((function(t) {
return at.fromTimestamp(new ut(t.lastRemoteSnapshotVersion.seconds, t.lastRemoteSnapshotVersion.nanoseconds));
}));
}, t.prototype.getHighestSequenceNumber = function(t) {
return this.Vn(t).next((function(t) {
return t.highestListenSequenceNumber;
}));
}, t.prototype.setTargetsMetadata = function(t, e, n) {
var r = this;
return this.Vn(t).next((function(i) {
return i.highestListenSequenceNumber = e, n && (i.lastRemoteSnapshotVersion = n.toTimestamp()),
e > i.highestListenSequenceNumber && (i.highestListenSequenceNumber = e), r.Sn(t, i);
}));
}, t.prototype.addTargetData = function(t, e) {
var n = this;
return this.Dn(t, e).next((function() {
return n.Vn(t).next((function(r) {
return r.targetCount += 1, n.Cn(e, r), n.Sn(t, r);
}));
}));
}, t.prototype.updateTargetData = function(t, e) {
return this.Dn(t, e);
}, t.prototype.removeTargetData = function(t, e) {
var n = this;
return this.removeMatchingKeysForTargetId(t, e.targetId).next((function() {
return Jo(t).delete(e.targetId);
})).next((function() {
return n.Vn(t);
})).next((function(e) {
return U(e.targetCount > 0), e.targetCount -= 1, n.Sn(t, e);
}));
},
/**
* Drops any targets with sequence number less than or equal to the upper bound, excepting those
* present in `activeTargetIds`. Document associations for the removed targets are also removed.
* Returns the number of targets removed.
*/
t.prototype.removeTargets = function(t, e, n) {
var r = this, i = 0, o = [];
return Jo(t).Z((function(u, a) {
var s = to(a);
s.sequenceNumber <= e && null === n.get(s.targetId) && (i++, o.push(r.removeTargetData(t, s)));
})).next((function() {
return xt.waitFor(o);
})).next((function() {
return i;
}));
},
/**
* Call provided function with each `TargetData` that we have cached.
*/
t.prototype.forEachTarget = function(t, e) {
return Jo(t).Z((function(t, n) {
var r = to(n);
e(r);
}));
}, t.prototype.Vn = function(t) {
return $o(t).get("targetGlobalKey").next((function(t) {
return U(null !== t), t;
}));
}, t.prototype.Sn = function(t, e) {
return $o(t).put("targetGlobalKey", e);
}, t.prototype.Dn = function(t, e) {
return Jo(t).put(eo(this.yt, e));
},
/**
* In-place updates the provided metadata to account for values in the given
* TargetData. Saving is done separately. Returns true if there were any
* changes to the metadata.
*/
t.prototype.Cn = function(t, e) {
var n = !1;
return t.targetId > e.highestTargetId && (e.highestTargetId = t.targetId, n = !0),
t.sequenceNumber > e.highestListenSequenceNumber && (e.highestListenSequenceNumber = t.sequenceNumber,
n = !0), n;
}, t.prototype.getTargetCount = function(t) {
return this.Vn(t).next((function(t) {
return t.targetCount;
}));
}, t.prototype.getTargetData = function(t, e) {
// Iterating by the canonicalId may yield more than one result because
// canonicalId values are not required to be unique per target. This query
// depends on the queryTargets index to be efficient.
var n = an(e), r = IDBKeyRange.bound([ n, Number.NEGATIVE_INFINITY ], [ n, Number.POSITIVE_INFINITY ]), i = null;
return Jo(t).Z({
range: r,
index: "queryTargetsIndex"
}, (function(t, n, r) {
var o = to(n);
// After finding a potential match, check that the target is
// actually equal to the requested target.
sn(e, o.target) && (i = o, r.done());
})).next((function() {
return i;
}));
}, t.prototype.addMatchingKeys = function(t, e, n) {
var r = this, i = [], o = tu(t);
// PORTING NOTE: The reverse index (documentsTargets) is maintained by
// IndexedDb.
return e.forEach((function(e) {
var u = yi(e.path);
i.push(o.put({
targetId: n,
path: u
})), i.push(r.referenceDelegate.addReference(t, n, e));
})), xt.waitFor(i);
}, t.prototype.removeMatchingKeys = function(t, e, n) {
var r = this, i = tu(t);
// PORTING NOTE: The reverse index (documentsTargets) is maintained by
// IndexedDb.
return xt.forEach(e, (function(e) {
var o = yi(e.path);
return xt.waitFor([ i.delete([ n, o ]), r.referenceDelegate.removeReference(t, n, e) ]);
}));
}, t.prototype.removeMatchingKeysForTargetId = function(t, e) {
var n = tu(t), r = IDBKeyRange.bound([ e ], [ e + 1 ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0);
return n.delete(r);
}, t.prototype.getMatchingKeysForTargetId = function(t, e) {
var n = IDBKeyRange.bound([ e ], [ e + 1 ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0), r = tu(t), i = _r();
return r.Z({
range: n,
X: !0
}, (function(t, e, n) {
var r = gi(t[1]), o = new ft(r);
i = i.add(o);
})).next((function() {
return i;
}));
}, t.prototype.containsKey = function(t, e) {
var n = yi(e.path), r = IDBKeyRange.bound([ n ], [ ot(n) ],
/*lowerOpen=*/ !1,
/*upperOpen=*/ !0), i = 0;
return tu(t).Z({
index: "documentTargetsIndex",
X: !0,
range: r
}, (function(t, e, n) {
var r = t[0];
t[1],
// Having a sentinel row for a document does not count as containing that document;
// For the target cache, containing the document means the document is part of some
// target.
0 !== r && (i++, n.done());
})).next((function() {
return i > 0;
}));
},
/**
* Looks up a TargetData entry by target ID.
*
* @param targetId - The target ID of the TargetData entry to look up.
* @returns The cached TargetData entry, or null if the cache has no entry for
* the target.
*/
// PORTING NOTE: Multi-tab only.
t.prototype.ne = function(t, e) {
return Jo(t).get(e).next((function(t) {
return t ? to(t) : null;
}));
}, t;
}();
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Helper to get a typed SimpleDbStore for the queries object store.
*/
function Jo(t) {
return Ki(t, "targets");
}
/**
* Helper to get a typed SimpleDbStore for the target globals object store.
*/ function $o(t) {
return Ki(t, "targetGlobal");
}
/**
* Helper to get a typed SimpleDbStore for the document target object store.
*/ function tu(t) {
return Ki(t, "targetDocuments");
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ function eu(t, e) {
var n = t[0], r = t[1], i = e[0], o = e[1], u = rt(n, i);
return 0 === u ? rt(r, o) : u;
}
/**
* Used to calculate the nth sequence number. Keeps a rolling buffer of the
* lowest n values passed to `addElement`, and finally reports the largest of
* them in `maxValue`.
*/ var nu = /** @class */ function() {
function t(t) {
this.xn = t, this.buffer = new Ze(eu), this.Nn = 0;
}
return t.prototype.kn = function() {
return ++this.Nn;
}, t.prototype.On = function(t) {
var e = [ t, this.kn() ];
if (this.buffer.size < this.xn) this.buffer = this.buffer.add(e); else {
var n = this.buffer.last();
eu(e, n) < 0 && (this.buffer = this.buffer.delete(n).add(e));
}
}, Object.defineProperty(t.prototype, "maxValue", {
get: function() {
// Guaranteed to be non-empty. If we decide we are not collecting any
// sequence numbers, nthSequenceNumber below short-circuits. If we have
// decided that we are collecting n sequence numbers, it's because n is some
// percentage of the existing sequence numbers. That means we should never
// be in a situation where we are collecting sequence numbers but don't
// actually have any.
return this.buffer.last()[0];
},
enumerable: !1,
configurable: !0
}), t;
}(), ru = /** @class */ function() {
function t(t, e, n) {
this.garbageCollector = t, this.asyncQueue = e, this.localStore = n, this.Mn = null;
}
return t.prototype.start = function() {
-1 !== this.garbageCollector.params.cacheSizeCollectionThreshold && this.Fn(6e4);
}, t.prototype.stop = function() {
this.Mn && (this.Mn.cancel(), this.Mn = null);
}, Object.defineProperty(t.prototype, "started", {
get: function() {
return null !== this.Mn;
},
enumerable: !1,
configurable: !0
}), t.prototype.Fn = function(t) {
var r = this;
V("LruGarbageCollector", "Garbage collection scheduled in ".concat(t, "ms")), this.Mn = this.asyncQueue.enqueueAfterDelay("lru_garbage_collection" /* TimerId.LruGarbageCollection */ , t, (function() {
return e(r, void 0, void 0, (function() {
var t;
return n(this, (function(e) {
switch (e.label) {
case 0:
this.Mn = null, e.label = 1;
case 1:
return e.trys.push([ 1, 3, , 7 ]), [ 4 /*yield*/ , this.localStore.collectGarbage(this.garbageCollector) ];
case 2:
return e.sent(), [ 3 /*break*/ , 7 ];
case 3:
return Ft(t = e.sent()) ? (V("LruGarbageCollector", "Ignoring IndexedDB error during garbage collection: ", t),
[ 3 /*break*/ , 6 ]) : [ 3 /*break*/ , 4 ];
case 4:
return [ 4 /*yield*/ , Dt(t) ];
case 5:
e.sent(), e.label = 6;
case 6:
return [ 3 /*break*/ , 7 ];
case 7:
return [ 4 /*yield*/ , this.Fn(3e5) ];
case 8:
return e.sent(), [ 2 /*return*/ ];
}
}));
}));
}));
}, t;
}(), iu = /** @class */ function() {
function t(t, e) {
this.$n = t, this.params = e;
}
return t.prototype.calculateTargetCount = function(t, e) {
return this.$n.Bn(t).next((function(t) {
return Math.floor(e / 100 * t);
}));
}, t.prototype.nthSequenceNumber = function(t, e) {
var n = this;
if (0 === e) return xt.resolve(qt.at);
var r = new nu(e);
return this.$n.forEachTarget(t, (function(t) {
return r.On(t.sequenceNumber);
})).next((function() {
return n.$n.Ln(t, (function(t) {
return r.On(t);
}));
})).next((function() {
return r.maxValue;
}));
}, t.prototype.removeTargets = function(t, e, n) {
return this.$n.removeTargets(t, e, n);
}, t.prototype.removeOrphanedDocuments = function(t, e) {
return this.$n.removeOrphanedDocuments(t, e);
}, t.prototype.collect = function(t, e) {
var n = this;
return -1 === this.params.cacheSizeCollectionThreshold ? (V("LruGarbageCollector", "Garbage collection skipped; disabled"),
xt.resolve(Bo)) : this.getCacheSize(t).next((function(r) {
return r < n.params.cacheSizeCollectionThreshold ? (V("LruGarbageCollector", "Garbage collection skipped; Cache size ".concat(r, " is lower than threshold ").concat(n.params.cacheSizeCollectionThreshold)),
Bo) : n.qn(t, e);
}));
}, t.prototype.getCacheSize = function(t) {
return this.$n.getCacheSize(t);
}, t.prototype.qn = function(t, e) {
var n, r, i, o, u, a, s, c = this, l = Date.now();
return this.calculateTargetCount(t, this.params.percentileToCollect).next((function(e) {
// Cap at the configured max
return e > c.params.maximumSequenceNumbersToCollect ? (V("LruGarbageCollector", "Capping sequence numbers to collect down to the maximum of ".concat(c.params.maximumSequenceNumbersToCollect, " from ").concat(e)),
r = c.params.maximumSequenceNumbersToCollect) : r = e, o = Date.now(), c.nthSequenceNumber(t, r);
})).next((function(r) {
return n = r, u = Date.now(), c.removeTargets(t, n, e);
})).next((function(e) {
return i = e, a = Date.now(), c.removeOrphanedDocuments(t, n);
})).next((function(t) {
return s = Date.now(), O() <= h.DEBUG && V("LruGarbageCollector", "LRU Garbage Collection\n\tCounted targets in ".concat(o - l, "ms\n\tDetermined least recently used ").concat(r, " in ") + (u - o) + "ms\n" + "\tRemoved ".concat(i, " targets in ") + (a - u) + "ms\n" + "\tRemoved ".concat(t, " documents in ") + (s - a) + "ms\n" + "Total Duration: ".concat(s - l, "ms")),
xt.resolve({
didRun: !0,
sequenceNumbersCollected: r,
targetsRemoved: i,
documentsRemoved: t
});
}));
}, t;
}(), ou = /** @class */ function() {
function t(t, e) {
this.db = t, this.garbageCollector = function(t, e) {
return new iu(t, e);
}(this, e);
}
return t.prototype.Bn = function(t) {
var e = this.Un(t);
return this.db.getTargetCache().getTargetCount(t).next((function(t) {
return e.next((function(e) {
return t + e;
}));
}));
}, t.prototype.Un = function(t) {
var e = 0;
return this.Ln(t, (function(t) {
e++;
})).next((function() {
return e;
}));
}, t.prototype.forEachTarget = function(t, e) {
return this.db.getTargetCache().forEachTarget(t, e);
}, t.prototype.Ln = function(t, e) {
return this.Kn(t, (function(t, n) {
return e(n);
}));
}, t.prototype.addReference = function(t, e, n) {
return uu(t, n);
}, t.prototype.removeReference = function(t, e, n) {
return uu(t, n);
}, t.prototype.removeTargets = function(t, e, n) {
return this.db.getTargetCache().removeTargets(t, e, n);
}, t.prototype.markPotentiallyOrphaned = function(t, e) {
return uu(t, e);
},
/**
* Returns true if anything would prevent this document from being garbage
* collected, given that the document in question is not present in any
* targets and has a sequence number less than or equal to the upper bound for
* the collection run.
*/
t.prototype.Gn = function(t, e) {
return function(t, e) {
var n = !1;
return Yo(t).tt((function(r) {
return zo(t, r, e).next((function(t) {
return t && (n = !0), xt.resolve(!t);
}));
})).next((function() {
return n;
}));
}(t, e);
}, t.prototype.removeOrphanedDocuments = function(t, e) {
var n = this, r = this.db.getRemoteDocumentCache().newChangeBuffer(), i = [], o = 0;
return this.Kn(t, (function(u, a) {
if (a <= e) {
var s = n.Gn(t, u).next((function(e) {
if (!e)
// Our size accounting requires us to read all documents before
// removing them.
return o++, r.getEntry(t, u).next((function() {
return r.removeEntry(u, at.min()), tu(t).delete([ 0, yi(u.path) ]);
}));
}));
i.push(s);
}
})).next((function() {
return xt.waitFor(i);
})).next((function() {
return r.apply(t);
})).next((function() {
return o;
}));
}, t.prototype.removeTarget = function(t, e) {
var n = e.withSequenceNumber(t.currentSequenceNumber);
return this.db.getTargetCache().updateTargetData(t, n);
}, t.prototype.updateLimboDocument = function(t, e) {
return uu(t, e);
},
/**
* Call provided function for each document in the cache that is 'orphaned'. Orphaned
* means not a part of any target, so the only entry in the target-document index for
* that document will be the sentinel row (targetId 0), which will also have the sequence
* number for the last time the document was accessed.
*/
t.prototype.Kn = function(t, e) {
var n, r = tu(t), i = qt.at;
return r.Z({
index: "documentTargetsIndex"
}, (function(t, r) {
var o = t[0];
t[1];
var u = r.path, a = r.sequenceNumber;
0 === o ? (
// if nextToReport is valid, report it, this is a new key so the
// last one must not be a member of any targets.
i !== qt.at && e(new ft(gi(n)), i),
// set nextToReport to be this sequence number. It's the next one we
// might report, if we don't find any targets for this document.
// Note that the sequence number must be defined when the targetId
// is 0.
i = a, n = u) :
// set nextToReport to be invalid, we know we don't need to report
// this one since we found a target for it.
i = qt.at;
})).next((function() {
// Since we report sequence numbers after getting to the next key, we
// need to check if the last key we iterated over was an orphaned
// document and report it.
i !== qt.at && e(new ft(gi(n)), i);
}));
}, t.prototype.getCacheSize = function(t) {
return this.db.getRemoteDocumentCache().getSize(t);
}, t;
}();
/**
* This class is responsible for the scheduling of LRU garbage collection. It handles checking
* whether or not GC is enabled, as well as which delay to use before the next run.
*/ function uu(t, e) {
return tu(t).put(
/**
* @returns A value suitable for writing a sentinel row in the target-document
* store.
*/
function(t, e) {
return {
targetId: 0,
path: yi(t.path),
sequenceNumber: e
};
}(e, t.currentSequenceNumber));
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* An in-memory buffer of entries to be written to a RemoteDocumentCache.
* It can be used to batch up a set of changes to be written to the cache, but
* additionally supports reading entries back with the `getEntry()` method,
* falling back to the underlying RemoteDocumentCache if no entry is
* buffered.
*
* Entries added to the cache *must* be read first. This is to facilitate
* calculating the size delta of the pending changes.
*
* PORTING NOTE: This class was implemented then removed from other platforms.
* If byte-counting ends up being needed on the other platforms, consider
* porting this class as part of that implementation work.
*/ var au = /** @class */ function() {
function t() {
// A mapping of document key to the new cache entry that should be written.
this.changes = new pr((function(t) {
return t.toString();
}), (function(t, e) {
return t.isEqual(e);
})), this.changesApplied = !1
/**
* Buffers a `RemoteDocumentCache.addEntry()` call.
*
* You can only modify documents that have already been retrieved via
* `getEntry()/getEntries()` (enforced via IndexedDbs `apply()`).
*/;
}
return t.prototype.addEntry = function(t) {
this.assertNotApplied(), this.changes.set(t.key, t);
},
/**
* Buffers a `RemoteDocumentCache.removeEntry()` call.
*
* You can only remove documents that have already been retrieved via
* `getEntry()/getEntries()` (enforced via IndexedDbs `apply()`).
*/
t.prototype.removeEntry = function(t, e) {
this.assertNotApplied(), this.changes.set(t, rn.newInvalidDocument(t).setReadTime(e));
},
/**
* Looks up an entry in the cache. The buffered changes will first be checked,
* and if no buffered change applies, this will forward to
* `RemoteDocumentCache.getEntry()`.
*
* @param transaction - The transaction in which to perform any persistence
* operations.
* @param documentKey - The key of the entry to look up.
* @returns The cached document or an invalid document if we have nothing
* cached.
*/
t.prototype.getEntry = function(t, e) {
this.assertNotApplied();
var n = this.changes.get(e);
return void 0 !== n ? xt.resolve(n) : this.getFromCache(t, e);
},
/**
* Looks up several entries in the cache, forwarding to
* `RemoteDocumentCache.getEntry()`.
*
* @param transaction - The transaction in which to perform any persistence
* operations.
* @param documentKeys - The keys of the entries to look up.
* @returns A map of cached documents, indexed by key. If an entry cannot be
* found, the corresponding key will be mapped to an invalid document.
*/
t.prototype.getEntries = function(t, e) {
return this.getAllFromCache(t, e);
},
/**
* Applies buffered changes to the underlying RemoteDocumentCache, using
* the provided transaction.
*/
t.prototype.apply = function(t) {
return this.assertNotApplied(), this.changesApplied = !0, this.applyChanges(t);
},
/** Helper to assert this.changes is not null */ t.prototype.assertNotApplied = function() {},
t;
}(), su = /** @class */ function() {
function t(t) {
this.yt = t;
}
return t.prototype.setIndexManager = function(t) {
this.indexManager = t;
},
/**
* Adds the supplied entries to the cache.
*
* All calls of `addEntry` are required to go through the RemoteDocumentChangeBuffer
* returned by `newChangeBuffer()` to ensure proper accounting of metadata.
*/
t.prototype.addEntry = function(t, e, n) {
return fu(t).put(n);
},
/**
* Removes a document from the cache.
*
* All calls of `removeEntry` are required to go through the RemoteDocumentChangeBuffer
* returned by `newChangeBuffer()` to ensure proper accounting of metadata.
*/
t.prototype.removeEntry = function(t, e, n) {
return fu(t).delete(
/**
* Returns a key that can be used for document lookups via the primary key of
* the DbRemoteDocument object store.
*/
function(t, e) {
var n = t.path.toArray();
return [
/* prefix path */ n.slice(0, n.length - 2),
/* collection id */ n[n.length - 2], Xi(e),
/* document id */ n[n.length - 1] ];
}(e, n));
},
/**
* Updates the current cache size.
*
* Callers to `addEntry()` and `removeEntry()` *must* call this afterwards to update the
* cache's metadata.
*/
t.prototype.updateMetadata = function(t, e) {
var n = this;
return this.getMetadata(t).next((function(r) {
return r.byteSize += e, n.Qn(t, r);
}));
}, t.prototype.getEntry = function(t, e) {
var n = this, r = rn.newInvalidDocument(e);
return fu(t).Z({
index: "documentKeyIndex",
range: IDBKeyRange.only(du(e))
}, (function(t, i) {
r = n.jn(e, i);
})).next((function() {
return r;
}));
},
/**
* Looks up an entry in the cache.
*
* @param documentKey - The key of the entry to look up.
* @returns The cached document entry and its size.
*/
t.prototype.Wn = function(t, e) {
var n = this, r = {
size: 0,
document: rn.newInvalidDocument(e)
};
return fu(t).Z({
index: "documentKeyIndex",
range: IDBKeyRange.only(du(e))
}, (function(t, i) {
r = {
document: n.jn(e, i),
size: jo(i)
};
})).next((function() {
return r;
}));
}, t.prototype.getEntries = function(t, e) {
var n = this, r = vr();
return this.zn(t, e, (function(t, e) {
var i = n.jn(t, e);
r = r.insert(t, i);
})).next((function() {
return r;
}));
},
/**
* Looks up several entries in the cache.
*
* @param documentKeys - The set of keys entries to look up.
* @returns A map of documents indexed by key and a map of sizes indexed by
* key (zero if the document does not exist).
*/
t.prototype.Hn = function(t, e) {
var n = this, r = vr(), i = new He(ft.comparator);
return this.zn(t, e, (function(t, e) {
var o = n.jn(t, e);
r = r.insert(t, o), i = i.insert(t, jo(e));
})).next((function() {
return {
documents: r,
Jn: i
};
}));
}, t.prototype.zn = function(t, e, n) {
if (e.isEmpty()) return xt.resolve();
var i = new Ze(yu);
e.forEach((function(t) {
return i = i.add(t);
}));
var o = IDBKeyRange.bound(du(i.first()), du(i.last())), u = i.getIterator(), a = u.getNext();
return fu(t).Z({
index: "documentKeyIndex",
range: o
}, (function(t, e, i) {
// Go through keys not found in cache.
for (var o = ft.fromSegments(r(r([], e.prefixPath, !0), [ e.collectionGroup, e.documentId ], !1)); a && yu(a, o) < 0; ) n(a, null),
a = u.getNext();
a && a.isEqual(o) && (
// Key found in cache.
n(a, e), a = u.hasNext() ? u.getNext() : null),
// Skip to the next key (if there is one).
a ? i.j(du(a)) : i.done();
})).next((function() {
// The rest of the keys are not in the cache. One case where `iterate`
// above won't go through them is when the cache is empty.
for (;a; ) n(a, null), a = u.hasNext() ? u.getNext() : null;
}));
}, t.prototype.getAllFromCollection = function(t, e, n) {
var r = this, i = [ e.popLast().toArray(), e.lastSegment(), Xi(n.readTime), n.documentKey.path.isEmpty() ? "" : n.documentKey.path.lastSegment() ], o = [ e.popLast().toArray(), e.lastSegment(), [ Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER ], "" ];
return fu(t).W(IDBKeyRange.bound(i, o, !0)).next((function(t) {
for (var e = vr(), n = 0, i = t; n < i.length; n++) {
var o = i[n], u = r.jn(ft.fromSegments(o.prefixPath.concat(o.collectionGroup, o.documentId)), o);
e = e.insert(u.key, u);
}
return e;
}));
}, t.prototype.getAllFromCollectionGroup = function(t, e, n, r) {
var i = this, o = vr(), u = pu(e, n), a = pu(e, Tt.max());
return fu(t).Z({
index: "collectionGroupIndex",
range: IDBKeyRange.bound(u, a, !0)
}, (function(t, e, n) {
var u = i.jn(ft.fromSegments(e.prefixPath.concat(e.collectionGroup, e.documentId)), e);
(o = o.insert(u.key, u)).size === r && n.done();
})).next((function() {
return o;
}));
}, t.prototype.newChangeBuffer = function(t) {
return new lu(this, !!t && t.trackRemovals);
}, t.prototype.getSize = function(t) {
return this.getMetadata(t).next((function(t) {
return t.byteSize;
}));
}, t.prototype.getMetadata = function(t) {
return hu(t).get("remoteDocumentGlobalKey").next((function(t) {
return U(!!t), t;
}));
}, t.prototype.Qn = function(t, e) {
return hu(t).put("remoteDocumentGlobalKey", e);
},
/**
* Decodes `dbRemoteDoc` and returns the document (or an invalid document if
* the document corresponds to the format used for sentinel deletes).
*/
t.prototype.jn = function(t, e) {
if (e) {
var n =
/** Decodes a remote document from storage locally to a Document. */ function(t, e) {
var n;
if (e.document) n = ti(t.ie, e.document, !!e.hasCommittedMutations); else if (e.noDocument) {
var r = ft.fromSegments(e.noDocument.path), i = Ji(e.noDocument.readTime);
n = rn.newNoDocument(r, i), e.hasCommittedMutations && n.setHasCommittedMutations();
} else {
if (!e.unknownDocument) return q();
var o = ft.fromSegments(e.unknownDocument.path), u = Ji(e.unknownDocument.version);
n = rn.newUnknownDocument(o, u);
}
return e.readTime && n.setReadTime(function(t) {
var e = new ut(t[0], t[1]);
return at.fromTimestamp(e);
}(e.readTime)), n;
}(this.yt, e);
// Whether the document is a sentinel removal and should only be used in the
// `getNewDocumentChanges()`
if (!n.isNoDocument() || !n.version.isEqual(at.min())) return n;
}
return rn.newInvalidDocument(t);
}, t;
}();
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The RemoteDocumentCache for IndexedDb. To construct, invoke
* `newIndexedDbRemoteDocumentCache()`.
*/
/** Creates a new IndexedDbRemoteDocumentCache. */ function cu(t) {
return new su(t);
}
/**
* Handles the details of adding and updating documents in the IndexedDbRemoteDocumentCache.
*
* Unlike the MemoryRemoteDocumentChangeBuffer, the IndexedDb implementation computes the size
* delta for all submitted changes. This avoids having to re-read all documents from IndexedDb
* when we apply the changes.
*/ var lu = /** @class */ function(e) {
/**
* @param documentCache - The IndexedDbRemoteDocumentCache to apply the changes to.
* @param trackRemovals - Whether to create sentinel deletes that can be tracked by
* `getNewDocumentChanges()`.
*/
function n(t, n) {
var r = this;
return (r = e.call(this) || this).Yn = t, r.trackRemovals = n,
// A map of document sizes and read times prior to applying the changes in
// this buffer.
r.Xn = new pr((function(t) {
return t.toString();
}), (function(t, e) {
return t.isEqual(e);
})), r;
}
return t(n, e), n.prototype.applyChanges = function(t) {
var e = this, n = [], r = 0, i = new Ze((function(t, e) {
return rt(t.canonicalString(), e.canonicalString());
}));
return this.changes.forEach((function(o, u) {
var a = e.Xn.get(o);
if (n.push(e.Yn.removeEntry(t, o, a.readTime)), u.isValidDocument()) {
var s = Yi(e.Yn.yt, u);
i = i.add(o.path.popLast());
var c = jo(s);
r += c - a.size, n.push(e.Yn.addEntry(t, o, s));
} else if (r -= a.size, e.trackRemovals) {
// In order to track removals, we store a "sentinel delete" in the
// RemoteDocumentCache. This entry is represented by a NoDocument
// with a version of 0 and ignored by `maybeDecodeDocument()` but
// preserved in `getNewDocumentChanges()`.
var l = Yi(e.Yn.yt, u.convertToNoDocument(at.min()));
n.push(e.Yn.addEntry(t, o, l));
}
})), i.forEach((function(r) {
n.push(e.Yn.indexManager.addToCollectionParentIndex(t, r));
})), n.push(this.Yn.updateMetadata(t, r)), xt.waitFor(n);
}, n.prototype.getFromCache = function(t, e) {
var n = this;
// Record the size of everything we load from the cache so we can compute a delta later.
return this.Yn.Wn(t, e).next((function(t) {
return n.Xn.set(e, {
size: t.size,
readTime: t.document.readTime
}), t.document;
}));
}, n.prototype.getAllFromCache = function(t, e) {
var n = this;
// Record the size of everything we load from the cache so we can compute
// a delta later.
return this.Yn.Hn(t, e).next((function(t) {
var e = t.documents;
// Note: `getAllFromCache` returns two maps instead of a single map from
// keys to `DocumentSizeEntry`s. This is to allow returning the
// `MutableDocumentMap` directly, without a conversion.
return t.Jn.forEach((function(t, r) {
n.Xn.set(t, {
size: r,
readTime: e.get(t).readTime
});
})), e;
}));
}, n;
}(au);
function hu(t) {
return Ki(t, "remoteDocumentGlobal");
}
/**
* Helper to get a typed SimpleDbStore for the remoteDocuments object store.
*/ function fu(t) {
return Ki(t, "remoteDocumentsV14");
}
/**
* Returns a key that can be used for document lookups on the
* `DbRemoteDocumentDocumentKeyIndex` index.
*/ function du(t) {
var e = t.path.toArray();
return [
/* prefix path */ e.slice(0, e.length - 2),
/* collection id */ e[e.length - 2],
/* document id */ e[e.length - 1] ];
}
function pu(t, e) {
var n = e.documentKey.path.toArray();
return [
/* collection id */ t, Xi(e.readTime),
/* prefix path */ n.slice(0, n.length - 2),
/* document id */ n.length > 0 ? n[n.length - 1] : "" ];
}
/**
* Comparator that compares document keys according to the primary key sorting
* used by the `DbRemoteDocumentDocument` store (by prefix path, collection id
* and then document ID).
*
* Visible for testing.
*/ function yu(t, e) {
for (var n = t.path.toArray(), r = e.path.toArray(), i = 0, o = 0
// The ordering is based on https://chromium.googlesource.com/chromium/blink/+/fe5c21fef94dae71c1c3344775b8d8a7f7e6d9ec/Source/modules/indexeddb/IDBKey.cpp#74
; o < n.length - 2 && o < r.length - 2; ++o) if (i = rt(n[o], r[o])) return i;
return (i = rt(n.length, r.length)) || ((i = rt(n[n.length - 2], r[r.length - 2])) || rt(n[n.length - 1], r[r.length - 1]));
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Schema Version for the Web client:
* 1. Initial version including Mutation Queue, Query Cache, and Remote
* Document Cache
* 2. Used to ensure a targetGlobal object exists and add targetCount to it. No
* longer required because migration 3 unconditionally clears it.
* 3. Dropped and re-created Query Cache to deal with cache corruption related
* to limbo resolution. Addresses
* https://github.com/firebase/firebase-ios-sdk/issues/1548
* 4. Multi-Tab Support.
* 5. Removal of held write acks.
* 6. Create document global for tracking document cache size.
* 7. Ensure every cached document has a sentinel row with a sequence number.
* 8. Add collection-parent index for Collection Group queries.
* 9. Change RemoteDocumentChanges store to be keyed by readTime rather than
* an auto-incrementing ID. This is required for Index-Free queries.
* 10. Rewrite the canonical IDs to the explicit Protobuf-based format.
* 11. Add bundles and named_queries for bundle support.
* 12. Add document overlays.
* 13. Rewrite the keys of the remote document cache to allow for efficient
* document lookup via `getAll()`.
* 14. Add overlays.
* 15. Add indexing support.
*/
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents a local view (overlay) of a document, and the fields that are
* locally mutated.
*/ var vu = function(t,
/**
* The fields that are locally mutated by patch mutations.
*
* If the overlayed document is from set or delete mutations, this is `null`.
* If there is no overlay (mutation) for the document, this is an empty `FieldMask`.
*/
e) {
this.overlayedDocument = t, this.mutatedFields = e;
}, mu = /** @class */ function() {
function t(t, e, n, r) {
this.remoteDocumentCache = t, this.mutationQueue = e, this.documentOverlayCache = n,
this.indexManager = r
/**
* Get the local view of the document identified by `key`.
*
* @returns Local view of the document or null if we don't have any cached
* state for it.
*/;
}
return t.prototype.getDocument = function(t, e) {
var n = this, r = null;
return this.documentOverlayCache.getOverlay(t, e).next((function(i) {
return r = i, n.remoteDocumentCache.getEntry(t, e);
})).next((function(t) {
return null !== r && $n(r.mutation, t, tn.empty(), ut.now()), t;
}));
},
/**
* Gets the local view of the documents identified by `keys`.
*
* If we don't have cached state for a document in `keys`, a NoDocument will
* be stored for that key in the resulting set.
*/
t.prototype.getDocuments = function(t, e) {
var n = this;
return this.remoteDocumentCache.getEntries(t, e).next((function(e) {
return n.getLocalViewOfDocuments(t, e, _r()).next((function() {
return e;
}));
}));
},
/**
* Similar to `getDocuments`, but creates the local view from the given
* `baseDocs` without retrieving documents from the local store.
*
* @param transaction - The transaction this operation is scoped to.
* @param docs - The documents to apply local mutations to get the local views.
* @param existenceStateChanged - The set of document keys whose existence state
* is changed. This is useful to determine if some documents overlay needs
* to be recalculated.
*/
t.prototype.getLocalViewOfDocuments = function(t, e, n) {
var r = this;
void 0 === n && (n = _r());
var i = br();
return this.populateOverlays(t, i, e).next((function() {
return r.computeViews(t, e, i, n).next((function(t) {
var e = gr();
return t.forEach((function(t, n) {
e = e.insert(t, n.overlayedDocument);
})), e;
}));
}));
},
/**
* Gets the overlayed documents for the given document map, which will include
* the local view of those documents and a `FieldMask` indicating which fields
* are mutated locally, `null` if overlay is a Set or Delete mutation.
*/
t.prototype.getOverlayedDocuments = function(t, e) {
var n = this, r = br();
return this.populateOverlays(t, r, e).next((function() {
return n.computeViews(t, e, r, _r());
}));
},
/**
* Fetches the overlays for {@code docs} and adds them to provided overlay map
* if the map does not already contain an entry for the given document key.
*/
t.prototype.populateOverlays = function(t, e, n) {
var r = [];
return n.forEach((function(t) {
e.has(t) || r.push(t);
})), this.documentOverlayCache.getOverlays(t, r).next((function(t) {
t.forEach((function(t, n) {
e.set(t, n);
}));
}));
},
/**
* Computes the local view for the given documents.
*
* @param docs - The documents to compute views for. It also has the base
* version of the documents.
* @param overlays - The overlays that need to be applied to the given base
* version of the documents.
* @param existenceStateChanged - A set of documents whose existence states
* might have changed. This is used to determine if we need to re-calculate
* overlays from mutation queues.
* @return A map represents the local documents view.
*/
t.prototype.computeViews = function(t, e, n, r) {
var i = vr(), o = Tr(), u = Tr();
return e.forEach((function(t, e) {
var u = n.get(e.key);
// Recalculate an overlay if the document's existence state changed due to
// a remote event *and* the overlay is a PatchMutation. This is because
// document existence state can change if some patch mutation's
// preconditions are met.
// NOTE: we recalculate when `overlay` is undefined as well, because there
// might be a patch mutation whose precondition does not match before the
// change (hence overlay is undefined), but would now match.
r.has(e.key) && (void 0 === u || u.mutation instanceof rr) ? i = i.insert(e.key, e) : void 0 !== u ? (o.set(e.key, u.mutation.getFieldMask()),
$n(u.mutation, e, u.mutation.getFieldMask(), ut.now())) :
// no overlay exists
// Using EMPTY to indicate there is no overlay for the document.
o.set(e.key, tn.empty());
})), this.recalculateAndSaveOverlays(t, i).next((function(t) {
return t.forEach((function(t, e) {
return o.set(t, e);
})), e.forEach((function(t, e) {
var n;
return u.set(t, new vu(e, null !== (n = o.get(t)) && void 0 !== n ? n : null));
})), u;
}));
}, t.prototype.recalculateAndSaveOverlays = function(t, e) {
var n = this, r = Tr(), i = new He((function(t, e) {
return t - e;
})), o = _r();
return this.mutationQueue.getAllMutationBatchesAffectingDocumentKeys(t, e).next((function(t) {
for (var n = function(t) {
t.keys().forEach((function(n) {
var o = e.get(n);
if (null !== o) {
var u = r.get(n) || tn.empty();
u = t.applyToLocalView(o, u), r.set(n, u);
var a = (i.get(t.batchId) || _r()).add(n);
i = i.insert(t.batchId, a);
}
}));
}, o = 0, u = t; o < u.length; o++) {
n(u[o]);
}
})).next((function() {
// Iterate in descending order of batch IDs, and skip documents that are
// already saved.
for (var u = [], a = i.getReverseIterator(), s = function() {
var i = a.getNext(), s = i.key, c = i.value, l = Ir();
c.forEach((function(t) {
if (!o.has(t)) {
var n = Zn(e.get(t), r.get(t));
null !== n && l.set(t, n), o = o.add(t);
}
})), u.push(n.documentOverlayCache.saveOverlays(t, s, l));
}; a.hasNext(); ) s();
return xt.waitFor(u);
})).next((function() {
return r;
}));
},
/**
* Recalculates overlays by reading the documents from remote document cache
* first, and saves them after they are calculated.
*/
t.prototype.recalculateAndSaveOverlaysForDocumentKeys = function(t, e) {
var n = this;
return this.remoteDocumentCache.getEntries(t, e).next((function(e) {
return n.recalculateAndSaveOverlays(t, e);
}));
},
/**
* Performs a query against the local view of all documents.
*
* @param transaction - The persistence transaction.
* @param query - The query to match documents against.
* @param offset - Read time and key to start scanning by (exclusive).
*/
t.prototype.getDocumentsMatchingQuery = function(t, e, n) {
/**
* Returns whether the query matches a single document by path (rather than a
* collection).
*/
return function(t) {
return ft.isDocumentKey(t.path) && null === t.collectionGroup && 0 === t.filters.length;
}(e) ? this.getDocumentsMatchingDocumentQuery(t, e.path) : wn(e) ? this.getDocumentsMatchingCollectionGroupQuery(t, e, n) : this.getDocumentsMatchingCollectionQuery(t, e, n);
},
/**
* Given a collection group, returns the next documents that follow the provided offset, along
* with an updated batch ID.
*
*
The documents returned by this method are ordered by remote version from the provided
* offset. If there are no more remote documents after the provided offset, documents with
* mutations in order of batch id from the offset are returned. Since all documents in a batch are
* returned together, the total number of documents returned can exceed {@code count}.
*
* @param transaction
* @param collectionGroup The collection group for the documents.
* @param offset The offset to index into.
* @param count The number of documents to return
* @return A LocalWriteResult with the documents that follow the provided offset and the last processed batch id.
*/
t.prototype.getNextDocuments = function(t, e, n, r) {
var i = this;
return this.remoteDocumentCache.getAllFromCollectionGroup(t, e, n, r).next((function(o) {
var u = r - o.size > 0 ? i.documentOverlayCache.getOverlaysForCollectionGroup(t, e, n.largestBatchId, r - o.size) : xt.resolve(br()), a = -1, s = o;
// The callsite will use the largest batch ID together with the latest read time to create
// a new index offset. Since we only process batch IDs if all remote documents have been read,
// no overlay will increase the overall read time. This is why we only need to special case
// the batch id.
return u.next((function(e) {
return xt.forEach(e, (function(e, n) {
return a < n.largestBatchId && (a = n.largestBatchId), o.get(e) ? xt.resolve() : i.remoteDocumentCache.getEntry(t, e).next((function(t) {
s = s.insert(e, t);
}));
})).next((function() {
return i.populateOverlays(t, e, o);
})).next((function() {
return i.computeViews(t, s, e, _r());
})).next((function(t) {
return {
batchId: a,
changes: wr(t)
};
}));
}));
}));
}, t.prototype.getDocumentsMatchingDocumentQuery = function(t, e) {
// Just do a simple document lookup.
return this.getDocument(t, new ft(e)).next((function(t) {
var e = gr();
return t.isFoundDocument() && (e = e.insert(t.key, t)), e;
}));
}, t.prototype.getDocumentsMatchingCollectionGroupQuery = function(t, e, n) {
var r = this, i = e.collectionGroup, o = gr();
return this.indexManager.getCollectionParents(t, i).next((function(u) {
return xt.forEach(u, (function(u) {
var a = function(t, e) {
return new dn(e,
/*collectionGroup=*/ null, t.explicitOrderBy.slice(), t.filters.slice(), t.limit, t.limitType, t.startAt, t.endAt);
}(e, u.child(i));
return r.getDocumentsMatchingCollectionQuery(t, a, n).next((function(t) {
t.forEach((function(t, e) {
o = o.insert(t, e);
}));
}));
})).next((function() {
return o;
}));
}));
}, t.prototype.getDocumentsMatchingCollectionQuery = function(t, e, n) {
var r, i = this;
// Query the remote documents and overlay mutations.
return this.remoteDocumentCache.getAllFromCollection(t, e.path, n).next((function(o) {
return r = o, i.documentOverlayCache.getOverlaysForCollection(t, e.path, n.largestBatchId);
})).next((function(t) {
// As documents might match the query because of their overlay we need to
// include documents for all overlays in the initial document set.
t.forEach((function(t, e) {
var n = e.getKey();
null === r.get(n) && (r = r.insert(n, rn.newInvalidDocument(n)));
}));
// Apply the overlays and match against the query.
var n = gr();
return r.forEach((function(r, i) {
var o = t.get(r);
void 0 !== o && $n(o.mutation, i, tn.empty(), ut.now()),
// Finally, insert the documents that still match the query
xn(e, i) && (n = n.insert(r, i));
})), n;
}));
}, t;
}(), gu = /** @class */ function() {
function t(t) {
this.yt = t, this.Zn = new Map, this.ts = new Map;
}
return t.prototype.getBundleMetadata = function(t, e) {
return xt.resolve(this.Zn.get(e));
}, t.prototype.saveBundleMetadata = function(t, e) {
/** Decodes a BundleMetadata proto into a BundleMetadata object. */
var n;
return this.Zn.set(e.id, {
id: (n = e).id,
version: n.version,
createTime: jr(n.createTime)
}), xt.resolve();
}, t.prototype.getNamedQuery = function(t, e) {
return xt.resolve(this.ts.get(e));
}, t.prototype.saveNamedQuery = function(t, e) {
return this.ts.set(e.name, function(t) {
return {
name: t.name,
query: no(t.bundledQuery),
readTime: jr(t.readTime)
};
}(e)), xt.resolve();
}, t;
}(), wu = /** @class */ function() {
function t() {
// A map sorted by DocumentKey, whose value is a pair of the largest batch id
// for the overlay and the overlay itself.
this.overlays = new He(ft.comparator), this.es = new Map;
}
return t.prototype.getOverlay = function(t, e) {
return xt.resolve(this.overlays.get(e));
}, t.prototype.getOverlays = function(t, e) {
var n = this, r = br();
return xt.forEach(e, (function(e) {
return n.getOverlay(t, e).next((function(t) {
null !== t && r.set(e, t);
}));
})).next((function() {
return r;
}));
}, t.prototype.saveOverlays = function(t, e, n) {
var r = this;
return n.forEach((function(n, i) {
r.oe(t, e, i);
})), xt.resolve();
}, t.prototype.removeOverlaysForBatchId = function(t, e, n) {
var r = this, i = this.es.get(n);
return void 0 !== i && (i.forEach((function(t) {
return r.overlays = r.overlays.remove(t);
})), this.es.delete(n)), xt.resolve();
}, t.prototype.getOverlaysForCollection = function(t, e, n) {
for (var r = br(), i = e.length + 1, o = new ft(e.child("")), u = this.overlays.getIteratorFrom(o); u.hasNext(); ) {
var a = u.getNext().value, s = a.getKey();
if (!e.isPrefixOf(s.path)) break;
// Documents from sub-collections
s.path.length === i && a.largestBatchId > n && r.set(a.getKey(), a);
}
return xt.resolve(r);
}, t.prototype.getOverlaysForCollectionGroup = function(t, e, n, r) {
for (var i = new He((function(t, e) {
return t - e;
})), o = this.overlays.getIterator(); o.hasNext(); ) {
var u = o.getNext().value;
if (u.getKey().getCollectionGroup() === e && u.largestBatchId > n) {
var a = i.get(u.largestBatchId);
null === a && (a = br(), i = i.insert(u.largestBatchId, a)), a.set(u.getKey(), u);
}
}
for (var s = br(), c = i.getIterator(); c.hasNext() && (c.getNext().value.forEach((function(t, e) {
return s.set(t, e);
})), !(s.size() >= r)); ) ;
return xt.resolve(s);
}, t.prototype.oe = function(t, e, n) {
// Remove the association of the overlay to its batch id.
var r = this.overlays.get(n.key);
if (null !== r) {
var i = this.es.get(r.largestBatchId).delete(n.key);
this.es.set(r.largestBatchId, i);
}
this.overlays = this.overlays.insert(n.key, new zi(e, n));
// Create the association of this overlay to the given largestBatchId.
var o = this.es.get(e);
void 0 === o && (o = _r(), this.es.set(e, o)), this.es.set(e, o.add(n.key));
}, t;
}(), bu = /** @class */ function() {
function t() {
// A set of outstanding references to a document sorted by key.
this.ns = new Ze(Iu.ss),
// A set of outstanding references to a document sorted by target id.
this.rs = new Ze(Iu.os)
/** Returns true if the reference set contains no references. */;
}
return t.prototype.isEmpty = function() {
return this.ns.isEmpty();
},
/** Adds a reference to the given document key for the given ID. */ t.prototype.addReference = function(t, e) {
var n = new Iu(t, e);
this.ns = this.ns.add(n), this.rs = this.rs.add(n);
},
/** Add references to the given document keys for the given ID. */ t.prototype.us = function(t, e) {
var n = this;
t.forEach((function(t) {
return n.addReference(t, e);
}));
},
/**
* Removes a reference to the given document key for the given
* ID.
*/
t.prototype.removeReference = function(t, e) {
this.cs(new Iu(t, e));
}, t.prototype.hs = function(t, e) {
var n = this;
t.forEach((function(t) {
return n.removeReference(t, e);
}));
},
/**
* Clears all references with a given ID. Calls removeRef() for each key
* removed.
*/
t.prototype.ls = function(t) {
var e = this, n = new ft(new ct([])), r = new Iu(n, t), i = new Iu(n, t + 1), o = [];
return this.rs.forEachInRange([ r, i ], (function(t) {
e.cs(t), o.push(t.key);
})), o;
}, t.prototype.fs = function() {
var t = this;
this.ns.forEach((function(e) {
return t.cs(e);
}));
}, t.prototype.cs = function(t) {
this.ns = this.ns.delete(t), this.rs = this.rs.delete(t);
}, t.prototype.ds = function(t) {
var e = new ft(new ct([])), n = new Iu(e, t), r = new Iu(e, t + 1), i = _r();
return this.rs.forEachInRange([ n, r ], (function(t) {
i = i.add(t.key);
})), i;
}, t.prototype.containsKey = function(t) {
var e = new Iu(t, 0), n = this.ns.firstAfterOrEqual(e);
return null !== n && t.isEqual(n.key);
}, t;
}(), Iu = /** @class */ function() {
function t(t, e) {
this.key = t, this._s = e
/** Compare by key then by ID */;
}
return t.ss = function(t, e) {
return ft.comparator(t.key, e.key) || rt(t._s, e._s);
},
/** Compare by ID then by key */ t.os = function(t, e) {
return rt(t._s, e._s) || ft.comparator(t.key, e.key);
}, t;
}(), Tu = /** @class */ function() {
function t(t, e) {
this.indexManager = t, this.referenceDelegate = e,
/**
* The set of all mutations that have been sent but not yet been applied to
* the backend.
*/
this.mutationQueue = [],
/** Next value to use when assigning sequential IDs to each mutation batch. */
this.ws = 1,
/** An ordered mapping between documents and the mutations batch IDs. */
this.gs = new Ze(Iu.ss);
}
return t.prototype.checkEmpty = function(t) {
return xt.resolve(0 === this.mutationQueue.length);
}, t.prototype.addMutationBatch = function(t, e, n, r) {
var i = this.ws;
this.ws++, this.mutationQueue.length > 0 && this.mutationQueue[this.mutationQueue.length - 1];
var o = new ji(i, e, n, r);
this.mutationQueue.push(o);
// Track references by document key and index collection parents.
for (var u = 0, a = r; u < a.length; u++) {
var s = a[u];
this.gs = this.gs.add(new Iu(s.key, i)), this.indexManager.addToCollectionParentIndex(t, s.key.path.popLast());
}
return xt.resolve(o);
}, t.prototype.lookupMutationBatch = function(t, e) {
return xt.resolve(this.ys(e));
}, t.prototype.getNextMutationBatchAfterBatchId = function(t, e) {
var n = e + 1, r = this.ps(n), i = r < 0 ? 0 : r;
// The requested batchId may still be out of range so normalize it to the
// start of the queue.
return xt.resolve(this.mutationQueue.length > i ? this.mutationQueue[i] : null);
}, t.prototype.getHighestUnacknowledgedBatchId = function() {
return xt.resolve(0 === this.mutationQueue.length ? -1 : this.ws - 1);
}, t.prototype.getAllMutationBatches = function(t) {
return xt.resolve(this.mutationQueue.slice());
}, t.prototype.getAllMutationBatchesAffectingDocumentKey = function(t, e) {
var n = this, r = new Iu(e, 0), i = new Iu(e, Number.POSITIVE_INFINITY), o = [];
return this.gs.forEachInRange([ r, i ], (function(t) {
var e = n.ys(t._s);
o.push(e);
})), xt.resolve(o);
}, t.prototype.getAllMutationBatchesAffectingDocumentKeys = function(t, e) {
var n = this, r = new Ze(rt);
return e.forEach((function(t) {
var e = new Iu(t, 0), i = new Iu(t, Number.POSITIVE_INFINITY);
n.gs.forEachInRange([ e, i ], (function(t) {
r = r.add(t._s);
}));
})), xt.resolve(this.Is(r));
}, t.prototype.getAllMutationBatchesAffectingQuery = function(t, e) {
// Use the query path as a prefix for testing if a document matches the
// query.
var n = e.path, r = n.length + 1, i = n;
// Construct a document reference for actually scanning the index. Unlike
// the prefix the document key in this reference must have an even number of
// segments. The empty segment can be used a suffix of the query path
// because it precedes all other segments in an ordered traversal.
ft.isDocumentKey(i) || (i = i.child(""));
var o = new Iu(new ft(i), 0), u = new Ze(rt);
// Find unique batchIDs referenced by all documents potentially matching the
// query.
return this.gs.forEachWhile((function(t) {
var e = t.key.path;
return !!n.isPrefixOf(e) && (
// Rows with document keys more than one segment longer than the query
// path can't be matches. For example, a query on 'rooms' can't match
// the document /rooms/abc/messages/xyx.
// TODO(mcg): we'll need a different scanner when we implement
// ancestor queries.
e.length === r && (u = u.add(t._s)), !0);
}), o), xt.resolve(this.Is(u));
}, t.prototype.Is = function(t) {
var e = this, n = [];
// Construct an array of matching batches, sorted by batchID to ensure that
// multiple mutations affecting the same document key are applied in order.
return t.forEach((function(t) {
var r = e.ys(t);
null !== r && n.push(r);
})), n;
}, t.prototype.removeMutationBatch = function(t, e) {
var n = this;
U(0 === this.Ts(e.batchId, "removed")), this.mutationQueue.shift();
var r = this.gs;
return xt.forEach(e.mutations, (function(i) {
var o = new Iu(i.key, e.batchId);
return r = r.delete(o), n.referenceDelegate.markPotentiallyOrphaned(t, i.key);
})).next((function() {
n.gs = r;
}));
}, t.prototype.An = function(t) {
// No-op since the memory mutation queue does not maintain a separate cache.
}, t.prototype.containsKey = function(t, e) {
var n = new Iu(e, 0), r = this.gs.firstAfterOrEqual(n);
return xt.resolve(e.isEqual(r && r.key));
}, t.prototype.performConsistencyCheck = function(t) {
return this.mutationQueue.length, xt.resolve();
},
/**
* Finds the index of the given batchId in the mutation queue and asserts that
* the resulting index is within the bounds of the queue.
*
* @param batchId - The batchId to search for
* @param action - A description of what the caller is doing, phrased in passive
* form (e.g. "acknowledged" in a routine that acknowledges batches).
*/
t.prototype.Ts = function(t, e) {
return this.ps(t);
},
/**
* Finds the index of the given batchId in the mutation queue. This operation
* is O(1).
*
* @returns The computed index of the batch with the given batchId, based on
* the state of the queue. Note this index can be negative if the requested
* batchId has already been remvoed from the queue or past the end of the
* queue if the batchId is larger than the last added batch.
*/
t.prototype.ps = function(t) {
return 0 === this.mutationQueue.length ? 0 : t - this.mutationQueue[0].batchId;
// Examine the front of the queue to figure out the difference between the
// batchId and indexes in the array. Note that since the queue is ordered
// by batchId, if the first batch has a larger batchId then the requested
// batchId doesn't exist in the queue.
},
/**
* A version of lookupMutationBatch that doesn't return a promise, this makes
* other functions that uses this code easier to read and more efficent.
*/
t.prototype.ys = function(t) {
var e = this.ps(t);
return e < 0 || e >= this.mutationQueue.length ? null : this.mutationQueue[e];
}, t;
}(), Eu = /** @class */ function() {
/**
* @param sizer - Used to assess the size of a document. For eager GC, this is
* expected to just return 0 to avoid unnecessarily doing the work of
* calculating the size.
*/
function t(t) {
this.Es = t,
/** Underlying cache of documents and their read times. */
this.docs = new He(ft.comparator),
/** Size of all cached documents. */
this.size = 0;
}
return t.prototype.setIndexManager = function(t) {
this.indexManager = t;
},
/**
* Adds the supplied entry to the cache and updates the cache size as appropriate.
*
* All calls of `addEntry` are required to go through the RemoteDocumentChangeBuffer
* returned by `newChangeBuffer()`.
*/
t.prototype.addEntry = function(t, e) {
var n = e.key, r = this.docs.get(n), i = r ? r.size : 0, o = this.Es(e);
return this.docs = this.docs.insert(n, {
document: e.mutableCopy(),
size: o
}), this.size += o - i, this.indexManager.addToCollectionParentIndex(t, n.path.popLast());
},
/**
* Removes the specified entry from the cache and updates the cache size as appropriate.
*
* All calls of `removeEntry` are required to go through the RemoteDocumentChangeBuffer
* returned by `newChangeBuffer()`.
*/
t.prototype.removeEntry = function(t) {
var e = this.docs.get(t);
e && (this.docs = this.docs.remove(t), this.size -= e.size);
}, t.prototype.getEntry = function(t, e) {
var n = this.docs.get(e);
return xt.resolve(n ? n.document.mutableCopy() : rn.newInvalidDocument(e));
}, t.prototype.getEntries = function(t, e) {
var n = this, r = vr();
return e.forEach((function(t) {
var e = n.docs.get(t);
r = r.insert(t, e ? e.document.mutableCopy() : rn.newInvalidDocument(t));
})), xt.resolve(r);
}, t.prototype.getAllFromCollection = function(t, e, n) {
for (var r = vr(), i = new ft(e.child("")), o = this.docs.getIteratorFrom(i)
// Documents are ordered by key, so we can use a prefix scan to narrow down
// the documents we need to match the query against.
; o.hasNext(); ) {
var u = o.getNext(), a = u.key, s = u.value.document;
if (!e.isPrefixOf(a.path)) break;
a.path.length > e.length + 1 || Et(It(s), n) <= 0 || (r = r.insert(s.key, s.mutableCopy()));
}
return xt.resolve(r);
}, t.prototype.getAllFromCollectionGroup = function(t, e, n, r) {
// This method should only be called from the IndexBackfiller if persistence
// is enabled.
q();
}, t.prototype.As = function(t, e) {
return xt.forEach(this.docs, (function(t) {
return e(t);
}));
}, t.prototype.newChangeBuffer = function(t) {
// `trackRemovals` is ignores since the MemoryRemoteDocumentCache keeps
// a separate changelog and does not need special handling for removals.
return new Su(this);
}, t.prototype.getSize = function(t) {
return xt.resolve(this.size);
}, t;
}(), Su = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this) || this).Yn = t, n;
}
return t(n, e), n.prototype.applyChanges = function(t) {
var e = this, n = [];
return this.changes.forEach((function(r, i) {
i.isValidDocument() ? n.push(e.Yn.addEntry(t, i)) : e.Yn.removeEntry(r);
})), xt.waitFor(n);
}, n.prototype.getFromCache = function(t, e) {
return this.Yn.getEntry(t, e);
}, n.prototype.getAllFromCache = function(t, e) {
return this.Yn.getEntries(t, e);
}, n;
}(au), _u = /** @class */ function() {
function t(t) {
this.persistence = t,
/**
* Maps a target to the data about that target
*/
this.Rs = new pr((function(t) {
return an(t);
}), sn),
/** The last received snapshot version. */
this.lastRemoteSnapshotVersion = at.min(),
/** The highest numbered target ID encountered. */
this.highestTargetId = 0,
/** The highest sequence number encountered. */
this.bs = 0,
/**
* A ordered bidirectional mapping between documents and the remote target
* IDs.
*/
this.Ps = new bu, this.targetCount = 0, this.vs = Xo.Pn();
}
return t.prototype.forEachTarget = function(t, e) {
return this.Rs.forEach((function(t, n) {
return e(n);
})), xt.resolve();
}, t.prototype.getLastRemoteSnapshotVersion = function(t) {
return xt.resolve(this.lastRemoteSnapshotVersion);
}, t.prototype.getHighestSequenceNumber = function(t) {
return xt.resolve(this.bs);
}, t.prototype.allocateTargetId = function(t) {
return this.highestTargetId = this.vs.next(), xt.resolve(this.highestTargetId);
}, t.prototype.setTargetsMetadata = function(t, e, n) {
return n && (this.lastRemoteSnapshotVersion = n), e > this.bs && (this.bs = e),
xt.resolve();
}, t.prototype.Dn = function(t) {
this.Rs.set(t.target, t);
var e = t.targetId;
e > this.highestTargetId && (this.vs = new Xo(e), this.highestTargetId = e), t.sequenceNumber > this.bs && (this.bs = t.sequenceNumber);
}, t.prototype.addTargetData = function(t, e) {
return this.Dn(e), this.targetCount += 1, xt.resolve();
}, t.prototype.updateTargetData = function(t, e) {
return this.Dn(e), xt.resolve();
}, t.prototype.removeTargetData = function(t, e) {
return this.Rs.delete(e.target), this.Ps.ls(e.targetId), this.targetCount -= 1,
xt.resolve();
}, t.prototype.removeTargets = function(t, e, n) {
var r = this, i = 0, o = [];
return this.Rs.forEach((function(u, a) {
a.sequenceNumber <= e && null === n.get(a.targetId) && (r.Rs.delete(u), o.push(r.removeMatchingKeysForTargetId(t, a.targetId)),
i++);
})), xt.waitFor(o).next((function() {
return i;
}));
}, t.prototype.getTargetCount = function(t) {
return xt.resolve(this.targetCount);
}, t.prototype.getTargetData = function(t, e) {
var n = this.Rs.get(e) || null;
return xt.resolve(n);
}, t.prototype.addMatchingKeys = function(t, e, n) {
return this.Ps.us(e, n), xt.resolve();
}, t.prototype.removeMatchingKeys = function(t, e, n) {
this.Ps.hs(e, n);
var r = this.persistence.referenceDelegate, i = [];
return r && e.forEach((function(e) {
i.push(r.markPotentiallyOrphaned(t, e));
})), xt.waitFor(i);
}, t.prototype.removeMatchingKeysForTargetId = function(t, e) {
return this.Ps.ls(e), xt.resolve();
}, t.prototype.getMatchingKeysForTargetId = function(t, e) {
var n = this.Ps.ds(e);
return xt.resolve(n);
}, t.prototype.containsKey = function(t, e) {
return xt.resolve(this.Ps.containsKey(e));
}, t;
}(), Du = /** @class */ function() {
/**
* The constructor accepts a factory for creating a reference delegate. This
* allows both the delegate and this instance to have strong references to
* each other without having nullable fields that would then need to be
* checked or asserted on every access.
*/
function t(t, e) {
var n = this;
this.Vs = {}, this.overlays = {}, this.Ss = new qt(0), this.Ds = !1, this.Ds = !0,
this.referenceDelegate = t(this), this.Cs = new _u(this), this.indexManager = new Fo,
this.remoteDocumentCache = new Eu((function(t) {
return n.referenceDelegate.xs(t);
})), this.yt = new Hi(e), this.Ns = new gu(this.yt);
}
return t.prototype.start = function() {
return Promise.resolve();
}, t.prototype.shutdown = function() {
// No durable state to ensure is closed on shutdown.
return this.Ds = !1, Promise.resolve();
}, Object.defineProperty(t.prototype, "started", {
get: function() {
return this.Ds;
},
enumerable: !1,
configurable: !0
}), t.prototype.setDatabaseDeletedListener = function() {
// No op.
}, t.prototype.setNetworkEnabled = function() {
// No op.
}, t.prototype.getIndexManager = function(t) {
// We do not currently support indices for memory persistence, so we can
// return the same shared instance of the memory index manager.
return this.indexManager;
}, t.prototype.getDocumentOverlayCache = function(t) {
var e = this.overlays[t.toKey()];
return e || (e = new wu, this.overlays[t.toKey()] = e), e;
}, t.prototype.getMutationQueue = function(t, e) {
var n = this.Vs[t.toKey()];
return n || (n = new Tu(e, this.referenceDelegate), this.Vs[t.toKey()] = n), n;
}, t.prototype.getTargetCache = function() {
return this.Cs;
}, t.prototype.getRemoteDocumentCache = function() {
return this.remoteDocumentCache;
}, t.prototype.getBundleCache = function() {
return this.Ns;
}, t.prototype.runTransaction = function(t, e, n) {
var r = this;
V("MemoryPersistence", "Starting transaction:", t);
var i = new xu(this.Ss.next());
return this.referenceDelegate.ks(), n(i).next((function(t) {
return r.referenceDelegate.Os(i).next((function() {
return t;
}));
})).toPromise().then((function(t) {
return i.raiseOnCommittedEvent(), t;
}));
}, t.prototype.Ms = function(t, e) {
return xt.or(Object.values(this.Vs).map((function(n) {
return function() {
return n.containsKey(t, e);
};
})));
}, t;
}(), xu = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this) || this).currentSequenceNumber = t, n;
}
return t(n, e), n;
}(_t), Au = /** @class */ function() {
function t(t) {
this.persistence = t,
/** Tracks all documents that are active in Query views. */
this.Fs = new bu,
/** The list of documents that are potentially GCed after each transaction. */
this.$s = null;
}
return t.Bs = function(e) {
return new t(e);
}, Object.defineProperty(t.prototype, "Ls", {
get: function() {
if (this.$s) return this.$s;
throw q();
},
enumerable: !1,
configurable: !0
}), t.prototype.addReference = function(t, e, n) {
return this.Fs.addReference(n, e), this.Ls.delete(n.toString()), xt.resolve();
}, t.prototype.removeReference = function(t, e, n) {
return this.Fs.removeReference(n, e), this.Ls.add(n.toString()), xt.resolve();
}, t.prototype.markPotentiallyOrphaned = function(t, e) {
return this.Ls.add(e.toString()), xt.resolve();
}, t.prototype.removeTarget = function(t, e) {
var n = this;
this.Fs.ls(e.targetId).forEach((function(t) {
return n.Ls.add(t.toString());
}));
var r = this.persistence.getTargetCache();
return r.getMatchingKeysForTargetId(t, e.targetId).next((function(t) {
t.forEach((function(t) {
return n.Ls.add(t.toString());
}));
})).next((function() {
return r.removeTargetData(t, e);
}));
}, t.prototype.ks = function() {
this.$s = new Set;
}, t.prototype.Os = function(t) {
var e = this, n = this.persistence.getRemoteDocumentCache().newChangeBuffer();
// Remove newly orphaned documents.
return xt.forEach(this.Ls, (function(r) {
var i = ft.fromPath(r);
return e.qs(t, i).next((function(t) {
t || n.removeEntry(i, at.min());
}));
})).next((function() {
return e.$s = null, n.apply(t);
}));
}, t.prototype.updateLimboDocument = function(t, e) {
var n = this;
return this.qs(t, e).next((function(t) {
t ? n.Ls.delete(e.toString()) : n.Ls.add(e.toString());
}));
}, t.prototype.xs = function(t) {
// For eager GC, we don't care about the document size, there are no size thresholds.
return 0;
}, t.prototype.qs = function(t, e) {
var n = this;
return xt.or([ function() {
return xt.resolve(n.Fs.containsKey(e));
}, function() {
return n.persistence.getTargetCache().containsKey(t, e);
}, function() {
return n.persistence.Ms(t, e);
} ]);
}, t;
}(), Cu = /** @class */ function() {
function t(t) {
this.yt = t;
}
/**
* Performs database creation and schema upgrades.
*
* Note that in production, this method is only ever used to upgrade the schema
* to SCHEMA_VERSION. Different values of toVersion are only used for testing
* and local feature development.
*/ return t.prototype.$ = function(t, e, n, r) {
var i = this, o = new At("createOrUpgrade", e);
n < 1 && r >= 1 && (function(t) {
t.createObjectStore("owner");
}(t), function(t) {
t.createObjectStore("mutationQueues", {
keyPath: "userId"
}), t.createObjectStore("mutations", {
keyPath: "batchId",
autoIncrement: !0
}).createIndex("userMutationsIndex", wi, {
unique: !0
}), t.createObjectStore("documentMutations");
}(t), Nu(t), function(t) {
t.createObjectStore("remoteDocuments");
}(t));
// Migration 2 to populate the targetGlobal object no longer needed since
// migration 3 unconditionally clears it.
var u = xt.resolve();
return n < 3 && r >= 3 && (
// Brand new clients don't need to drop and recreate--only clients that
// potentially have corrupt data.
0 !== n && (function(t) {
t.deleteObjectStore("targetDocuments"), t.deleteObjectStore("targets"), t.deleteObjectStore("targetGlobal");
}(t), Nu(t)), u = u.next((function() {
/**
* Creates the target global singleton row.
*
* @param txn - The version upgrade transaction for indexeddb
*/
return function(t) {
var e = t.store("targetGlobal"), n = {
highestTargetId: 0,
highestListenSequenceNumber: 0,
lastRemoteSnapshotVersion: at.min().toTimestamp(),
targetCount: 0
};
return e.put("targetGlobalKey", n);
}(o);
}))), n < 4 && r >= 4 && (0 !== n && (
// Schema version 3 uses auto-generated keys to generate globally unique
// mutation batch IDs (this was previously ensured internally by the
// client). To migrate to the new schema, we have to read all mutations
// and write them back out. We preserve the existing batch IDs to guarantee
// consistency with other object stores. Any further mutation batch IDs will
// be auto-generated.
u = u.next((function() {
return function(t, e) {
return e.store("mutations").W().next((function(n) {
t.deleteObjectStore("mutations"), t.createObjectStore("mutations", {
keyPath: "batchId",
autoIncrement: !0
}).createIndex("userMutationsIndex", wi, {
unique: !0
});
var r = e.store("mutations"), i = n.map((function(t) {
return r.put(t);
}));
return xt.waitFor(i);
}));
}(t, o);
}))), u = u.next((function() {
!function(t) {
t.createObjectStore("clientMetadata", {
keyPath: "clientId"
});
}(t);
}))), n < 5 && r >= 5 && (u = u.next((function() {
return i.Us(o);
}))), n < 6 && r >= 6 && (u = u.next((function() {
return function(t) {
t.createObjectStore("remoteDocumentGlobal");
}(t), i.Ks(o);
}))), n < 7 && r >= 7 && (u = u.next((function() {
return i.Gs(o);
}))), n < 8 && r >= 8 && (u = u.next((function() {
return i.Qs(t, o);
}))), n < 9 && r >= 9 && (u = u.next((function() {
// Multi-Tab used to manage its own changelog, but this has been moved
// to the DbRemoteDocument object store itself. Since the previous change
// log only contained transient data, we can drop its object store.
!function(t) {
t.objectStoreNames.contains("remoteDocumentChanges") && t.deleteObjectStore("remoteDocumentChanges");
}(t);
// Note: Schema version 9 used to create a read time index for the
// RemoteDocumentCache. This is now done with schema version 13.
}))), n < 10 && r >= 10 && (u = u.next((function() {
return i.js(o);
}))), n < 11 && r >= 11 && (u = u.next((function() {
!function(t) {
t.createObjectStore("bundles", {
keyPath: "bundleId"
});
}(t), function(t) {
t.createObjectStore("namedQueries", {
keyPath: "name"
});
}(t);
}))), n < 12 && r >= 12 && (u = u.next((function() {
!function(t) {
var e = t.createObjectStore("documentOverlays", {
keyPath: Ri
});
e.createIndex("collectionPathOverlayIndex", Vi, {
unique: !1
}), e.createIndex("collectionGroupOverlayIndex", Mi, {
unique: !1
});
}(t);
}))), n < 13 && r >= 13 && (u = u.next((function() {
return function(t) {
var e = t.createObjectStore("remoteDocumentsV14", {
keyPath: Ei
});
e.createIndex("documentKeyIndex", Si), e.createIndex("collectionGroupIndex", _i);
}(t);
})).next((function() {
return i.Ws(t, o);
})).next((function() {
return t.deleteObjectStore("remoteDocuments");
}))), n < 14 && r >= 14 && (u = u.next((function() {
return i.zs(t, o);
}))), n < 15 && r >= 15 && (u = u.next((function() {
return function(t) {
t.createObjectStore("indexConfiguration", {
keyPath: "indexId",
autoIncrement: !0
}).createIndex("collectionGroupIndex", "collectionGroup", {
unique: !1
}), t.createObjectStore("indexState", {
keyPath: Ni
}).createIndex("sequenceNumberIndex", ki, {
unique: !1
}), t.createObjectStore("indexEntries", {
keyPath: Fi
}).createIndex("documentKeyIndex", Oi, {
unique: !1
});
}(t);
}))), u;
}, t.prototype.Ks = function(t) {
var e = 0;
return t.store("remoteDocuments").Z((function(t, n) {
e += jo(n);
})).next((function() {
var n = {
byteSize: e
};
return t.store("remoteDocumentGlobal").put("remoteDocumentGlobalKey", n);
}));
}, t.prototype.Us = function(t) {
var e = this, n = t.store("mutationQueues"), r = t.store("mutations");
return n.W().next((function(n) {
return xt.forEach(n, (function(n) {
var i = IDBKeyRange.bound([ n.userId, -1 ], [ n.userId, n.lastAcknowledgedBatchId ]);
return r.W("userMutationsIndex", i).next((function(r) {
return xt.forEach(r, (function(r) {
U(r.userId === n.userId);
var i = $i(e.yt, r);
return Ko(t, n.userId, i).next((function() {}));
}));
}));
}));
}));
},
/**
* Ensures that every document in the remote document cache has a corresponding sentinel row
* with a sequence number. Missing rows are given the most recently used sequence number.
*/
t.prototype.Gs = function(t) {
var e = t.store("targetDocuments"), n = t.store("remoteDocuments");
return t.store("targetGlobal").get("targetGlobalKey").next((function(t) {
var r = [];
return n.Z((function(n, i) {
var o = new ct(n), u = function(t) {
return [ 0, yi(t) ];
}(o);
r.push(e.get(u).next((function(n) {
return n ? xt.resolve() : function(n) {
return e.put({
targetId: 0,
path: yi(n),
sequenceNumber: t.highestListenSequenceNumber
});
}(o);
})));
})).next((function() {
return xt.waitFor(r);
}));
}));
}, t.prototype.Qs = function(t, e) {
// Create the index.
t.createObjectStore("collectionParents", {
keyPath: Ci
});
var n = e.store("collectionParents"), r = new Oo, i = function(t) {
if (r.add(t)) {
var e = t.lastSegment(), i = t.popLast();
return n.put({
collectionId: e,
parent: yi(i)
});
}
};
// Helper to add an index entry iff we haven't already written it.
// Index existing remote documents.
return e.store("remoteDocuments").Z({
X: !0
}, (function(t, e) {
var n = new ct(t);
return i(n.popLast());
})).next((function() {
return e.store("documentMutations").Z({
X: !0
}, (function(t, e) {
t[0];
var n = t[1];
t[2];
var r = gi(n);
return i(r.popLast());
}));
}));
}, t.prototype.js = function(t) {
var e = this, n = t.store("targets");
return n.Z((function(t, r) {
var i = to(r), o = eo(e.yt, i);
return n.put(o);
}));
}, t.prototype.Ws = function(t, e) {
var n = e.store("remoteDocuments"), r = [];
return n.Z((function(t, n) {
var i, o = e.store("remoteDocumentsV14"), u = (i = n, i.document ? new ft(ct.fromString(i.document.name).popFirst(5)) : i.noDocument ? ft.fromSegments(i.noDocument.path) : i.unknownDocument ? ft.fromSegments(i.unknownDocument.path) : q()).path.toArray(), a = {
prefixPath: u.slice(0, u.length - 2),
collectionGroup: u[u.length - 2],
documentId: u[u.length - 1],
readTime: n.readTime || [ 0, 0 ],
unknownDocument: n.unknownDocument,
noDocument: n.noDocument,
document: n.document,
hasCommittedMutations: !!n.hasCommittedMutations
};
r.push(o.put(a));
})).next((function() {
return xt.waitFor(r);
}));
}, t.prototype.zs = function(t, e) {
var n = this, r = e.store("mutations"), i = cu(this.yt), o = new Du(Au.Bs, this.yt.ie);
return r.W().next((function(t) {
var r = new Map;
return t.forEach((function(t) {
var e, i = null !== (e = r.get(t.userId)) && void 0 !== e ? e : _r();
$i(n.yt, t).keys().forEach((function(t) {
return i = i.add(t);
})), r.set(t.userId, i);
})), xt.forEach(r, (function(t, r) {
var u = new N(r), a = co.re(n.yt, u), s = o.getIndexManager(u), c = Qo.re(u, n.yt, s, o.referenceDelegate);
return new mu(i, c, a, s).recalculateAndSaveOverlaysForDocumentKeys(new Gi(e, qt.at), t).next();
}));
}));
}, t;
}();
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A readonly view of the local state of all documents we're tracking (i.e. we
* have a cached version in remoteDocumentCache or local mutations for the
* document). The view is computed by applying the mutations in the
* MutationQueue to the RemoteDocumentCache.
*/ function Nu(t) {
t.createObjectStore("targetDocuments", {
keyPath: xi
}).createIndex("documentTargetsIndex", Ai, {
unique: !0
}),
// NOTE: This is unique only because the TargetId is the suffix.
t.createObjectStore("targets", {
keyPath: "targetId"
}).createIndex("queryTargetsIndex", Di, {
unique: !0
}), t.createObjectStore("targetGlobal");
}
var ku = "Failed to obtain exclusive access to the persistence layer. To allow shared access, multi-tab synchronization has to be enabled in all tabs. If you are using `experimentalForceOwningTab:true`, make sure that only one tab has persistence enabled at any given time.", Fu = /** @class */ function() {
function t(
/**
* Whether to synchronize the in-memory state of multiple tabs and share
* access to local persistence.
*/
e, n, r, i, o, u, a, s, c,
/**
* If set to true, forcefully obtains database access. Existing tabs will
* no longer be able to access IndexedDB.
*/
l, h) {
if (void 0 === h && (h = 15), this.allowTabSynchronization = e, this.persistenceKey = n,
this.clientId = r, this.Hs = o, this.window = u, this.document = a, this.Js = c,
this.Ys = l, this.Xs = h, this.Ss = null, this.Ds = !1, this.isPrimary = !1, this.networkEnabled = !0,
/** Our window.unload handler, if registered. */
this.Zs = null, this.inForeground = !1,
/** Our 'visibilitychange' listener if registered. */
this.ti = null,
/** The client metadata refresh task. */
this.ei = null,
/** The last time we garbage collected the client metadata object store. */
this.ni = Number.NEGATIVE_INFINITY,
/** A listener to notify on primary state changes. */
this.si = function(t) {
return Promise.resolve();
}, !t.C()) throw new j(K.UNIMPLEMENTED, "This platform is either missing IndexedDB or is known to have an incomplete implementation. Offline persistence has been disabled.");
this.referenceDelegate = new ou(this, i), this.ii = n + "main", this.yt = new Hi(s),
this.ri = new Ct(this.ii, this.Xs, new Cu(this.yt)), this.Cs = new Zo(this.referenceDelegate, this.yt),
this.remoteDocumentCache = cu(this.yt), this.Ns = new uo, this.window && this.window.localStorage ? this.oi = this.window.localStorage : (this.oi = null,
!1 === l && M("IndexedDbPersistence", "LocalStorage is unavailable. As a result, persistence may not work reliably. In particular enablePersistence() could fail immediately after refreshing the page."));
}
/**
* Attempt to start IndexedDb persistence.
*
* @returns Whether persistence was enabled.
*/ return t.prototype.start = function() {
var t = this;
// NOTE: This is expected to fail sometimes (in the case of another tab
// already having the persistence lock), so it's the first thing we should
// do.
return this.ui().then((function() {
if (!t.isPrimary && !t.allowTabSynchronization)
// Fail `start()` if `synchronizeTabs` is disabled and we cannot
// obtain the primary lease.
throw new j(K.FAILED_PRECONDITION, ku);
return t.ci(), t.ai(), t.hi(), t.runTransaction("getHighestListenSequenceNumber", "readonly", (function(e) {
return t.Cs.getHighestSequenceNumber(e);
}));
})).then((function(e) {
t.Ss = new qt(e, t.Js);
})).then((function() {
t.Ds = !0;
})).catch((function(e) {
return t.ri && t.ri.close(), Promise.reject(e);
}));
},
/**
* Registers a listener that gets called when the primary state of the
* instance changes. Upon registering, this listener is invoked immediately
* with the current primary state.
*
* PORTING NOTE: This is only used for Web multi-tab.
*/
t.prototype.li = function(t) {
var r = this;
return this.si = function(i) {
return e(r, void 0, void 0, (function() {
return n(this, (function(e) {
return this.started ? [ 2 /*return*/ , t(i) ] : [ 2 /*return*/ ];
}));
}));
}, t(this.isPrimary);
},
/**
* Registers a listener that gets called when the database receives a
* version change event indicating that it has deleted.
*
* PORTING NOTE: This is only used for Web multi-tab.
*/
t.prototype.setDatabaseDeletedListener = function(t) {
var r = this;
this.ri.L((function(i) {
return e(r, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return null === i.newVersion ? [ 4 /*yield*/ , t() ] : [ 3 /*break*/ , 2 ];
case 1:
e.sent(), e.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
}));
},
/**
* Adjusts the current network state in the client's metadata, potentially
* affecting the primary lease.
*
* PORTING NOTE: This is only used for Web multi-tab.
*/
t.prototype.setNetworkEnabled = function(t) {
var r = this;
this.networkEnabled !== t && (this.networkEnabled = t,
// Schedule a primary lease refresh for immediate execution. The eventual
// lease update will be propagated via `primaryStateListener`.
this.Hs.enqueueAndForget((function() {
return e(r, void 0, void 0, (function() {
return n(this, (function(t) {
switch (t.label) {
case 0:
return this.started ? [ 4 /*yield*/ , this.ui() ] : [ 3 /*break*/ , 2 ];
case 1:
t.sent(), t.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
})));
},
/**
* Updates the client metadata in IndexedDb and attempts to either obtain or
* extend the primary lease for the local client. Asynchronously notifies the
* primary state listener if the client either newly obtained or released its
* primary lease.
*/
t.prototype.ui = function() {
var t = this;
return this.runTransaction("updateClientMetadataAndTryBecomePrimary", "readwrite", (function(e) {
return Ru(e).put({
clientId: t.clientId,
updateTimeMs: Date.now(),
networkEnabled: t.networkEnabled,
inForeground: t.inForeground
}).next((function() {
if (t.isPrimary) return t.fi(e).next((function(e) {
e || (t.isPrimary = !1, t.Hs.enqueueRetryable((function() {
return t.si(!1);
})));
}));
})).next((function() {
return t.di(e);
})).next((function(n) {
return t.isPrimary && !n ? t._i(e).next((function() {
return !1;
})) : !!n && t.wi(e).next((function() {
return !0;
}));
}));
})).catch((function(e) {
if (Ft(e))
// Proceed with the existing state. Any subsequent access to
// IndexedDB will verify the lease.
return V("IndexedDbPersistence", "Failed to extend owner lease: ", e), t.isPrimary;
if (!t.allowTabSynchronization) throw e;
return V("IndexedDbPersistence", "Releasing owner lease after error during lease refresh", e),
/* isPrimary= */ !1;
})).then((function(e) {
t.isPrimary !== e && t.Hs.enqueueRetryable((function() {
return t.si(e);
})), t.isPrimary = e;
}));
}, t.prototype.fi = function(t) {
var e = this;
return Ou(t).get("owner").next((function(t) {
return xt.resolve(e.mi(t));
}));
}, t.prototype.gi = function(t) {
return Ru(t).delete(this.clientId);
},
/**
* If the garbage collection threshold has passed, prunes the
* RemoteDocumentChanges and the ClientMetadata store based on the last update
* time of all clients.
*/
t.prototype.yi = function() {
return e(this, void 0, void 0, (function() {
var t, e, r, i, o = this;
return n(this, (function(n) {
switch (n.label) {
case 0:
return !this.isPrimary || this.pi(this.ni, 18e5) ? [ 3 /*break*/ , 2 ] : (this.ni = Date.now(),
[ 4 /*yield*/ , this.runTransaction("maybeGarbageCollectMultiClientState", "readwrite-primary", (function(t) {
var e = Ki(t, "clientMetadata");
return e.W().next((function(t) {
var n = o.Ii(t, 18e5), r = t.filter((function(t) {
return -1 === n.indexOf(t);
}));
// Delete metadata for clients that are no longer considered active.
return xt.forEach(r, (function(t) {
return e.delete(t.clientId);
})).next((function() {
return r;
}));
}));
})).catch((function() {
return [];
})) ]);
case 1:
// Delete potential leftover entries that may continue to mark the
// inactive clients as zombied in LocalStorage.
// Ideally we'd delete the IndexedDb and LocalStorage zombie entries for
// the client atomically, but we can't. So we opt to delete the IndexedDb
// entries first to avoid potentially reviving a zombied client.
if (t = n.sent(), this.oi) for (e = 0, r = t; e < r.length; e++) i = r[e], this.oi.removeItem(this.Ti(i.clientId));
n.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
},
/**
* Schedules a recurring timer to update the client metadata and to either
* extend or acquire the primary lease if the client is eligible.
*/
t.prototype.hi = function() {
var t = this;
this.ei = this.Hs.enqueueAfterDelay("client_metadata_refresh" /* TimerId.ClientMetadataRefresh */ , 4e3, (function() {
return t.ui().then((function() {
return t.yi();
})).then((function() {
return t.hi();
}));
}));
},
/** Checks whether `client` is the local client. */ t.prototype.mi = function(t) {
return !!t && t.ownerId === this.clientId;
},
/**
* Evaluate the state of all active clients and determine whether the local
* client is or can act as the holder of the primary lease. Returns whether
* the client is eligible for the lease, but does not actually acquire it.
* May return 'false' even if there is no active leaseholder and another
* (foreground) client should become leaseholder instead.
*/
t.prototype.di = function(t) {
var e = this;
return this.Ys ? xt.resolve(!0) : Ou(t).get("owner").next((function(n) {
// A client is eligible for the primary lease if:
// - its network is enabled and the client's tab is in the foreground.
// - its network is enabled and no other client's tab is in the
// foreground.
// - every clients network is disabled and the client's tab is in the
// foreground.
// - every clients network is disabled and no other client's tab is in
// the foreground.
// - the `forceOwningTab` setting was passed in.
if (null !== n && e.pi(n.leaseTimestampMs, 5e3) && !e.Ei(n.ownerId)) {
if (e.mi(n) && e.networkEnabled) return !0;
if (!e.mi(n)) {
if (!n.allowTabSynchronization)
// Fail the `canActAsPrimary` check if the current leaseholder has
// not opted into multi-tab synchronization. If this happens at
// client startup, we reject the Promise returned by
// `enablePersistence()` and the user can continue to use Firestore
// with in-memory persistence.
// If this fails during a lease refresh, we will instead block the
// AsyncQueue from executing further operations. Note that this is
// acceptable since mixing & matching different `synchronizeTabs`
// settings is not supported.
// TODO(b/114226234): Remove this check when `synchronizeTabs` can
// no longer be turned off.
throw new j(K.FAILED_PRECONDITION, ku);
return !1;
}
}
return !(!e.networkEnabled || !e.inForeground) || Ru(t).W().next((function(t) {
return void 0 === e.Ii(t, 5e3).find((function(t) {
if (e.clientId !== t.clientId) {
var n = !e.networkEnabled && t.networkEnabled, r = !e.inForeground && t.inForeground, i = e.networkEnabled === t.networkEnabled;
if (n || r && i) return !0;
}
return !1;
}));
}));
})).next((function(t) {
return e.isPrimary !== t && V("IndexedDbPersistence", "Client ".concat(t ? "is" : "is not", " eligible for a primary lease.")),
t;
}));
}, t.prototype.shutdown = function() {
return e(this, void 0, void 0, (function() {
var t = this;
return n(this, (function(e) {
switch (e.label) {
case 0:
// Use `SimpleDb.runTransaction` directly to avoid failing if another tab
// has obtained the primary lease.
// The shutdown() operations are idempotent and can be called even when
// start() aborted (e.g. because it couldn't acquire the persistence lease).
return this.Ds = !1, this.Ai(), this.ei && (this.ei.cancel(), this.ei = null), this.Ri(),
this.bi(), [ 4 /*yield*/ , this.ri.runTransaction("shutdown", "readwrite", [ "owner", "clientMetadata" ], (function(e) {
var n = new Gi(e, qt.at);
return t._i(n).next((function() {
return t.gi(n);
}));
})) ];
case 1:
// The shutdown() operations are idempotent and can be called even when
// start() aborted (e.g. because it couldn't acquire the persistence lease).
// Use `SimpleDb.runTransaction` directly to avoid failing if another tab
// has obtained the primary lease.
return e.sent(), this.ri.close(),
// Remove the entry marking the client as zombied from LocalStorage since
// we successfully deleted its metadata from IndexedDb.
this.Pi(), [ 2 /*return*/ ];
}
}));
}));
},
/**
* Returns clients that are not zombied and have an updateTime within the
* provided threshold.
*/
t.prototype.Ii = function(t, e) {
var n = this;
return t.filter((function(t) {
return n.pi(t.updateTimeMs, e) && !n.Ei(t.clientId);
}));
},
/**
* Returns the IDs of the clients that are currently active. If multi-tab
* is not supported, returns an array that only contains the local client's
* ID.
*
* PORTING NOTE: This is only used for Web multi-tab.
*/
t.prototype.vi = function() {
var t = this;
return this.runTransaction("getActiveClients", "readonly", (function(e) {
return Ru(e).W().next((function(e) {
return t.Ii(e, 18e5).map((function(t) {
return t.clientId;
}));
}));
}));
}, Object.defineProperty(t.prototype, "started", {
get: function() {
return this.Ds;
},
enumerable: !1,
configurable: !0
}), t.prototype.getMutationQueue = function(t, e) {
return Qo.re(t, this.yt, e, this.referenceDelegate);
}, t.prototype.getTargetCache = function() {
return this.Cs;
}, t.prototype.getRemoteDocumentCache = function() {
return this.remoteDocumentCache;
}, t.prototype.getIndexManager = function(t) {
return new Vo(t, this.yt.ie.databaseId);
}, t.prototype.getDocumentOverlayCache = function(t) {
return co.re(this.yt, t);
}, t.prototype.getBundleCache = function() {
return this.Ns;
}, t.prototype.runTransaction = function(t, e, n) {
var r = this;
V("IndexedDbPersistence", "Starting transaction:", t);
var i, o, u = "readonly" === e ? "readonly" : "readwrite", a = 15 === (i = this.Xs) ? Bi : 14 === i ? Ui : 13 === i ? qi : 12 === i ? Pi : 11 === i ? Li : void q();
/** Returns the object stores for the provided schema. */
// Do all transactions as readwrite against all object stores, since we
// are the only reader/writer.
return this.ri.runTransaction(t, u, a, (function(i) {
return o = new Gi(i, r.Ss ? r.Ss.next() : qt.at), "readwrite-primary" === e ? r.fi(o).next((function(t) {
return !!t || r.di(o);
})).next((function(e) {
if (!e) throw M("Failed to obtain primary lease for action '".concat(t, "'.")),
r.isPrimary = !1, r.Hs.enqueueRetryable((function() {
return r.si(!1);
})), new j(K.FAILED_PRECONDITION, St);
return n(o);
})).next((function(t) {
return r.wi(o).next((function() {
return t;
}));
})) : r.Vi(o).next((function() {
return n(o);
}));
})).then((function(t) {
return o.raiseOnCommittedEvent(), t;
}));
},
/**
* Verifies that the current tab is the primary leaseholder or alternatively
* that the leaseholder has opted into multi-tab synchronization.
*/
// TODO(b/114226234): Remove this check when `synchronizeTabs` can no longer
// be turned off.
t.prototype.Vi = function(t) {
var e = this;
return Ou(t).get("owner").next((function(t) {
if (null !== t && e.pi(t.leaseTimestampMs, 5e3) && !e.Ei(t.ownerId) && !e.mi(t) && !(e.Ys || e.allowTabSynchronization && t.allowTabSynchronization)) throw new j(K.FAILED_PRECONDITION, ku);
}));
},
/**
* Obtains or extends the new primary lease for the local client. This
* method does not verify that the client is eligible for this lease.
*/
t.prototype.wi = function(t) {
var e = {
ownerId: this.clientId,
allowTabSynchronization: this.allowTabSynchronization,
leaseTimestampMs: Date.now()
};
return Ou(t).put("owner", e);
}, t.C = function() {
return Ct.C();
},
/** Checks the primary lease and removes it if we are the current primary. */ t.prototype._i = function(t) {
var e = this, n = Ou(t);
return n.get("owner").next((function(t) {
return e.mi(t) ? (V("IndexedDbPersistence", "Releasing primary lease."), n.delete("owner")) : xt.resolve();
}));
},
/** Verifies that `updateTimeMs` is within `maxAgeMs`. */ t.prototype.pi = function(t, e) {
var n = Date.now();
return !(t < n - e || t > n && (M("Detected an update time that is in the future: ".concat(t, " > ").concat(n)),
1));
}, t.prototype.ci = function() {
var t = this;
null !== this.document && "function" == typeof this.document.addEventListener && (this.ti = function() {
t.Hs.enqueueAndForget((function() {
return t.inForeground = "visible" === t.document.visibilityState, t.ui();
}));
}, this.document.addEventListener("visibilitychange", this.ti), this.inForeground = "visible" === this.document.visibilityState);
}, t.prototype.Ri = function() {
this.ti && (this.document.removeEventListener("visibilitychange", this.ti), this.ti = null);
},
/**
* Attaches a window.unload handler that will synchronously write our
* clientId to a "zombie client id" location in LocalStorage. This can be used
* by tabs trying to acquire the primary lease to determine that the lease
* is no longer valid even if the timestamp is recent. This is particularly
* important for the refresh case (so the tab correctly re-acquires the
* primary lease). LocalStorage is used for this rather than IndexedDb because
* it is a synchronous API and so can be used reliably from an unload
* handler.
*/
t.prototype.ai = function() {
var t, e = this;
"function" == typeof (null === (t = this.window) || void 0 === t ? void 0 : t.addEventListener) && (this.Zs = function() {
// Note: In theory, this should be scheduled on the AsyncQueue since it
// accesses internal state. We execute this code directly during shutdown
// to make sure it gets a chance to run.
e.Ai(), p() && navigator.appVersion.match(/Version\/1[45]/) &&
// On Safari 14 and 15, we do not run any cleanup actions as it might
// trigger a bug that prevents Safari from re-opening IndexedDB during
// the next page load.
// See https://bugs.webkit.org/show_bug.cgi?id=226547
e.Hs.enterRestrictedMode(/* purgeExistingTasks= */ !0), e.Hs.enqueueAndForget((function() {
return e.shutdown();
}));
}, this.window.addEventListener("pagehide", this.Zs));
}, t.prototype.bi = function() {
this.Zs && (this.window.removeEventListener("pagehide", this.Zs), this.Zs = null);
},
/**
* Returns whether a client is "zombied" based on its LocalStorage entry.
* Clients become zombied when their tab closes without running all of the
* cleanup logic in `shutdown()`.
*/
t.prototype.Ei = function(t) {
var e;
try {
var n = null !== (null === (e = this.oi) || void 0 === e ? void 0 : e.getItem(this.Ti(t)));
return V("IndexedDbPersistence", "Client '".concat(t, "' ").concat(n ? "is" : "is not", " zombied in LocalStorage")),
n;
} catch (t) {
// Gracefully handle if LocalStorage isn't working.
return M("IndexedDbPersistence", "Failed to get zombied client id.", t), !1;
}
},
/**
* Record client as zombied (a client that had its tab closed). Zombied
* clients are ignored during primary tab selection.
*/
t.prototype.Ai = function() {
if (this.oi) try {
this.oi.setItem(this.Ti(this.clientId), String(Date.now()));
} catch (t) {
// Gracefully handle if LocalStorage isn't available / working.
M("Failed to set zombie client id.", t);
}
},
/** Removes the zombied client entry if it exists. */ t.prototype.Pi = function() {
if (this.oi) try {
this.oi.removeItem(this.Ti(this.clientId));
} catch (t) {
// Ignore
}
}, t.prototype.Ti = function(t) {
return "firestore_zombie_".concat(this.persistenceKey, "_").concat(t);
}, t;
}();
/**
* Oldest acceptable age in milliseconds for client metadata before the client
* is considered inactive and its associated data is garbage collected.
*/
/**
* An IndexedDB-backed instance of Persistence. Data is stored persistently
* across sessions.
*
* On Web only, the Firestore SDKs support shared access to its persistence
* layer. This allows multiple browser tabs to read and write to IndexedDb and
* to synchronize state even without network connectivity. Shared access is
* currently optional and not enabled unless all clients invoke
* `enablePersistence()` with `{synchronizeTabs:true}`.
*
* In multi-tab mode, if multiple clients are active at the same time, the SDK
* will designate one client as the “primary client”. An effort is made to pick
* a visible, network-connected and active client, and this client is
* responsible for letting other clients know about its presence. The primary
* client writes a unique client-generated identifier (the client ID) to
* IndexedDb’s “owner” store every 4 seconds. If the primary client fails to
* update this entry, another client can acquire the lease and take over as
* primary.
*
* Some persistence operations in the SDK are designated as primary-client only
* operations. This includes the acknowledgment of mutations and all updates of
* remote documents. The effects of these operations are written to persistence
* and then broadcast to other tabs via LocalStorage (see
* `WebStorageSharedClientState`), which then refresh their state from
* persistence.
*
* Similarly, the primary client listens to notifications sent by secondary
* clients to discover persistence changes written by secondary clients, such as
* the addition of new mutations and query targets.
*
* If multi-tab is not enabled and another tab already obtained the primary
* lease, IndexedDbPersistence enters a failed state and all subsequent
* operations will automatically fail.
*
* Additionally, there is an optimization so that when a tab is closed, the
* primary lease is released immediately (this is especially important to make
* sure that a refreshed tab is able to immediately re-acquire the primary
* lease). Unfortunately, IndexedDB cannot be reliably used in window.unload
* since it is an asynchronous API. So in addition to attempting to give up the
* lease, the leaseholder writes its client ID to a "zombiedClient" entry in
* LocalStorage which acts as an indicator that another tab should go ahead and
* take the primary lease immediately regardless of the current lease timestamp.
*
* TODO(b/114226234): Remove `synchronizeTabs` section when multi-tab is no
* longer optional.
*/
/**
* Helper to get a typed SimpleDbStore for the primary client object store.
*/
function Ou(t) {
return Ki(t, "owner");
}
/**
* Helper to get a typed SimpleDbStore for the client metadata object store.
*/ function Ru(t) {
return Ki(t, "clientMetadata");
}
/**
* Generates a string used as a prefix when storing data in IndexedDB and
* LocalStorage.
*/ function Vu(t, e) {
// Use two different prefix formats:
// * firestore / persistenceKey / projectID . databaseID / ...
// * firestore / persistenceKey / projectID / ...
// projectIDs are DNS-compatible names and cannot contain dots
// so there's no danger of collisions.
var n = t.projectId;
return t.isDefaultDatabase || (n += "." + t.database), "firestore/" + e + "/" + n + "/"
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A set of changes to what documents are currently in view and out of view for
* a given query. These changes are sent to the LocalStore by the View (via
* the SyncEngine) and are used to pin / unpin documents as appropriate.
*/;
}
var Mu = /** @class */ function() {
function t(t, e, n, r) {
this.targetId = t, this.fromCache = e, this.Si = n, this.Di = r;
}
return t.Ci = function(e, n) {
for (var r = _r(), i = _r(), o = 0, u = n.docChanges; o < u.length; o++) {
var a = u[o];
switch (a.type) {
case 0 /* ChangeType.Added */ :
r = r.add(a.doc.key);
break;
case 1 /* ChangeType.Removed */ :
i = i.add(a.doc.key);
// do nothing
}
}
return new t(e, n.fromCache, r, i);
}, t;
}(), Lu = /** @class */ function() {
function t() {
this.xi = !1;
}
/** Sets the document view to query against. */ return t.prototype.initialize = function(t, e) {
this.Ni = t, this.indexManager = e, this.xi = !0;
},
/** Returns all local documents matching the specified query. */ t.prototype.getDocumentsMatchingQuery = function(t, e, n, r) {
var i = this;
return this.ki(t, e).next((function(o) {
return o || i.Oi(t, e, r, n);
})).next((function(n) {
return n || i.Mi(t, e);
}));
},
/**
* Performs an indexed query that evaluates the query based on a collection's
* persisted index values. Returns `null` if an index is not available.
*/
t.prototype.ki = function(t, e) {
var n = this;
if (vn(e))
// Queries that match all documents don't benefit from using
// key-based lookups. It is more efficient to scan all documents in a
// collection, rather than to perform individual lookups.
return xt.resolve(null);
var r = In(e);
return this.indexManager.getIndexType(t, r).next((function(i) {
return 0 /* IndexType.NONE */ === i ? null : (null !== e.limit && 1 /* IndexType.PARTIAL */ === i && (
// We cannot apply a limit for targets that are served using a partial
// index. If a partial index will be used to serve the target, the
// query may return a superset of documents that match the target
// (e.g. if the index doesn't include all the target's filters), or
// may return the correct set of documents in the wrong order (e.g. if
// the index doesn't include a segment for one of the orderBys).
// Therefore, a limit should not be applied in such cases.
e = En(e, null, "F" /* LimitType.First */), r = In(e)), n.indexManager.getDocumentsMatchingTarget(t, r).next((function(i) {
var o = _r.apply(void 0, i);
return n.Ni.getDocuments(t, o).next((function(i) {
return n.indexManager.getMinOffset(t, r).next((function(r) {
var u = n.Fi(e, i);
return n.$i(e, u, o, r.readTime) ? n.ki(t, En(e, null, "F" /* LimitType.First */)) : n.Bi(t, u, e, r);
}));
}));
})));
}));
},
/**
* Performs a query based on the target's persisted query mapping. Returns
* `null` if the mapping is not available or cannot be used.
*/
t.prototype.Oi = function(t, e, n, r) {
var i = this;
return vn(e) || r.isEqual(at.min()) ? this.Mi(t, e) : this.Ni.getDocuments(t, n).next((function(o) {
var u = i.Fi(e, o);
return i.$i(e, u, n, r) ? i.Mi(t, e) : (O() <= h.DEBUG && V("QueryEngine", "Re-using previous result from %s to execute query: %s", r.toString(), Dn(e)),
i.Bi(t, u, e, bt(r, -1)));
}));
// Queries that have never seen a snapshot without limbo free documents
// should also be run as a full collection scan.
},
/** Applies the query filter and sorting to the provided documents. */ t.prototype.Fi = function(t, e) {
// Sort the documents and re-apply the query filter since previously
// matching documents do not necessarily still match the query.
var n = new Ze(Cn(t));
return e.forEach((function(e, r) {
xn(t, r) && (n = n.add(r));
})), n;
},
/**
* Determines if a limit query needs to be refilled from cache, making it
* ineligible for index-free execution.
*
* @param query - The query.
* @param sortedPreviousResults - The documents that matched the query when it
* was last synchronized, sorted by the query's comparator.
* @param remoteKeys - The document keys that matched the query at the last
* snapshot.
* @param limboFreeSnapshotVersion - The version of the snapshot when the
* query was last synchronized.
*/
t.prototype.$i = function(t, e, n, r) {
if (null === t.limit)
// Queries without limits do not need to be refilled.
return !1;
if (n.size !== e.size)
// The query needs to be refilled if a previously matching document no
// longer matches.
return !0;
// Limit queries are not eligible for index-free query execution if there is
// a potential that an older document from cache now sorts before a document
// that was previously part of the limit. This, however, can only happen if
// the document at the edge of the limit goes out of limit.
// If a document that is not the limit boundary sorts differently,
// the boundary of the limit itself did not change and documents from cache
// will continue to be "rejected" by this boundary. Therefore, we can ignore
// any modifications that don't affect the last document.
var i = "F" /* LimitType.First */ === t.limitType ? e.last() : e.first();
return !!i && (i.hasPendingWrites || i.version.compareTo(r) > 0);
}, t.prototype.Mi = function(t, e) {
return O() <= h.DEBUG && V("QueryEngine", "Using full collection scan to execute query:", Dn(e)),
this.Ni.getDocumentsMatchingQuery(t, e, Tt.min());
},
/**
* Combines the results from an indexed execution with the remaining documents
* that have not yet been indexed.
*/
t.prototype.Bi = function(t, e, n, r) {
// Retrieve all results for documents that were updated since the offset.
return this.Ni.getDocumentsMatchingQuery(t, n, r).next((function(t) {
// Merge with existing results
return e.forEach((function(e) {
t = t.insert(e.key, e);
})), t;
}));
}, t;
}(), Pu = /** @class */ function() {
function t(
/** Manages our in-memory or durable persistence. */
t, e, n, r) {
this.persistence = t, this.Li = e, this.yt = r,
/**
* Maps a targetID to data about its target.
*
* PORTING NOTE: We are using an immutable data structure on Web to make re-runs
* of `applyRemoteEvent()` idempotent.
*/
this.qi = new He(rt),
/** Maps a target to its targetID. */
// TODO(wuandy): Evaluate if TargetId can be part of Target.
this.Ui = new pr((function(t) {
return an(t);
}), sn),
/**
* A per collection group index of the last read time processed by
* `getNewDocumentChanges()`.
*
* PORTING NOTE: This is only used for multi-tab synchronization.
*/
this.Ki = new Map, this.Gi = t.getRemoteDocumentCache(), this.Cs = t.getTargetCache(),
this.Ns = t.getBundleCache(), this.Qi(n);
}
return t.prototype.Qi = function(t) {
// TODO(indexing): Add spec tests that test these components change after a
// user change
this.documentOverlayCache = this.persistence.getDocumentOverlayCache(t), this.indexManager = this.persistence.getIndexManager(t),
this.mutationQueue = this.persistence.getMutationQueue(t, this.indexManager), this.localDocuments = new mu(this.Gi, this.mutationQueue, this.documentOverlayCache, this.indexManager),
this.Gi.setIndexManager(this.indexManager), this.Li.initialize(this.localDocuments, this.indexManager);
}, t.prototype.collectGarbage = function(t) {
var e = this;
return this.persistence.runTransaction("Collect garbage", "readwrite-primary", (function(n) {
return t.collect(n, e.qi);
}));
}, t;
}();
/**
* @license
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The Firestore query engine.
*
* Firestore queries can be executed in three modes. The Query Engine determines
* what mode to use based on what data is persisted. The mode only determines
* the runtime complexity of the query - the result set is equivalent across all
* implementations.
*
* The Query engine will use indexed-based execution if a user has configured
* any index that can be used to execute query (via `setIndexConfiguration()`).
* Otherwise, the engine will try to optimize the query by re-using a previously
* persisted query result. If that is not possible, the query will be executed
* via a full collection scan.
*
* Index-based execution is the default when available. The query engine
* supports partial indexed execution and merges the result from the index
* lookup with documents that have not yet been indexed. The index evaluation
* matches the backend's format and as such, the SDK can use indexing for all
* queries that the backend supports.
*
* If no index exists, the query engine tries to take advantage of the target
* document mapping in the TargetCache. These mappings exists for all queries
* that have been synced with the backend at least once and allow the query
* engine to only read documents that previously matched a query plus any
* documents that were edited after the query was last listened to.
*
* There are some cases when this optimization is not guaranteed to produce
* the same results as full collection scans. In these cases, query
* processing falls back to full scans. These cases are:
*
* - Limit queries where a document that matched the query previously no longer
* matches the query.
*
* - Limit queries where a document edit may cause the document to sort below
* another document that is in the local cache.
*
* - Queries that have never been CURRENT or free of limbo documents.
*/ function qu(
/** Manages our in-memory or durable persistence. */
t, e, n, r) {
return new Pu(t, e, n, r);
}
/**
* Tells the LocalStore that the currently authenticated user has changed.
*
* In response the local store switches the mutation queue to the new user and
* returns any resulting document changes.
*/
// PORTING NOTE: Android and iOS only return the documents affected by the
// change.
function Uu(t, r) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , (e = G(t)).persistence.runTransaction("Handle user change", "readonly", (function(t) {
// Swap out the mutation queue, grabbing the pending mutation batches
// before and after.
var n;
return e.mutationQueue.getAllMutationBatches(t).next((function(i) {
return n = i, e.Qi(r), e.mutationQueue.getAllMutationBatches(t);
})).next((function(r) {
for (var i = [], o = [], u = _r(), a = 0, s = n
// Union the old/new changed keys.
; a < s.length; a++) {
var c = s[a];
i.push(c.batchId);
for (var l = 0, h = c.mutations; l < h.length; l++) {
var f = h[l];
u = u.add(f.key);
}
}
for (var d = 0, p = r; d < p.length; d++) {
var y = p[d];
o.push(y.batchId);
for (var v = 0, m = y.mutations; v < m.length; v++) {
var g = m[v];
u = u.add(g.key);
}
}
// Return the set of all (potentially) changed documents and the list
// of mutation batch IDs that were affected by change.
return e.localDocuments.getDocuments(t, u).next((function(t) {
return {
ji: t,
removedBatchIds: i,
addedBatchIds: o
};
}));
}));
})) ];
case 1:
return [ 2 /*return*/ , n.sent() ];
}
}));
}));
}
/* Accepts locally generated Mutations and commit them to storage. */
/**
* Acknowledges the given batch.
*
* On the happy path when a batch is acknowledged, the local store will
*
* + remove the batch from the mutation queue;
* + apply the changes to the remote document cache;
* + recalculate the latency compensated view implied by those changes (there
* may be mutations in the queue that affect the documents but haven't been
* acknowledged yet); and
* + give the changed documents back the sync engine
*
* @returns The resulting (modified) documents.
*/ function Bu(t, e) {
var n = G(t);
return n.persistence.runTransaction("Acknowledge batch", "readwrite-primary", (function(t) {
var r = e.batch.keys(), i = n.Gi.newChangeBuffer({
trackRemovals: !0
});
return function(t, e, n, r) {
var i = n.batch, o = i.keys(), u = xt.resolve();
return o.forEach((function(t) {
u = u.next((function() {
return r.getEntry(e, t);
})).next((function(e) {
var o = n.docVersions.get(t);
U(null !== o), e.version.compareTo(o) < 0 && (i.applyToRemoteDocument(e, n), e.isValidDocument() && (
// We use the commitVersion as the readTime rather than the
// document's updateTime since the updateTime is not advanced
// for updates that do not modify the underlying document.
e.setReadTime(n.commitVersion), r.addEntry(e)));
}));
})), u.next((function() {
return t.mutationQueue.removeMutationBatch(e, i);
}));
}(n, t, e, i).next((function() {
return i.apply(t);
})).next((function() {
return n.mutationQueue.performConsistencyCheck(t);
})).next((function() {
return n.documentOverlayCache.removeOverlaysForBatchId(t, r, e.batch.batchId);
})).next((function() {
return n.localDocuments.recalculateAndSaveOverlaysForDocumentKeys(t, function(t) {
for (var e = _r(), n = 0; n < t.mutationResults.length; ++n) t.mutationResults[n].transformResults.length > 0 && (e = e.add(t.batch.mutations[n].key));
return e;
}(e));
})).next((function() {
return n.localDocuments.getDocuments(t, r);
}));
}));
}
/**
* Returns the last consistent snapshot processed (used by the RemoteStore to
* determine whether to buffer incoming snapshots from the backend).
*/ function Gu(t) {
var e = G(t);
return e.persistence.runTransaction("Get last remote snapshot version", "readonly", (function(t) {
return e.Cs.getLastRemoteSnapshotVersion(t);
}));
}
/**
* Updates the "ground-state" (remote) documents. We assume that the remote
* event reflects any write batches that have been acknowledged or rejected
* (i.e. we do not re-apply local mutations to updates from this event).
*
* LocalDocuments are re-calculated if there are remaining mutations in the
* queue.
*/ function Ku(t, e) {
var n = G(t), r = e.snapshotVersion, i = n.qi;
return n.persistence.runTransaction("Apply remote event", "readwrite-primary", (function(t) {
var o = n.Gi.newChangeBuffer({
trackRemovals: !0
});
// Reset newTargetDataByTargetMap in case this transaction gets re-run.
i = n.qi;
var u = [];
e.targetChanges.forEach((function(o, a) {
var s = i.get(a);
if (s) {
// Only update the remote keys if the target is still active. This
// ensures that we can persist the updated target data along with
// the updated assignment.
u.push(n.Cs.removeMatchingKeys(t, o.removedDocuments, a).next((function() {
return n.Cs.addMatchingKeys(t, o.addedDocuments, a);
})));
var c = s.withSequenceNumber(t.currentSequenceNumber);
e.targetMismatches.has(a) ? c = c.withResumeToken(Yt.EMPTY_BYTE_STRING, at.min()).withLastLimboFreeSnapshotVersion(at.min()) : o.resumeToken.approximateByteSize() > 0 && (c = c.withResumeToken(o.resumeToken, r)),
i = i.insert(a, c),
// Update the target data if there are target changes (or if
// sufficient time has passed since the last update).
/**
* Returns true if the newTargetData should be persisted during an update of
* an active target. TargetData should always be persisted when a target is
* being released and should not call this function.
*
* While the target is active, TargetData updates can be omitted when nothing
* about the target has changed except metadata like the resume token or
* snapshot version. Occasionally it's worth the extra write to prevent these
* values from getting too stale after a crash, but this doesn't have to be
* too frequent.
*/
function(t, e, n) {
// Always persist target data if we don't already have a resume token.
return 0 === t.resumeToken.approximateByteSize() || (
// Don't allow resume token changes to be buffered indefinitely. This
// allows us to be reasonably up-to-date after a crash and avoids needing
// to loop over all active queries on shutdown. Especially in the browser
// we may not get time to do anything interesting while the current tab is
// closing.
e.snapshotVersion.toMicroseconds() - t.snapshotVersion.toMicroseconds() >= 3e8 || n.addedDocuments.size + n.modifiedDocuments.size + n.removedDocuments.size > 0);
}(s, c, o) && u.push(n.Cs.updateTargetData(t, c));
}
}));
var a = vr(), s = _r();
// HACK: The only reason we allow a null snapshot version is so that we
// can synthesize remote events when we get permission denied errors while
// trying to resolve the state of a locally cached document that is in
// limbo.
if (e.documentUpdates.forEach((function(r) {
e.resolvedLimboDocuments.has(r) && u.push(n.persistence.referenceDelegate.updateLimboDocument(t, r));
})),
// Each loop iteration only affects its "own" doc, so it's safe to get all
// the remote documents in advance in a single call.
u.push(ju(t, o, e.documentUpdates).next((function(t) {
a = t.Wi, s = t.zi;
}))), !r.isEqual(at.min())) {
var c = n.Cs.getLastRemoteSnapshotVersion(t).next((function(e) {
return n.Cs.setTargetsMetadata(t, t.currentSequenceNumber, r);
}));
u.push(c);
}
return xt.waitFor(u).next((function() {
return o.apply(t);
})).next((function() {
return n.localDocuments.getLocalViewOfDocuments(t, a, s);
})).next((function() {
return a;
}));
})).then((function(t) {
return n.qi = i, t;
}));
}
/**
* Populates document change buffer with documents from backend or a bundle.
* Returns the document changes resulting from applying those documents, and
* also a set of documents whose existence state are changed as a result.
*
* @param txn - Transaction to use to read existing documents from storage.
* @param documentBuffer - Document buffer to collect the resulted changes to be
* applied to storage.
* @param documents - Documents to be applied.
*/ function ju(t, e, n) {
var r = _r(), i = _r();
return n.forEach((function(t) {
return r = r.add(t);
})), e.getEntries(t, r).next((function(t) {
var r = vr();
return n.forEach((function(n, o) {
var u = t.get(n);
// Check if see if there is a existence state change for this document.
o.isFoundDocument() !== u.isFoundDocument() && (i = i.add(n)),
// Note: The order of the steps below is important, since we want
// to ensure that rejected limbo resolutions (which fabricate
// NoDocuments with SnapshotVersion.min()) never add documents to
// cache.
o.isNoDocument() && o.version.isEqual(at.min()) ? (
// NoDocuments with SnapshotVersion.min() are used in manufactured
// events. We remove these documents from cache since we lost
// access.
e.removeEntry(n, o.readTime), r = r.insert(n, o)) : !u.isValidDocument() || o.version.compareTo(u.version) > 0 || 0 === o.version.compareTo(u.version) && u.hasPendingWrites ? (e.addEntry(o),
r = r.insert(n, o)) : V("LocalStore", "Ignoring outdated watch update for ", n, ". Current version:", u.version, " Watch version:", o.version);
})), {
Wi: r,
zi: i
};
}))
/**
* Gets the mutation batch after the passed in batchId in the mutation queue
* or null if empty.
* @param afterBatchId - If provided, the batch to search after.
* @returns The next mutation or null if there wasn't one.
*/;
}
function Qu(t, e) {
var n = G(t);
return n.persistence.runTransaction("Get next mutation batch", "readonly", (function(t) {
return void 0 === e && (e = -1), n.mutationQueue.getNextMutationBatchAfterBatchId(t, e);
}));
}
/**
* Reads the current value of a Document with a given key or null if not
* found - used for testing.
*/
/**
* Assigns the given target an internal ID so that its results can be pinned so
* they don't get GC'd. A target must be allocated in the local store before
* the store can be used to manage its view.
*
* Allocating an already allocated `Target` will return the existing `TargetData`
* for that `Target`.
*/ function zu(t, e) {
var n = G(t);
return n.persistence.runTransaction("Allocate target", "readwrite", (function(t) {
var r;
return n.Cs.getTargetData(t, e).next((function(i) {
return i ? (
// This target has been listened to previously, so reuse the
// previous targetID.
// TODO(mcg): freshen last accessed date?
r = i, xt.resolve(r)) : n.Cs.allocateTargetId(t).next((function(i) {
return r = new Wi(e, i, 0 /* TargetPurpose.Listen */ , t.currentSequenceNumber),
n.Cs.addTargetData(t, r).next((function() {
return r;
}));
}));
}));
})).then((function(t) {
// If Multi-Tab is enabled, the existing target data may be newer than
// the in-memory data
var r = n.qi.get(t.targetId);
return (null === r || t.snapshotVersion.compareTo(r.snapshotVersion) > 0) && (n.qi = n.qi.insert(t.targetId, t),
n.Ui.set(e, t.targetId)), t;
}));
}
/**
* Returns the TargetData as seen by the LocalStore, including updates that may
* have not yet been persisted to the TargetCache.
*/
// Visible for testing.
/**
* Unpins all the documents associated with the given target. If
* `keepPersistedTargetData` is set to false and Eager GC enabled, the method
* directly removes the associated target data from the target cache.
*
* Releasing a non-existing `Target` is a no-op.
*/
// PORTING NOTE: `keepPersistedTargetData` is multi-tab only.
function Wu(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u, a;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), o = e.qi.get(r), u = i ? "readwrite" : "readwrite-primary", n.label = 1;
case 1:
return n.trys.push([ 1, 4, , 5 ]), i ? [ 3 /*break*/ , 3 ] : [ 4 /*yield*/ , e.persistence.runTransaction("Release target", u, (function(t) {
return e.persistence.referenceDelegate.removeTarget(t, o);
})) ];
case 2:
n.sent(), n.label = 3;
case 3:
return [ 3 /*break*/ , 5 ];
case 4:
if (!Ft(a = n.sent())) throw a;
// All `releaseTarget` does is record the final metadata state for the
// target, but we've been recording this periodically during target
// activity. If we lose this write this could cause a very slight
// difference in the order of target deletion during GC, but we
// don't define exact LRU semantics so this is acceptable.
return V("LocalStore", "Failed to update sequence numbers for target ".concat(r, ": ").concat(a)),
[ 3 /*break*/ , 5 ];
case 5:
return e.qi = e.qi.remove(r), e.Ui.delete(o.target), [ 2 /*return*/ ];
}
}));
}));
}
/**
* Runs the specified query against the local store and returns the results,
* potentially taking advantage of query data from previous executions (such
* as the set of remote keys).
*
* @param usePreviousResults - Whether results from previous executions can
* be used to optimize this query execution.
*/ function Hu(t, e, n) {
var r = G(t), i = at.min(), o = _r();
return r.persistence.runTransaction("Execute query", "readonly", (function(t) {
return function(t, e, n) {
var r = G(t), i = r.Ui.get(n);
return void 0 !== i ? xt.resolve(r.qi.get(i)) : r.Cs.getTargetData(e, n);
}(r, t, In(e)).next((function(e) {
if (e) return i = e.lastLimboFreeSnapshotVersion, r.Cs.getMatchingKeysForTargetId(t, e.targetId).next((function(t) {
o = t;
}));
})).next((function() {
return r.Li.getDocumentsMatchingQuery(t, e, n ? i : at.min(), n ? o : _r());
})).next((function(t) {
return Zu(r, An(e), t), {
documents: t,
Hi: o
};
}));
}));
}
// PORTING NOTE: Multi-Tab only.
function Yu(t, e) {
var n = G(t), r = G(n.Cs), i = n.qi.get(e);
return i ? Promise.resolve(i.target) : n.persistence.runTransaction("Get target data", "readonly", (function(t) {
return r.ne(t, e).next((function(t) {
return t ? t.target : null;
}));
}));
}
/**
* Returns the set of documents that have been updated since the last call.
* If this is the first call, returns the set of changes since client
* initialization. Further invocations will return document that have changed
* since the prior call.
*/
// PORTING NOTE: Multi-Tab only.
function Xu(t, e) {
var n = G(t), r = n.Ki.get(e) || at.min();
// Get the current maximum read time for the collection. This should always
// exist, but to reduce the chance for regressions we default to
// SnapshotVersion.Min()
// TODO(indexing): Consider removing the default value.
return n.persistence.runTransaction("Get new document changes", "readonly", (function(t) {
return n.Gi.getAllFromCollectionGroup(t, e, bt(r, -1),
/* limit= */ Number.MAX_SAFE_INTEGER);
})).then((function(t) {
return Zu(n, e, t), t;
}));
}
/** Sets the collection group's maximum read time from the given documents. */
// PORTING NOTE: Multi-Tab only.
function Zu(t, e, n) {
var r = t.Ki.get(e) || at.min();
n.forEach((function(t, e) {
e.readTime.compareTo(r) > 0 && (r = e.readTime);
})), t.Ki.set(e, r);
}
/**
* Creates a new target using the given bundle name, which will be used to
* hold the keys of all documents from the bundle in query-document mappings.
* This ensures that the loaded documents do not get garbage collected
* right away.
*/
/**
* Applies the documents from a bundle to the "ground-state" (remote)
* documents.
*
* LocalDocuments are re-calculated if there are remaining mutations in the
* queue.
*/ function Ju(t, r, i, o) {
return e(this, void 0, void 0, (function() {
var e, u, a, s, c, l, h, f, d, p;
return n(this, (function(n) {
switch (n.label) {
case 0:
for (e = G(t), u = _r(), a = vr(), s = 0, c = i; s < c.length; s++) l = c[s], h = r.Ji(l.metadata.name),
l.document && (u = u.add(h)), (f = r.Yi(l)).setReadTime(r.Xi(l.metadata.readTime)),
a = a.insert(h, f);
return d = e.Gi.newChangeBuffer({
trackRemovals: !0
}), [ 4 /*yield*/ , zu(e, function(t) {
// It is OK that the path used for the query is not valid, because this will
// not be read and queried.
return In(yn(ct.fromString("__bundle__/docs/".concat(t))));
}(o)) ];
case 1:
// Allocates a target to hold all document keys from the bundle, such that
// they will not get garbage collected right away.
return p = n.sent(), [ 2 /*return*/ , e.persistence.runTransaction("Apply bundle documents", "readwrite", (function(t) {
return ju(t, d, a).next((function(e) {
return d.apply(t), e;
})).next((function(n) {
return e.Cs.removeMatchingKeysForTargetId(t, p.targetId).next((function() {
return e.Cs.addMatchingKeys(t, u, p.targetId);
})).next((function() {
return e.localDocuments.getLocalViewOfDocuments(t, n.Wi, n.zi);
})).next((function() {
return n.Wi;
}));
}));
})) ];
}
}));
}));
}
/**
* Returns a promise of a boolean to indicate if the given bundle has already
* been loaded and the create time is newer than the current loading bundle.
*/
/**
* Saves the given `NamedQuery` to local persistence.
*/ function $u(t, r, i) {
return void 0 === i && (i = _r()), e(this, void 0, void 0, (function() {
var e, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , zu(t, In(no(r.bundledQuery))) ];
case 1:
return e = n.sent(), [ 2 /*return*/ , (o = G(t)).persistence.runTransaction("Save named query", "readwrite", (function(t) {
var n = jr(r.readTime);
// Simply save the query itself if it is older than what the SDK already
// has.
if (e.snapshotVersion.compareTo(n) >= 0) return o.Ns.saveNamedQuery(t, r);
// Update existing target data because the query from the bundle is newer.
var u = e.withResumeToken(Yt.EMPTY_BYTE_STRING, n);
return o.qi = o.qi.insert(u.targetId, u), o.Cs.updateTargetData(t, u).next((function() {
return o.Cs.removeMatchingKeysForTargetId(t, e.targetId);
})).next((function() {
return o.Cs.addMatchingKeys(t, i, e.targetId);
})).next((function() {
return o.Ns.saveNamedQuery(t, r);
}));
})) ];
}
}));
}));
}
/** Assembles the key for a client state in WebStorage */ function ta(t, e) {
return "firestore_clients_".concat(t, "_").concat(e);
}
// The format of the WebStorage key that stores the mutation state is:
// firestore_mutations__
// (for unauthenticated users)
// or: firestore_mutations___
// 'user_uid' is last to avoid needing to escape '_' characters that it might
// contain.
/** Assembles the key for a mutation batch in WebStorage */ function ea(t, e, n) {
var r = "firestore_mutations_".concat(t, "_").concat(n);
return e.isAuthenticated() && (r += "_".concat(e.uid)), r;
}
// The format of the WebStorage key that stores a query target's metadata is:
// firestore_targets__
/** Assembles the key for a query state in WebStorage */ function na(t, e) {
return "firestore_targets_".concat(t, "_").concat(e);
}
// The WebStorage prefix that stores the primary tab's online state. The
// format of the key is:
// firestore_online_state_
/**
* Holds the state of a mutation batch, including its user ID, batch ID and
* whether the batch is 'pending', 'acknowledged' or 'rejected'.
*/
// Visible for testing
var ra = /** @class */ function() {
function t(t, e, n, r) {
this.user = t, this.batchId = e, this.state = n, this.error = r
/**
* Parses a MutationMetadata from its JSON representation in WebStorage.
* Logs a warning and returns null if the format of the data is not valid.
*/;
}
return t.Zi = function(e, n, r) {
var i, o = JSON.parse(r), u = "object" == typeof o && -1 !== [ "pending", "acknowledged", "rejected" ].indexOf(o.state) && (void 0 === o.error || "object" == typeof o.error);
return u && o.error && ((u = "string" == typeof o.error.message && "string" == typeof o.error.code) && (i = new j(o.error.code, o.error.message))),
u ? new t(e, n, o.state, i) : (M("SharedClientState", "Failed to parse mutation state for ID '".concat(n, "': ").concat(r)),
null);
}, t.prototype.tr = function() {
var t = {
state: this.state,
updateTimeMs: Date.now()
};
return this.error && (t.error = {
code: this.error.code,
message: this.error.message
}), JSON.stringify(t);
}, t;
}(), ia = /** @class */ function() {
function t(t, e, n) {
this.targetId = t, this.state = e, this.error = n
/**
* Parses a QueryTargetMetadata from its JSON representation in WebStorage.
* Logs a warning and returns null if the format of the data is not valid.
*/;
}
return t.Zi = function(e, n) {
var r, i = JSON.parse(n), o = "object" == typeof i && -1 !== [ "not-current", "current", "rejected" ].indexOf(i.state) && (void 0 === i.error || "object" == typeof i.error);
return o && i.error && ((o = "string" == typeof i.error.message && "string" == typeof i.error.code) && (r = new j(i.error.code, i.error.message))),
o ? new t(e, i.state, r) : (M("SharedClientState", "Failed to parse target state for ID '".concat(e, "': ").concat(n)),
null);
}, t.prototype.tr = function() {
var t = {
state: this.state,
updateTimeMs: Date.now()
};
return this.error && (t.error = {
code: this.error.code,
message: this.error.message
}), JSON.stringify(t);
}, t;
}(), oa = /** @class */ function() {
function t(t, e) {
this.clientId = t, this.activeTargetIds = e
/**
* Parses a RemoteClientState from the JSON representation in WebStorage.
* Logs a warning and returns null if the format of the data is not valid.
*/;
}
return t.Zi = function(e, n) {
for (var r = JSON.parse(n), i = "object" == typeof r && r.activeTargetIds instanceof Array, o = xr(), u = 0; i && u < r.activeTargetIds.length; ++u) i = Wt(r.activeTargetIds[u]),
o = o.add(r.activeTargetIds[u]);
return i ? new t(e, o) : (M("SharedClientState", "Failed to parse client data for instance '".concat(e, "': ").concat(n)),
null);
}, t;
}(), ua = /** @class */ function() {
function t(t, e) {
this.clientId = t, this.onlineState = e
/**
* Parses a SharedOnlineState from its JSON representation in WebStorage.
* Logs a warning and returns null if the format of the data is not valid.
*/;
}
return t.Zi = function(e) {
var n = JSON.parse(e);
return "object" == typeof n && -1 !== [ "Unknown", "Online", "Offline" ].indexOf(n.onlineState) && "string" == typeof n.clientId ? new t(n.clientId, n.onlineState) : (M("SharedClientState", "Failed to parse online state: ".concat(e)),
null);
}, t;
}(), aa = /** @class */ function() {
function t() {
this.activeTargetIds = xr();
}
return t.prototype.er = function(t) {
this.activeTargetIds = this.activeTargetIds.add(t);
}, t.prototype.nr = function(t) {
this.activeTargetIds = this.activeTargetIds.delete(t);
},
/**
* Converts this entry into a JSON-encoded format we can use for WebStorage.
* Does not encode `clientId` as it is part of the key in WebStorage.
*/
t.prototype.tr = function() {
var t = {
activeTargetIds: this.activeTargetIds.toArray(),
updateTimeMs: Date.now()
};
return JSON.stringify(t);
}, t;
}(), sa = /** @class */ function() {
function t(t, e, n, r, i) {
this.window = t, this.Hs = e, this.persistenceKey = n, this.sr = r, this.syncEngine = null,
this.onlineStateHandler = null, this.sequenceNumberHandler = null, this.ir = this.rr.bind(this),
this.ur = new He(rt), this.started = !1,
/**
* Captures WebStorage events that occur before `start()` is called. These
* events are replayed once `WebStorageSharedClientState` is started.
*/
this.cr = [];
// Escape the special characters mentioned here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
var o = n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
this.storage = this.window.localStorage, this.currentUser = i, this.ar = ta(this.persistenceKey, this.sr),
this.hr =
/** Assembles the key for the current sequence number. */
function(t) {
return "firestore_sequence_number_".concat(t);
}(this.persistenceKey), this.ur = this.ur.insert(this.sr, new aa), this.lr = new RegExp("^firestore_clients_".concat(o, "_([^_]*)$")),
this.dr = new RegExp("^firestore_mutations_".concat(o, "_(\\d+)(?:_(.*))?$")), this._r = new RegExp("^firestore_targets_".concat(o, "_(\\d+)$")),
this.wr =
/** Assembles the key for the online state of the primary tab. */
function(t) {
return "firestore_online_state_".concat(t);
}(this.persistenceKey), this.mr = function(t) {
return "firestore_bundle_loaded_v2_".concat(t);
}(this.persistenceKey),
// Rather than adding the storage observer during start(), we add the
// storage observer during initialization. This ensures that we collect
// events before other components populate their initial state (during their
// respective start() calls). Otherwise, we might for example miss a
// mutation that is added after LocalStore's start() processed the existing
// mutations but before we observe WebStorage events.
this.window.addEventListener("storage", this.ir);
}
/** Returns 'true' if WebStorage is available in the current environment. */ return t.C = function(t) {
return !(!t || !t.localStorage);
}, t.prototype.start = function() {
return e(this, void 0, void 0, (function() {
var t, e, r, i, o, u, a, s, c, l, h, f = this;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , this.syncEngine.vi() ];
case 1:
for (t = n.sent(), e = 0, r = t; e < r.length; e++) (i = r[e]) !== this.sr && (o = this.getItem(ta(this.persistenceKey, i))) && (u = oa.Zi(i, o)) && (this.ur = this.ur.insert(u.clientId, u));
for (this.gr(), (a = this.storage.getItem(this.wr)) && (s = this.yr(a)) && this.pr(s),
c = 0, l = this.cr; c < l.length; c++) h = l[c], this.rr(h);
return this.cr = [],
// Register a window unload hook to remove the client metadata entry from
// WebStorage even if `shutdown()` was not called.
this.window.addEventListener("pagehide", (function() {
return f.shutdown();
})), this.started = !0, [ 2 /*return*/ ];
}
}));
}));
}, t.prototype.writeSequenceNumber = function(t) {
this.setItem(this.hr, JSON.stringify(t));
}, t.prototype.getAllActiveQueryTargets = function() {
return this.Ir(this.ur);
}, t.prototype.isActiveQueryTarget = function(t) {
var e = !1;
return this.ur.forEach((function(n, r) {
r.activeTargetIds.has(t) && (e = !0);
})), e;
}, t.prototype.addPendingMutation = function(t) {
this.Tr(t, "pending");
}, t.prototype.updateMutationState = function(t, e, n) {
this.Tr(t, e, n),
// Once a final mutation result is observed by other clients, they no longer
// access the mutation's metadata entry. Since WebStorage replays events
// in order, it is safe to delete the entry right after updating it.
this.Er(t);
}, t.prototype.addLocalQueryTarget = function(t) {
var e = "not-current";
// Lookup an existing query state if the target ID was already registered
// by another tab
if (this.isActiveQueryTarget(t)) {
var n = this.storage.getItem(na(this.persistenceKey, t));
if (n) {
var r = ia.Zi(t, n);
r && (e = r.state);
}
}
return this.Ar.er(t), this.gr(), e;
}, t.prototype.removeLocalQueryTarget = function(t) {
this.Ar.nr(t), this.gr();
}, t.prototype.isLocalQueryTarget = function(t) {
return this.Ar.activeTargetIds.has(t);
}, t.prototype.clearQueryState = function(t) {
this.removeItem(na(this.persistenceKey, t));
}, t.prototype.updateQueryState = function(t, e, n) {
this.Rr(t, e, n);
}, t.prototype.handleUserChange = function(t, e, n) {
var r = this;
e.forEach((function(t) {
r.Er(t);
})), this.currentUser = t, n.forEach((function(t) {
r.addPendingMutation(t);
}));
}, t.prototype.setOnlineState = function(t) {
this.br(t);
}, t.prototype.notifyBundleLoaded = function(t) {
this.Pr(t);
}, t.prototype.shutdown = function() {
this.started && (this.window.removeEventListener("storage", this.ir), this.removeItem(this.ar),
this.started = !1);
}, t.prototype.getItem = function(t) {
var e = this.storage.getItem(t);
return V("SharedClientState", "READ", t, e), e;
}, t.prototype.setItem = function(t, e) {
V("SharedClientState", "SET", t, e), this.storage.setItem(t, e);
}, t.prototype.removeItem = function(t) {
V("SharedClientState", "REMOVE", t), this.storage.removeItem(t);
}, t.prototype.rr = function(t) {
var r = this, i = t;
// Note: The function is typed to take Event to be interface-compatible with
// `Window.addEventListener`.
if (i.storageArea === this.storage) {
if (V("SharedClientState", "EVENT", i.key, i.newValue), i.key === this.ar) return void M("Received WebStorage notification for local change. Another client might have garbage-collected our state");
this.Hs.enqueueRetryable((function() {
return e(r, void 0, void 0, (function() {
var t, e, r, o, u, a, s, c = this;
return n(this, (function(n) {
switch (n.label) {
case 0:
return this.started ? null === i.key ? [ 3 /*break*/ , 7 ] : this.lr.test(i.key) ? null == i.newValue ? (t = this.vr(i.key),
[ 2 /*return*/ , this.Vr(t, null) ]) : (e = this.Sr(i.key, i.newValue)) ? [ 2 /*return*/ , this.Vr(e.clientId, e) ] : [ 3 /*break*/ , 7 ] : [ 3 /*break*/ , 1 ] : [ 3 /*break*/ , 8 ];
case 1:
return this.dr.test(i.key) ? null !== i.newValue && (r = this.Dr(i.key, i.newValue)) ? [ 2 /*return*/ , this.Cr(r) ] : [ 3 /*break*/ , 7 ] : [ 3 /*break*/ , 2 ];
case 2:
return this._r.test(i.key) ? null !== i.newValue && (o = this.Nr(i.key, i.newValue)) ? [ 2 /*return*/ , this.kr(o) ] : [ 3 /*break*/ , 7 ] : [ 3 /*break*/ , 3 ];
case 3:
return i.key !== this.wr ? [ 3 /*break*/ , 4 ] : null !== i.newValue && (u = this.yr(i.newValue)) ? [ 2 /*return*/ , this.pr(u) ] : [ 3 /*break*/ , 7 ];
case 4:
return i.key !== this.hr ? [ 3 /*break*/ , 5 ] : (a = function(t) {
var e = qt.at;
if (null != t) try {
var n = JSON.parse(t);
U("number" == typeof n), e = n;
} catch (t) {
M("SharedClientState", "Failed to read sequence number from WebStorage", t);
}
return e;
}(i.newValue), a !== qt.at && this.sequenceNumberHandler(a), [ 3 /*break*/ , 7 ]);
case 5:
return i.key !== this.mr ? [ 3 /*break*/ , 7 ] : (s = this.Or(i.newValue), [ 4 /*yield*/ , Promise.all(s.map((function(t) {
return c.syncEngine.Mr(t);
}))) ]);
case 6:
n.sent(), n.label = 7;
case 7:
return [ 3 /*break*/ , 9 ];
case 8:
this.cr.push(i), n.label = 9;
case 9:
return [ 2 /*return*/ ];
}
}));
}));
}));
}
}, Object.defineProperty(t.prototype, "Ar", {
get: function() {
return this.ur.get(this.sr);
},
enumerable: !1,
configurable: !0
}), t.prototype.gr = function() {
this.setItem(this.ar, this.Ar.tr());
}, t.prototype.Tr = function(t, e, n) {
var r = new ra(this.currentUser, t, e, n), i = ea(this.persistenceKey, this.currentUser, t);
this.setItem(i, r.tr());
}, t.prototype.Er = function(t) {
var e = ea(this.persistenceKey, this.currentUser, t);
this.removeItem(e);
}, t.prototype.br = function(t) {
var e = {
clientId: this.sr,
onlineState: t
};
this.storage.setItem(this.wr, JSON.stringify(e));
}, t.prototype.Rr = function(t, e, n) {
var r = na(this.persistenceKey, t), i = new ia(t, e, n);
this.setItem(r, i.tr());
}, t.prototype.Pr = function(t) {
var e = JSON.stringify(Array.from(t));
this.setItem(this.mr, e);
},
/**
* Parses a client state key in WebStorage. Returns null if the key does not
* match the expected key format.
*/
t.prototype.vr = function(t) {
var e = this.lr.exec(t);
return e ? e[1] : null;
},
/**
* Parses a client state in WebStorage. Returns 'null' if the value could not
* be parsed.
*/
t.prototype.Sr = function(t, e) {
var n = this.vr(t);
return oa.Zi(n, e);
},
/**
* Parses a mutation batch state in WebStorage. Returns 'null' if the value
* could not be parsed.
*/
t.prototype.Dr = function(t, e) {
var n = this.dr.exec(t), r = Number(n[1]), i = void 0 !== n[2] ? n[2] : null;
return ra.Zi(new N(i), r, e);
},
/**
* Parses a query target state from WebStorage. Returns 'null' if the value
* could not be parsed.
*/
t.prototype.Nr = function(t, e) {
var n = this._r.exec(t), r = Number(n[1]);
return ia.Zi(r, e);
},
/**
* Parses an online state from WebStorage. Returns 'null' if the value
* could not be parsed.
*/
t.prototype.yr = function(t) {
return ua.Zi(t);
}, t.prototype.Or = function(t) {
return JSON.parse(t);
}, t.prototype.Cr = function(t) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
return t.user.uid === this.currentUser.uid ? [ 2 /*return*/ , this.syncEngine.Fr(t.batchId, t.state, t.error) ] : (V("SharedClientState", "Ignoring mutation for non-active user ".concat(t.user.uid)),
[ 2 /*return*/ ]);
}));
}));
}, t.prototype.kr = function(t) {
return this.syncEngine.$r(t.targetId, t.state, t.error);
}, t.prototype.Vr = function(t, e) {
var n = this, r = e ? this.ur.insert(t, e) : this.ur.remove(t), i = this.Ir(this.ur), o = this.Ir(r), u = [], a = [];
return o.forEach((function(t) {
i.has(t) || u.push(t);
})), i.forEach((function(t) {
o.has(t) || a.push(t);
})), this.syncEngine.Br(u, a).then((function() {
n.ur = r;
}));
}, t.prototype.pr = function(t) {
// We check whether the client that wrote this online state is still active
// by comparing its client ID to the list of clients kept active in
// IndexedDb. If a client does not update their IndexedDb client state
// within 5 seconds, it is considered inactive and we don't emit an online
// state event.
this.ur.get(t.clientId) && this.onlineStateHandler(t.onlineState);
}, t.prototype.Ir = function(t) {
var e = xr();
return t.forEach((function(t, n) {
e = e.unionWith(n.activeTargetIds);
})), e;
}, t;
}(), ca = /** @class */ function() {
function t() {
this.Lr = new aa, this.qr = {}, this.onlineStateHandler = null, this.sequenceNumberHandler = null;
}
return t.prototype.addPendingMutation = function(t) {
// No op.
}, t.prototype.updateMutationState = function(t, e, n) {
// No op.
}, t.prototype.addLocalQueryTarget = function(t) {
return this.Lr.er(t), this.qr[t] || "not-current";
}, t.prototype.updateQueryState = function(t, e, n) {
this.qr[t] = e;
}, t.prototype.removeLocalQueryTarget = function(t) {
this.Lr.nr(t);
}, t.prototype.isLocalQueryTarget = function(t) {
return this.Lr.activeTargetIds.has(t);
}, t.prototype.clearQueryState = function(t) {
delete this.qr[t];
}, t.prototype.getAllActiveQueryTargets = function() {
return this.Lr.activeTargetIds;
}, t.prototype.isActiveQueryTarget = function(t) {
return this.Lr.activeTargetIds.has(t);
}, t.prototype.start = function() {
return this.Lr = new aa, Promise.resolve();
}, t.prototype.handleUserChange = function(t, e, n) {
// No op.
}, t.prototype.setOnlineState = function(t) {
// No op.
}, t.prototype.shutdown = function() {}, t.prototype.writeSequenceNumber = function(t) {},
t.prototype.notifyBundleLoaded = function(t) {
// No op.
}, t;
}(), la = /** @class */ function() {
function t() {}
return t.prototype.Ur = function(t) {
// No-op.
}, t.prototype.shutdown = function() {
// No-op.
}, t;
}(), ha = /** @class */ function() {
function t() {
var t = this;
this.Kr = function() {
return t.Gr();
}, this.Qr = function() {
return t.jr();
}, this.Wr = [], this.zr();
}
return t.prototype.Ur = function(t) {
this.Wr.push(t);
}, t.prototype.shutdown = function() {
window.removeEventListener("online", this.Kr), window.removeEventListener("offline", this.Qr);
}, t.prototype.zr = function() {
window.addEventListener("online", this.Kr), window.addEventListener("offline", this.Qr);
}, t.prototype.Gr = function() {
V("ConnectivityMonitor", "Network connectivity changed: AVAILABLE");
for (var t = 0, e = this.Wr; t < e.length; t++) {
(0, e[t])(0 /* NetworkStatus.AVAILABLE */);
}
}, t.prototype.jr = function() {
V("ConnectivityMonitor", "Network connectivity changed: UNAVAILABLE");
for (var t = 0, e = this.Wr; t < e.length; t++) {
(0, e[t])(1 /* NetworkStatus.UNAVAILABLE */);
}
},
// TODO(chenbrian): Consider passing in window either into this component or
// here for testing via FakeWindow.
/** Checks that all used attributes of window are available. */
t.C = function() {
return "undefined" != typeof window && void 0 !== window.addEventListener && void 0 !== window.removeEventListener;
}, t;
}(), fa = {
BatchGetDocuments: "batchGet",
Commit: "commit",
RunQuery: "runQuery",
RunAggregationQuery: "runAggregationQuery"
}, da = /** @class */ function() {
function t(t) {
this.Hr = t.Hr, this.Jr = t.Jr;
}
return t.prototype.Yr = function(t) {
this.Xr = t;
}, t.prototype.Zr = function(t) {
this.eo = t;
}, t.prototype.onMessage = function(t) {
this.no = t;
}, t.prototype.close = function() {
this.Jr();
}, t.prototype.send = function(t) {
this.Hr(t);
}, t.prototype.so = function() {
this.Xr();
}, t.prototype.io = function(t) {
this.eo(t);
}, t.prototype.ro = function(t) {
this.no(t);
}, t;
}(), pa = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this, t) || this).forceLongPolling = t.forceLongPolling, n.autoDetectLongPolling = t.autoDetectLongPolling,
n.useFetchStreams = t.useFetchStreams, n;
}
/**
* Base class for all Rest-based connections to the backend (WebChannel and
* HTTP).
*/
return t(n, e), n.prototype.fo = function(t, e, n, r) {
return new Promise((function(i, o) {
var u = new b;
u.setWithCredentials(!0), u.listenOnce(I.COMPLETE, (function() {
try {
switch (u.getLastErrorCode()) {
case T.NO_ERROR:
var e = u.getResponseJson();
V("Connection", "XHR received:", JSON.stringify(e)), i(e);
break;
case T.TIMEOUT:
V("Connection", 'RPC "' + t + '" timed out'), o(new j(K.DEADLINE_EXCEEDED, "Request time out"));
break;
case T.HTTP_ERROR:
var n = u.getStatus();
if (V("Connection", 'RPC "' + t + '" failed with status:', n, "response text:", u.getResponseText()),
n > 0) {
var r = u.getResponseJson();
Array.isArray(r) && (r = r[0]);
var a = null == r ? void 0 : r.error;
if (a && a.status && a.message) {
var s = function(t) {
var e = t.toLowerCase().replace(/_/g, "-");
return Object.values(K).indexOf(e) >= 0 ? e : K.UNKNOWN;
}(a.status);
o(new j(s, a.message));
} else o(new j(K.UNKNOWN, "Server responded with status " + u.getStatus()));
} else
// If we received an HTTP_ERROR but there's no status code,
// it's most probably a connection issue
o(new j(K.UNAVAILABLE, "Connection failed."));
break;
default:
q();
}
} finally {
V("Connection", 'RPC "' + t + '" completed.');
}
}));
var a = JSON.stringify(r);
u.send(e, "POST", a, n, 15);
}));
}, n.prototype.wo = function(t, e, n) {
var r = [ this.oo, "/", "google.firestore.v1.Firestore", "/", t, "/channel" ], i = E(), o = S(), u = {
// Required for backend stickiness, routing behavior is based on this
// parameter.
httpSessionIdParam: "gsessionid",
initMessageHeaders: {},
messageUrlParams: {
// This param is used to improve routing and project isolation by the
// backend and must be included in every request.
database: "projects/".concat(this.databaseId.projectId, "/databases/").concat(this.databaseId.database)
},
sendRawJson: !0,
supportsCrossDomainXhr: !0,
internalChannelParams: {
// Override the default timeout (randomized between 10-20 seconds) since
// a large write batch on a slow internet connection may take a long
// time to send to the backend. Rather than have WebChannel impose a
// tight timeout which could lead to infinite timeouts and retries, we
// set it very large (5-10 minutes) and rely on the browser's builtin
// timeouts to kick in if the request isn't working.
forwardChannelRequestTimeoutMs: 6e5
},
forceLongPolling: this.forceLongPolling,
detectBufferingProxy: this.autoDetectLongPolling
};
this.useFetchStreams && (u.xmlHttpFactory = new _({})), this.lo(u.initMessageHeaders, e, n),
// Sending the custom headers we just added to request.initMessageHeaders
// (Authorization, etc.) will trigger the browser to make a CORS preflight
// request because the XHR will no longer meet the criteria for a "simple"
// CORS request:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests
// Therefore to avoid the CORS preflight request (an extra network
// roundtrip), we use the encodeInitMessageHeaders option to specify that
// the headers should instead be encoded in the request's POST payload,
// which is recognized by the webchannel backend.
u.encodeInitMessageHeaders = !0;
var a = r.join("");
V("Connection", "Creating WebChannel: " + a, u);
var s = i.createWebChannel(a, u), c = !1, l = !1, h = new da({
Hr: function(t) {
l ? V("Connection", "Not sending because WebChannel is closed:", t) : (c || (V("Connection", "Opening WebChannel transport."),
s.open(), c = !0), V("Connection", "WebChannel sending:", t), s.send(t));
},
Jr: function() {
return s.close();
}
}), f = function(t, e, n) {
// TODO(dimond): closure typing seems broken because WebChannel does
// not implement goog.events.Listenable
t.listen(e, (function(t) {
try {
n(t);
} catch (t) {
setTimeout((function() {
throw t;
}), 0);
}
}));
};
// WebChannel supports sending the first message with the handshake - saving
// a network round trip. However, it will have to call send in the same
// JS event loop as open. In order to enforce this, we delay actually
// opening the WebChannel until send is called. Whether we have called
// open is tracked with this variable.
// Closure events are guarded and exceptions are swallowed, so catch any
// exception and rethrow using a setTimeout so they become visible again.
// Note that eventually this function could go away if we are confident
// enough the code is exception free.
return f(s, D.EventType.OPEN, (function() {
l || V("Connection", "WebChannel transport opened.");
})), f(s, D.EventType.CLOSE, (function() {
l || (l = !0, V("Connection", "WebChannel transport closed"), h.io());
})), f(s, D.EventType.ERROR, (function(t) {
l || (l = !0, L("Connection", "WebChannel transport errored:", t), h.io(new j(K.UNAVAILABLE, "The operation could not be completed")));
})), f(s, D.EventType.MESSAGE, (function(t) {
var e;
if (!l) {
var n = t.data[0];
U(!!n);
// TODO(b/35143891): There is a bug in One Platform that caused errors
// (and only errors) to be wrapped in an extra array. To be forward
// compatible with the bug we need to check either condition. The latter
// can be removed once the fix has been rolled out.
// Use any because msgData.error is not typed.
var r = n, i = r.error || (null === (e = r[0]) || void 0 === e ? void 0 : e.error);
if (i) {
V("Connection", "WebChannel received error:", i);
// error.status will be a string like 'OK' or 'NOT_FOUND'.
var o = i.status, u =
/**
* Maps an error Code from a GRPC status identifier like 'NOT_FOUND'.
*
* @returns The Code equivalent to the given status string or undefined if
* there is no match.
*/
function(t) {
// lookup by string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var e = ar[t];
if (void 0 !== e) return dr(e);
}(o), a = i.message;
void 0 === u && (u = K.INTERNAL, a = "Unknown error status: " + o + " with message " + i.message),
// Mark closed so no further events are propagated
l = !0, h.io(new j(u, a)), s.close();
} else V("Connection", "WebChannel received:", n), h.ro(n);
}
})), f(o, x.STAT_EVENT, (function(t) {
t.stat === A.PROXY ? V("Connection", "Detected buffering proxy") : t.stat === A.NOPROXY && V("Connection", "Detected no buffering proxy");
})), setTimeout((function() {
// Technically we could/should wait for the WebChannel opened event,
// but because we want to send the first message with the WebChannel
// handshake we pretend the channel opened here (asynchronously), and
// then delay the actual open until the first message is sent.
h.so();
}), 0), h;
}, n;
}(/** @class */ function() {
function t(t) {
this.databaseInfo = t, this.databaseId = t.databaseId;
var e = t.ssl ? "https" : "http";
this.oo = e + "://" + t.host, this.uo = "projects/" + this.databaseId.projectId + "/databases/" + this.databaseId.database + "/documents";
}
return Object.defineProperty(t.prototype, "co", {
get: function() {
// Both `invokeRPC()` and `invokeStreamingRPC()` use their `path` arguments to determine
// where to run the query, and expect the `request` to NOT specify the "path".
return !1;
},
enumerable: !1,
configurable: !0
}), t.prototype.ao = function(t, e, n, r, i) {
var o = this.ho(t, e);
V("RestConnection", "Sending: ", o, n);
var u = {};
return this.lo(u, r, i), this.fo(t, o, u, n).then((function(t) {
return V("RestConnection", "Received: ", t), t;
}), (function(e) {
throw L("RestConnection", "".concat(t, " failed with error: "), e, "url: ", o, "request:", n),
e;
}));
}, t.prototype._o = function(t, e, n, r, i, o) {
// The REST API automatically aggregates all of the streamed results, so we
// can just use the normal invoke() method.
return this.ao(t, e, n, r, i);
},
/**
* Modifies the headers for a request, adding any authorization token if
* present and any additional headers for the request.
*/
t.prototype.lo = function(t, e, n) {
t["X-Goog-Api-Client"] = "gl-js/ fire/" + k,
// Content-Type: text/plain will avoid preflight requests which might
// mess with CORS and redirects by proxies. If we add custom headers
// we will need to change this code to potentially use the $httpOverwrite
// parameter supported by ESF to avoid triggering preflight requests.
t["Content-Type"] = "text/plain", this.databaseInfo.appId && (t["X-Firebase-GMPID"] = this.databaseInfo.appId),
e && e.headers.forEach((function(e, n) {
return t[n] = e;
})), n && n.headers.forEach((function(e, n) {
return t[n] = e;
}));
}, t.prototype.ho = function(t, e) {
var n = fa[t];
return "".concat(this.oo, "/v1/").concat(e, ":").concat(n);
}, t;
}());
/**
* Holds the state of a query target, including its target ID and whether the
* target is 'not-current', 'current' or 'rejected'.
*/
// Visible for testing
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** Initializes the WebChannelConnection for the browser. */
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** The Platform's 'window' implementation or null if not available. */
function ya() {
// `window` is not always available, e.g. in ReactNative and WebWorkers.
// eslint-disable-next-line no-restricted-globals
return "undefined" != typeof window ? window : null;
}
/** The Platform's 'document' implementation or null if not available. */ function va() {
// `document` is not always available, e.g. in ReactNative and WebWorkers.
// eslint-disable-next-line no-restricted-globals
return "undefined" != typeof document ? document : null;
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ function ma(t) {
return new Ur(t, /* useProto3Json= */ !0);
}
/**
* An instance of the Platform's 'TextEncoder' implementation.
*/
/**
* A helper for running delayed tasks following an exponential backoff curve
* between attempts.
*
* Each delay is made up of a "base" delay which follows the exponential
* backoff curve, and a +/- 50% "jitter" that is calculated and added to the
* base delay. This prevents clients from accidentally synchronizing their
* delays causing spikes of load to the backend.
*/ var ga = /** @class */ function() {
function t(
/**
* The AsyncQueue to run backoff operations on.
*/
t,
/**
* The ID to use when scheduling backoff operations on the AsyncQueue.
*/
e,
/**
* The initial delay (used as the base delay on the first retry attempt).
* Note that jitter will still be applied, so the actual delay could be as
* little as 0.5*initialDelayMs.
*/
n
/**
* The multiplier to use to determine the extended base delay after each
* attempt.
*/ , r
/**
* The maximum base delay after which no further backoff is performed.
* Note that jitter will still be applied, so the actual delay could be as
* much as 1.5*maxDelayMs.
*/ , i) {
void 0 === n && (n = 1e3), void 0 === r && (r = 1.5), void 0 === i && (i = 6e4),
this.Hs = t, this.timerId = e, this.mo = n, this.yo = r, this.po = i, this.Io = 0,
this.To = null,
/** The last backoff attempt, as epoch milliseconds. */
this.Eo = Date.now(), this.reset();
}
/**
* Resets the backoff delay.
*
* The very next backoffAndWait() will have no delay. If it is called again
* (i.e. due to an error), initialDelayMs (plus jitter) will be used, and
* subsequent ones will increase according to the backoffFactor.
*/ return t.prototype.reset = function() {
this.Io = 0;
},
/**
* Resets the backoff delay to the maximum delay (e.g. for use after a
* RESOURCE_EXHAUSTED error).
*/
t.prototype.Ao = function() {
this.Io = this.po;
},
/**
* Returns a promise that resolves after currentDelayMs, and increases the
* delay for any subsequent attempts. If there was a pending backoff operation
* already, it will be canceled.
*/
t.prototype.Ro = function(t) {
var e = this;
// Cancel any pending backoff operation.
this.cancel();
// First schedule using the current base (which may be 0 and should be
// honored as such).
var n = Math.floor(this.Io + this.bo()), r = Math.max(0, Date.now() - this.Eo), i = Math.max(0, n - r);
// Guard against lastAttemptTime being in the future due to a clock change.
i > 0 && V("ExponentialBackoff", "Backing off for ".concat(i, " ms (base delay: ").concat(this.Io, " ms, delay with jitter: ").concat(n, " ms, last attempt: ").concat(r, " ms ago)")),
this.To = this.Hs.enqueueAfterDelay(this.timerId, i, (function() {
return e.Eo = Date.now(), t();
})),
// Apply backoff factor to determine next delay and ensure it is within
// bounds.
this.Io *= this.yo, this.Io < this.mo && (this.Io = this.mo), this.Io > this.po && (this.Io = this.po);
}, t.prototype.Po = function() {
null !== this.To && (this.To.skipDelay(), this.To = null);
}, t.prototype.cancel = function() {
null !== this.To && (this.To.cancel(), this.To = null);
},
/** Returns a random value in the range [-currentBaseMs/2, currentBaseMs/2] */ t.prototype.bo = function() {
return (Math.random() - .5) * this.Io;
}, t;
}(), wa = /** @class */ function() {
function t(t, e, n, r, i, o, u, a) {
this.Hs = t, this.vo = n, this.Vo = r, this.connection = i, this.authCredentialsProvider = o,
this.appCheckCredentialsProvider = u, this.listener = a, this.state = 0 /* PersistentStreamState.Initial */ ,
/**
* A close count that's incremented every time the stream is closed; used by
* getCloseGuardedDispatcher() to invalidate callbacks that happen after
* close.
*/
this.So = 0, this.Do = null, this.Co = null, this.stream = null, this.xo = new ga(t, e)
/**
* Returns true if start() has been called and no error has occurred. True
* indicates the stream is open or in the process of opening (which
* encompasses respecting backoff, getting auth tokens, and starting the
* actual RPC). Use isOpen() to determine if the stream is open and ready for
* outbound requests.
*/;
}
return t.prototype.No = function() {
return 1 /* PersistentStreamState.Starting */ === this.state || 5 /* PersistentStreamState.Backoff */ === this.state || this.ko();
},
/**
* Returns true if the underlying RPC is open (the onOpen() listener has been
* called) and the stream is ready for outbound requests.
*/
t.prototype.ko = function() {
return 2 /* PersistentStreamState.Open */ === this.state || 3 /* PersistentStreamState.Healthy */ === this.state;
},
/**
* Starts the RPC. Only allowed if isStarted() returns false. The stream is
* not immediately ready for use: onOpen() will be invoked when the RPC is
* ready for outbound requests, at which point isOpen() will return true.
*
* When start returns, isStarted() will return true.
*/
t.prototype.start = function() {
4 /* PersistentStreamState.Error */ !== this.state ? this.auth() : this.Oo();
},
/**
* Stops the RPC. This call is idempotent and allowed regardless of the
* current isStarted() state.
*
* When stop returns, isStarted() and isOpen() will both return false.
*/
t.prototype.stop = function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
switch (t.label) {
case 0:
return this.No() ? [ 4 /*yield*/ , this.close(0 /* PersistentStreamState.Initial */) ] : [ 3 /*break*/ , 2 ];
case 1:
t.sent(), t.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
},
/**
* After an error the stream will usually back off on the next attempt to
* start it. If the error warrants an immediate restart of the stream, the
* sender can use this to indicate that the receiver should not back off.
*
* Each error will call the onClose() listener. That function can decide to
* inhibit backoff if required.
*/
t.prototype.Mo = function() {
this.state = 0 /* PersistentStreamState.Initial */ , this.xo.reset();
},
/**
* Marks this stream as idle. If no further actions are performed on the
* stream for one minute, the stream will automatically close itself and
* notify the stream's onClose() handler with Status.OK. The stream will then
* be in a !isStarted() state, requiring the caller to start the stream again
* before further use.
*
* Only streams that are in state 'Open' can be marked idle, as all other
* states imply pending network operations.
*/
t.prototype.Fo = function() {
var t = this;
// Starts the idle time if we are in state 'Open' and are not yet already
// running a timer (in which case the previous idle timeout still applies).
this.ko() && null === this.Do && (this.Do = this.Hs.enqueueAfterDelay(this.vo, 6e4, (function() {
return t.$o();
})));
},
/** Sends a message to the underlying stream. */ t.prototype.Bo = function(t) {
this.Lo(), this.stream.send(t);
},
/** Called by the idle timer when the stream should close due to inactivity. */ t.prototype.$o = function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
return this.ko() ? [ 2 /*return*/ , this.close(0 /* PersistentStreamState.Initial */) ] : [ 2 /*return*/ ];
}));
}));
},
/** Marks the stream as active again. */ t.prototype.Lo = function() {
this.Do && (this.Do.cancel(), this.Do = null);
},
/** Cancels the health check delayed operation. */ t.prototype.qo = function() {
this.Co && (this.Co.cancel(), this.Co = null);
},
/**
* Closes the stream and cleans up as necessary:
*
* * closes the underlying GRPC stream;
* * calls the onClose handler with the given 'error';
* * sets internal stream state to 'finalState';
* * adjusts the backoff timer based on the error
*
* A new stream can be opened by calling start().
*
* @param finalState - the intended state of the stream after closing.
* @param error - the error the connection was closed with.
*/
t.prototype.close = function(t, r) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
// Notify the listener that the stream closed.
// Cancel any outstanding timers (they're guaranteed not to execute).
return this.Lo(), this.qo(), this.xo.cancel(),
// Invalidates any stream-related callbacks (e.g. from auth or the
// underlying stream), guaranteeing they won't execute.
this.So++, 4 /* PersistentStreamState.Error */ !== t ?
// If this is an intentional close ensure we don't delay our next connection attempt.
this.xo.reset() : r && r.code === K.RESOURCE_EXHAUSTED ? (
// Log the error. (Probably either 'quota exceeded' or 'max queue length reached'.)
M(r.toString()), M("Using maximum backoff delay to prevent overloading the backend."),
this.xo.Ao()) : r && r.code === K.UNAUTHENTICATED && 3 /* PersistentStreamState.Healthy */ !== this.state && (
// "unauthenticated" error means the token was rejected. This should rarely
// happen since both Auth and AppCheck ensure a sufficient TTL when we
// request a token. If a user manually resets their system clock this can
// fail, however. In this case, we should get a Code.UNAUTHENTICATED error
// before we received the first message and we need to invalidate the token
// to ensure that we fetch a new token.
this.authCredentialsProvider.invalidateToken(), this.appCheckCredentialsProvider.invalidateToken()),
// Clean up the underlying stream because we are no longer interested in events.
null !== this.stream && (this.Uo(), this.stream.close(), this.stream = null),
// This state must be assigned before calling onClose() to allow the callback to
// inhibit backoff or otherwise manipulate the state in its non-started state.
this.state = t, [ 4 /*yield*/ , this.listener.Zr(r) ];
case 1:
// Cancel any outstanding timers (they're guaranteed not to execute).
// Notify the listener that the stream closed.
return e.sent(), [ 2 /*return*/ ];
}
}));
}));
},
/**
* Can be overridden to perform additional cleanup before the stream is closed.
* Calling super.tearDown() is not required.
*/
t.prototype.Uo = function() {}, t.prototype.auth = function() {
var t = this;
this.state = 1 /* PersistentStreamState.Starting */;
var e = this.Ko(this.So), n = this.So;
// TODO(mikelehen): Just use dispatchIfNotClosed, but see TODO below.
Promise.all([ this.authCredentialsProvider.getToken(), this.appCheckCredentialsProvider.getToken() ]).then((function(e) {
var r = e[0], i = e[1];
// Stream can be stopped while waiting for authentication.
// TODO(mikelehen): We really should just use dispatchIfNotClosed
// and let this dispatch onto the queue, but that opened a spec test can
// of worms that I don't want to deal with in this PR.
t.So === n &&
// Normally we'd have to schedule the callback on the AsyncQueue.
// However, the following calls are safe to be called outside the
// AsyncQueue since they don't chain asynchronous calls
t.Go(r, i);
}), (function(n) {
e((function() {
var e = new j(K.UNKNOWN, "Fetching auth token failed: " + n.message);
return t.Qo(e);
}));
}));
}, t.prototype.Go = function(t, e) {
var n = this, r = this.Ko(this.So);
this.stream = this.jo(t, e), this.stream.Yr((function() {
r((function() {
return n.state = 2 /* PersistentStreamState.Open */ , n.Co = n.Hs.enqueueAfterDelay(n.Vo, 1e4, (function() {
return n.ko() && (n.state = 3 /* PersistentStreamState.Healthy */), Promise.resolve();
})), n.listener.Yr();
}));
})), this.stream.Zr((function(t) {
r((function() {
return n.Qo(t);
}));
})), this.stream.onMessage((function(t) {
r((function() {
return n.onMessage(t);
}));
}));
}, t.prototype.Oo = function() {
var t = this;
this.state = 5 /* PersistentStreamState.Backoff */ , this.xo.Ro((function() {
return e(t, void 0, void 0, (function() {
return n(this, (function(t) {
return this.state = 0 /* PersistentStreamState.Initial */ , this.start(), [ 2 /*return*/ ];
}));
}));
}));
},
// Visible for tests
t.prototype.Qo = function(t) {
// In theory the stream could close cleanly, however, in our current model
// we never expect this to happen because if we stop a stream ourselves,
// this callback will never be called. To prevent cases where we retry
// without a backoff accidentally, we set the stream to error in all cases.
return V("PersistentStream", "close with error: ".concat(t)), this.stream = null,
this.close(4 /* PersistentStreamState.Error */ , t);
},
/**
* Returns a "dispatcher" function that dispatches operations onto the
* AsyncQueue but only runs them if closeCount remains unchanged. This allows
* us to turn auth / stream callbacks into no-ops if the stream is closed /
* re-opened, etc.
*/
t.prototype.Ko = function(t) {
var e = this;
return function(n) {
e.Hs.enqueueAndForget((function() {
return e.So === t ? n() : (V("PersistentStream", "stream callback skipped by getCloseGuardedDispatcher."),
Promise.resolve());
}));
};
}, t;
}(), ba = /** @class */ function(e) {
function n(t, n, r, i, o, u) {
var a = this;
return (a = e.call(this, t, "listen_stream_connection_backoff" /* TimerId.ListenStreamConnectionBackoff */ , "listen_stream_idle" /* TimerId.ListenStreamIdle */ , "health_check_timeout" /* TimerId.HealthCheckTimeout */ , n, r, i, u) || this).yt = o,
a;
}
return t(n, e), n.prototype.jo = function(t, e) {
return this.connection.wo("Listen", t, e);
}, n.prototype.onMessage = function(t) {
// A successful response means the stream is healthy
this.xo.reset();
var e = function(t, e) {
var n;
if ("targetChange" in e) {
e.targetChange;
// proto3 default value is unset in JSON (undefined), so use 'NO_CHANGE'
// if unset
var r = function(t) {
return "NO_CHANGE" === t ? 0 /* WatchTargetChangeState.NoChange */ : "ADD" === t ? 1 /* WatchTargetChangeState.Added */ : "REMOVE" === t ? 2 /* WatchTargetChangeState.Removed */ : "CURRENT" === t ? 3 /* WatchTargetChangeState.Current */ : "RESET" === t ? 4 /* WatchTargetChangeState.Reset */ : q();
}(e.targetChange.targetChangeType || "NO_CHANGE"), i = e.targetChange.targetIds || [], o = function(t, e) {
return t.wt ? (U(void 0 === e || "string" == typeof e), Yt.fromBase64String(e || "")) : (U(void 0 === e || e instanceof Uint8Array),
Yt.fromUint8Array(e || new Uint8Array));
}(t, e.targetChange.resumeToken), u = e.targetChange.cause, a = u && function(t) {
var e = void 0 === t.code ? K.UNKNOWN : dr(t.code);
return new j(e, t.message || "");
}(u);
n = new Fr(r, i, o, a || null);
} else if ("documentChange" in e) {
e.documentChange;
var s = e.documentChange;
s.document, s.document.name, s.document.updateTime;
var c = Hr(t, s.document.name), l = jr(s.document.updateTime), h = s.document.createTime ? jr(s.document.createTime) : at.min(), f = new en({
mapValue: {
fields: s.document.fields
}
}), d = rn.newFoundDocument(c, l, h, f), p = s.targetIds || [], y = s.removedTargetIds || [];
n = new Nr(p, y, d.key, d);
} else if ("documentDelete" in e) {
e.documentDelete;
var v = e.documentDelete;
v.document;
var m = Hr(t, v.document), g = v.readTime ? jr(v.readTime) : at.min(), w = rn.newNoDocument(m, g), b = v.removedTargetIds || [];
n = new Nr([], b, w.key, w);
} else if ("documentRemove" in e) {
e.documentRemove;
var I = e.documentRemove;
I.document;
var T = Hr(t, I.document), E = I.removedTargetIds || [];
n = new Nr([], E, T, null);
} else {
if (!("filter" in e)) return q();
e.filter;
var S = e.filter;
S.targetId;
var _ = S.count || 0, D = new hr(_), x = S.targetId;
n = new kr(x, D);
}
return n;
}(this.yt, t), n = function(t) {
// We have only reached a consistent snapshot for the entire stream if there
// is a read_time set and it applies to all targets (i.e. the list of
// targets is empty). The backend is guaranteed to send such responses.
if (!("targetChange" in t)) return at.min();
var e = t.targetChange;
return e.targetIds && e.targetIds.length ? at.min() : e.readTime ? jr(e.readTime) : at.min();
}(t);
return this.listener.Wo(e, n);
},
/**
* Registers interest in the results of the given target. If the target
* includes a resumeToken it will be included in the request. Results that
* affect the target will be streamed back as WatchChange messages that
* reference the targetId.
*/
n.prototype.zo = function(t) {
var e = {};
e.database = Zr(this.yt), e.addTarget = function(t, e) {
var n, r = e.target;
return (n = cn(r) ? {
documents: ri(t, r)
} : {
query: ii(t, r)
}).targetId = e.targetId, e.resumeToken.approximateByteSize() > 0 ? n.resumeToken = Gr(t, e.resumeToken) : e.snapshotVersion.compareTo(at.min()) > 0 && (
// TODO(wuandy): Consider removing above check because it is most likely true.
// Right now, many tests depend on this behaviour though (leaving min() out
// of serialization).
n.readTime = Br(t, e.snapshotVersion.toTimestamp())), n;
}(this.yt, t);
var n = function(t, e) {
var n = function(t, e) {
switch (e) {
case 0 /* TargetPurpose.Listen */ :
return null;
case 1 /* TargetPurpose.ExistenceFilterMismatch */ :
return "existence-filter-mismatch";
case 2 /* TargetPurpose.LimboResolution */ :
return "limbo-document";
default:
return q();
}
}(0, e.purpose);
return null == n ? null : {
"goog-listen-tags": n
};
}(this.yt, t);
n && (e.labels = n), this.Bo(e);
},
/**
* Unregisters interest in the results of the target associated with the
* given targetId.
*/
n.prototype.Ho = function(t) {
var e = {};
e.database = Zr(this.yt), e.removeTarget = t, this.Bo(e);
}, n;
}(wa), Ia = /** @class */ function(e) {
function n(t, n, r, i, o, u) {
var a = this;
return (a = e.call(this, t, "write_stream_connection_backoff" /* TimerId.WriteStreamConnectionBackoff */ , "write_stream_idle" /* TimerId.WriteStreamIdle */ , "health_check_timeout" /* TimerId.HealthCheckTimeout */ , n, r, i, u) || this).yt = o,
a.Jo = !1, a;
}
return t(n, e), Object.defineProperty(n.prototype, "Yo", {
/**
* Tracks whether or not a handshake has been successfully exchanged and
* the stream is ready to accept mutations.
*/
get: function() {
return this.Jo;
},
enumerable: !1,
configurable: !0
}),
// Override of PersistentStream.start
n.prototype.start = function() {
this.Jo = !1, this.lastStreamToken = void 0, e.prototype.start.call(this);
}, n.prototype.Uo = function() {
this.Jo && this.Xo([]);
}, n.prototype.jo = function(t, e) {
return this.connection.wo("Write", t, e);
}, n.prototype.onMessage = function(t) {
if (
// Always capture the last stream token.
U(!!t.streamToken), this.lastStreamToken = t.streamToken, this.Jo) {
// A successful first write response means the stream is healthy,
// Note, that we could consider a successful handshake healthy, however,
// the write itself might be causing an error we want to back off from.
this.xo.reset();
var e = function(t, e) {
return t && t.length > 0 ? (U(void 0 !== e), t.map((function(t) {
return function(t, e) {
// NOTE: Deletes don't have an updateTime.
var n = t.updateTime ? jr(t.updateTime) : jr(e);
return n.isEqual(at.min()) && (
// The Firestore Emulator currently returns an update time of 0 for
// deletes of non-existing documents (rather than null). This breaks the
// test "get deleted doc while offline with source=cache" as NoDocuments
// with version 0 are filtered by IndexedDb's RemoteDocumentCache.
// TODO(#2149): Remove this when Emulator is fixed
n = jr(e)), new Wn(n, t.transformResults || []);
}(t, e);
}))) : [];
}(t.writeResults, t.commitTime), n = jr(t.commitTime);
return this.listener.Zo(n, e);
}
// The first response is always the handshake response
return U(!t.writeResults || 0 === t.writeResults.length), this.Jo = !0,
this.listener.tu();
},
/**
* Sends an initial streamToken to the server, performing the handshake
* required to make the StreamingWrite RPC work. Subsequent
* calls should wait until onHandshakeComplete was called.
*/
n.prototype.eu = function() {
// TODO(dimond): Support stream resumption. We intentionally do not set the
// stream token on the handshake, ignoring any stream token we might have.
var t = {};
t.database = Zr(this.yt), this.Bo(t);
},
/** Sends a group of mutations to the Firestore backend to apply. */ n.prototype.Xo = function(t) {
var e = this, n = {
streamToken: this.lastStreamToken,
writes: t.map((function(t) {
return ei(e.yt, t);
}))
};
this.Bo(n);
}, n;
}(wa), Ta = /** @class */ function(e) {
function n(t, n, r, i) {
var o = this;
return (o = e.call(this) || this).authCredentials = t, o.appCheckCredentials = n,
o.connection = r, o.yt = i, o.nu = !1, o;
}
return t(n, e), n.prototype.su = function() {
if (this.nu) throw new j(K.FAILED_PRECONDITION, "The client has already been terminated.");
},
/** Invokes the provided RPC with auth and AppCheck tokens. */ n.prototype.ao = function(t, e, n) {
var r = this;
return this.su(), Promise.all([ this.authCredentials.getToken(), this.appCheckCredentials.getToken() ]).then((function(i) {
var o = i[0], u = i[1];
return r.connection.ao(t, e, n, o, u);
})).catch((function(t) {
throw "FirebaseError" === t.name ? (t.code === K.UNAUTHENTICATED && (r.authCredentials.invalidateToken(),
r.appCheckCredentials.invalidateToken()), t) : new j(K.UNKNOWN, t.toString());
}));
},
/** Invokes the provided RPC with streamed results with auth and AppCheck tokens. */ n.prototype._o = function(t, e, n, r) {
var i = this;
return this.su(), Promise.all([ this.authCredentials.getToken(), this.appCheckCredentials.getToken() ]).then((function(o) {
var u = o[0], a = o[1];
return i.connection._o(t, e, n, u, a, r);
})).catch((function(t) {
throw "FirebaseError" === t.name ? (t.code === K.UNAUTHENTICATED && (i.authCredentials.invalidateToken(),
i.appCheckCredentials.invalidateToken()), t) : new j(K.UNKNOWN, t.toString());
}));
}, n.prototype.terminate = function() {
this.nu = !0;
}, n;
}((function() {}));
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A PersistentStream is an abstract base class that represents a streaming RPC
* to the Firestore backend. It's built on top of the connections own support
* for streaming RPCs, and adds several critical features for our clients:
*
* - Exponential backoff on failure
* - Authentication via CredentialsProvider
* - Dispatching all callbacks into the shared worker queue
* - Closing idle streams after 60 seconds of inactivity
*
* Subclasses of PersistentStream implement serialization of models to and
* from the JSON representation of the protocol buffers for a specific
* streaming RPC.
*
* ## Starting and Stopping
*
* Streaming RPCs are stateful and need to be start()ed before messages can
* be sent and received. The PersistentStream will call the onOpen() function
* of the listener once the stream is ready to accept requests.
*
* Should a start() fail, PersistentStream will call the registered onClose()
* listener with a FirestoreError indicating what went wrong.
*
* A PersistentStream can be started and stopped repeatedly.
*
* Generic types:
* SendType: The type of the outgoing message of the underlying
* connection stream
* ReceiveType: The type of the incoming message of the underlying
* connection stream
* ListenerType: The type of the listener that will be used for callbacks
*/
/**
* A component used by the RemoteStore to track the OnlineState (that is,
* whether or not the client as a whole should be considered to be online or
* offline), implementing the appropriate heuristics.
*
* In particular, when the client is trying to connect to the backend, we
* allow up to MAX_WATCH_STREAM_FAILURES within ONLINE_STATE_TIMEOUT_MS for
* a connection to succeed. If we have too many failures or the timeout elapses,
* then we set the OnlineState to Offline, and the client will behave as if
* it is offline (get()s will return cached data, etc.).
*/
var Ea = /** @class */ function() {
function t(t, e) {
this.asyncQueue = t, this.onlineStateHandler = e,
/** The current OnlineState. */
this.state = "Unknown" /* OnlineState.Unknown */ ,
/**
* A count of consecutive failures to open the stream. If it reaches the
* maximum defined by MAX_WATCH_STREAM_FAILURES, we'll set the OnlineState to
* Offline.
*/
this.iu = 0,
/**
* A timer that elapses after ONLINE_STATE_TIMEOUT_MS, at which point we
* transition from OnlineState.Unknown to OnlineState.Offline without waiting
* for the stream to actually fail (MAX_WATCH_STREAM_FAILURES times).
*/
this.ru = null,
/**
* Whether the client should log a warning message if it fails to connect to
* the backend (initially true, cleared after a successful stream, or if we've
* logged the message already).
*/
this.ou = !0
/**
* Called by RemoteStore when a watch stream is started (including on each
* backoff attempt).
*
* If this is the first attempt, it sets the OnlineState to Unknown and starts
* the onlineStateTimer.
*/;
}
return t.prototype.uu = function() {
var t = this;
0 === this.iu && (this.cu("Unknown" /* OnlineState.Unknown */), this.ru = this.asyncQueue.enqueueAfterDelay("online_state_timeout" /* TimerId.OnlineStateTimeout */ , 1e4, (function() {
return t.ru = null, t.au("Backend didn't respond within 10 seconds."), t.cu("Offline" /* OnlineState.Offline */),
Promise.resolve();
})));
},
/**
* Updates our OnlineState as appropriate after the watch stream reports a
* failure. The first failure moves us to the 'Unknown' state. We then may
* allow multiple failures (based on MAX_WATCH_STREAM_FAILURES) before we
* actually transition to the 'Offline' state.
*/
t.prototype.hu = function(t) {
"Online" /* OnlineState.Online */ === this.state ? this.cu("Unknown" /* OnlineState.Unknown */) : (this.iu++,
this.iu >= 1 && (this.lu(), this.au("Connection failed 1 times. Most recent error: ".concat(t.toString())),
this.cu("Offline" /* OnlineState.Offline */)));
},
/**
* Explicitly sets the OnlineState to the specified state.
*
* Note that this resets our timers / failure counters, etc. used by our
* Offline heuristics, so must not be used in place of
* handleWatchStreamStart() and handleWatchStreamFailure().
*/
t.prototype.set = function(t) {
this.lu(), this.iu = 0, "Online" /* OnlineState.Online */ === t && (
// We've connected to watch at least once. Don't warn the developer
// about being offline going forward.
this.ou = !1), this.cu(t);
}, t.prototype.cu = function(t) {
t !== this.state && (this.state = t, this.onlineStateHandler(t));
}, t.prototype.au = function(t) {
var e = "Could not reach Cloud Firestore backend. ".concat(t, "\nThis typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.");
this.ou ? (M(e), this.ou = !1) : V("OnlineStateTracker", e);
}, t.prototype.lu = function() {
null !== this.ru && (this.ru.cancel(), this.ru = null);
}, t;
}(), Sa = function(
/**
* The local store, used to fill the write pipeline with outbound mutations.
*/
t,
/** The client-side proxy for interacting with the backend. */
r, i, o, u) {
var a = this;
this.localStore = t, this.datastore = r, this.asyncQueue = i, this.remoteSyncer = {},
/**
* A list of up to MAX_PENDING_WRITES writes that we have fetched from the
* LocalStore via fillWritePipeline() and have or will send to the write
* stream.
*
* Whenever writePipeline.length > 0 the RemoteStore will attempt to start or
* restart the write stream. When the stream is established the writes in the
* pipeline will be sent in order.
*
* Writes remain in writePipeline until they are acknowledged by the backend
* and thus will automatically be re-sent if the stream is interrupted /
* restarted before they're acknowledged.
*
* Write responses from the backend are linked to their originating request
* purely based on order, and so we can just shift() writes from the front of
* the writePipeline as we receive responses.
*/
this.fu = [],
/**
* A mapping of watched targets that the client cares about tracking and the
* user has explicitly called a 'listen' for this target.
*
* These targets may or may not have been sent to or acknowledged by the
* server. On re-establishing the listen stream, these targets should be sent
* to the server. The targets removed with unlistens are removed eagerly
* without waiting for confirmation from the listen stream.
*/
this.du = new Map,
/**
* A set of reasons for why the RemoteStore may be offline. If empty, the
* RemoteStore may start its network connections.
*/
this._u = new Set,
/**
* Event handlers that get called when the network is disabled or enabled.
*
* PORTING NOTE: These functions are used on the Web client to create the
* underlying streams (to support tree-shakeable streams). On Android and iOS,
* the streams are created during construction of RemoteStore.
*/
this.wu = [], this.mu = u, this.mu.Ur((function(t) {
i.enqueueAndForget((function() {
return e(a, void 0, void 0, (function() {
return n(this, (function(t) {
switch (t.label) {
case 0:
return Oa(this) ? (V("RemoteStore", "Restarting streams for network reachability change."),
[ 4 /*yield*/ , function(t) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return (e = G(t))._u.add(4 /* OfflineCause.ConnectivityChange */), [ 4 /*yield*/ , Da(e) ];
case 1:
return n.sent(), e.gu.set("Unknown" /* OnlineState.Unknown */), e._u.delete(4 /* OfflineCause.ConnectivityChange */),
[ 4 /*yield*/ , _a(e) ];
case 2:
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}(this) ]) : [ 3 /*break*/ , 2 ];
case 1:
t.sent(), t.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
}));
})), this.gu = new Ea(i, o);
};
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ function _a(t) {
return e(this, void 0, void 0, (function() {
var e, r;
return n(this, (function(n) {
switch (n.label) {
case 0:
if (!Oa(t)) return [ 3 /*break*/ , 4 ];
e = 0, r = t.wu, n.label = 1;
case 1:
return e < r.length ? [ 4 /*yield*/ , (0, r[e])(/* enabled= */ !0) ] : [ 3 /*break*/ , 4 ];
case 2:
n.sent(), n.label = 3;
case 3:
return e++, [ 3 /*break*/ , 1 ];
case 4:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Temporarily disables the network. The network can be re-enabled using
* enableNetwork().
*/ function Da(t) {
return e(this, void 0, void 0, (function() {
var e, r;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = 0, r = t.wu, n.label = 1;
case 1:
return e < r.length ? [ 4 /*yield*/ , (0, r[e])(/* enabled= */ !1) ] : [ 3 /*break*/ , 4 ];
case 2:
n.sent(), n.label = 3;
case 3:
return e++, [ 3 /*break*/ , 1 ];
case 4:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Starts new listen for the given target. Uses resume token if provided. It
* is a no-op if the target of given `TargetData` is already being listened to.
*/ function xa(t, e) {
var n = G(t);
n.du.has(e.targetId) || (
// Mark this as something the client is currently listening for.
n.du.set(e.targetId, e), Fa(n) ?
// The listen will be sent in onWatchStreamOpen
ka(n) : Ya(n).ko() && Ca(n, e));
}
/**
* Removes the listen from server. It is a no-op if the given target id is
* not being listened to.
*/ function Aa(t, e) {
var n = G(t), r = Ya(n);
n.du.delete(e), r.ko() && Na(n, e), 0 === n.du.size && (r.ko() ? r.Fo() : Oa(n) &&
// Revert to OnlineState.Unknown if the watch stream is not open and we
// have no listeners, since without any listens to send we cannot
// confirm if the stream is healthy and upgrade to OnlineState.Online.
n.gu.set("Unknown" /* OnlineState.Unknown */));
}
/**
* We need to increment the the expected number of pending responses we're due
* from watch so we wait for the ack to process any messages from this target.
*/ function Ca(t, e) {
t.yu.Ot(e.targetId), Ya(t).zo(e)
/**
* We need to increment the expected number of pending responses we're due
* from watch so we wait for the removal on the server before we process any
* messages from this target.
*/;
}
function Na(t, e) {
t.yu.Ot(e), Ya(t).Ho(e);
}
function ka(t) {
t.yu = new Rr({
getRemoteKeysForTarget: function(e) {
return t.remoteSyncer.getRemoteKeysForTarget(e);
},
ne: function(e) {
return t.du.get(e) || null;
}
}), Ya(t).start(), t.gu.uu()
/**
* Returns whether the watch stream should be started because it's necessary
* and has not yet been started.
*/;
}
function Fa(t) {
return Oa(t) && !Ya(t).No() && t.du.size > 0;
}
function Oa(t) {
return 0 === G(t)._u.size;
}
function Ra(t) {
t.yu = void 0;
}
function Va(t) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
return t.du.forEach((function(e, n) {
Ca(t, e);
})), [ 2 /*return*/ ];
}));
}));
}
function Ma(t, r) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
return Ra(t),
// If we still need the watch stream, retry the connection.
Fa(t) ? (t.gu.hu(r), ka(t)) :
// No need to restart watch stream because there are no active targets.
// The online state is set to unknown because there is no active attempt
// at establishing a connection
t.gu.set("Unknown" /* OnlineState.Unknown */), [ 2 /*return*/ ];
}));
}));
}
function La(t, r, i) {
return e(this, void 0, void 0, (function() {
var o, u, a;
return n(this, (function(s) {
switch (s.label) {
case 0:
if (t.gu.set("Online" /* OnlineState.Online */), !(r instanceof Fr && 2 /* WatchTargetChangeState.Removed */ === r.state && r.cause))
// Mark the client as online since we got a message from the server
return [ 3 /*break*/ , 6 ];
s.label = 1;
case 1:
return s.trys.push([ 1, 3, , 5 ]), [ 4 /*yield*/ ,
/** Handles an error on a target */
function(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = r.cause, i = 0, o = r.targetIds, n.label = 1;
case 1:
return i < o.length ? (u = o[i], t.du.has(u) ? [ 4 /*yield*/ , t.remoteSyncer.rejectListen(u, e) ] : [ 3 /*break*/ , 3 ]) : [ 3 /*break*/ , 5 ];
case 2:
n.sent(), t.du.delete(u), t.yu.removeTarget(u), n.label = 3;
case 3:
n.label = 4;
case 4:
return i++, [ 3 /*break*/ , 1 ];
case 5:
return [ 2 /*return*/ ];
}
}));
}));
}(t, r) ];
case 2:
return s.sent(), [ 3 /*break*/ , 5 ];
case 3:
return o = s.sent(), V("RemoteStore", "Failed to remove targets %s: %s ", r.targetIds.join(","), o),
[ 4 /*yield*/ , Pa(t, o) ];
case 4:
return s.sent(), [ 3 /*break*/ , 5 ];
case 5:
return [ 3 /*break*/ , 13 ];
case 6:
if (r instanceof Nr ? t.yu.Kt(r) : r instanceof kr ? t.yu.Jt(r) : t.yu.jt(r), i.isEqual(at.min())) return [ 3 /*break*/ , 13 ];
s.label = 7;
case 7:
return s.trys.push([ 7, 11, , 13 ]), [ 4 /*yield*/ , Gu(t.localStore) ];
case 8:
return u = s.sent(), i.compareTo(u) >= 0 ? [ 4 /*yield*/ ,
/**
* Takes a batch of changes from the Datastore, repackages them as a
* RemoteEvent, and passes that on to the listener, which is typically the
* SyncEngine.
*/
function(t, e) {
var n = t.yu.Zt(e);
// Update in-memory resume tokens. LocalStore will update the
// persistent view of these when applying the completed RemoteEvent.
return n.targetChanges.forEach((function(n, r) {
if (n.resumeToken.approximateByteSize() > 0) {
var i = t.du.get(r);
// A watched target might have been removed already.
i && t.du.set(r, i.withResumeToken(n.resumeToken, e));
}
})),
// Re-establish listens for the targets that have been invalidated by
// existence filter mismatches.
n.targetMismatches.forEach((function(e) {
var n = t.du.get(e);
if (n) {
// Clear the resume token for the target, since we're in a known mismatch
// state.
t.du.set(e, n.withResumeToken(Yt.EMPTY_BYTE_STRING, n.snapshotVersion)),
// Cause a hard reset by unwatching and rewatching immediately, but
// deliberately don't send a resume token so that we get a full update.
Na(t, e);
// Mark the target we send as being on behalf of an existence filter
// mismatch, but don't actually retain that in listenTargets. This ensures
// that we flag the first re-listen this way without impacting future
// listens of this target (that might happen e.g. on reconnect).
var r = new Wi(n.target, e, 1 /* TargetPurpose.ExistenceFilterMismatch */ , n.sequenceNumber);
Ca(t, r);
}
})), t.remoteSyncer.applyRemoteEvent(n);
}(t, i) ] : [ 3 /*break*/ , 10 ];
// We have received a target change with a global snapshot if the snapshot
// version is not equal to SnapshotVersion.min().
case 9:
// We have received a target change with a global snapshot if the snapshot
// version is not equal to SnapshotVersion.min().
s.sent(), s.label = 10;
case 10:
return [ 3 /*break*/ , 13 ];
case 11:
return V("RemoteStore", "Failed to raise snapshot:", a = s.sent()), [ 4 /*yield*/ , Pa(t, a) ];
case 12:
return s.sent(), [ 3 /*break*/ , 13 ];
case 13:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Recovery logic for IndexedDB errors that takes the network offline until
* `op` succeeds. Retries are scheduled with backoff using
* `enqueueRetryable()`. If `op()` is not provided, IndexedDB access is
* validated via a generic operation.
*
* The returned Promise is resolved once the network is disabled and before
* any retry attempt.
*/ function Pa(t, r, i) {
return e(this, void 0, void 0, (function() {
var o = this;
return n(this, (function(u) {
switch (u.label) {
case 0:
if (!Ft(r)) throw r;
// Disable network and raise offline snapshots
return t._u.add(1 /* OfflineCause.IndexedDbFailed */), [ 4 /*yield*/ , Da(t) ];
case 1:
// Disable network and raise offline snapshots
return u.sent(), t.gu.set("Offline" /* OnlineState.Offline */), i || (
// Use a simple read operation to determine if IndexedDB recovered.
// Ideally, we would expose a health check directly on SimpleDb, but
// RemoteStore only has access to persistence through LocalStore.
i = function() {
return Gu(t.localStore);
}),
// Probe IndexedDB periodically and re-enable network
t.asyncQueue.enqueueRetryable((function() {
return e(o, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return V("RemoteStore", "Retrying IndexedDB access"), [ 4 /*yield*/ , i() ];
case 1:
return e.sent(), t._u.delete(1 /* OfflineCause.IndexedDbFailed */), [ 4 /*yield*/ , _a(t) ];
case 2:
return e.sent(), [ 2 /*return*/ ];
}
}));
}));
})), [ 2 /*return*/ ];
}
}));
}));
}
/**
* Executes `op`. If `op` fails, takes the network offline until `op`
* succeeds. Returns after the first attempt.
*/ function qa(t, e) {
return e().catch((function(n) {
return Pa(t, n, e);
}));
}
function Ua(t) {
return e(this, void 0, void 0, (function() {
var e, r, i, o, u;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), r = Xa(e), i = e.fu.length > 0 ? e.fu[e.fu.length - 1].batchId : -1, n.label = 1;
case 1:
if (!
/**
* Returns true if we can add to the write pipeline (i.e. the network is
* enabled and the write pipeline is not full).
*/
function(t) {
return Oa(t) && t.fu.length < 10;
}
/**
* Queues additional writes to be sent to the write stream, sending them
* immediately if the write stream is established.
*/ (e)) return [ 3 /*break*/ , 7 ];
n.label = 2;
case 2:
return n.trys.push([ 2, 4, , 6 ]), [ 4 /*yield*/ , Qu(e.localStore, i) ];
case 3:
return null === (o = n.sent()) ? (0 === e.fu.length && r.Fo(), [ 3 /*break*/ , 7 ]) : (i = o.batchId,
function(t, e) {
t.fu.push(e);
var n = Xa(t);
n.ko() && n.Yo && n.Xo(e.mutations);
}(e, o), [ 3 /*break*/ , 6 ]);
case 4:
return u = n.sent(), [ 4 /*yield*/ , Pa(e, u) ];
case 5:
return n.sent(), [ 3 /*break*/ , 6 ];
case 6:
return [ 3 /*break*/ , 1 ];
case 7:
return Ba(e) && Ga(e), [ 2 /*return*/ ];
}
}));
}));
}
function Ba(t) {
return Oa(t) && !Xa(t).No() && t.fu.length > 0;
}
function Ga(t) {
Xa(t).start();
}
function Ka(t) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
return Xa(t).eu(), [ 2 /*return*/ ];
}));
}));
}
function ja(t) {
return e(this, void 0, void 0, (function() {
var e, r, i, o;
return n(this, (function(n) {
// Send the write pipeline now that the stream is established.
for (e = Xa(t), r = 0, i = t.fu; r < i.length; r++) o = i[r], e.Xo(o.mutations);
return [ 2 /*return*/ ];
}));
}));
}
function Qa(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = t.fu.shift(), o = Qi.from(e, r, i), [ 4 /*yield*/ , qa(t, (function() {
return t.remoteSyncer.applySuccessfulWrite(o);
})) ];
case 1:
// It's possible that with the completion of this mutation another
// slot has freed up.
return n.sent(), [ 4 /*yield*/ , Ua(t) ];
case 2:
// It's possible that with the completion of this mutation another
// slot has freed up.
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}
function za(t, r) {
return e(this, void 0, void 0, (function() {
return n(this, (function(i) {
switch (i.label) {
case 0:
return r && Xa(t).Yo ? [ 4 /*yield*/ , function(t, r) {
return e(this, void 0, void 0, (function() {
var e, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
return fr(i = r.code) && i !== K.ABORTED ? (e = t.fu.shift(),
// In this case it's also unlikely that the server itself is melting
// down -- this was just a bad request so inhibit backoff on the next
// restart.
Xa(t).Mo(), [ 4 /*yield*/ , qa(t, (function() {
return t.remoteSyncer.rejectFailedWrite(e.batchId, r);
})) ]) : [ 3 /*break*/ , 3 ];
case 1:
// It's possible that with the completion of this mutation
// another slot has freed up.
return n.sent(), [ 4 /*yield*/ , Ua(t) ];
case 2:
// In this case it's also unlikely that the server itself is melting
// down -- this was just a bad request so inhibit backoff on the next
// restart.
// It's possible that with the completion of this mutation
// another slot has freed up.
n.sent(), n.label = 3;
case 3:
return [ 2 /*return*/ ];
}
}));
}));
}(t, r) ] : [ 3 /*break*/ , 2 ];
// This error affects the actual write.
case 1:
// This error affects the actual write.
i.sent(), i.label = 2;
case 2:
// If the write stream closed after the write handshake completes, a write
// operation failed and we fail the pending operation.
// The write stream might have been started by refilling the write
// pipeline for failed writes
return Ba(t) && Ga(t), [ 2 /*return*/ ];
}
}));
}));
}
function Wa(t, r) {
return e(this, void 0, void 0, (function() {
var e, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
return (e = G(t)).asyncQueue.verifyOperationInProgress(), V("RemoteStore", "RemoteStore received new credentials"),
i = Oa(e),
// Tear down and re-create our network streams. This will ensure we get a
// fresh auth token for the new user and re-fill the write pipeline with
// new mutations from the LocalStore (since mutations are per-user).
e._u.add(3 /* OfflineCause.CredentialChange */), [ 4 /*yield*/ , Da(e) ];
case 1:
return n.sent(), i &&
// Don't set the network status to Unknown if we are offline.
e.gu.set("Unknown" /* OnlineState.Unknown */), [ 4 /*yield*/ , e.remoteSyncer.handleCredentialChange(r) ];
case 2:
return n.sent(), e._u.delete(3 /* OfflineCause.CredentialChange */), [ 4 /*yield*/ , _a(e) ];
case 3:
// Tear down and re-create our network streams. This will ensure we get a
// fresh auth token for the new user and re-fill the write pipeline with
// new mutations from the LocalStore (since mutations are per-user).
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}
/**
* Toggles the network state when the client gains or loses its primary lease.
*/ function Ha(t, r) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = G(t), r ? (e._u.delete(2 /* OfflineCause.IsSecondary */), [ 4 /*yield*/ , _a(e) ]) : [ 3 /*break*/ , 2 ];
case 1:
return n.sent(), [ 3 /*break*/ , 5 ];
case 2:
return r ? [ 3 /*break*/ , 4 ] : (e._u.add(2 /* OfflineCause.IsSecondary */), [ 4 /*yield*/ , Da(e) ]);
case 3:
n.sent(), e.gu.set("Unknown" /* OnlineState.Unknown */), n.label = 4;
case 4:
n.label = 5;
case 5:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* If not yet initialized, registers the WatchStream and its network state
* callback with `remoteStoreImpl`. Returns the existing stream if one is
* already available.
*
* PORTING NOTE: On iOS and Android, the WatchStream gets registered on startup.
* This is not done on Web to allow it to be tree-shaken.
*/ function Ya(t) {
var r = this;
return t.pu || (
// Create stream (but note that it is not started yet).
t.pu = function(t, e, n) {
var r = G(t);
return r.su(), new ba(e, r.connection, r.authCredentials, r.appCheckCredentials, r.yt, n);
}(t.datastore, t.asyncQueue, {
Yr: Va.bind(null, t),
Zr: Ma.bind(null, t),
Wo: La.bind(null, t)
}), t.wu.push((function(i) {
return e(r, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return i ? (t.pu.Mo(), Fa(t) ? ka(t) : t.gu.set("Unknown" /* OnlineState.Unknown */),
[ 3 /*break*/ , 3 ]) : [ 3 /*break*/ , 1 ];
case 1:
return [ 4 /*yield*/ , t.pu.stop() ];
case 2:
e.sent(), Ra(t), e.label = 3;
case 3:
return [ 2 /*return*/ ];
}
}));
}));
}))), t.pu
/**
* If not yet initialized, registers the WriteStream and its network state
* callback with `remoteStoreImpl`. Returns the existing stream if one is
* already available.
*
* PORTING NOTE: On iOS and Android, the WriteStream gets registered on startup.
* This is not done on Web to allow it to be tree-shaken.
*/;
}
function Xa(t) {
var r = this;
return t.Iu || (
// Create stream (but note that it is not started yet).
t.Iu = function(t, e, n) {
var r = G(t);
return r.su(), new Ia(e, r.connection, r.authCredentials, r.appCheckCredentials, r.yt, n);
}(t.datastore, t.asyncQueue, {
Yr: Ka.bind(null, t),
Zr: za.bind(null, t),
tu: ja.bind(null, t),
Zo: Qa.bind(null, t)
}), t.wu.push((function(i) {
return e(r, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return i ? (t.Iu.Mo(), [ 4 /*yield*/ , Ua(t) ]) : [ 3 /*break*/ , 2 ];
case 1:
// This will start the write stream if necessary.
return e.sent(), [ 3 /*break*/ , 4 ];
case 2:
return [ 4 /*yield*/ , t.Iu.stop() ];
case 3:
e.sent(), t.fu.length > 0 && (V("RemoteStore", "Stopping write stream with ".concat(t.fu.length, " pending writes")),
t.fu = []), e.label = 4;
case 4:
return [ 2 /*return*/ ];
}
}));
}));
}))), t.Iu
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents an operation scheduled to be run in the future on an AsyncQueue.
*
* It is created via DelayedOperation.createAndSchedule().
*
* Supports cancellation (via cancel()) and early execution (via skipDelay()).
*
* Note: We implement `PromiseLike` instead of `Promise`, as the `Promise` type
* in newer versions of TypeScript defines `finally`, which is not available in
* IE.
*/;
}
var Za = /** @class */ function() {
function t(t, e, n, r, i) {
this.asyncQueue = t, this.timerId = e, this.targetTimeMs = n, this.op = r, this.removalCallback = i,
this.deferred = new Q, this.then = this.deferred.promise.then.bind(this.deferred.promise),
// It's normal for the deferred promise to be canceled (due to cancellation)
// and so we attach a dummy catch callback to avoid
// 'UnhandledPromiseRejectionWarning' log spam.
this.deferred.promise.catch((function(t) {}))
/**
* Creates and returns a DelayedOperation that has been scheduled to be
* executed on the provided asyncQueue after the provided delayMs.
*
* @param asyncQueue - The queue to schedule the operation on.
* @param id - A Timer ID identifying the type of operation this is.
* @param delayMs - The delay (ms) before the operation should be scheduled.
* @param op - The operation to run.
* @param removalCallback - A callback to be called synchronously once the
* operation is executed or canceled, notifying the AsyncQueue to remove it
* from its delayedOperations list.
* PORTING NOTE: This exists to prevent making removeDelayedOperation() and
* the DelayedOperation class public.
*/;
}
return t.createAndSchedule = function(e, n, r, i, o) {
var u = new t(e, n, Date.now() + r, i, o);
return u.start(r), u;
},
/**
* Starts the timer. This is called immediately after construction by
* createAndSchedule().
*/
t.prototype.start = function(t) {
var e = this;
this.timerHandle = setTimeout((function() {
return e.handleDelayElapsed();
}), t);
},
/**
* Queues the operation to run immediately (if it hasn't already been run or
* canceled).
*/
t.prototype.skipDelay = function() {
return this.handleDelayElapsed();
},
/**
* Cancels the operation if it hasn't already been executed or canceled. The
* promise will be rejected.
*
* As long as the operation has not yet been run, calling cancel() provides a
* guarantee that the operation will not be run.
*/
t.prototype.cancel = function(t) {
null !== this.timerHandle && (this.clearTimeout(), this.deferred.reject(new j(K.CANCELLED, "Operation cancelled" + (t ? ": " + t : ""))));
}, t.prototype.handleDelayElapsed = function() {
var t = this;
this.asyncQueue.enqueueAndForget((function() {
return null !== t.timerHandle ? (t.clearTimeout(), t.op().then((function(e) {
return t.deferred.resolve(e);
}))) : Promise.resolve();
}));
}, t.prototype.clearTimeout = function() {
null !== this.timerHandle && (this.removalCallback(this), clearTimeout(this.timerHandle),
this.timerHandle = null);
}, t;
}();
/**
* Returns a FirestoreError that can be surfaced to the user if the provided
* error is an IndexedDbTransactionError. Re-throws the error otherwise.
*/ function Ja(t, e) {
if (M("AsyncQueue", "".concat(e, ": ").concat(t)), Ft(t)) return new j(K.UNAVAILABLE, "".concat(e, ": ").concat(t));
throw t;
}
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* DocumentSet is an immutable (copy-on-write) collection that holds documents
* in order specified by the provided comparator. We always add a document key
* comparator on top of what is provided to guarantee document equality based on
* the key.
*/ var $a = /** @class */ function() {
/** The default ordering is by key if the comparator is omitted */
function t(t) {
// We are adding document key comparator to the end as it's the only
// guaranteed unique property of a document.
this.comparator = t ? function(e, n) {
return t(e, n) || ft.comparator(e.key, n.key);
} : function(t, e) {
return ft.comparator(t.key, e.key);
}, this.keyedMap = gr(), this.sortedSet = new He(this.comparator)
/**
* Returns an empty copy of the existing DocumentSet, using the same
* comparator.
*/;
}
return t.emptySet = function(e) {
return new t(e.comparator);
}, t.prototype.has = function(t) {
return null != this.keyedMap.get(t);
}, t.prototype.get = function(t) {
return this.keyedMap.get(t);
}, t.prototype.first = function() {
return this.sortedSet.minKey();
}, t.prototype.last = function() {
return this.sortedSet.maxKey();
}, t.prototype.isEmpty = function() {
return this.sortedSet.isEmpty();
},
/**
* Returns the index of the provided key in the document set, or -1 if the
* document key is not present in the set;
*/
t.prototype.indexOf = function(t) {
var e = this.keyedMap.get(t);
return e ? this.sortedSet.indexOf(e) : -1;
}, Object.defineProperty(t.prototype, "size", {
get: function() {
return this.sortedSet.size;
},
enumerable: !1,
configurable: !0
}),
/** Iterates documents in order defined by "comparator" */ t.prototype.forEach = function(t) {
this.sortedSet.inorderTraversal((function(e, n) {
return t(e), !1;
}));
},
/** Inserts or updates a document with the same key */ t.prototype.add = function(t) {
// First remove the element if we have it.
var e = this.delete(t.key);
return e.copy(e.keyedMap.insert(t.key, t), e.sortedSet.insert(t, null));
},
/** Deletes a document with a given key */ t.prototype.delete = function(t) {
var e = this.get(t);
return e ? this.copy(this.keyedMap.remove(t), this.sortedSet.remove(e)) : this;
}, t.prototype.isEqual = function(e) {
if (!(e instanceof t)) return !1;
if (this.size !== e.size) return !1;
for (var n = this.sortedSet.getIterator(), r = e.sortedSet.getIterator(); n.hasNext(); ) {
var i = n.getNext().key, o = r.getNext().key;
if (!i.isEqual(o)) return !1;
}
return !0;
}, t.prototype.toString = function() {
var t = [];
return this.forEach((function(e) {
t.push(e.toString());
})), 0 === t.length ? "DocumentSet ()" : "DocumentSet (\n " + t.join(" \n") + "\n)";
}, t.prototype.copy = function(e, n) {
var r = new t;
return r.comparator = this.comparator, r.keyedMap = e, r.sortedSet = n, r;
}, t;
}(), ts = /** @class */ function() {
function t() {
this.Tu = new He(ft.comparator);
}
return t.prototype.track = function(t) {
var e = t.doc.key, n = this.Tu.get(e);
n ?
// Merge the new change with the existing change.
0 /* ChangeType.Added */ !== t.type && 3 /* ChangeType.Metadata */ === n.type ? this.Tu = this.Tu.insert(e, t) : 3 /* ChangeType.Metadata */ === t.type && 1 /* ChangeType.Removed */ !== n.type ? this.Tu = this.Tu.insert(e, {
type: n.type,
doc: t.doc
}) : 2 /* ChangeType.Modified */ === t.type && 2 /* ChangeType.Modified */ === n.type ? this.Tu = this.Tu.insert(e, {
type: 2 /* ChangeType.Modified */ ,
doc: t.doc
}) : 2 /* ChangeType.Modified */ === t.type && 0 /* ChangeType.Added */ === n.type ? this.Tu = this.Tu.insert(e, {
type: 0 /* ChangeType.Added */ ,
doc: t.doc
}) : 1 /* ChangeType.Removed */ === t.type && 0 /* ChangeType.Added */ === n.type ? this.Tu = this.Tu.remove(e) : 1 /* ChangeType.Removed */ === t.type && 2 /* ChangeType.Modified */ === n.type ? this.Tu = this.Tu.insert(e, {
type: 1 /* ChangeType.Removed */ ,
doc: n.doc
}) : 0 /* ChangeType.Added */ === t.type && 1 /* ChangeType.Removed */ === n.type ? this.Tu = this.Tu.insert(e, {
type: 2 /* ChangeType.Modified */ ,
doc: t.doc
}) :
// This includes these cases, which don't make sense:
// Added->Added
// Removed->Removed
// Modified->Added
// Removed->Modified
// Metadata->Added
// Removed->Metadata
q() : this.Tu = this.Tu.insert(e, t);
}, t.prototype.Eu = function() {
var t = [];
return this.Tu.inorderTraversal((function(e, n) {
t.push(n);
})), t;
}, t;
}(), es = /** @class */ function() {
function t(t, e, n, r, i, o, u, a, s) {
this.query = t, this.docs = e, this.oldDocs = n, this.docChanges = r, this.mutatedKeys = i,
this.fromCache = o, this.syncStateChanged = u, this.excludesMetadataChanges = a,
this.hasCachedResults = s
/** Returns a view snapshot as if all documents in the snapshot were added. */;
}
return t.fromInitialDocuments = function(e, n, r, i, o) {
var u = [];
return n.forEach((function(t) {
u.push({
type: 0 /* ChangeType.Added */ ,
doc: t
});
})), new t(e, n, $a.emptySet(n), u, r, i,
/* syncStateChanged= */ !0,
/* excludesMetadataChanges= */ !1, o);
}, Object.defineProperty(t.prototype, "hasPendingWrites", {
get: function() {
return !this.mutatedKeys.isEmpty();
},
enumerable: !1,
configurable: !0
}), t.prototype.isEqual = function(t) {
if (!(this.fromCache === t.fromCache && this.hasCachedResults === t.hasCachedResults && this.syncStateChanged === t.syncStateChanged && this.mutatedKeys.isEqual(t.mutatedKeys) && Sn(this.query, t.query) && this.docs.isEqual(t.docs) && this.oldDocs.isEqual(t.oldDocs))) return !1;
var e = this.docChanges, n = t.docChanges;
if (e.length !== n.length) return !1;
for (var r = 0; r < e.length; r++) if (e[r].type !== n[r].type || !e[r].doc.isEqual(n[r].doc)) return !1;
return !0;
}, t;
}(), ns = function() {
this.Au = void 0, this.listeners = [];
}, rs = function() {
this.queries = new pr((function(t) {
return _n(t);
}), Sn), this.onlineState = "Unknown" /* OnlineState.Unknown */ , this.Ru = new Set;
};
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* DocumentChangeSet keeps track of a set of changes to docs in a query, merging
* duplicate events for the same doc.
*/ function is(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a, s, c;
return n(this, (function(n) {
switch (n.label) {
case 0:
if (e = G(t), i = r.query, o = !1, (u = e.queries.get(i)) || (o = !0, u = new ns),
!o) return [ 3 /*break*/ , 4 ];
n.label = 1;
case 1:
return n.trys.push([ 1, 3, , 4 ]), a = u, [ 4 /*yield*/ , e.onListen(i) ];
case 2:
return a.Au = n.sent(), [ 3 /*break*/ , 4 ];
case 3:
return s = n.sent(), c = Ja(s, "Initialization of query '".concat(Dn(r.query), "' failed")),
[ 2 /*return*/ , void r.onError(c) ];
case 4:
return e.queries.set(i, u), u.listeners.push(r),
// Run global snapshot listeners if a consistent snapshot has been emitted.
r.bu(e.onlineState), u.Au && r.Pu(u.Au) && ss(e), [ 2 /*return*/ ];
}
}));
}));
}
function os(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a;
return n(this, (function(n) {
return e = G(t), i = r.query, o = !1, (u = e.queries.get(i)) && (a = u.listeners.indexOf(r)) >= 0 && (u.listeners.splice(a, 1),
o = 0 === u.listeners.length), o ? [ 2 /*return*/ , (e.queries.delete(i), e.onUnlisten(i)) ] : [ 2 /*return*/ ];
}));
}));
}
function us(t, e) {
for (var n = G(t), r = !1, i = 0, o = e; i < o.length; i++) {
var u = o[i], a = u.query, s = n.queries.get(a);
if (s) {
for (var c = 0, l = s.listeners; c < l.length; c++) {
l[c].Pu(u) && (r = !0);
}
s.Au = u;
}
}
r && ss(n);
}
function as(t, e, n) {
var r = G(t), i = r.queries.get(e);
if (i) for (var o = 0, u = i.listeners; o < u.length; o++) {
u[o].onError(n);
}
// Remove all listeners. NOTE: We don't need to call syncEngine.unlisten()
// after an error.
r.queries.delete(e);
}
// Call all global snapshot listeners that have been set.
function ss(t) {
t.Ru.forEach((function(t) {
t.next();
}));
}
/**
* QueryListener takes a series of internal view snapshots and determines
* when to raise the event.
*
* It uses an Observer to dispatch events.
*/ var cs = /** @class */ function() {
function t(t, e, n) {
this.query = t, this.vu = e,
/**
* Initial snapshots (e.g. from cache) may not be propagated to the wrapped
* observer. This flag is set to true once we've actually raised an event.
*/
this.Vu = !1, this.Su = null, this.onlineState = "Unknown" /* OnlineState.Unknown */ ,
this.options = n || {}
/**
* Applies the new ViewSnapshot to this listener, raising a user-facing event
* if applicable (depending on what changed, whether the user has opted into
* metadata-only changes, etc.). Returns true if a user-facing event was
* indeed raised.
*/;
}
return t.prototype.Pu = function(t) {
if (!this.options.includeMetadataChanges) {
for (
// Remove the metadata only changes.
var e = [], n = 0, r = t.docChanges; n < r.length; n++) {
var i = r[n];
3 /* ChangeType.Metadata */ !== i.type && e.push(i);
}
t = new es(t.query, t.docs, t.oldDocs, e, t.mutatedKeys, t.fromCache, t.syncStateChanged,
/* excludesMetadataChanges= */ !0, t.hasCachedResults);
}
var o = !1;
return this.Vu ? this.Du(t) && (this.vu.next(t), o = !0) : this.Cu(t, this.onlineState) && (this.xu(t),
o = !0), this.Su = t, o;
}, t.prototype.onError = function(t) {
this.vu.error(t);
},
/** Returns whether a snapshot was raised. */ t.prototype.bu = function(t) {
this.onlineState = t;
var e = !1;
return this.Su && !this.Vu && this.Cu(this.Su, t) && (this.xu(this.Su), e = !0),
e;
}, t.prototype.Cu = function(t, e) {
// Always raise the first event when we're synced
if (!t.fromCache) return !0;
// NOTE: We consider OnlineState.Unknown as online (it should become Offline
// or Online if we wait long enough).
var n = "Offline" /* OnlineState.Offline */ !== e;
// Don't raise the event if we're online, aren't synced yet (checked
// above) and are waiting for a sync.
return (!this.options.Nu || !n) && (!t.docs.isEmpty() || t.hasCachedResults || "Offline" /* OnlineState.Offline */ === e);
// Raise data from cache if we have any documents, have cached results before,
// or we are offline.
}, t.prototype.Du = function(t) {
// We don't need to handle includeDocumentMetadataChanges here because
// the Metadata only changes have already been stripped out if needed.
// At this point the only changes we will see are the ones we should
// propagate.
if (t.docChanges.length > 0) return !0;
var e = this.Su && this.Su.hasPendingWrites !== t.hasPendingWrites;
return !(!t.syncStateChanged && !e) && !0 === this.options.includeMetadataChanges;
// Generally we should have hit one of the cases above, but it's possible
// to get here if there were only metadata docChanges and they got
// stripped out.
}, t.prototype.xu = function(t) {
t = es.fromInitialDocuments(t.query, t.docs, t.mutatedKeys, t.fromCache, t.hasCachedResults),
this.Vu = !0, this.vu.next(t);
}, t;
}(), ls = /** @class */ function() {
function t(t,
// How many bytes this element takes to store in the bundle.
e) {
this.ku = t, this.byteLength = e;
}
return t.prototype.Ou = function() {
return "metadata" in this.ku;
}, t;
}(), hs = /** @class */ function() {
function t(t) {
this.yt = t;
}
return t.prototype.Ji = function(t) {
return Hr(this.yt, t);
},
/**
* Converts a BundleDocument to a MutableDocument.
*/
t.prototype.Yi = function(t) {
return t.metadata.exists ? ti(this.yt, t.document, !1) : rn.newNoDocument(this.Ji(t.metadata.name), this.Xi(t.metadata.readTime));
}, t.prototype.Xi = function(t) {
return jr(t);
}, t;
}(), fs = /** @class */ function() {
function t(t, e, n) {
this.Mu = t, this.localStore = e, this.yt = n,
/** Batched queries to be saved into storage */
this.queries = [],
/** Batched documents to be saved into storage */
this.documents = [],
/** The collection groups affected by this bundle. */
this.collectionGroups = new Set, this.progress = ds(t)
/**
* Adds an element from the bundle to the loader.
*
* Returns a new progress if adding the element leads to a new progress,
* otherwise returns null.
*/;
}
return t.prototype.Fu = function(t) {
this.progress.bytesLoaded += t.byteLength;
var e = this.progress.documentsLoaded;
if (t.ku.namedQuery) this.queries.push(t.ku.namedQuery); else if (t.ku.documentMetadata) {
this.documents.push({
metadata: t.ku.documentMetadata
}), t.ku.documentMetadata.exists || ++e;
var n = ct.fromString(t.ku.documentMetadata.name);
this.collectionGroups.add(n.get(n.length - 2));
} else t.ku.document && (this.documents[this.documents.length - 1].document = t.ku.document,
++e);
return e !== this.progress.documentsLoaded ? (this.progress.documentsLoaded = e,
Object.assign({}, this.progress)) : null;
}, t.prototype.$u = function(t) {
for (var e = new Map, n = new hs(this.yt), r = 0, i = t; r < i.length; r++) {
var o = i[r];
if (o.metadata.queries) for (var u = n.Ji(o.metadata.name), a = 0, s = o.metadata.queries; a < s.length; a++) {
var c = s[a], l = (e.get(c) || _r()).add(u);
e.set(c, l);
}
}
return e;
},
/**
* Update the progress to 'Success' and return the updated progress.
*/
t.prototype.complete = function() {
return e(this, void 0, void 0, (function() {
var t, e, r, i, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , Ju(this.localStore, new hs(this.yt), this.documents, this.Mu.id) ];
case 1:
t = n.sent(), e = this.$u(this.documents), r = 0, i = this.queries, n.label = 2;
case 2:
return r < i.length ? (o = i[r], [ 4 /*yield*/ , $u(this.localStore, o, e.get(o.name)) ]) : [ 3 /*break*/ , 5 ];
case 3:
n.sent(), n.label = 4;
case 4:
return r++, [ 3 /*break*/ , 2 ];
case 5:
return [ 2 /*return*/ , (this.progress.taskState = "Success", {
progress: this.progress,
Bu: this.collectionGroups,
Lu: t
}) ];
}
}));
}));
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A complete element in the bundle stream, together with the byte length it
* occupies in the stream.
*/
/**
* Returns a `LoadBundleTaskProgress` representing the initial progress of
* loading a bundle.
*/
function ds(t) {
return {
taskState: "Running",
documentsLoaded: 0,
bytesLoaded: 0,
totalDocuments: t.totalDocuments,
totalBytes: t.totalBytes
};
}
/**
* Returns a `LoadBundleTaskProgress` representing the progress that the loading
* has succeeded.
*/
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var ps = function(t) {
this.key = t;
}, ys = function(t) {
this.key = t;
}, vs = /** @class */ function() {
function t(t,
/** Documents included in the remote target */
e) {
this.query = t, this.qu = e, this.Uu = null, this.hasCachedResults = !1,
/**
* A flag whether the view is current with the backend. A view is considered
* current after it has seen the current flag from the backend and did not
* lose consistency within the watch stream (e.g. because of an existence
* filter mismatch).
*/
this.current = !1,
/** Documents in the view but not in the remote target */
this.Ku = _r(),
/** Document Keys that have local changes */
this.mutatedKeys = _r(), this.Gu = Cn(t), this.Qu = new $a(this.Gu);
}
return Object.defineProperty(t.prototype, "ju", {
/**
* The set of remote documents that the server has told us belongs to the target associated with
* this view.
*/
get: function() {
return this.qu;
},
enumerable: !1,
configurable: !0
}),
/**
* Iterates over a set of doc changes, applies the query limit, and computes
* what the new results should be, what the changes were, and whether we may
* need to go back to the local cache for more results. Does not make any
* changes to the view.
* @param docChanges - The doc changes to apply to this view.
* @param previousChanges - If this is being called with a refill, then start
* with this set of docs and changes instead of the current view.
* @returns a new set of docs, changes, and refill flag.
*/
t.prototype.Wu = function(t, e) {
var n = this, r = e ? e.zu : new ts, i = e ? e.Qu : this.Qu, o = e ? e.mutatedKeys : this.mutatedKeys, u = i, a = !1, s = "F" /* LimitType.First */ === this.query.limitType && i.size === this.query.limit ? i.last() : null, c = "L" /* LimitType.Last */ === this.query.limitType && i.size === this.query.limit ? i.first() : null;
// Drop documents out to meet limit/limitToLast requirement.
if (t.inorderTraversal((function(t, e) {
var l = i.get(t), h = xn(n.query, e) ? e : null, f = !!l && n.mutatedKeys.has(l.key), d = !!h && (h.hasLocalMutations ||
// We only consider committed mutations for documents that were
// mutated during the lifetime of the view.
n.mutatedKeys.has(h.key) && h.hasCommittedMutations), p = !1;
// Calculate change
l && h ? l.data.isEqual(h.data) ? f !== d && (r.track({
type: 3 /* ChangeType.Metadata */ ,
doc: h
}), p = !0) : n.Hu(l, h) || (r.track({
type: 2 /* ChangeType.Modified */ ,
doc: h
}), p = !0, (s && n.Gu(h, s) > 0 || c && n.Gu(h, c) < 0) && (
// This doc moved from inside the limit to outside the limit.
// That means there may be some other doc in the local cache
// that should be included instead.
a = !0)) : !l && h ? (r.track({
type: 0 /* ChangeType.Added */ ,
doc: h
}), p = !0) : l && !h && (r.track({
type: 1 /* ChangeType.Removed */ ,
doc: l
}), p = !0, (s || c) && (
// A doc was removed from a full limit query. We'll need to
// requery from the local cache to see if we know about some other
// doc that should be in the results.
a = !0)), p && (h ? (u = u.add(h), o = d ? o.add(t) : o.delete(t)) : (u = u.delete(t),
o = o.delete(t)));
})), null !== this.query.limit) for (;u.size > this.query.limit; ) {
var l = "F" /* LimitType.First */ === this.query.limitType ? u.last() : u.first();
u = u.delete(l.key), o = o.delete(l.key), r.track({
type: 1 /* ChangeType.Removed */ ,
doc: l
});
}
return {
Qu: u,
zu: r,
$i: a,
mutatedKeys: o
};
}, t.prototype.Hu = function(t, e) {
// We suppress the initial change event for documents that were modified as
// part of a write acknowledgment (e.g. when the value of a server transform
// is applied) as Watch will send us the same document again.
// By suppressing the event, we only raise two user visible events (one with
// `hasPendingWrites` and the final state of the document) instead of three
// (one with `hasPendingWrites`, the modified document with
// `hasPendingWrites` and the final state of the document).
return t.hasLocalMutations && e.hasCommittedMutations && !e.hasLocalMutations;
},
/**
* Updates the view with the given ViewDocumentChanges and optionally updates
* limbo docs and sync state from the provided target change.
* @param docChanges - The set of changes to make to the view's docs.
* @param updateLimboDocuments - Whether to update limbo documents based on
* this change.
* @param targetChange - A target change to apply for computing limbo docs and
* sync state.
* @returns A new ViewChange with the given docs, changes, and sync state.
*/
// PORTING NOTE: The iOS/Android clients always compute limbo document changes.
t.prototype.applyChanges = function(t, e, n) {
var r = this, i = this.Qu;
this.Qu = t.Qu, this.mutatedKeys = t.mutatedKeys;
// Sort changes based on type and query comparator
var o = t.zu.Eu();
o.sort((function(t, e) {
return function(t, e) {
var n = function(t) {
switch (t) {
case 0 /* ChangeType.Added */ :
return 1;
case 2 /* ChangeType.Modified */ :
case 3 /* ChangeType.Metadata */ :
// A metadata change is converted to a modified change at the public
// api layer. Since we sort by document key and then change type,
// metadata and modified changes must be sorted equivalently.
return 2;
case 1 /* ChangeType.Removed */ :
return 0;
default:
return q();
}
};
return n(t) - n(e);
}(t.type, e.type) || r.Gu(t.doc, e.doc);
})), this.Ju(n);
var u = e ? this.Yu() : [], a = 0 === this.Ku.size && this.current ? 1 /* SyncState.Synced */ : 0 /* SyncState.Local */ , s = a !== this.Uu;
return this.Uu = a, 0 !== o.length || s ? {
snapshot: new es(this.query, t.Qu, i, o, t.mutatedKeys, 0 /* SyncState.Local */ === a, s,
/* excludesMetadataChanges= */ !1, !!n && n.resumeToken.approximateByteSize() > 0),
Xu: u
} : {
Xu: u
};
// no changes
},
/**
* Applies an OnlineState change to the view, potentially generating a
* ViewChange if the view's syncState changes as a result.
*/
t.prototype.bu = function(t) {
return this.current && "Offline" /* OnlineState.Offline */ === t ? (
// If we're offline, set `current` to false and then call applyChanges()
// to refresh our syncState and generate a ViewChange as appropriate. We
// are guaranteed to get a new TargetChange that sets `current` back to
// true once the client is back online.
this.current = !1, this.applyChanges({
Qu: this.Qu,
zu: new ts,
mutatedKeys: this.mutatedKeys,
$i: !1
},
/* updateLimboDocuments= */ !1)) : {
Xu: []
};
},
/**
* Returns whether the doc for the given key should be in limbo.
*/
t.prototype.Zu = function(t) {
// If the remote end says it's part of this query, it's not in limbo.
return !this.qu.has(t) &&
// The local store doesn't think it's a result, so it shouldn't be in limbo.
!!this.Qu.has(t) && !this.Qu.get(t).hasLocalMutations;
},
/**
* Updates syncedDocuments, current, and limbo docs based on the given change.
* Returns the list of changes to which docs are in limbo.
*/
t.prototype.Ju = function(t) {
var e = this;
t && (t.addedDocuments.forEach((function(t) {
return e.qu = e.qu.add(t);
})), t.modifiedDocuments.forEach((function(t) {})), t.removedDocuments.forEach((function(t) {
return e.qu = e.qu.delete(t);
})), this.current = t.current);
}, t.prototype.Yu = function() {
var t = this;
// We can only determine limbo documents when we're in-sync with the server.
if (!this.current) return [];
// TODO(klimt): Do this incrementally so that it's not quadratic when
// updating many documents.
var e = this.Ku;
this.Ku = _r(), this.Qu.forEach((function(e) {
t.Zu(e.key) && (t.Ku = t.Ku.add(e.key));
}));
// Diff the new limbo docs with the old limbo docs.
var n = [];
return e.forEach((function(e) {
t.Ku.has(e) || n.push(new ys(e));
})), this.Ku.forEach((function(t) {
e.has(t) || n.push(new ps(t));
})), n;
},
/**
* Update the in-memory state of the current view with the state read from
* persistence.
*
* We update the query view whenever a client's primary status changes:
* - When a client transitions from primary to secondary, it can miss
* LocalStorage updates and its query views may temporarily not be
* synchronized with the state on disk.
* - For secondary to primary transitions, the client needs to update the list
* of `syncedDocuments` since secondary clients update their query views
* based purely on synthesized RemoteEvents.
*
* @param queryResult.documents - The documents that match the query according
* to the LocalStore.
* @param queryResult.remoteKeys - The keys of the documents that match the
* query according to the backend.
*
* @returns The ViewChange that resulted from this synchronization.
*/
// PORTING NOTE: Multi-tab only.
t.prototype.tc = function(t) {
this.qu = t.Hi, this.Ku = _r();
var e = this.Wu(t.documents);
return this.applyChanges(e, /*updateLimboDocuments=*/ !0);
},
/**
* Returns a view snapshot as if this query was just listened to. Contains
* a document add for every existing document and the `fromCache` and
* `hasPendingWrites` status of the already established view.
*/
// PORTING NOTE: Multi-tab only.
t.prototype.ec = function() {
return es.fromInitialDocuments(this.query, this.Qu, this.mutatedKeys, 0 /* SyncState.Local */ === this.Uu, this.hasCachedResults);
}, t;
}(), ms = function(
/**
* The query itself.
*/
t,
/**
* The target number created by the client that is used in the watch
* stream to identify this query.
*/
e,
/**
* The view is responsible for computing the final merged truth of what
* docs are in the query. It gets notified of local and remote changes,
* and applies the query filters and limits to determine the most correct
* possible results.
*/
n) {
this.query = t, this.targetId = e, this.view = n;
}, gs = function(t) {
this.key = t,
/**
* Set to true once we've received a document. This is used in
* getRemoteKeysForTarget() and ultimately used by WatchChangeAggregator to
* decide whether it needs to manufacture a delete event for the target once
* the target is CURRENT.
*/
this.nc = !1;
}, ws = /** @class */ function() {
function t(t, e, n,
// PORTING NOTE: Manages state synchronization in multi-tab environments.
r, i, o) {
this.localStore = t, this.remoteStore = e, this.eventManager = n, this.sharedClientState = r,
this.currentUser = i, this.maxConcurrentLimboResolutions = o, this.sc = {}, this.ic = new pr((function(t) {
return _n(t);
}), Sn), this.rc = new Map,
/**
* The keys of documents that are in limbo for which we haven't yet started a
* limbo resolution query. The strings in this set are the result of calling
* `key.path.canonicalString()` where `key` is a `DocumentKey` object.
*
* The `Set` type was chosen because it provides efficient lookup and removal
* of arbitrary elements and it also maintains insertion order, providing the
* desired queue-like FIFO semantics.
*/
this.oc = new Set,
/**
* Keeps track of the target ID for each document that is in limbo with an
* active target.
*/
this.uc = new He(ft.comparator),
/**
* Keeps track of the information about an active limbo resolution for each
* active target ID that was started for the purpose of limbo resolution.
*/
this.cc = new Map, this.ac = new bu,
/** Stores user completion handlers, indexed by User and BatchId. */
this.hc = {},
/** Stores user callbacks waiting for all pending writes to be acknowledged. */
this.lc = new Map, this.fc = Xo.vn(), this.onlineState = "Unknown" /* OnlineState.Unknown */ ,
// The primary state is set to `true` or `false` immediately after Firestore
// startup. In the interim, a client should only be considered primary if
// `isPrimary` is true.
this.dc = void 0;
}
return Object.defineProperty(t.prototype, "isPrimaryClient", {
get: function() {
return !0 === this.dc;
},
enumerable: !1,
configurable: !0
}), t;
}();
/**
* Initiates the new listen, resolves promise when listen enqueued to the
* server. All the subsequent view snapshots or errors are sent to the
* subscribed handlers. Returns the initial snapshot.
*/
function bs(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a, s;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = Ys(t), (u = e.ic.get(r)) ? (
// PORTING NOTE: With Multi-Tab Web, it is possible that a query view
// already exists when EventManager calls us for the first time. This
// happens when the primary tab is already listening to this query on
// behalf of another tab and the user of the primary also starts listening
// to the query. EventManager will not have an assigned target ID in this
// case and calls `listen` to obtain this ID.
i = u.targetId, e.sharedClientState.addLocalQueryTarget(i), o = u.view.ec(), [ 3 /*break*/ , 4 ]) : [ 3 /*break*/ , 1 ];
case 1:
return [ 4 /*yield*/ , zu(e.localStore, In(r)) ];
case 2:
return a = n.sent(), e.isPrimaryClient && xa(e.remoteStore, a), s = e.sharedClientState.addLocalQueryTarget(a.targetId),
i = a.targetId, [ 4 /*yield*/ , Is(e, r, i, "current" === s, a.resumeToken) ];
case 3:
o = n.sent(), n.label = 4;
case 4:
return [ 2 /*return*/ , o ];
}
}));
}));
}
/**
* Registers a view for a previously unknown query and computes its initial
* snapshot.
*/ function Is(t, r, i, o, u) {
return e(this, void 0, void 0, (function() {
var a, s, c, l, h, f;
return n(this, (function(d) {
switch (d.label) {
case 0:
// PORTING NOTE: On Web only, we inject the code that registers new Limbo
// targets based on view changes. This allows us to only depend on Limbo
// changes when user code includes queries.
return t._c = function(r, i, o) {
return function(t, r, i, o) {
return e(this, void 0, void 0, (function() {
var e, u, a;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = r.view.Wu(i), e.$i ? [ 4 /*yield*/ , Hu(t.localStore, r.query,
/* usePreviousResults= */ !1).then((function(t) {
var n = t.documents;
return r.view.Wu(n, e);
})) ] : [ 3 /*break*/ , 2 ];
case 1:
// The query has a limit and some docs were removed, so we need
// to re-run the query against the local store to make sure we
// didn't lose any good docs that had been past the limit.
e = n.sent(), n.label = 2;
case 2:
return u = o && o.targetChanges.get(r.targetId), a = r.view.applyChanges(e,
/* updateLimboDocuments= */ t.isPrimaryClient, u), [ 2 /*return*/ , (Rs(t, r.targetId, a.Xu),
a.snapshot) ];
}
}));
}));
}(t, r, i, o);
}, [ 4 /*yield*/ , Hu(t.localStore, r,
/* usePreviousResults= */ !0) ];
case 1:
return a = d.sent(), s = new vs(r, a.Hi), c = s.Wu(a.documents), l = Cr.createSynthesizedTargetChangeForCurrentChange(i, o && "Offline" /* OnlineState.Offline */ !== t.onlineState, u),
h = s.applyChanges(c,
/* updateLimboDocuments= */ t.isPrimaryClient, l), Rs(t, i, h.Xu), f = new ms(r, i, s),
[ 2 /*return*/ , (t.ic.set(r, f), t.rc.has(i) ? t.rc.get(i).push(r) : t.rc.set(i, [ r ]),
h.snapshot) ];
}
}));
}));
}
/** Stops listening to the query. */ function Ts(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = G(t), i = e.ic.get(r), (o = e.rc.get(i.targetId)).length > 1 ? [ 2 /*return*/ , (e.rc.set(i.targetId, o.filter((function(t) {
return !Sn(t, r);
}))), void e.ic.delete(r)) ] : e.isPrimaryClient ? (
// We need to remove the local query target first to allow us to verify
// whether any other client is still interested in this target.
e.sharedClientState.removeLocalQueryTarget(i.targetId), e.sharedClientState.isActiveQueryTarget(i.targetId) ? [ 3 /*break*/ , 2 ] : [ 4 /*yield*/ , Wu(e.localStore, i.targetId,
/*keepPersistedTargetData=*/ !1).then((function() {
e.sharedClientState.clearQueryState(i.targetId), Aa(e.remoteStore, i.targetId),
Fs(e, i.targetId);
})).catch(Dt) ]) : [ 3 /*break*/ , 3 ];
case 1:
n.sent(), n.label = 2;
case 2:
return [ 3 /*break*/ , 5 ];
case 3:
return Fs(e, i.targetId), [ 4 /*yield*/ , Wu(e.localStore, i.targetId,
/*keepPersistedTargetData=*/ !0) ];
case 4:
n.sent(), n.label = 5;
case 5:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Initiates the write of local mutation batch which involves adding the
* writes to the mutation queue, notifying the remote store about new
* mutations and raising events for any changes this write caused.
*
* The promise returned by this call is resolved when the above steps
* have completed, *not* when the write was acked by the backend. The
* userCallback is resolved once the write was acked/rejected by the
* backend (or failed locally for any other reason).
*/ function Es(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u, a;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = Xs(t), n.label = 1;
case 1:
return n.trys.push([ 1, 5, , 6 ]), [ 4 /*yield*/ , function(t, e) {
var n, r, i = G(t), o = ut.now(), u = e.reduce((function(t, e) {
return t.add(e.key);
}), _r());
return i.persistence.runTransaction("Locally write mutations", "readwrite", (function(t) {
// Figure out which keys do not have a remote version in the cache, this
// is needed to create the right overlay mutation: if no remote version
// presents, we do not need to create overlays as patch mutations.
// TODO(Overlay): Is there a better way to determine this? Using the
// document version does not work because local mutations set them back
// to 0.
var a = vr(), s = _r();
return i.Gi.getEntries(t, u).next((function(t) {
(a = t).forEach((function(t, e) {
e.isValidDocument() || (s = s.add(t));
}));
})).next((function() {
return i.localDocuments.getOverlayedDocuments(t, a);
})).next((function(r) {
n = r;
for (
// For non-idempotent mutations (such as `FieldValue.increment()`),
// we record the base state in a separate patch mutation. This is
// later used to guarantee consistent values and prevents flicker
// even if the backend sends us an update that already includes our
// transform.
var u = [], a = 0, s = e; a < s.length; a++) {
var c = s[a], l = tr(c, n.get(c.key).overlayedDocument);
null != l &&
// NOTE: The base state should only be applied if there's some
// existing document to override, so use a Precondition of
// exists=true
u.push(new rr(c.key, l, nn(l.value.mapValue), Hn.exists(!0)));
}
return i.mutationQueue.addMutationBatch(t, o, u, e);
})).next((function(e) {
r = e;
var o = e.applyToLocalDocumentSet(n, s);
return i.documentOverlayCache.saveOverlays(t, e.batchId, o);
}));
})).then((function() {
return {
batchId: r.batchId,
changes: wr(n)
};
}));
}(e.localStore, r) ];
case 2:
return o = n.sent(), e.sharedClientState.addPendingMutation(o.batchId), function(t, e, n) {
var r = t.hc[t.currentUser.toKey()];
r || (r = new He(rt)), r = r.insert(e, n), t.hc[t.currentUser.toKey()] = r;
}(e, o.batchId, i), [ 4 /*yield*/ , Ls(e, o.changes) ];
case 3:
return n.sent(), [ 4 /*yield*/ , Ua(e.remoteStore) ];
case 4:
return n.sent(), [ 3 /*break*/ , 6 ];
case 5:
return u = n.sent(), a = Ja(u, "Failed to persist write"), i.reject(a), [ 3 /*break*/ , 6 ];
case 6:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Applies one remote event to the sync engine, notifying any views of the
* changes, and releasing any pending mutation batches that would become
* visible because of the snapshot version the remote event contains.
*/ function Ss(t, r) {
return e(this, void 0, void 0, (function() {
var e, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), n.label = 1;
case 1:
return n.trys.push([ 1, 4, , 6 ]), [ 4 /*yield*/ , Ku(e.localStore, r) ];
case 2:
return i = n.sent(),
// Update `receivedDocument` as appropriate for any limbo targets.
r.targetChanges.forEach((function(t, n) {
var r = e.cc.get(n);
r && (
// Since this is a limbo resolution lookup, it's for a single document
// and it could be added, modified, or removed, but not a combination.
U(t.addedDocuments.size + t.modifiedDocuments.size + t.removedDocuments.size <= 1),
t.addedDocuments.size > 0 ? r.nc = !0 : t.modifiedDocuments.size > 0 ? U(r.nc) : t.removedDocuments.size > 0 && (U(r.nc),
r.nc = !1));
})), [ 4 /*yield*/ , Ls(e, i, r) ];
case 3:
// Update `receivedDocument` as appropriate for any limbo targets.
return n.sent(), [ 3 /*break*/ , 6 ];
case 4:
return [ 4 /*yield*/ , Dt(n.sent()) ];
case 5:
return n.sent(), [ 3 /*break*/ , 6 ];
case 6:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Applies an OnlineState change to the sync engine and notifies any views of
* the change.
*/ function _s(t, e, n) {
var r = G(t);
// If we are the secondary client, we explicitly ignore the remote store's
// online state (the local client may go offline, even though the primary
// tab remains online) and only apply the primary tab's online state from
// SharedClientState.
if (r.isPrimaryClient && 0 /* OnlineStateSource.RemoteStore */ === n || !r.isPrimaryClient && 1 /* OnlineStateSource.SharedClientState */ === n) {
var i = [];
r.ic.forEach((function(t, n) {
var r = n.view.bu(e);
r.snapshot && i.push(r.snapshot);
})), function(t, e) {
var n = G(t);
n.onlineState = e;
var r = !1;
n.queries.forEach((function(t, n) {
for (var i = 0, o = n.listeners; i < o.length; i++) {
// Run global snapshot listeners if a consistent snapshot has been emitted.
o[i].bu(e) && (r = !0);
}
})), r && ss(n);
}(r.eventManager, e), i.length && r.sc.Wo(i), r.onlineState = e, r.isPrimaryClient && r.sharedClientState.setOnlineState(e);
}
}
/**
* Rejects the listen for the given targetID. This can be triggered by the
* backend for any active target.
*
* @param syncEngine - The sync engine implementation.
* @param targetId - The targetID corresponds to one previously initiated by the
* user as part of TargetData passed to listen() on RemoteStore.
* @param err - A description of the condition that has forced the rejection.
* Nearly always this will be an indication that the user is no longer
* authorized to see the data matching the target.
*/ function Ds(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u, a, s, c;
return n(this, (function(n) {
switch (n.label) {
case 0:
// PORTING NOTE: Multi-tab only.
return (e = G(t)).sharedClientState.updateQueryState(r, "rejected", i), o = e.cc.get(r),
(u = o && o.key) ? (
// TODO(b/217189216): This limbo document should ideally have a read time,
// so that it is picked up by any read-time based scans. The backend,
// however, does not send a read time for target removals.
a = (a = new He(ft.comparator)).insert(u, rn.newNoDocument(u, at.min())), s = _r().add(u),
c = new Ar(at.min(),
/* targetChanges= */ new Map,
/* targetMismatches= */ new Ze(rt), a, s), [ 4 /*yield*/ , Ss(e, c) ]) : [ 3 /*break*/ , 2 ];
case 1:
return n.sent(),
// Since this query failed, we won't want to manually unlisten to it.
// We only remove it from bookkeeping after we successfully applied the
// RemoteEvent. If `applyRemoteEvent()` throws, we want to re-listen to
// this query when the RemoteStore restarts the Watch stream, which should
// re-trigger the target failure.
e.uc = e.uc.remove(u), e.cc.delete(r), Ms(e), [ 3 /*break*/ , 4 ];
case 2:
return [ 4 /*yield*/ , Wu(e.localStore, r,
/* keepPersistedTargetData */ !1).then((function() {
return Fs(e, r, i);
})).catch(Dt) ];
case 3:
n.sent(), n.label = 4;
case 4:
return [ 2 /*return*/ ];
}
}));
}));
}
function xs(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), i = r.batch.batchId, n.label = 1;
case 1:
return n.trys.push([ 1, 4, , 6 ]), [ 4 /*yield*/ , Bu(e.localStore, r) ];
case 2:
return o = n.sent(),
// The local store may or may not be able to apply the write result and
// raise events immediately (depending on whether the watcher is caught
// up), so we raise user callbacks first so that they consistently happen
// before listen events.
ks(e, i, /*error=*/ null), Ns(e, i), e.sharedClientState.updateMutationState(i, "acknowledged"),
[ 4 /*yield*/ , Ls(e, o) ];
case 3:
// The local store may or may not be able to apply the write result and
// raise events immediately (depending on whether the watcher is caught
// up), so we raise user callbacks first so that they consistently happen
// before listen events.
return n.sent(), [ 3 /*break*/ , 6 ];
case 4:
return [ 4 /*yield*/ , Dt(n.sent()) ];
case 5:
return n.sent(), [ 3 /*break*/ , 6 ];
case 6:
return [ 2 /*return*/ ];
}
}));
}));
}
function As(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), n.label = 1;
case 1:
return n.trys.push([ 1, 4, , 6 ]), [ 4 /*yield*/ , function(t, e) {
var n = G(t);
return n.persistence.runTransaction("Reject batch", "readwrite-primary", (function(t) {
var r;
return n.mutationQueue.lookupMutationBatch(t, e).next((function(e) {
return U(null !== e), r = e.keys(), n.mutationQueue.removeMutationBatch(t, e);
})).next((function() {
return n.mutationQueue.performConsistencyCheck(t);
})).next((function() {
return n.documentOverlayCache.removeOverlaysForBatchId(t, r, e);
})).next((function() {
return n.localDocuments.recalculateAndSaveOverlaysForDocumentKeys(t, r);
})).next((function() {
return n.localDocuments.getDocuments(t, r);
}));
}));
}(e.localStore, r) ];
case 2:
return o = n.sent(),
// The local store may or may not be able to apply the write result and
// raise events immediately (depending on whether the watcher is caught up),
// so we raise user callbacks first so that they consistently happen before
// listen events.
ks(e, r, i), Ns(e, r), e.sharedClientState.updateMutationState(r, "rejected", i),
[ 4 /*yield*/ , Ls(e, o) ];
case 3:
// The local store may or may not be able to apply the write result and
// raise events immediately (depending on whether the watcher is caught up),
// so we raise user callbacks first so that they consistently happen before
// listen events.
return n.sent(), [ 3 /*break*/ , 6 ];
case 4:
return [ 4 /*yield*/ , Dt(n.sent()) ];
case 5:
return n.sent(), [ 3 /*break*/ , 6 ];
case 6:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Registers a user callback that resolves when all pending mutations at the moment of calling
* are acknowledged .
*/ function Cs(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a;
return n(this, (function(n) {
switch (n.label) {
case 0:
Oa((e = G(t)).remoteStore) || V("SyncEngine", "The network is disabled. The task returned by 'awaitPendingWrites()' will not complete until the network is enabled."),
n.label = 1;
case 1:
return n.trys.push([ 1, 3, , 4 ]), [ 4 /*yield*/ , function(t) {
var e = G(t);
return e.persistence.runTransaction("Get highest unacknowledged batch id", "readonly", (function(t) {
return e.mutationQueue.getHighestUnacknowledgedBatchId(t);
}));
}(e.localStore) ];
case 2:
return -1 === (i = n.sent()) ? [ 2 /*return*/ , void r.resolve() ] : ((o = e.lc.get(i) || []).push(r),
e.lc.set(i, o), [ 3 /*break*/ , 4 ]);
case 3:
return u = n.sent(), a = Ja(u, "Initialization of waitForPendingWrites() operation failed"),
r.reject(a), [ 3 /*break*/ , 4 ];
case 4:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* Triggers the callbacks that are waiting for this batch id to get acknowledged by server,
* if there are any.
*/ function Ns(t, e) {
(t.lc.get(e) || []).forEach((function(t) {
t.resolve();
})), t.lc.delete(e)
/** Reject all outstanding callbacks waiting for pending writes to complete. */;
}
function ks(t, e, n) {
var r = G(t), i = r.hc[r.currentUser.toKey()];
// NOTE: Mutations restored from persistence won't have callbacks, so it's
// okay for there to be no callback for this ID.
if (i) {
var o = i.get(e);
o && (n ? o.reject(n) : o.resolve(), i = i.remove(e)), r.hc[r.currentUser.toKey()] = i;
}
}
function Fs(t, e, n) {
void 0 === n && (n = null), t.sharedClientState.removeLocalQueryTarget(e);
for (var r = 0, i = t.rc.get(e); r < i.length; r++) {
var o = i[r];
t.ic.delete(o), n && t.sc.wc(o, n);
}
t.rc.delete(e), t.isPrimaryClient && t.ac.ls(e).forEach((function(e) {
t.ac.containsKey(e) ||
// We removed the last reference for this key
Os(t, e);
}));
}
function Os(t, e) {
t.oc.delete(e.path.canonicalString());
// It's possible that the target already got removed because the query failed. In that case,
// the key won't exist in `limboTargetsByKey`. Only do the cleanup if we still have the target.
var n = t.uc.get(e);
null !== n && (Aa(t.remoteStore, n), t.uc = t.uc.remove(e), t.cc.delete(n), Ms(t));
}
function Rs(t, e, n) {
for (var r = 0, i = n; r < i.length; r++) {
var o = i[r];
o instanceof ps ? (t.ac.addReference(o.key, e), Vs(t, o)) : o instanceof ys ? (V("SyncEngine", "Document no longer in limbo: " + o.key),
t.ac.removeReference(o.key, e), t.ac.containsKey(o.key) ||
// We removed the last reference for this key
Os(t, o.key)) : q();
}
}
function Vs(t, e) {
var n = e.key, r = n.path.canonicalString();
t.uc.get(n) || t.oc.has(r) || (V("SyncEngine", "New document in limbo: " + n), t.oc.add(r),
Ms(t));
}
/**
* Starts listens for documents in limbo that are enqueued for resolution,
* subject to a maximum number of concurrent resolutions.
*
* Without bounding the number of concurrent resolutions, the server can fail
* with "resource exhausted" errors which can lead to pathological client
* behavior as seen in https://github.com/firebase/firebase-js-sdk/issues/2683.
*/ function Ms(t) {
for (;t.oc.size > 0 && t.uc.size < t.maxConcurrentLimboResolutions; ) {
var e = t.oc.values().next().value;
t.oc.delete(e);
var n = new ft(ct.fromString(e)), r = t.fc.next();
t.cc.set(r, new gs(n)), t.uc = t.uc.insert(n, r), xa(t.remoteStore, new Wi(In(yn(n.path)), r, 2 /* TargetPurpose.LimboResolution */ , qt.at));
}
}
function Ls(t, r, i) {
return e(this, void 0, void 0, (function() {
var o, u, a, s;
return n(this, (function(c) {
switch (c.label) {
case 0:
return o = G(t), u = [], a = [], s = [], o.ic.isEmpty() ? [ 3 /*break*/ , 3 ] : (o.ic.forEach((function(t, e) {
s.push(o._c(e, r, i).then((function(t) {
// Update views if there are actual changes.
if (
// If there are changes, or we are handling a global snapshot, notify
// secondary clients to update query state.
(t || i) && o.isPrimaryClient && o.sharedClientState.updateQueryState(e.targetId, (null == t ? void 0 : t.fromCache) ? "not-current" : "current"),
t) {
u.push(t);
var n = Mu.Ci(e.targetId, t);
a.push(n);
}
})));
})), [ 4 /*yield*/ , Promise.all(s) ]);
case 1:
return c.sent(), o.sc.Wo(u), [ 4 /*yield*/ , function(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a, s, c, l, h;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), n.label = 1;
case 1:
return n.trys.push([ 1, 3, , 4 ]), [ 4 /*yield*/ , e.persistence.runTransaction("notifyLocalViewChanges", "readwrite", (function(t) {
return xt.forEach(r, (function(n) {
return xt.forEach(n.Si, (function(r) {
return e.persistence.referenceDelegate.addReference(t, n.targetId, r);
})).next((function() {
return xt.forEach(n.Di, (function(r) {
return e.persistence.referenceDelegate.removeReference(t, n.targetId, r);
}));
}));
}));
})) ];
case 2:
return n.sent(), [ 3 /*break*/ , 4 ];
case 3:
if (!Ft(i = n.sent())) throw i;
// If `notifyLocalViewChanges` fails, we did not advance the sequence
// number for the documents that were included in this transaction.
// This might trigger them to be deleted earlier than they otherwise
// would have, but it should not invalidate the integrity of the data.
return V("LocalStore", "Failed to update sequence numbers: " + i),
[ 3 /*break*/ , 4 ];
case 4:
for (o = 0, u = r; o < u.length; o++) a = u[o], s = a.targetId, a.fromCache || (c = e.qi.get(s),
l = c.snapshotVersion, h = c.withLastLimboFreeSnapshotVersion(l),
// Advance the last limbo free snapshot version
e.qi = e.qi.insert(s, h));
return [ 2 /*return*/ ];
}
}));
}));
}(o.localStore, a) ];
case 2:
c.sent(), c.label = 3;
case 3:
return [ 2 /*return*/ ];
}
}));
}));
}
function Ps(t, r) {
return e(this, void 0, void 0, (function() {
var e, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
return (e = G(t)).currentUser.isEqual(r) ? [ 3 /*break*/ , 3 ] : (V("SyncEngine", "User change. New user:", r.toKey()),
[ 4 /*yield*/ , Uu(e.localStore, r) ]);
case 1:
return i = n.sent(), e.currentUser = r,
// Fails tasks waiting for pending writes requested by previous user.
function(t, e) {
t.lc.forEach((function(t) {
t.forEach((function(t) {
t.reject(new j(K.CANCELLED, "'waitForPendingWrites' promise is rejected due to a user change."));
}));
})), t.lc.clear();
}(e),
// TODO(b/114226417): Consider calling this only in the primary tab.
e.sharedClientState.handleUserChange(r, i.removedBatchIds, i.addedBatchIds), [ 4 /*yield*/ , Ls(e, i.ji) ];
case 2:
n.sent(), n.label = 3;
case 3:
return [ 2 /*return*/ ];
}
}));
}));
}
function qs(t, e) {
var n = G(t), r = n.cc.get(e);
if (r && r.nc) return _r().add(r.key);
var i = _r(), o = n.rc.get(e);
if (!o) return i;
for (var u = 0, a = o; u < a.length; u++) {
var s = a[u], c = n.ic.get(s);
i = i.unionWith(c.view.ju);
}
return i;
}
/**
* Reconcile the list of synced documents in an existing view with those
* from persistence.
*/ function Us(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , Hu((e = G(t)).localStore, r.query,
/* usePreviousResults= */ !0) ];
case 1:
return i = n.sent(), o = r.view.tc(i), [ 2 /*return*/ , (e.isPrimaryClient && Rs(e, r.targetId, o.Xu),
o) ];
}
}));
}));
}
/**
* Retrieves newly changed documents from remote document cache and raises
* snapshots if needed.
*/
// PORTING NOTE: Multi-Tab only.
function Bs(t, r) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
return [ 2 /*return*/ , Xu((e = G(t)).localStore, r).then((function(t) {
return Ls(e, t);
})) ];
}));
}));
}
/** Applies a mutation state to an existing batch. */
// PORTING NOTE: Multi-Tab only.
function Gs(t, r, i, o) {
return e(this, void 0, void 0, (function() {
var e, u;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , function(t, e) {
var n = G(t), r = G(n.mutationQueue);
return n.persistence.runTransaction("Lookup mutation documents", "readonly", (function(t) {
return r.Tn(t, e).next((function(e) {
return e ? n.localDocuments.getDocuments(t, e) : xt.resolve(null);
}));
}));
}((e = G(t)).localStore, r) ];
case 1:
return null === (u = n.sent()) ? [ 3 /*break*/ , 6 ] : "pending" !== i ? [ 3 /*break*/ , 3 ] : [ 4 /*yield*/ , Ua(e.remoteStore) ];
case 2:
// If we are the primary client, we need to send this write to the
// backend. Secondary clients will ignore these writes since their remote
// connection is disabled.
return n.sent(), [ 3 /*break*/ , 4 ];
case 3:
"acknowledged" === i || "rejected" === i ? (
// NOTE: Both these methods are no-ops for batches that originated from
// other clients.
ks(e, r, o || null), Ns(e, r), function(t, e) {
G(G(t).mutationQueue).An(e);
}(e.localStore, r)) : q(), n.label = 4;
case 4:
return [ 4 /*yield*/ , Ls(e, u) ];
case 5:
return n.sent(), [ 3 /*break*/ , 7 ];
case 6:
// A throttled tab may not have seen the mutation before it was completed
// and removed from the mutation queue, in which case we won't have cached
// the affected documents. In this case we can safely ignore the update
// since that means we didn't apply the mutation locally at all (if we
// had, we would have cached the affected documents), and so we will just
// see any resulting document changes via normal remote document updates
// as applicable.
V("SyncEngine", "Cannot apply mutation batch with id: " + r), n.label = 7;
case 7:
return [ 2 /*return*/ ];
}
}));
}));
}
/** Applies a query target change from a different tab. */
// PORTING NOTE: Multi-Tab only.
function Ks(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a, s, c, l;
return n(this, (function(n) {
switch (n.label) {
case 0:
return Ys(e = G(t)), Xs(e), !0 !== r || !0 === e.dc ? [ 3 /*break*/ , 3 ] : (i = e.sharedClientState.getAllActiveQueryTargets(),
[ 4 /*yield*/ , js(e, i.toArray()) ]);
case 1:
return o = n.sent(), e.dc = !0, [ 4 /*yield*/ , Ha(e.remoteStore, !0) ];
case 2:
for (n.sent(), u = 0, a = o; u < a.length; u++) s = a[u], xa(e.remoteStore, s);
return [ 3 /*break*/ , 7 ];
case 3:
return !1 !== r || !1 === e.dc ? [ 3 /*break*/ , 7 ] : (c = [], l = Promise.resolve(),
e.rc.forEach((function(t, n) {
e.sharedClientState.isLocalQueryTarget(n) ? c.push(n) : l = l.then((function() {
return Fs(e, n), Wu(e.localStore, n,
/*keepPersistedTargetData=*/ !0);
})), Aa(e.remoteStore, n);
})), [ 4 /*yield*/ , l ]);
case 4:
return n.sent(), [ 4 /*yield*/ , js(e, c) ];
case 5:
return n.sent(),
// PORTING NOTE: Multi-Tab only.
function(t) {
var e = G(t);
e.cc.forEach((function(t, n) {
Aa(e.remoteStore, n);
})), e.ac.fs(), e.cc = new Map, e.uc = new He(ft.comparator);
}(e), e.dc = !1, [ 4 /*yield*/ , Ha(e.remoteStore, !1) ];
case 6:
n.sent(), n.label = 7;
case 7:
return [ 2 /*return*/ ];
}
}));
}));
}
function js(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a, s, c, l, h, f, d, p, y, v;
return n(this, (function(n) {
switch (n.label) {
case 0:
e = G(t), i = [], o = [], u = 0, a = r, n.label = 1;
case 1:
return u < a.length ? (s = a[u], c = void 0, (l = e.rc.get(s)) && 0 !== l.length ? [ 4 /*yield*/ , zu(e.localStore, In(l[0])) ] : [ 3 /*break*/ , 7 ]) : [ 3 /*break*/ , 13 ];
case 2:
// For queries that have a local View, we fetch their current state
// from LocalStore (as the resume token and the snapshot version
// might have changed) and reconcile their views with the persisted
// state (the list of syncedDocuments may have gotten out of sync).
c = n.sent(), h = 0, f = l, n.label = 3;
case 3:
return h < f.length ? (d = f[h], p = e.ic.get(d), [ 4 /*yield*/ , Us(e, p) ]) : [ 3 /*break*/ , 6 ];
case 4:
(y = n.sent()).snapshot && o.push(y.snapshot), n.label = 5;
case 5:
return h++, [ 3 /*break*/ , 3 ];
case 6:
return [ 3 /*break*/ , 11 ];
case 7:
return [ 4 /*yield*/ , Yu(e.localStore, s) ];
case 8:
return v = n.sent(), [ 4 /*yield*/ , zu(e.localStore, v) ];
case 9:
return c = n.sent(), [ 4 /*yield*/ , Is(e, Qs(v), s,
/*current=*/ !1, c.resumeToken) ];
case 10:
n.sent(), n.label = 11;
case 11:
i.push(c), n.label = 12;
case 12:
return u++, [ 3 /*break*/ , 1 ];
case 13:
return [ 2 /*return*/ , (e.sc.Wo(o), i) ];
}
}));
}));
}
/**
* Creates a `Query` object from the specified `Target`. There is no way to
* obtain the original `Query`, so we synthesize a `Query` from the `Target`
* object.
*
* The synthesized result might be different from the original `Query`, but
* since the synthesized `Query` should return the same results as the
* original one (only the presentation of results might differ), the potential
* difference will not cause issues.
*/
// PORTING NOTE: Multi-Tab only.
function Qs(t) {
return pn(t.path, t.collectionGroup, t.orderBy, t.filters, t.limit, "F" /* LimitType.First */ , t.startAt, t.endAt);
}
/** Returns the IDs of the clients that are currently active. */
// PORTING NOTE: Multi-Tab only.
function zs(t) {
var e = G(t);
return G(G(e.localStore).persistence).vi();
}
/** Applies a query target change from a different tab. */
// PORTING NOTE: Multi-Tab only.
function Ws(t, r, i, o) {
return e(this, void 0, void 0, (function() {
var e, u, a, s;
return n(this, (function(n) {
switch (n.label) {
case 0:
if ((e = G(t)).dc)
// If we receive a target state notification via WebStorage, we are
// either already secondary or another tab has taken the primary lease.
return [ 2 /*return*/ , void V("SyncEngine", "Ignoring unexpected query state notification.") ];
if (!((u = e.rc.get(r)) && u.length > 0)) return [ 3 /*break*/ , 7 ];
switch (i) {
case "current":
case "not-current":
return [ 3 /*break*/ , 1 ];
case "rejected":
return [ 3 /*break*/ , 4 ];
}
return [ 3 /*break*/ , 6 ];
case 1:
return [ 4 /*yield*/ , Xu(e.localStore, An(u[0])) ];
case 2:
return a = n.sent(), s = Ar.createSynthesizedRemoteEventForCurrentChange(r, "current" === i, Yt.EMPTY_BYTE_STRING),
[ 4 /*yield*/ , Ls(e, a, s) ];
case 3:
return n.sent(), [ 3 /*break*/ , 7 ];
case 4:
return [ 4 /*yield*/ , Wu(e.localStore, r,
/* keepPersistedTargetData */ !0) ];
case 5:
return n.sent(), Fs(e, r, o), [ 3 /*break*/ , 7 ];
case 6:
q(), n.label = 7;
case 7:
return [ 2 /*return*/ ];
}
}));
}));
}
/** Adds or removes Watch targets for queries from different tabs. */ function Hs(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u, a, s, c, l, h, f, d;
return n(this, (function(p) {
switch (p.label) {
case 0:
if (!(e = Ys(t)).dc) return [ 3 /*break*/ , 10 ];
o = 0, u = r, p.label = 1;
case 1:
return o < u.length ? (a = u[o], e.rc.has(a) ? (
// A target might have been added in a previous attempt
V("SyncEngine", "Adding an already active target " + a), [ 3 /*break*/ , 5 ]) : [ 4 /*yield*/ , Yu(e.localStore, a) ]) : [ 3 /*break*/ , 6 ];
case 2:
return s = p.sent(), [ 4 /*yield*/ , zu(e.localStore, s) ];
case 3:
return c = p.sent(), [ 4 /*yield*/ , Is(e, Qs(s), c.targetId,
/*current=*/ !1, c.resumeToken) ];
case 4:
p.sent(), xa(e.remoteStore, c), p.label = 5;
case 5:
return o++, [ 3 /*break*/ , 1 ];
case 6:
l = function(t) {
return n(this, (function(n) {
switch (n.label) {
case 0:
return e.rc.has(t) ? [ 4 /*yield*/ , Wu(e.localStore, t,
/* keepPersistedTargetData */ !1).then((function() {
Aa(e.remoteStore, t), Fs(e, t);
})).catch(Dt) ] : [ 3 /*break*/ , 2 ];
// Release queries that are still active.
case 1:
// Release queries that are still active.
n.sent(), n.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}, h = 0, f = i, p.label = 7;
case 7:
return h < f.length ? (d = f[h], [ 5 /*yield**/ , l(d) ]) : [ 3 /*break*/ , 10 ];
case 8:
p.sent(), p.label = 9;
case 9:
return h++, [ 3 /*break*/ , 7 ];
case 10:
return [ 2 /*return*/ ];
}
}));
}));
}
function Ys(t) {
var e = G(t);
return e.remoteStore.remoteSyncer.applyRemoteEvent = Ss.bind(null, e), e.remoteStore.remoteSyncer.getRemoteKeysForTarget = qs.bind(null, e),
e.remoteStore.remoteSyncer.rejectListen = Ds.bind(null, e), e.sc.Wo = us.bind(null, e.eventManager),
e.sc.wc = as.bind(null, e.eventManager), e;
}
function Xs(t) {
var e = G(t);
return e.remoteStore.remoteSyncer.applySuccessfulWrite = xs.bind(null, e), e.remoteStore.remoteSyncer.rejectFailedWrite = As.bind(null, e),
e
/**
* Loads a Firestore bundle into the SDK. The returned promise resolves when
* the bundle finished loading.
*
* @param syncEngine - SyncEngine to use.
* @param bundleReader - Bundle to load into the SDK.
* @param task - LoadBundleTask used to update the loading progress to public API.
*/;
}
function Zs(t, r, i) {
var o = G(t);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
/** Loads a bundle and returns the list of affected collection groups. */
(function(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u, a, s, c;
return n(this, (function(n) {
switch (n.label) {
case 0:
return n.trys.push([ 0, 14, , 15 ]), [ 4 /*yield*/ , r.getMetadata() ];
case 1:
return e = n.sent(), [ 4 /*yield*/ , function(t, e) {
var n = G(t), r = jr(e.createTime);
return n.persistence.runTransaction("hasNewerBundle", "readonly", (function(t) {
return n.Ns.getBundleMetadata(t, e.id);
})).then((function(t) {
return !!t && t.createTime.compareTo(r) >= 0;
}));
}(t.localStore, e) ];
case 2:
return n.sent() ? [ 4 /*yield*/ , r.close() ] : [ 3 /*break*/ , 4 ];
case 3:
return [ 2 /*return*/ , (n.sent(), i._completeWith(function(t) {
return {
taskState: "Success",
documentsLoaded: t.totalDocuments,
bytesLoaded: t.totalBytes,
totalDocuments: t.totalDocuments,
totalBytes: t.totalBytes
};
}(e)), Promise.resolve(new Set)) ];
case 4:
return i._updateProgress(ds(e)), o = new fs(e, t.localStore, r.yt), [ 4 /*yield*/ , r.mc() ];
case 5:
u = n.sent(), n.label = 6;
case 6:
return u ? [ 4 /*yield*/ , o.Fu(u) ] : [ 3 /*break*/ , 10 ];
case 7:
return (a = n.sent()) && i._updateProgress(a), [ 4 /*yield*/ , r.mc() ];
case 8:
u = n.sent(), n.label = 9;
case 9:
return [ 3 /*break*/ , 6 ];
case 10:
return [ 4 /*yield*/ , o.complete() ];
case 11:
return s = n.sent(), [ 4 /*yield*/ , Ls(t, s.Lu,
/* remoteEvent */ void 0) ];
case 12:
// Save metadata, so loading the same bundle will skip.
return n.sent(), [ 4 /*yield*/ , function(t, e) {
var n = G(t);
return n.persistence.runTransaction("Save bundle", "readwrite", (function(t) {
return n.Ns.saveBundleMetadata(t, e);
}));
}(t.localStore, e) ];
case 13:
return [ 2 /*return*/ , (
// Save metadata, so loading the same bundle will skip.
n.sent(), i._completeWith(s.progress), Promise.resolve(s.Bu)) ];
case 14:
return c = n.sent(), [ 2 /*return*/ , (L("SyncEngine", "Loading bundle failed with ".concat(c)),
i._failWith(c), Promise.resolve(new Set)) ];
case 15:
return [ 2 /*return*/ ];
}
}));
}));
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Provides all components needed for Firestore with in-memory persistence.
* Uses EagerGC garbage collection.
*/)(o, r, i).then((function(t) {
o.sharedClientState.notifyBundleLoaded(t);
}));
}
var Js = /** @class */ function() {
function t() {
this.synchronizeTabs = !1;
}
return t.prototype.initialize = function(t) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return this.yt = ma(t.databaseInfo.databaseId), this.sharedClientState = this.gc(t),
this.persistence = this.yc(t), [ 4 /*yield*/ , this.persistence.start() ];
case 1:
return e.sent(), this.localStore = this.Ic(t), this.gcScheduler = this.Tc(t, this.localStore),
this.indexBackfillerScheduler = this.Ec(t, this.localStore), [ 2 /*return*/ ];
}
}));
}));
}, t.prototype.Tc = function(t, e) {
return null;
}, t.prototype.Ec = function(t, e) {
return null;
}, t.prototype.Ic = function(t) {
return qu(this.persistence, new Lu, t.initialUser, this.yt);
}, t.prototype.yc = function(t) {
return new Du(Au.Bs, this.yt);
}, t.prototype.gc = function(t) {
return new ca;
}, t.prototype.terminate = function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
switch (t.label) {
case 0:
return this.gcScheduler && this.gcScheduler.stop(), [ 4 /*yield*/ , this.sharedClientState.shutdown() ];
case 1:
return t.sent(), [ 4 /*yield*/ , this.persistence.shutdown() ];
case 2:
return t.sent(), [ 2 /*return*/ ];
}
}));
}));
}, t;
}(), $s = /** @class */ function(r) {
function i(t, e, n) {
var i = this;
return (i = r.call(this) || this).Ac = t, i.cacheSizeBytes = e, i.forceOwnership = n,
i.synchronizeTabs = !1, i;
}
return t(i, r), i.prototype.initialize = function(t) {
return e(this, void 0, void 0, (function() {
var e = this;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , r.prototype.initialize.call(this, t) ];
case 1:
return n.sent(), [ 4 /*yield*/ , this.Ac.initialize(this, t) ];
case 2:
// Enqueue writes from a previous session
return n.sent(), [ 4 /*yield*/ , Xs(this.Ac.syncEngine) ];
case 3:
// Enqueue writes from a previous session
return n.sent(), [ 4 /*yield*/ , Ua(this.Ac.remoteStore) ];
case 4:
// NOTE: This will immediately call the listener, so we make sure to
// set it after localStore / remoteStore are started.
return n.sent(), [ 4 /*yield*/ , this.persistence.li((function() {
return e.gcScheduler && !e.gcScheduler.started && e.gcScheduler.start(), e.indexBackfillerScheduler && !e.indexBackfillerScheduler.started && e.indexBackfillerScheduler.start(),
Promise.resolve();
})) ];
case 5:
// NOTE: This will immediately call the listener, so we make sure to
// set it after localStore / remoteStore are started.
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}, i.prototype.Ic = function(t) {
return qu(this.persistence, new Lu, t.initialUser, this.yt);
}, i.prototype.Tc = function(t, e) {
var n = this.persistence.referenceDelegate.garbageCollector;
return new ru(n, t.asyncQueue, e);
}, i.prototype.Ec = function(t, e) {
var n = new Pt(e, this.persistence);
return new Lt(t.asyncQueue, n);
}, i.prototype.yc = function(t) {
var e = Vu(t.databaseInfo.databaseId, t.databaseInfo.persistenceKey), n = void 0 !== this.cacheSizeBytes ? Go.withCacheSize(this.cacheSizeBytes) : Go.DEFAULT;
return new Fu(this.synchronizeTabs, e, t.clientId, n, t.asyncQueue, ya(), va(), this.yt, this.sharedClientState, !!this.forceOwnership);
}, i.prototype.gc = function(t) {
return new ca;
}, i;
}(Js), tc = /** @class */ function(r) {
function i(t, e) {
var n = this;
return (n = r.call(this, t, e, /* forceOwnership= */ !1) || this).Ac = t, n.cacheSizeBytes = e,
n.synchronizeTabs = !0, n;
}
return t(i, r), i.prototype.initialize = function(t) {
return e(this, void 0, void 0, (function() {
var i, o = this;
return n(this, (function(u) {
switch (u.label) {
case 0:
return [ 4 /*yield*/ , r.prototype.initialize.call(this, t) ];
case 1:
return u.sent(), i = this.Ac.syncEngine, this.sharedClientState instanceof sa ? (this.sharedClientState.syncEngine = {
Fr: Gs.bind(null, i),
$r: Ws.bind(null, i),
Br: Hs.bind(null, i),
vi: zs.bind(null, i),
Mr: Bs.bind(null, i)
}, [ 4 /*yield*/ , this.sharedClientState.start() ]) : [ 3 /*break*/ , 3 ];
case 2:
u.sent(), u.label = 3;
case 3:
// NOTE: This will immediately call the listener, so we make sure to
// set it after localStore / remoteStore are started.
return [ 4 /*yield*/ , this.persistence.li((function(t) {
return e(o, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return [ 4 /*yield*/ , Ks(this.Ac.syncEngine, t) ];
case 1:
return e.sent(), this.gcScheduler && (t && !this.gcScheduler.started ? this.gcScheduler.start() : t || this.gcScheduler.stop()),
this.indexBackfillerScheduler && (t && !this.indexBackfillerScheduler.started ? this.indexBackfillerScheduler.start() : t || this.indexBackfillerScheduler.stop()),
[ 2 /*return*/ ];
}
}));
}));
})) ];
case 4:
// NOTE: This will immediately call the listener, so we make sure to
// set it after localStore / remoteStore are started.
return u.sent(), [ 2 /*return*/ ];
}
}));
}));
}, i.prototype.gc = function(t) {
var e = ya();
if (!sa.C(e)) throw new j(K.UNIMPLEMENTED, "IndexedDB persistence is only available on platforms that support LocalStorage.");
var n = Vu(t.databaseInfo.databaseId, t.databaseInfo.persistenceKey);
return new sa(e, t.asyncQueue, n, t.clientId, t.initialUser);
}, i;
}($s), ec = /** @class */ function() {
function t() {}
return t.prototype.initialize = function(t, r) {
return e(this, void 0, void 0, (function() {
var e = this;
return n(this, (function(n) {
switch (n.label) {
case 0:
return this.localStore ? [ 3 /*break*/ , 2 ] : (this.localStore = t.localStore,
this.sharedClientState = t.sharedClientState, this.datastore = this.createDatastore(r),
this.remoteStore = this.createRemoteStore(r), this.eventManager = this.createEventManager(r),
this.syncEngine = this.createSyncEngine(r,
/* startAsPrimary=*/ !t.synchronizeTabs), this.sharedClientState.onlineStateHandler = function(t) {
return _s(e.syncEngine, t, 1 /* OnlineStateSource.SharedClientState */);
}, this.remoteStore.remoteSyncer.handleCredentialChange = Ps.bind(null, this.syncEngine),
[ 4 /*yield*/ , Ha(this.remoteStore, this.syncEngine.isPrimaryClient) ]);
case 1:
n.sent(), n.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
}, t.prototype.createEventManager = function(t) {
return new rs;
}, t.prototype.createDatastore = function(t) {
var e, n = ma(t.databaseInfo.databaseId), r = (e = t.databaseInfo, new pa(e));
/** Return the Platform-specific connectivity monitor. */ return function(t, e, n, r) {
return new Ta(t, e, n, r);
}(t.authCredentials, t.appCheckCredentials, r, n);
}, t.prototype.createRemoteStore = function(t) {
var e, n, r, i, o, u = this;
return e = this.localStore, n = this.datastore, r = t.asyncQueue, i = function(t) {
return _s(u.syncEngine, t, 0 /* OnlineStateSource.RemoteStore */);
}, o = ha.C() ? new ha : new la, new Sa(e, n, r, i, o);
}, t.prototype.createSyncEngine = function(t, e) {
return function(t, e, n,
// PORTING NOTE: Manages state synchronization in multi-tab environments.
r, i, o, u) {
var a = new ws(t, e, n, r, i, o);
return u && (a.dc = !0), a;
}(this.localStore, this.remoteStore, this.eventManager, this.sharedClientState, t.initialUser, t.maxConcurrentLimboResolutions, e);
}, t.prototype.terminate = function() {
return function(t) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = G(t), V("RemoteStore", "RemoteStore shutting down."), e._u.add(5 /* OfflineCause.Shutdown */),
[ 4 /*yield*/ , Da(e) ];
case 1:
return n.sent(), e.mu.shutdown(),
// Set the OnlineState to Unknown (rather than Offline) to avoid potentially
// triggering spurious listener events with cached data, etc.
e.gu.set("Unknown" /* OnlineState.Unknown */), [ 2 /*return*/ ];
}
}));
}));
}(this.remoteStore);
}, t;
}();
/**
* Provides all components needed for Firestore with IndexedDB persistence.
*/
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function nc(t, e, n) {
if (!n) throw new j(K.INVALID_ARGUMENT, "Function ".concat(t, "() cannot be called with an empty ").concat(e, "."));
}
/**
* Validates that two boolean options are not set at the same time.
* @internal
*/ function rc(t, e, n, r) {
if (!0 === e && !0 === r) throw new j(K.INVALID_ARGUMENT, "".concat(t, " and ").concat(n, " cannot be used together."));
}
/**
* Validates that `path` refers to a document (indicated by the fact it contains
* an even numbers of segments).
*/ function ic(t) {
if (!ft.isDocumentKey(t)) throw new j(K.INVALID_ARGUMENT, "Invalid document reference. Document references must have an even number of segments, but ".concat(t, " has ").concat(t.length, "."));
}
/**
* Validates that `path` refers to a collection (indicated by the fact it
* contains an odd numbers of segments).
*/ function oc(t) {
if (ft.isDocumentKey(t)) throw new j(K.INVALID_ARGUMENT, "Invalid collection reference. Collection references must have an odd number of segments, but ".concat(t, " has ").concat(t.length, "."));
}
/**
* Returns true if it's a non-null object without a custom prototype
* (i.e. excludes Array, Date, etc.).
*/
/** Returns a string describing the type / value of the provided input. */ function uc(t) {
if (void 0 === t) return "undefined";
if (null === t) return "null";
if ("string" == typeof t) return t.length > 20 && (t = "".concat(t.substring(0, 20), "...")),
JSON.stringify(t);
if ("number" == typeof t || "boolean" == typeof t) return "" + t;
if ("object" == typeof t) {
if (t instanceof Array) return "an array";
var e =
/** try to get the constructor name for an object. */
function(t) {
return t.constructor ? t.constructor.name : null;
}(t);
return e ? "a custom ".concat(e, " object") : "an object";
}
return "function" == typeof t ? "a function" : q();
}
function ac(t,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
e) {
if ("_delegate" in t && (
// Unwrap Compat types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
t = t._delegate), !(t instanceof e)) {
if (e.name === t.constructor.name) throw new j(K.INVALID_ARGUMENT, "Type does not match the expected instance. Did you pass a reference from a different Firestore SDK?");
var n = uc(t);
throw new j(K.INVALID_ARGUMENT, "Expected type '".concat(e.name, "', but it was: ").concat(n));
}
return t;
}
function sc(t, e) {
if (e <= 0) throw new j(K.INVALID_ARGUMENT, "Function ".concat(t, "() requires a positive number, but it was: ").concat(e, "."));
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var cc = new Map, lc = /** @class */ function() {
function t(t) {
var e;
if (void 0 === t.host) {
if (void 0 !== t.ssl) throw new j(K.INVALID_ARGUMENT, "Can't provide ssl option if host option is not set");
this.host = "firestore.googleapis.com", this.ssl = !0;
} else this.host = t.host, this.ssl = null === (e = t.ssl) || void 0 === e || e;
if (this.credentials = t.credentials, this.ignoreUndefinedProperties = !!t.ignoreUndefinedProperties,
void 0 === t.cacheSizeBytes) this.cacheSizeBytes = 41943040; else {
if (-1 !== t.cacheSizeBytes && t.cacheSizeBytes < 1048576) throw new j(K.INVALID_ARGUMENT, "cacheSizeBytes must be at least 1048576");
this.cacheSizeBytes = t.cacheSizeBytes;
}
this.experimentalForceLongPolling = !!t.experimentalForceLongPolling, this.experimentalAutoDetectLongPolling = !!t.experimentalAutoDetectLongPolling,
this.useFetchStreams = !!t.useFetchStreams, rc("experimentalForceLongPolling", t.experimentalForceLongPolling, "experimentalAutoDetectLongPolling", t.experimentalAutoDetectLongPolling);
}
return t.prototype.isEqual = function(t) {
return this.host === t.host && this.ssl === t.ssl && this.credentials === t.credentials && this.cacheSizeBytes === t.cacheSizeBytes && this.experimentalForceLongPolling === t.experimentalForceLongPolling && this.experimentalAutoDetectLongPolling === t.experimentalAutoDetectLongPolling && this.ignoreUndefinedProperties === t.ignoreUndefinedProperties && this.useFetchStreams === t.useFetchStreams;
}, t;
}(), hc = /** @class */ function() {
/** @hideconstructor */
function t(t, e, n, r) {
this._authCredentials = t, this._appCheckCredentials = e, this._databaseId = n,
this._app = r,
/**
* Whether it's a Firestore or Firestore Lite instance.
*/
this.type = "firestore-lite", this._persistenceKey = "(lite)", this._settings = new lc({}),
this._settingsFrozen = !1;
}
return Object.defineProperty(t.prototype, "app", {
/**
* The {@link @firebase/app#FirebaseApp} associated with this `Firestore` service
* instance.
*/
get: function() {
if (!this._app) throw new j(K.FAILED_PRECONDITION, "Firestore was not initialized using the Firebase SDK. 'app' is not available");
return this._app;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "_initialized", {
get: function() {
return this._settingsFrozen;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "_terminated", {
get: function() {
return void 0 !== this._terminateTask;
},
enumerable: !1,
configurable: !0
}), t.prototype._setSettings = function(t) {
if (this._settingsFrozen) throw new j(K.FAILED_PRECONDITION, "Firestore has already been started and its settings can no longer be changed. You can only modify settings before calling any other methods on a Firestore object.");
this._settings = new lc(t), void 0 !== t.credentials && (this._authCredentials = function(t) {
if (!t) return new W;
switch (t.type) {
case "gapi":
var e = t.client;
return new Z(e, t.sessionIndex || "0", t.iamToken || null, t.authTokenFactory || null);
case "provider":
return t.client;
default:
throw new j(K.INVALID_ARGUMENT, "makeAuthCredentialsProvider failed due to invalid credential type");
}
}(t.credentials));
}, t.prototype._getSettings = function() {
return this._settings;
}, t.prototype._freezeSettings = function() {
return this._settingsFrozen = !0, this._settings;
}, t.prototype._delete = function() {
return this._terminateTask || (this._terminateTask = this._terminate()), this._terminateTask;
},
/** Returns a JSON-serializable representation of this `Firestore` instance. */ t.prototype.toJSON = function() {
return {
app: this._app,
databaseId: this._databaseId,
settings: this._settings
};
},
/**
* Terminates all components used by this client. Subclasses can override
* this method to clean up their own dependencies, but must also call this
* method.
*
* Only ever called once.
*/
t.prototype._terminate = function() {
/**
* Removes all components associated with the provided instance. Must be called
* when the `Firestore` instance is terminated.
*/
return t = this, (e = cc.get(t)) && (V("ComponentProvider", "Removing Datastore"),
cc.delete(t), e.terminate()), Promise.resolve();
var t, e;
}, t;
}();
/**
* An instance map that ensures only one Datastore exists per Firestore
* instance.
*/
/**
* A concrete type describing all the values that can be applied via a
* user-supplied `FirestoreSettings` object. This is a separate type so that
* defaults can be supplied and the value can be checked for equality.
*/
/**
* Modify this instance to communicate with the Cloud Firestore emulator.
*
* Note: This must be called before this instance has been used to do any
* operations.
*
* @param firestore - The `Firestore` instance to configure to connect to the
* emulator.
* @param host - the emulator host (ex: localhost).
* @param port - the emulator port (ex: 9000).
* @param options.mockUserToken - the mock auth token to use for unit testing
* Security Rules.
*/
function fc(t, e, n, r) {
var i;
void 0 === r && (r = {});
var o = (t = ac(t, hc))._getSettings();
if ("firestore.googleapis.com" !== o.host && o.host !== e && L("Host has been set in both settings() and useEmulator(), emulator host will be used"),
t._setSettings(Object.assign(Object.assign({}, o), {
host: "".concat(e, ":").concat(n),
ssl: !1
})), r.mockUserToken) {
var u, a;
if ("string" == typeof r.mockUserToken) u = r.mockUserToken, a = N.MOCK_USER; else {
// Let createMockUserToken validate first (catches common mistakes like
// invalid field "uid" and missing field "sub" / "user_id".)
u = m(r.mockUserToken, null === (i = t._app) || void 0 === i ? void 0 : i.options.projectId);
var s = r.mockUserToken.sub || r.mockUserToken.user_id;
if (!s) throw new j(K.INVALID_ARGUMENT, "mockUserToken must contain 'sub' or 'user_id' field!");
a = new N(s);
}
t._authCredentials = new H(new z(u, a));
}
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A `DocumentReference` refers to a document location in a Firestore database
* and can be used to write, read, or listen to the location. The document at
* the referenced location may or may not exist.
*/ var dc = /** @class */ function() {
/** @hideconstructor */
function t(t,
/**
* If provided, the `FirestoreDataConverter` associated with this instance.
*/
e, n) {
this.converter = e, this._key = n,
/** The type of this Firestore reference. */
this.type = "document", this.firestore = t;
}
return Object.defineProperty(t.prototype, "_path", {
get: function() {
return this._key.path;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "id", {
/**
* The document's identifier within its collection.
*/
get: function() {
return this._key.path.lastSegment();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "path", {
/**
* A string representing the path of the referenced document (relative
* to the root of the database).
*/
get: function() {
return this._key.path.canonicalString();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "parent", {
/**
* The collection this `DocumentReference` belongs to.
*/
get: function() {
return new yc(this.firestore, this.converter, this._key.path.popLast());
},
enumerable: !1,
configurable: !0
}), t.prototype.withConverter = function(e) {
return new t(this.firestore, e, this._key);
}, t;
}(), pc = /** @class */ function() {
// This is the lite version of the Query class in the main SDK.
/** @hideconstructor protected */
function t(t,
/**
* If provided, the `FirestoreDataConverter` associated with this instance.
*/
e, n) {
this.converter = e, this._query = n,
/** The type of this Firestore reference. */
this.type = "query", this.firestore = t;
}
return t.prototype.withConverter = function(e) {
return new t(this.firestore, e, this._query);
}, t;
}(), yc = /** @class */ function(e) {
/** @hideconstructor */
function n(t, n, r) {
var i = this;
return (i = e.call(this, t, n, yn(r)) || this)._path = r,
/** The type of this Firestore reference. */
i.type = "collection", i;
}
return t(n, e), Object.defineProperty(n.prototype, "id", {
/** The collection's identifier. */ get: function() {
return this._query.path.lastSegment();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(n.prototype, "path", {
/**
* A string representing the path of the referenced collection (relative
* to the root of the database).
*/
get: function() {
return this._query.path.canonicalString();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(n.prototype, "parent", {
/**
* A reference to the containing `DocumentReference` if this is a
* subcollection. If this isn't a subcollection, the reference is null.
*/
get: function() {
var t = this._path.popLast();
return t.isEmpty() ? null : new dc(this.firestore,
/* converter= */ null, new ft(t));
},
enumerable: !1,
configurable: !0
}), n.prototype.withConverter = function(t) {
return new n(this.firestore, t, this._path);
}, n;
}(pc);
/**
* A `Query` refers to a query which you can read or listen to. You can also
* construct refined `Query` objects by adding filters and ordering.
*/ function vc(t, e) {
for (var n = [], i = 2; i < arguments.length; i++) n[i - 2] = arguments[i];
if (t = y(t), nc("collection", "path", e), t instanceof hc) {
var o = ct.fromString.apply(ct, r([ e ], n, !1));
return oc(o), new yc(t, /* converter= */ null, o);
}
if (!(t instanceof dc || t instanceof yc)) throw new j(K.INVALID_ARGUMENT, "Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore");
var u = t._path.child(ct.fromString.apply(ct, r([ e ], n, !1)));
return oc(u), new yc(t.firestore,
/* converter= */ null, u);
}
// TODO(firestorelite): Consider using ErrorFactory -
// https://github.com/firebase/firebase-js-sdk/blob/0131e1f/packages/util/src/errors.ts#L106
/**
* Creates and returns a new `Query` instance that includes all documents in the
* database that are contained in a collection or subcollection with the
* given `collectionId`.
*
* @param firestore - A reference to the root `Firestore` instance.
* @param collectionId - Identifies the collections to query over. Every
* collection or subcollection with this ID as the last segment of its path
* will be included. Cannot contain a slash.
* @returns The created `Query`.
*/ function mc(t, e) {
if (t = ac(t, hc), nc("collectionGroup", "collection id", e), e.indexOf("/") >= 0) throw new j(K.INVALID_ARGUMENT, "Invalid collection ID '".concat(e, "' passed to function collectionGroup(). Collection IDs must not contain '/'."));
return new pc(t,
/* converter= */ null, function(t) {
return new dn(ct.emptyPath(), t);
}(e));
}
function gc(t, e) {
for (var n = [], i = 2; i < arguments.length; i++) n[i - 2] = arguments[i];
if (t = y(t),
// We allow omission of 'pathString' but explicitly prohibit passing in both
// 'undefined' and 'null'.
1 === arguments.length && (e = nt.R()), nc("doc", "path", e), t instanceof hc) {
var o = ct.fromString.apply(ct, r([ e ], n, !1));
return ic(o), new dc(t,
/* converter= */ null, new ft(o));
}
if (!(t instanceof dc || t instanceof yc)) throw new j(K.INVALID_ARGUMENT, "Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore");
var u = t._path.child(ct.fromString.apply(ct, r([ e ], n, !1)));
return ic(u), new dc(t.firestore, t instanceof yc ? t.converter : null, new ft(u));
}
/**
* Returns true if the provided references are equal.
*
* @param left - A reference to compare.
* @param right - A reference to compare.
* @returns true if the references point to the same location in the same
* Firestore database.
*/ function wc(t, e) {
return t = y(t), e = y(e), (t instanceof dc || t instanceof yc) && (e instanceof dc || e instanceof yc) && t.firestore === e.firestore && t.path === e.path && t.converter === e.converter
/**
* Returns true if the provided queries point to the same collection and apply
* the same constraints.
*
* @param left - A `Query` to compare.
* @param right - A `Query` to compare.
* @returns true if the references point to the same location in the same
* Firestore database.
*/;
}
function bc(t, e) {
return t = y(t), e = y(e), t instanceof pc && e instanceof pc && t.firestore === e.firestore && Sn(t._query, e._query) && t.converter === e.converter
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* How many bytes to read each time when `ReadableStreamReader.read()` is
* called. Only applicable for byte streams that we control (e.g. those backed
* by an UInt8Array).
*/
/**
* Builds a `ByteStreamReader` from a UInt8Array.
* @param source - The data source to use.
* @param bytesPerRead - How many bytes each `read()` from the returned reader
* will read.
*/;
}
function Ic(t, r) {
void 0 === r && (r = 10240);
var i = 0;
// The TypeScript definition for ReadableStreamReader changed. We use
// `any` here to allow this code to compile with different versions.
// See https://github.com/microsoft/TypeScript/issues/42970
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
read: function() {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
return i < t.byteLength ? (e = {
value: t.slice(i, i + r),
done: !1
}, [ 2 /*return*/ , (i += r, e) ]) : [ 2 /*return*/ , {
done: !0
} ];
}));
}));
},
cancel: function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
return [ 2 /*return*/ ];
}));
}));
},
releaseLock: function() {},
closed: Promise.reject("unimplemented")
};
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* On web, a `ReadableStream` is wrapped around by a `ByteStreamReader`.
*/
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* A wrapper implementation of Observer that will dispatch events
* asynchronously. To allow immediate silencing, a mute call is added which
* causes events scheduled to no longer be raised.
*/ var Tc = /** @class */ function() {
function t(t) {
this.observer = t,
/**
* When set to true, will not raise future events. Necessary to deal with
* async detachment of listener.
*/
this.muted = !1;
}
return t.prototype.next = function(t) {
this.observer.next && this.Rc(this.observer.next, t);
}, t.prototype.error = function(t) {
this.observer.error ? this.Rc(this.observer.error, t) : M("Uncaught Error in snapshot listener:", t.toString());
}, t.prototype.bc = function() {
this.muted = !0;
}, t.prototype.Rc = function(t, e) {
var n = this;
this.muted || setTimeout((function() {
n.muted || t(e);
}), 0);
}, t;
}(), Ec = /** @class */ function() {
function t(
/** The reader to read from underlying binary bundle data source. */
t, e) {
var n = this;
this.Pc = t, this.yt = e,
/** Cached bundle metadata. */
this.metadata = new Q,
/**
* Internal buffer to hold bundle content, accumulating incomplete element
* content.
*/
this.buffer = new Uint8Array, this.vc = new TextDecoder("utf-8"),
// Read the metadata (which is the first element).
this.Vc().then((function(t) {
t && t.Ou() ? n.metadata.resolve(t.ku.metadata) : n.metadata.reject(new Error("The first element of the bundle is not a metadata, it is\n ".concat(JSON.stringify(null == t ? void 0 : t.ku))));
}), (function(t) {
return n.metadata.reject(t);
}));
}
return t.prototype.close = function() {
return this.Pc.cancel();
}, t.prototype.getMetadata = function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
return [ 2 /*return*/ , this.metadata.promise ];
}));
}));
}, t.prototype.mc = function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
switch (t.label) {
case 0:
return [ 4 /*yield*/ , this.getMetadata() ];
case 1:
// Makes sure metadata is read before proceeding.
return [ 2 /*return*/ , (t.sent(), this.Vc()) ];
}
}));
}));
},
/**
* Reads from the head of internal buffer, and pulling more data from
* underlying stream if a complete element cannot be found, until an
* element(including the prefixed length and the JSON string) is found.
*
* Once a complete element is read, it is dropped from internal buffer.
*
* Returns either the bundled element, or null if we have reached the end of
* the stream.
*/
t.prototype.Vc = function() {
return e(this, void 0, void 0, (function() {
var t, e, r, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , this.Sc() ];
case 1:
return null === (t = n.sent()) ? [ 2 /*return*/ , null ] : (e = this.vc.decode(t),
r = Number(e), isNaN(r) && this.Dc("length string (".concat(e, ") is not valid number")),
[ 4 /*yield*/ , this.Cc(r) ]);
case 2:
return i = n.sent(), [ 2 /*return*/ , new ls(JSON.parse(i), t.length + r) ];
}
}));
}));
},
/** First index of '{' from the underlying buffer. */ t.prototype.xc = function() {
return this.buffer.findIndex((function(t) {
return t === "{".charCodeAt(0);
}));
},
/**
* Reads from the beginning of the internal buffer, until the first '{', and
* return the content.
*
* If reached end of the stream, returns a null.
*/
t.prototype.Sc = function() {
return e(this, void 0, void 0, (function() {
var t, e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return this.xc() < 0 ? [ 4 /*yield*/ , this.Nc() ] : [ 3 /*break*/ , 3 ];
case 1:
if (n.sent()) return [ 3 /*break*/ , 3 ];
n.label = 2;
case 2:
return [ 3 /*break*/ , 0 ];
case 3:
// Broke out of the loop because underlying stream is closed, and there
// happens to be no more data to process.
return 0 === this.buffer.length ? [ 2 /*return*/ , null ] : (
// Broke out of the loop because underlying stream is closed, but still
// cannot find an open bracket.
(t = this.xc()) < 0 && this.Dc("Reached the end of bundle when a length string is expected."),
e = this.buffer.slice(0, t), [ 2 /*return*/ , (this.buffer = this.buffer.slice(t),
e) ]);
}
}));
}));
},
/**
* Reads from a specified position from the internal buffer, for a specified
* number of bytes, pulling more data from the underlying stream if needed.
*
* Returns a string decoded from the read bytes.
*/
t.prototype.Cc = function(t) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return this.buffer.length < t ? [ 4 /*yield*/ , this.Nc() ] : [ 3 /*break*/ , 3 ];
case 1:
n.sent() && this.Dc("Reached the end of bundle when more is expected."), n.label = 2;
case 2:
return [ 3 /*break*/ , 0 ];
case 3:
// Update the internal buffer to drop the read json string.
return e = this.vc.decode(this.buffer.slice(0, t)), [ 2 /*return*/ , (this.buffer = this.buffer.slice(t),
e) ];
}
}));
}));
}, t.prototype.Dc = function(t) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
throw this.Pc.cancel(), new Error("Invalid bundle format: ".concat(t));
},
/**
* Pulls more data from underlying stream to internal buffer.
* Returns a boolean indicating whether the stream is finished.
*/
t.prototype.Nc = function() {
return e(this, void 0, void 0, (function() {
var t, e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , this.Pc.read() ];
case 1:
return (t = n.sent()).done || ((e = new Uint8Array(this.buffer.length + t.value.length)).set(this.buffer),
e.set(t.value, this.buffer.length), this.buffer = e), [ 2 /*return*/ , t.done ];
}
}));
}));
}, t;
}(), Sc = function() {
/** A type string to uniquely identify instances of this class. */
this.type = "AggregateField";
}, _c = /** @class */ function() {
/** @hideconstructor */
function t(t, e) {
this._data = e,
/** A type string to uniquely identify instances of this class. */
this.type = "AggregateQuerySnapshot", this.query = t
/**
* Returns the results of the aggregations performed over the underlying
* query.
*
* The keys of the returned object will be the same as those of the
* `AggregateSpec` object specified to the aggregation method, and the values
* will be the corresponding aggregation result.
*
* @returns The results of the aggregations performed over the underlying
* query.
*/;
}
return t.prototype.data = function() {
return this._data;
}, t;
}(), Dc = /** @class */ function() {
function t(t, e, n) {
this.query = t, this.datastore = e, this.userDataWriter = n;
}
return t.prototype.run = function() {
var t = this;
// TODO(firestorexp): Make sure there is only one Datastore instance per
// firestore-exp client.
return function(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = G(t), i = function(t, e) {
var n = ii(t, e);
return {
structuredAggregationQuery: {
aggregations: [ {
count: {},
alias: "count_alias"
} ],
structuredQuery: n.structuredQuery
},
parent: n.parent
};
}(e.yt, In(r)), o = i.parent, e.connection.co || delete i.parent, [ 4 /*yield*/ , e._o("RunAggregationQuery", o, i, /*expectedResponseCount=*/ 1) ];
case 1:
return [ 2 /*return*/ , n.sent().filter((function(t) {
return !!t.result;
})).map((function(t) {
return t.result.aggregateFields;
})) ];
}
}));
}));
}(this.datastore, this.query._query).then((function(e) {
U(void 0 !== e[0]);
var n = Object.entries(e[0]).filter((function(t) {
var e = t[0];
return t[1], "count_alias" === e;
})).map((function(e) {
e[0];
var n = e[1];
return t.userDataWriter.convertValue(n);
}))[0];
return U("number" == typeof n), Promise.resolve(new _c(t.query, {
count: n
}));
}));
}, t;
}(), xc = /** @class */ function() {
function t(t) {
this.datastore = t,
// The version of each document that was read during this transaction.
this.readVersions = new Map, this.mutations = [], this.committed = !1,
/**
* A deferred usage error that occurred previously in this transaction that
* will cause the transaction to fail once it actually commits.
*/
this.lastWriteError = null,
/**
* Set of documents that have been written in the transaction.
*
* When there's more than one write to the same key in a transaction, any
* writes after the first are handled differently.
*/
this.writtenDocs = new Set;
}
return t.prototype.lookup = function(t) {
return e(this, void 0, void 0, (function() {
var r, i = this;
return n(this, (function(o) {
switch (o.label) {
case 0:
if (this.ensureCommitNotCalled(), this.mutations.length > 0) throw new j(K.INVALID_ARGUMENT, "Firestore transactions require all reads to be executed before all writes.");
return [ 4 /*yield*/ , function(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o, u, a, s;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = G(t), i = Zr(e.yt) + "/documents", o = {
documents: r.map((function(t) {
return Wr(e.yt, t);
}))
}, [ 4 /*yield*/ , e._o("BatchGetDocuments", i, o, r.length) ];
case 1:
return u = n.sent(), a = new Map, u.forEach((function(t) {
var n = function(t, e) {
return "found" in e ? function(t, e) {
U(!!e.found), e.found.name, e.found.updateTime;
var n = Hr(t, e.found.name), r = jr(e.found.updateTime), i = e.found.createTime ? jr(e.found.createTime) : at.min(), o = new en({
mapValue: {
fields: e.found.fields
}
});
return rn.newFoundDocument(n, r, i, o);
}(t, e) : "missing" in e ? function(t, e) {
U(!!e.missing), U(!!e.readTime);
var n = Hr(t, e.missing), r = jr(e.readTime);
return rn.newNoDocument(n, r);
}(t, e) : q();
}(e.yt, t);
a.set(n.key.toString(), n);
})), s = [], [ 2 /*return*/ , (r.forEach((function(t) {
var e = a.get(t.toString());
U(!!e), s.push(e);
})), s) ];
}
}));
}));
}(this.datastore, t) ];
case 1:
return [ 2 /*return*/ , ((r = o.sent()).forEach((function(t) {
return i.recordVersion(t);
})), r) ];
}
}));
}));
}, t.prototype.set = function(t, e) {
this.write(e.toMutation(t, this.precondition(t))), this.writtenDocs.add(t.toString());
}, t.prototype.update = function(t, e) {
try {
this.write(e.toMutation(t, this.preconditionForUpdate(t)));
} catch (t) {
this.lastWriteError = t;
}
this.writtenDocs.add(t.toString());
}, t.prototype.delete = function(t) {
this.write(new cr(t, this.precondition(t))), this.writtenDocs.add(t.toString());
}, t.prototype.commit = function() {
return e(this, void 0, void 0, (function() {
var t, r = this;
return n(this, (function(i) {
switch (i.label) {
case 0:
if (this.ensureCommitNotCalled(), this.lastWriteError) throw this.lastWriteError;
return t = this.readVersions,
// For each mutation, note that the doc was written.
this.mutations.forEach((function(e) {
t.delete(e.key.toString());
})),
// For each document that was read but not written to, we want to perform
// a `verify` operation.
t.forEach((function(t, e) {
var n = ft.fromPath(e);
r.mutations.push(new lr(n, r.precondition(n)));
})), [ 4 /*yield*/ , function(t, r) {
return e(this, void 0, void 0, (function() {
var e, i, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = G(t), i = Zr(e.yt) + "/documents", o = {
writes: r.map((function(t) {
return ei(e.yt, t);
}))
}, [ 4 /*yield*/ , e.ao("Commit", i, o) ];
case 1:
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}(this.datastore, this.mutations) ];
case 1:
// For each mutation, note that the doc was written.
return i.sent(), this.committed = !0, [ 2 /*return*/ ];
}
}));
}));
}, t.prototype.recordVersion = function(t) {
var e;
if (t.isFoundDocument()) e = t.version; else {
if (!t.isNoDocument()) throw q();
// Represent a deleted doc using SnapshotVersion.min().
e = at.min();
}
var n = this.readVersions.get(t.key.toString());
if (n) {
if (!e.isEqual(n))
// This transaction will fail no matter what.
throw new j(K.ABORTED, "Document version changed between two reads.");
} else this.readVersions.set(t.key.toString(), e);
},
/**
* Returns the version of this document when it was read in this transaction,
* as a precondition, or no precondition if it was not read.
*/
t.prototype.precondition = function(t) {
var e = this.readVersions.get(t.toString());
return !this.writtenDocs.has(t.toString()) && e ? e.isEqual(at.min()) ? Hn.exists(!1) : Hn.updateTime(e) : Hn.none();
},
/**
* Returns the precondition for a document if the operation is an update.
*/
t.prototype.preconditionForUpdate = function(t) {
var e = this.readVersions.get(t.toString());
// The first time a document is written, we want to take into account the
// read time and existence
if (!this.writtenDocs.has(t.toString()) && e) {
if (e.isEqual(at.min()))
// The document doesn't exist, so fail the transaction.
// This has to be validated locally because you can't send a
// precondition that a document does not exist without changing the
// semantics of the backend write to be an insert. This is the reverse
// of what we want, since we want to assert that the document doesn't
// exist but then send the update and have it fail. Since we can't
// express that to the backend, we have to validate locally.
// Note: this can change once we can send separate verify writes in the
// transaction.
throw new j(K.INVALID_ARGUMENT, "Can't update a document that doesn't exist.");
// Document exists, base precondition on document update time.
return Hn.updateTime(e);
}
// Document was not read, so we just use the preconditions for a blind
// update.
return Hn.exists(!0);
}, t.prototype.write = function(t) {
this.ensureCommitNotCalled(), this.mutations.push(t);
}, t.prototype.ensureCommitNotCalled = function() {}, t;
}(), Ac = /** @class */ function() {
function t(t, e, n, r, i) {
this.asyncQueue = t, this.datastore = e, this.options = n, this.updateFunction = r,
this.deferred = i, this.kc = n.maxAttempts, this.xo = new ga(this.asyncQueue, "transaction_retry" /* TimerId.TransactionRetry */)
/** Runs the transaction and sets the result on deferred. */;
}
return t.prototype.run = function() {
this.kc -= 1, this.Oc();
}, t.prototype.Oc = function() {
var t = this;
this.xo.Ro((function() {
return e(t, void 0, void 0, (function() {
var t, e, r = this;
return n(this, (function(n) {
return t = new xc(this.datastore), (e = this.Mc(t)) && e.then((function(e) {
r.asyncQueue.enqueueAndForget((function() {
return t.commit().then((function() {
r.deferred.resolve(e);
})).catch((function(t) {
r.Fc(t);
}));
}));
})).catch((function(t) {
r.Fc(t);
})), [ 2 /*return*/ ];
}));
}));
}));
}, t.prototype.Mc = function(t) {
try {
var e = this.updateFunction(t);
return !Qt(e) && e.catch && e.then ? e : (this.deferred.reject(Error("Transaction callback must return a Promise")),
null);
} catch (t) {
// Do not retry errors thrown by user provided updateFunction.
return this.deferred.reject(t), null;
}
}, t.prototype.Fc = function(t) {
var e = this;
this.kc > 0 && this.$c(t) ? (this.kc -= 1, this.asyncQueue.enqueueAndForget((function() {
return e.Oc(), Promise.resolve();
}))) : this.deferred.reject(t);
}, t.prototype.$c = function(t) {
if ("FirebaseError" === t.name) {
// In transactions, the backend will fail outdated reads with FAILED_PRECONDITION and
// non-matching document versions with ABORTED. These errors should be retried.
var e = t.code;
return "aborted" === e || "failed-precondition" === e || "already-exists" === e || !fr(e);
}
return !1;
}, t;
}(), Cc = /** @class */ function() {
function t(t, r,
/**
* Asynchronous queue responsible for all of our internal processing. When
* we get incoming work from the user (via public API) or the network
* (incoming GRPC messages), we should always schedule onto this queue.
* This ensures all of our work is properly serialized (e.g. we don't
* start processing a new operation while the previous one is waiting for
* an async I/O to complete).
*/
i, o) {
var u = this;
this.authCredentials = t, this.appCheckCredentials = r, this.asyncQueue = i, this.databaseInfo = o,
this.user = N.UNAUTHENTICATED, this.clientId = nt.R(), this.authCredentialListener = function() {
return Promise.resolve();
}, this.appCheckCredentialListener = function() {
return Promise.resolve();
}, this.authCredentials.start(i, (function(t) {
return e(u, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return V("FirestoreClient", "Received user=", t.uid), [ 4 /*yield*/ , this.authCredentialListener(t) ];
case 1:
return e.sent(), this.user = t, [ 2 /*return*/ ];
}
}));
}));
})), this.appCheckCredentials.start(i, (function(t) {
return V("FirestoreClient", "Received new app check token=", t), u.appCheckCredentialListener(t, u.user);
}));
}
return t.prototype.getConfiguration = function() {
return e(this, void 0, void 0, (function() {
return n(this, (function(t) {
return [ 2 /*return*/ , {
asyncQueue: this.asyncQueue,
databaseInfo: this.databaseInfo,
clientId: this.clientId,
authCredentials: this.authCredentials,
appCheckCredentials: this.appCheckCredentials,
initialUser: this.user,
maxConcurrentLimboResolutions: 100
} ];
}));
}));
}, t.prototype.setCredentialChangeListener = function(t) {
this.authCredentialListener = t;
}, t.prototype.setAppCheckTokenChangeListener = function(t) {
this.appCheckCredentialListener = t;
},
/**
* Checks that the client has not been terminated. Ensures that other methods on
* this class cannot be called after the client is terminated.
*/
t.prototype.verifyNotTerminated = function() {
if (this.asyncQueue.isShuttingDown) throw new j(K.FAILED_PRECONDITION, "The client has already been terminated.");
}, t.prototype.terminate = function() {
var t = this;
this.asyncQueue.enterRestrictedMode();
var r = new Q;
return this.asyncQueue.enqueueAndForgetEvenWhileRestricted((function() {
return e(t, void 0, void 0, (function() {
var t, e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return n.trys.push([ 0, 5, , 6 ]), this.onlineComponents ? [ 4 /*yield*/ , this.onlineComponents.terminate() ] : [ 3 /*break*/ , 2 ];
case 1:
n.sent(), n.label = 2;
case 2:
return this.offlineComponents ? [ 4 /*yield*/ , this.offlineComponents.terminate() ] : [ 3 /*break*/ , 4 ];
case 3:
n.sent(), n.label = 4;
case 4:
// The credentials provider must be terminated after shutting down the
// RemoteStore as it will prevent the RemoteStore from retrieving auth
// tokens.
return this.authCredentials.shutdown(), this.appCheckCredentials.shutdown(), r.resolve(),
[ 3 /*break*/ , 6 ];
case 5:
return t = n.sent(), e = Ja(t, "Failed to shutdown persistence"), r.reject(e), [ 3 /*break*/ , 6 ];
case 6:
return [ 2 /*return*/ ];
}
}));
}));
})), r.promise;
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A class representing a bundle.
*
* Takes a bundle stream or buffer, and presents abstractions to read bundled
* elements out of the underlying content.
*/ function Nc(t, r) {
return e(this, void 0, void 0, (function() {
var i, o, u = this;
return n(this, (function(a) {
switch (a.label) {
case 0:
return t.asyncQueue.verifyOperationInProgress(), V("FirestoreClient", "Initializing OfflineComponentProvider"),
[ 4 /*yield*/ , t.getConfiguration() ];
case 1:
return i = a.sent(), [ 4 /*yield*/ , r.initialize(i) ];
case 2:
return a.sent(), o = i.initialUser, t.setCredentialChangeListener((function(t) {
return e(u, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return o.isEqual(t) ? [ 3 /*break*/ , 2 ] : [ 4 /*yield*/ , Uu(r.localStore, t) ];
case 1:
e.sent(), o = t, e.label = 2;
case 2:
return [ 2 /*return*/ ];
}
}));
}));
})),
// When a user calls clearPersistence() in one client, all other clients
// need to be terminated to allow the delete to succeed.
r.persistence.setDatabaseDeletedListener((function() {
return t.terminate();
})), t.offlineComponents = r, [ 2 /*return*/ ];
}
}));
}));
}
function kc(t, r) {
return e(this, void 0, void 0, (function() {
var e, i;
return n(this, (function(n) {
switch (n.label) {
case 0:
return t.asyncQueue.verifyOperationInProgress(), [ 4 /*yield*/ , Fc(t) ];
case 1:
return e = n.sent(), V("FirestoreClient", "Initializing OnlineComponentProvider"),
[ 4 /*yield*/ , t.getConfiguration() ];
case 2:
return i = n.sent(), [ 4 /*yield*/ , r.initialize(e, i) ];
case 3:
return n.sent(),
// The CredentialChangeListener of the online component provider takes
// precedence over the offline component provider.
t.setCredentialChangeListener((function(t) {
return Wa(r.remoteStore, t);
})), t.setAppCheckTokenChangeListener((function(t, e) {
return Wa(r.remoteStore, e);
})), t.onlineComponents = r, [ 2 /*return*/ ];
}
}));
}));
}
function Fc(t) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return t.offlineComponents ? [ 3 /*break*/ , 2 ] : (V("FirestoreClient", "Using default OfflineComponentProvider"),
[ 4 /*yield*/ , Nc(t, new Js) ]);
case 1:
e.sent(), e.label = 2;
case 2:
return [ 2 /*return*/ , t.offlineComponents ];
}
}));
}));
}
function Oc(t) {
return e(this, void 0, void 0, (function() {
return n(this, (function(e) {
switch (e.label) {
case 0:
return t.onlineComponents ? [ 3 /*break*/ , 2 ] : (V("FirestoreClient", "Using default OnlineComponentProvider"),
[ 4 /*yield*/ , kc(t, new ec) ]);
case 1:
e.sent(), e.label = 2;
case 2:
return [ 2 /*return*/ , t.onlineComponents ];
}
}));
}));
}
function Rc(t) {
return Fc(t).then((function(t) {
return t.persistence;
}));
}
function Vc(t) {
return Fc(t).then((function(t) {
return t.localStore;
}));
}
function Mc(t) {
return Oc(t).then((function(t) {
return t.remoteStore;
}));
}
function Lc(t) {
return Oc(t).then((function(t) {
return t.syncEngine;
}));
}
function Pc(t) {
return Oc(t).then((function(t) {
return t.datastore;
}));
}
function qc(t) {
return e(this, void 0, void 0, (function() {
var e, r;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , Oc(t) ];
case 1:
return e = n.sent(), [ 2 /*return*/ , ((r = e.eventManager).onListen = bs.bind(null, e.syncEngine),
r.onUnlisten = Ts.bind(null, e.syncEngine), r) ];
}
}));
}));
}
/** Enables the network connection and re-enqueues all pending operations. */ function Uc(t, r, i) {
var o = this;
void 0 === i && (i = {});
var u = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(o, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = function(t, e, n, r, i) {
var o = new Tc({
next: function(o) {
// Remove query first before passing event to user to avoid
// user actions affecting the now stale query.
e.enqueueAndForget((function() {
return os(t, u);
}));
var a = o.docs.has(n);
!a && o.fromCache ?
// TODO(dimond): If we're online and the document doesn't
// exist then we resolve with a doc.exists set to false. If
// we're offline however, we reject the Promise in this
// case. Two options: 1) Cache the negative response from
// the server so we can deliver that even when you're
// offline 2) Actually reject the Promise in the online case
// if the document doesn't exist.
i.reject(new j(K.UNAVAILABLE, "Failed to get document because the client is offline.")) : a && o.fromCache && r && "server" === r.source ? i.reject(new j(K.UNAVAILABLE, 'Failed to get document from server. (However, this document does exist in the local cache. Run again without setting source to "server" to retrieve the cached document.)')) : i.resolve(o);
},
error: function(t) {
return i.reject(t);
}
}), u = new cs(yn(n.path), o, {
includeMetadataChanges: !0,
Nu: !0
});
return is(t, u);
}, [ 4 /*yield*/ , qc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), t.asyncQueue, r, i, u ]) ];
}
}));
}));
})), u.promise;
}
function Bc(t, r, i) {
var o = this;
void 0 === i && (i = {});
var u = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(o, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = function(t, e, n, r, i) {
var o = new Tc({
next: function(n) {
// Remove query first before passing event to user to avoid
// user actions affecting the now stale query.
e.enqueueAndForget((function() {
return os(t, u);
})), n.fromCache && "server" === r.source ? i.reject(new j(K.UNAVAILABLE, 'Failed to get documents from server. (However, these documents may exist in the local cache. Run again without setting source to "server" to retrieve the cached documents.)')) : i.resolve(n);
},
error: function(t) {
return i.reject(t);
}
}), u = new cs(n, o, {
includeMetadataChanges: !0,
Nu: !0
});
return is(t, u);
}, [ 4 /*yield*/ , qc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), t.asyncQueue, r, i, u ]) ];
}
}));
}));
})), u.promise;
}
var Gc = /** @class */ function() {
function t() {
var t = this;
// The last promise in the queue.
this.Bc = Promise.resolve(),
// A list of retryable operations. Retryable operations are run in order and
// retried with backoff.
this.Lc = [],
// Is this AsyncQueue being shut down? Once it is set to true, it will not
// be changed again.
this.qc = !1,
// Operations scheduled to be queued in the future. Operations are
// automatically removed after they are run or canceled.
this.Uc = [],
// visible for testing
this.Kc = null,
// Flag set while there's an outstanding AsyncQueue operation, used for
// assertion sanity-checks.
this.Gc = !1,
// Enabled during shutdown on Safari to prevent future access to IndexedDB.
this.Qc = !1,
// List of TimerIds to fast-forward delays for.
this.jc = [],
// Backoff timer used to schedule retries for retryable operations
this.xo = new ga(this, "async_queue_retry" /* TimerId.AsyncQueueRetry */),
// Visibility handler that triggers an immediate retry of all retryable
// operations. Meant to speed up recovery when we regain file system access
// after page comes into foreground.
this.Wc = function() {
var e = va();
e && V("AsyncQueue", "Visibility state changed to " + e.visibilityState), t.xo.Po();
};
var e = va();
e && "function" == typeof e.addEventListener && e.addEventListener("visibilitychange", this.Wc);
}
return Object.defineProperty(t.prototype, "isShuttingDown", {
get: function() {
return this.qc;
},
enumerable: !1,
configurable: !0
}),
/**
* Adds a new operation to the queue without waiting for it to complete (i.e.
* we ignore the Promise result).
*/
t.prototype.enqueueAndForget = function(t) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.enqueue(t);
}, t.prototype.enqueueAndForgetEvenWhileRestricted = function(t) {
this.zc(),
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.Hc(t);
}, t.prototype.enterRestrictedMode = function(t) {
if (!this.qc) {
this.qc = !0, this.Qc = t || !1;
var e = va();
e && "function" == typeof e.removeEventListener && e.removeEventListener("visibilitychange", this.Wc);
}
}, t.prototype.enqueue = function(t) {
var e = this;
if (this.zc(), this.qc)
// Return a Promise which never resolves.
return new Promise((function() {}));
// Create a deferred Promise that we can return to the callee. This
// allows us to return a "hanging Promise" only to the callee and still
// advance the queue even when the operation is not run.
var n = new Q;
return this.Hc((function() {
return e.qc && e.Qc ? Promise.resolve() : (t().then(n.resolve, n.reject), n.promise);
})).then((function() {
return n.promise;
}));
}, t.prototype.enqueueRetryable = function(t) {
var e = this;
this.enqueueAndForget((function() {
return e.Lc.push(t), e.Jc();
}));
},
/**
* Runs the next operation from the retryable queue. If the operation fails,
* reschedules with backoff.
*/
t.prototype.Jc = function() {
return e(this, void 0, void 0, (function() {
var t, e = this;
return n(this, (function(n) {
switch (n.label) {
case 0:
if (0 === this.Lc.length) return [ 3 /*break*/ , 5 ];
n.label = 1;
case 1:
return n.trys.push([ 1, 3, , 4 ]), [ 4 /*yield*/ , this.Lc[0]() ];
case 2:
return n.sent(), this.Lc.shift(), this.xo.reset(), [ 3 /*break*/ , 4 ];
case 3:
if (!Ft(t = n.sent())) throw t;
// Failure will be handled by AsyncQueue
return V("AsyncQueue", "Operation failed with retryable error: " + t),
[ 3 /*break*/ , 4 ];
case 4:
this.Lc.length > 0 &&
// If there are additional operations, we re-schedule `retryNextOp()`.
// This is necessary to run retryable operations that failed during
// their initial attempt since we don't know whether they are already
// enqueued. If, for example, `op1`, `op2`, `op3` are enqueued and `op1`
// needs to be re-run, we will run `op1`, `op1`, `op2` using the
// already enqueued calls to `retryNextOp()`. `op3()` will then run in the
// call scheduled here.
// Since `backoffAndRun()` cancels an existing backoff and schedules a
// new backoff on every call, there is only ever a single additional
// operation in the queue.
this.xo.Ro((function() {
return e.Jc();
})), n.label = 5;
case 5:
return [ 2 /*return*/ ];
}
}));
}));
}, t.prototype.Hc = function(t) {
var e = this, n = this.Bc.then((function() {
return e.Gc = !0, t().catch((function(t) {
e.Kc = t, e.Gc = !1;
var n =
/**
* Chrome includes Error.message in Error.stack. Other browsers do not.
* This returns expected output of message + stack when available.
* @param error - Error or FirestoreError
*/
function(t) {
var e = t.message || "";
return t.stack && (e = t.stack.includes(t.message) ? t.stack : t.message + "\n" + t.stack),
e;
}(t);
// Re-throw the error so that this.tail becomes a rejected Promise and
// all further attempts to chain (via .then) will just short-circuit
// and return the rejected Promise.
throw M("INTERNAL UNHANDLED ERROR: ", n), t;
})).then((function(t) {
return e.Gc = !1, t;
}));
}));
return this.Bc = n, n;
}, t.prototype.enqueueAfterDelay = function(t, e, n) {
var r = this;
this.zc(),
// Fast-forward delays for timerIds that have been overriden.
this.jc.indexOf(t) > -1 && (e = 0);
var i = Za.createAndSchedule(this, t, e, n, (function(t) {
return r.Yc(t);
}));
return this.Uc.push(i), i;
}, t.prototype.zc = function() {
this.Kc && q();
}, t.prototype.verifyOperationInProgress = function() {},
/**
* Waits until all currently queued tasks are finished executing. Delayed
* operations are not run.
*/
t.prototype.Xc = function() {
return e(this, void 0, void 0, (function() {
var t;
return n(this, (function(e) {
switch (e.label) {
case 0:
return [ 4 /*yield*/ , t = this.Bc ];
case 1:
e.sent(), e.label = 2;
case 2:
if (t !== this.Bc) return [ 3 /*break*/ , 0 ];
e.label = 3;
case 3:
return [ 2 /*return*/ ];
}
}));
}));
},
/**
* For Tests: Determine if a delayed operation with a particular TimerId
* exists.
*/
t.prototype.Zc = function(t) {
for (var e = 0, n = this.Uc; e < n.length; e++) {
if (n[e].timerId === t) return !0;
}
return !1;
},
/**
* For Tests: Runs some or all delayed operations early.
*
* @param lastTimerId - Delayed operations up to and including this TimerId
* will be drained. Pass TimerId.All to run all delayed operations.
* @returns a Promise that resolves once all operations have been run.
*/
t.prototype.ta = function(t) {
var e = this;
// Note that draining may generate more delayed ops, so we do that first.
return this.Xc().then((function() {
// Run ops in the same order they'd run if they ran naturally.
e.Uc.sort((function(t, e) {
return t.targetTimeMs - e.targetTimeMs;
}));
for (var n = 0, r = e.Uc; n < r.length; n++) {
var i = r[n];
if (i.skipDelay(), "all" /* TimerId.All */ !== t && i.timerId === t) break;
}
return e.Xc();
}));
},
/**
* For Tests: Skip all subsequent delays for a timer id.
*/
t.prototype.ea = function(t) {
this.jc.push(t);
},
/** Called once a DelayedOperation is run or canceled. */ t.prototype.Yc = function(t) {
// NOTE: indexOf / slice are O(n), but delayedOperations is expected to be small.
var e = this.Uc.indexOf(t);
this.Uc.splice(e, 1);
}, t;
}();
function Kc(t) {
/**
* Returns true if obj is an object and contains at least one of the specified
* methods.
*/
return function(t, e) {
if ("object" != typeof t || null === t) return !1;
for (var n = t, r = 0, i = [ "next", "error", "complete" ]; r < i.length; r++) {
var o = i[r];
if (o in n && "function" == typeof n[o]) return !0;
}
return !1;
}(t);
}
var jc = /** @class */ function() {
function t() {
this._progressObserver = {}, this._taskCompletionResolver = new Q, this._lastProgress = {
taskState: "Running",
totalBytes: 0,
totalDocuments: 0,
bytesLoaded: 0,
documentsLoaded: 0
}
/**
* Registers functions to listen to bundle loading progress events.
* @param next - Called when there is a progress update from bundle loading. Typically `next` calls occur
* each time a Firestore document is loaded from the bundle.
* @param error - Called when an error occurs during bundle loading. The task aborts after reporting the
* error, and there should be no more updates after this.
* @param complete - Called when the loading task is complete.
*/;
}
return t.prototype.onProgress = function(t, e, n) {
this._progressObserver = {
next: t,
error: e,
complete: n
};
},
/**
* Implements the `Promise.catch` interface.
*
* @param onRejected - Called when an error occurs during bundle loading.
*/
t.prototype.catch = function(t) {
return this._taskCompletionResolver.promise.catch(t);
},
/**
* Implements the `Promise.then` interface.
*
* @param onFulfilled - Called on the completion of the loading task with a final `LoadBundleTaskProgress` update.
* The update will always have its `taskState` set to `"Success"`.
* @param onRejected - Called when an error occurs during bundle loading.
*/
t.prototype.then = function(t, e) {
return this._taskCompletionResolver.promise.then(t, e);
},
/**
* Notifies all observers that bundle loading has completed, with a provided
* `LoadBundleTaskProgress` object.
*
* @private
*/
t.prototype._completeWith = function(t) {
this._updateProgress(t), this._progressObserver.complete && this._progressObserver.complete(),
this._taskCompletionResolver.resolve(t);
},
/**
* Notifies all observers that bundle loading has failed, with a provided
* `Error` as the reason.
*
* @private
*/
t.prototype._failWith = function(t) {
this._lastProgress.taskState = "Error", this._progressObserver.next && this._progressObserver.next(this._lastProgress),
this._progressObserver.error && this._progressObserver.error(t), this._taskCompletionResolver.reject(t);
},
/**
* Notifies a progress update of loading a bundle.
* @param progress - The new progress.
*
* @private
*/
t.prototype._updateProgress = function(t) {
this._lastProgress = t, this._progressObserver.next && this._progressObserver.next(t);
}, t;
}(), Qc = -1, zc = /** @class */ function(e) {
/** @hideconstructor */
function n(t, n, r, i) {
var o = this;
/**
* Whether it's a {@link Firestore} or Firestore Lite instance.
*/
return (o = e.call(this, t, n, r, i) || this).type = "firestore", o._queue = new Gc,
o._persistenceKey = (null == i ? void 0 : i.name) || "[DEFAULT]", o;
}
return t(n, e), n.prototype._terminate = function() {
return this._firestoreClient ||
// The client must be initialized to ensure that all subsequent API
// usage throws an exception.
Xc(this), this._firestoreClient.terminate();
}, n;
}(hc);
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** DOMException error code constants. */
/**
* Initializes a new instance of {@link Firestore} with the provided settings.
* Can only be called before any other function, including
* {@link (getFirestore:1)}. If the custom settings are empty, this function is
* equivalent to calling {@link (getFirestore:1)}.
*
* @param app - The {@link @firebase/app#FirebaseApp} with which the {@link Firestore} instance will
* be associated.
* @param settings - A settings object to configure the {@link Firestore} instance.
* @param databaseId - The name of database.
* @returns A newly initialized {@link Firestore} instance.
*/
function Wc(t, e, n) {
n || (n = "(default)");
var r = _getProvider(t, "firestore");
if (r.isInitialized(n)) {
var i = r.getImmediate({
identifier: n
}), o = r.getOptions(n);
if (g(o, e)) return i;
throw new j(K.FAILED_PRECONDITION, "initializeFirestore() has already been called with different options. To avoid this error, call initializeFirestore() with the same options as when it was originally called, or call getFirestore() to return the already initialized instance.");
}
if (void 0 !== e.cacheSizeBytes && -1 !== e.cacheSizeBytes && e.cacheSizeBytes < 1048576) throw new j(K.INVALID_ARGUMENT, "cacheSizeBytes must be at least 1048576");
return r.initialize({
options: e,
instanceIdentifier: n
});
}
function Hc(t, e) {
var n = "object" == typeof t ? t : a(), i = "string" == typeof t ? t : e || "(default)", o = _getProvider(n, "firestore").getImmediate({
identifier: i
});
if (!o._initialized) {
var u = w("firestore");
u && fc.apply(void 0, r([ o ], u, !1));
}
return o;
}
/**
* @internal
*/ function Yc(t) {
return t._firestoreClient || Xc(t), t._firestoreClient.verifyNotTerminated(), t._firestoreClient;
}
function Xc(t) {
var e, n = t._freezeSettings(), r = function(t, e, n, r) {
return new Ut(t, e, n, r.host, r.ssl, r.experimentalForceLongPolling, r.experimentalAutoDetectLongPolling, r.useFetchStreams);
}(t._databaseId, (null === (e = t._app) || void 0 === e ? void 0 : e.options.appId) || "", t._persistenceKey, n);
t._firestoreClient = new Cc(t._authCredentials, t._appCheckCredentials, t._queue, r);
}
/**
* Attempts to enable persistent storage, if possible.
*
* Must be called before any other functions (other than
* {@link initializeFirestore}, {@link (getFirestore:1)} or
* {@link clearIndexedDbPersistence}.
*
* If this fails, `enableIndexedDbPersistence()` will reject the promise it
* returns. Note that even after this failure, the {@link Firestore} instance will
* remain usable, however offline persistence will be disabled.
*
* There are several reasons why this can fail, which can be identified by
* the `code` on the error.
*
* * failed-precondition: The app is already open in another browser tab.
* * unimplemented: The browser is incompatible with the offline
* persistence implementation.
*
* @param firestore - The {@link Firestore} instance to enable persistence for.
* @param persistenceSettings - Optional settings object to configure
* persistence.
* @returns A `Promise` that represents successfully enabling persistent storage.
*/ function Zc(t, e) {
al(t = ac(t, zc));
var n = Yc(t), r = t._freezeSettings(), i = new ec;
return $c(n, i, new $s(i, r.cacheSizeBytes, null == e ? void 0 : e.forceOwnership));
}
/**
* Attempts to enable multi-tab persistent storage, if possible. If enabled
* across all tabs, all operations share access to local persistence, including
* shared execution of queries and latency-compensated local document updates
* across all connected instances.
*
* If this fails, `enableMultiTabIndexedDbPersistence()` will reject the promise
* it returns. Note that even after this failure, the {@link Firestore} instance will
* remain usable, however offline persistence will be disabled.
*
* There are several reasons why this can fail, which can be identified by
* the `code` on the error.
*
* * failed-precondition: The app is already open in another browser tab and
* multi-tab is not enabled.
* * unimplemented: The browser is incompatible with the offline
* persistence implementation.
*
* @param firestore - The {@link Firestore} instance to enable persistence for.
* @returns A `Promise` that represents successfully enabling persistent
* storage.
*/ function Jc(t) {
al(t = ac(t, zc));
var e = Yc(t), n = t._freezeSettings(), r = new ec;
return $c(e, r, new tc(r, n.cacheSizeBytes));
}
/**
* Registers both the `OfflineComponentProvider` and `OnlineComponentProvider`.
* If the operation fails with a recoverable error (see
* `canRecoverFromIndexedDbError()` below), the returned Promise is rejected
* but the client remains usable.
*/ function $c(t, r, i) {
var o = this, u = new Q;
return t.asyncQueue.enqueue((function() {
return e(o, void 0, void 0, (function() {
var e, o;
return n(this, (function(n) {
switch (n.label) {
case 0:
return n.trys.push([ 0, 3, , 4 ]), [ 4 /*yield*/ , Nc(t, i) ];
case 1:
return n.sent(), [ 4 /*yield*/ , kc(t, r) ];
case 2:
return n.sent(), u.resolve(), [ 3 /*break*/ , 4 ];
case 3:
if (e = n.sent(), !
/**
* Decides whether the provided error allows us to gracefully disable
* persistence (as opposed to crashing the client).
*/
function(t) {
return "FirebaseError" === t.name ? t.code === K.FAILED_PRECONDITION || t.code === K.UNIMPLEMENTED : !("undefined" != typeof DOMException && t instanceof DOMException) || (22 === t.code || 20 === t.code ||
// Firefox Private Browsing mode disables IndexedDb and returns
// INVALID_STATE for any usage.
11 === t.code);
}(o = e)) throw o;
return L("Error enabling offline persistence. Falling back to persistence disabled: " + o),
u.reject(o), [ 3 /*break*/ , 4 ];
case 4:
return [ 2 /*return*/ ];
}
}));
}));
})).then((function() {
return u.promise;
}));
}
function tl(t) {
var r = this;
if (t._initialized && !t._terminated) throw new j(K.FAILED_PRECONDITION, "Persistence can only be cleared before a Firestore instance is initialized or after it is terminated.");
var i = new Q;
return t._queue.enqueueAndForgetEvenWhileRestricted((function() {
return e(r, void 0, void 0, (function() {
var r;
return n(this, (function(o) {
switch (o.label) {
case 0:
return o.trys.push([ 0, 2, , 3 ]), [ 4 /*yield*/ , function(t) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return Ct.C() ? (e = t + "main", [ 4 /*yield*/ , Ct.delete(e) ]) : [ 2 /*return*/ , Promise.resolve() ];
case 1:
return n.sent(), [ 2 /*return*/ ];
}
}));
}));
}(Vu(t._databaseId, t._persistenceKey)) ];
case 1:
return o.sent(), i.resolve(), [ 3 /*break*/ , 3 ];
case 2:
return r = o.sent(), i.reject(r), [ 3 /*break*/ , 3 ];
case 3:
return [ 2 /*return*/ ];
}
}));
}));
})), i.promise
/**
* Waits until all currently pending writes for the active user have been
* acknowledged by the backend.
*
* The returned promise resolves immediately if there are no outstanding writes.
* Otherwise, the promise waits for all previously issued writes (including
* those written in a previous app session), but it does not wait for writes
* that were added after the function is called. If you want to wait for
* additional writes, call `waitForPendingWrites()` again.
*
* Any outstanding `waitForPendingWrites()` promises are rejected during user
* changes.
*
* @returns A `Promise` which resolves when all currently pending writes have been
* acknowledged by the backend.
*/;
}
function el(t) {
return function(t) {
var r = this, i = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(r, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = Cs, [ 4 /*yield*/ , Lc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), i ]) ];
}
}));
}));
})), i.promise;
}(Yc(t = ac(t, zc)));
}
/**
* Re-enables use of the network for this {@link Firestore} instance after a prior
* call to {@link disableNetwork}.
*
* @returns A `Promise` that is resolved once the network has been enabled.
*/ function nl(t) {
return function(t) {
var r = this;
return t.asyncQueue.enqueue((function() {
return e(r, void 0, void 0, (function() {
var e, r;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , Rc(t) ];
case 1:
return e = n.sent(), [ 4 /*yield*/ , Mc(t) ];
case 2:
return r = n.sent(), [ 2 /*return*/ , (e.setNetworkEnabled(!0), function(t) {
var e = G(t);
return e._u.delete(0 /* OfflineCause.UserDisabled */), _a(e);
}(r)) ];
}
}));
}));
}));
}
/** Disables the network connection. Pending operations will not complete. */ (Yc(t = ac(t, zc)));
}
/**
* Disables network usage for this instance. It can be re-enabled via {@link
* enableNetwork}. While the network is disabled, any snapshot listeners,
* `getDoc()` or `getDocs()` calls will return results from cache, and any write
* operations will be queued until the network is restored.
*
* @returns A `Promise` that is resolved once the network has been disabled.
*/ function rl(t) {
return function(t) {
var r = this;
return t.asyncQueue.enqueue((function() {
return e(r, void 0, void 0, (function() {
var r, i;
return n(this, (function(o) {
switch (o.label) {
case 0:
return [ 4 /*yield*/ , Rc(t) ];
case 1:
return r = o.sent(), [ 4 /*yield*/ , Mc(t) ];
case 2:
return i = o.sent(), [ 2 /*return*/ , (r.setNetworkEnabled(!1), function(t) {
return e(this, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return (e = G(t))._u.add(0 /* OfflineCause.UserDisabled */), [ 4 /*yield*/ , Da(e) ];
case 1:
return n.sent(),
// Set the OnlineState to Offline so get()s return from cache, etc.
e.gu.set("Offline" /* OnlineState.Offline */), [ 2 /*return*/ ];
}
}));
}));
}(i)) ];
}
}));
}));
}));
}
/**
* Returns a Promise that resolves when all writes that were pending at the time
* this method was called received server acknowledgement. An acknowledgement
* can be either acceptance or rejection.
*/ (Yc(t = ac(t, zc)));
}
/**
* Terminates the provided {@link Firestore} instance.
*
* After calling `terminate()` only the `clearIndexedDbPersistence()` function
* may be used. Any other function will throw a `FirestoreError`.
*
* To restart after termination, create a new instance of FirebaseFirestore with
* {@link (getFirestore:1)}.
*
* Termination does not cancel any pending writes, and any promises that are
* awaiting a response from the server will not be resolved. If you have
* persistence enabled, the next time you start this instance, it will resume
* sending these writes to the server.
*
* Note: Under normal circumstances, calling `terminate()` is not required. This
* function is useful only when you want to force this instance to release all
* of its resources or in combination with `clearIndexedDbPersistence()` to
* ensure that all local state is destroyed between test runs.
*
* @returns A `Promise` that is resolved when the instance has been successfully
* terminated.
*/ function il(t) {
return s(t.app, "firestore", t._databaseId.database), t._delete()
/**
* Loads a Firestore bundle into the local cache.
*
* @param firestore - The {@link Firestore} instance to load bundles for.
* @param bundleData - An object representing the bundle to be loaded. Valid
* objects are `ArrayBuffer`, `ReadableStream` or `string`.
*
* @returns A `LoadBundleTask` object, which notifies callers with progress
* updates, and completion or error events. It can be used as a
* `Promise`.
*/;
}
function ol(t, r) {
var i = Yc(t = ac(t, zc)), o = new jc;
/**
* Takes an updateFunction in which a set of reads and writes can be performed
* atomically. In the updateFunction, the client can read and write values
* using the supplied transaction object. After the updateFunction, all
* changes will be committed. If a retryable error occurs (ex: some other
* client has changed any of the data referenced), then the updateFunction
* will be called again after a backoff. If the updateFunction still fails
* after all retries, then the transaction will be rejected.
*
* The transaction object passed to the updateFunction contains methods for
* accessing documents and collections. Unlike other datastore access, data
* accessed with the transaction will not reflect local changes that have not
* been committed. For this reason, it is required that all reads are
* performed before any writes. Transactions must be performed while online.
*/
return function(t, r, i, o) {
var u = this, a = function(t, e) {
return function(t, e) {
return new Ec(t, e);
}(function(t, e) {
if (t instanceof Uint8Array) return Ic(t, e);
if (t instanceof ArrayBuffer) return Ic(new Uint8Array(t), e);
if (t instanceof ReadableStream) return t.getReader();
throw new Error("Source of `toByteStreamReader` has to be a ArrayBuffer or ReadableStream");
}("string" == typeof t ? (new TextEncoder).encode(t) : t), e);
}(i, ma(r));
t.asyncQueue.enqueueAndForget((function() {
return e(u, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = Zs, [ 4 /*yield*/ , Lc(t) ];
case 1:
return e.apply(void 0, [ n.sent(), a, o ]), [ 2 /*return*/ ];
}
}));
}));
}));
}(i, t._databaseId, r, o), o
/**
* Reads a Firestore {@link Query} from local cache, identified by the given
* name.
*
* The named queries are packaged into bundles on the server side (along
* with resulting documents), and loaded to local cache using `loadBundle`. Once
* in local cache, use this method to extract a {@link Query} by name.
*
* @param firestore - The {@link Firestore} instance to read the query from.
* @param name - The name of the query.
* @returns A `Promise` that is resolved with the Query or `null`.
*/;
}
function ul(t, r) {
return function(t, r) {
var i = this;
return t.asyncQueue.enqueue((function() {
return e(i, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = function(t, e) {
var n = G(t);
return n.persistence.runTransaction("Get named query", "readonly", (function(t) {
return n.Ns.getNamedQuery(t, e);
}));
}, [ 4 /*yield*/ , Vc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), r ]) ];
}
}));
}));
}));
}(Yc(t = ac(t, zc)), r).then((function(e) {
return e ? new pc(t, null, e.query) : null;
}));
}
function al(t) {
if (t._initialized || t._terminated) throw new j(K.FAILED_PRECONDITION, "Firestore has already been started and persistence can no longer be enabled. You can only enable persistence before calling any other methods on a Firestore object.");
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* An immutable object representing an array of bytes.
*/ var sl = /** @class */ function() {
/** @hideconstructor */
function t(t) {
this._byteString = t;
}
/**
* Creates a new `Bytes` object from the given Base64 string, converting it to
* bytes.
*
* @param base64 - The Base64 string used to create the `Bytes` object.
*/ return t.fromBase64String = function(e) {
try {
return new t(Yt.fromBase64String(e));
} catch (e) {
throw new j(K.INVALID_ARGUMENT, "Failed to construct data from Base64 string: " + e);
}
},
/**
* Creates a new `Bytes` object from the given Uint8Array.
*
* @param array - The Uint8Array used to create the `Bytes` object.
*/
t.fromUint8Array = function(e) {
return new t(Yt.fromUint8Array(e));
},
/**
* Returns the underlying bytes as a Base64-encoded string.
*
* @returns The Base64-encoded string created from the `Bytes` object.
*/
t.prototype.toBase64 = function() {
return this._byteString.toBase64();
},
/**
* Returns the underlying bytes in a new `Uint8Array`.
*
* @returns The Uint8Array created from the `Bytes` object.
*/
t.prototype.toUint8Array = function() {
return this._byteString.toUint8Array();
},
/**
* Returns a string representation of the `Bytes` object.
*
* @returns A string representation of the `Bytes` object.
*/
t.prototype.toString = function() {
return "Bytes(base64: " + this.toBase64() + ")";
},
/**
* Returns true if this `Bytes` object is equal to the provided one.
*
* @param other - The `Bytes` object to compare against.
* @returns true if this `Bytes` object is equal to the provided one.
*/
t.prototype.isEqual = function(t) {
return this._byteString.isEqual(t._byteString);
}, t;
}(), cl = /** @class */ function() {
/**
* Creates a `FieldPath` from the provided field names. If more than one field
* name is provided, the path will point to a nested field in a document.
*
* @param fieldNames - A list of field names.
*/
function t() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
for (var n = 0; n < t.length; ++n) if (0 === t[n].length) throw new j(K.INVALID_ARGUMENT, "Invalid field name at argument $(i + 1). Field names must not be empty.");
this._internalPath = new ht(t);
}
/**
* Returns true if this `FieldPath` is equal to the provided one.
*
* @param other - The `FieldPath` to compare against.
* @returns true if this `FieldPath` is equal to the provided one.
*/ return t.prototype.isEqual = function(t) {
return this._internalPath.isEqual(t._internalPath);
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A `FieldPath` refers to a field in a document. The path may consist of a
* single field name (referring to a top-level field in the document), or a
* list of field names (referring to a nested field in the document).
*
* Create a `FieldPath` by providing field names. If more than one field
* name is provided, the path will point to a nested field in a document.
*/
/**
* Returns a special sentinel `FieldPath` to refer to the ID of a document.
* It can be used in queries to sort or filter by the document ID.
*/
function ll() {
return new cl("__name__");
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Sentinel values that can be used when writing document fields with `set()`
* or `update()`.
*/ var hl =
/**
* @param _methodName - The public API endpoint that returns this class.
* @hideconstructor
*/
function(t) {
this._methodName = t;
}, fl = /** @class */ function() {
/**
* Creates a new immutable `GeoPoint` object with the provided latitude and
* longitude values.
* @param latitude - The latitude as number between -90 and 90.
* @param longitude - The longitude as number between -180 and 180.
*/
function t(t, e) {
if (!isFinite(t) || t < -90 || t > 90) throw new j(K.INVALID_ARGUMENT, "Latitude must be a number between -90 and 90, but was: " + t);
if (!isFinite(e) || e < -180 || e > 180) throw new j(K.INVALID_ARGUMENT, "Longitude must be a number between -180 and 180, but was: " + e);
this._lat = t, this._long = e;
}
return Object.defineProperty(t.prototype, "latitude", {
/**
* The latitude of this `GeoPoint` instance.
*/
get: function() {
return this._lat;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "longitude", {
/**
* The longitude of this `GeoPoint` instance.
*/
get: function() {
return this._long;
},
enumerable: !1,
configurable: !0
}),
/**
* Returns true if this `GeoPoint` is equal to the provided one.
*
* @param other - The `GeoPoint` to compare against.
* @returns true if this `GeoPoint` is equal to the provided one.
*/
t.prototype.isEqual = function(t) {
return this._lat === t._lat && this._long === t._long;
},
/** Returns a JSON-serializable representation of this GeoPoint. */ t.prototype.toJSON = function() {
return {
latitude: this._lat,
longitude: this._long
};
},
/**
* Actually private to JS consumers of our API, so this function is prefixed
* with an underscore.
*/
t.prototype._compareTo = function(t) {
return rt(this._lat, t._lat) || rt(this._long, t._long);
}, t;
}(), dl = /^__.*__$/, pl = /** @class */ function() {
function t(t, e, n) {
this.data = t, this.fieldMask = e, this.fieldTransforms = n;
}
return t.prototype.toMutation = function(t, e) {
return null !== this.fieldMask ? new rr(t, this.data, this.fieldMask, e, this.fieldTransforms) : new nr(t, this.data, e, this.fieldTransforms);
}, t;
}(), yl = /** @class */ function() {
function t(t,
// The fieldMask does not include document transforms.
e, n) {
this.data = t, this.fieldMask = e, this.fieldTransforms = n;
}
return t.prototype.toMutation = function(t, e) {
return new rr(t, this.data, this.fieldMask, e, this.fieldTransforms);
}, t;
}();
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* An immutable object representing a geographic location in Firestore. The
* location is represented as latitude/longitude pair.
*
* Latitude values are in the range of [-90, 90].
* Longitude values are in the range of [-180, 180].
*/ function vl(t) {
switch (t) {
case 0 /* UserDataSource.Set */ :
// fall through
case 2 /* UserDataSource.MergeSet */ :
// fall through
case 1 /* UserDataSource.Update */ :
return !0;
case 3 /* UserDataSource.Argument */ :
case 4 /* UserDataSource.ArrayArgument */ :
return !1;
default:
throw q();
}
}
/** A "context" object passed around while parsing user data. */ var ml = /** @class */ function() {
/**
* Initializes a ParseContext with the given source and path.
*
* @param settings - The settings for the parser.
* @param databaseId - The database ID of the Firestore instance.
* @param serializer - The serializer to use to generate the Value proto.
* @param ignoreUndefinedProperties - Whether to ignore undefined properties
* rather than throw.
* @param fieldTransforms - A mutable list of field transforms encountered
* while parsing the data.
* @param fieldMask - A mutable list of field paths encountered while parsing
* the data.
*
* TODO(b/34871131): We don't support array paths right now, so path can be
* null to indicate the context represents any location within an array (in
* which case certain features will not work and errors will be somewhat
* compromised).
*/
function t(t, e, n, r, i, o) {
this.settings = t, this.databaseId = e, this.yt = n, this.ignoreUndefinedProperties = r,
// Minor hack: If fieldTransforms is undefined, we assume this is an
// external call and we need to validate the entire path.
void 0 === i && this.na(), this.fieldTransforms = i || [], this.fieldMask = o || [];
}
return Object.defineProperty(t.prototype, "path", {
get: function() {
return this.settings.path;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "sa", {
get: function() {
return this.settings.sa;
},
enumerable: !1,
configurable: !0
}),
/** Returns a new context with the specified settings overwritten. */ t.prototype.ia = function(e) {
return new t(Object.assign(Object.assign({}, this.settings), e), this.databaseId, this.yt, this.ignoreUndefinedProperties, this.fieldTransforms, this.fieldMask);
}, t.prototype.ra = function(t) {
var e, n = null === (e = this.path) || void 0 === e ? void 0 : e.child(t), r = this.ia({
path: n,
oa: !1
});
return r.ua(t), r;
}, t.prototype.ca = function(t) {
var e, n = null === (e = this.path) || void 0 === e ? void 0 : e.child(t), r = this.ia({
path: n,
oa: !1
});
return r.na(), r;
}, t.prototype.aa = function(t) {
// TODO(b/34871131): We don't support array paths right now; so make path
// undefined.
return this.ia({
path: void 0,
oa: !0
});
}, t.prototype.ha = function(t) {
return Ll(t, this.settings.methodName, this.settings.la || !1, this.path, this.settings.fa);
},
/** Returns 'true' if 'fieldPath' was traversed when creating this context. */ t.prototype.contains = function(t) {
return void 0 !== this.fieldMask.find((function(e) {
return t.isPrefixOf(e);
})) || void 0 !== this.fieldTransforms.find((function(e) {
return t.isPrefixOf(e.field);
}));
}, t.prototype.na = function() {
// TODO(b/34871131): Remove null check once we have proper paths for fields
// within arrays.
if (this.path) for (var t = 0; t < this.path.length; t++) this.ua(this.path.get(t));
}, t.prototype.ua = function(t) {
if (0 === t.length) throw this.ha("Document fields must not be empty");
if (vl(this.sa) && dl.test(t)) throw this.ha('Document fields cannot begin and end with "__"');
}, t;
}(), gl = /** @class */ function() {
function t(t, e, n) {
this.databaseId = t, this.ignoreUndefinedProperties = e, this.yt = n || ma(t)
/** Creates a new top-level parse context. */;
}
return t.prototype.da = function(t, e, n, r) {
return void 0 === r && (r = !1), new ml({
sa: t,
methodName: e,
fa: n,
path: ht.emptyPath(),
oa: !1,
la: r
}, this.databaseId, this.yt, this.ignoreUndefinedProperties);
}, t;
}();
/**
* Helper for parsing raw user input (provided via the API) into internal model
* classes.
*/ function wl(t) {
var e = t._freezeSettings(), n = ma(t._databaseId);
return new gl(t._databaseId, !!e.ignoreUndefinedProperties, n);
}
/** Parse document data from a set() call. */ function bl(t, e, n, r, i, o) {
void 0 === o && (o = {});
var u = t.da(o.merge || o.mergeFields ? 2 /* UserDataSource.MergeSet */ : 0 /* UserDataSource.Set */ , e, n, i);
Ol("Data must be an object, but it was:", u, r);
var a, s, c = kl(r, u);
if (o.merge) a = new tn(u.fieldMask), s = u.fieldTransforms; else if (o.mergeFields) {
for (var l = [], h = 0, f = o.mergeFields; h < f.length; h++) {
var d = Rl(e, f[h], n);
if (!u.contains(d)) throw new j(K.INVALID_ARGUMENT, "Field '".concat(d, "' is specified in your field mask but missing from your input data."));
Pl(l, d) || l.push(d);
}
a = new tn(l), s = u.fieldTransforms.filter((function(t) {
return a.covers(t.field);
}));
} else a = null, s = u.fieldTransforms;
return new pl(new en(c), a, s);
}
var Il = /** @class */ function(e) {
function n() {
return null !== e && e.apply(this, arguments) || this;
}
return t(n, e), n.prototype._toFieldTransform = function(t) {
if (2 /* UserDataSource.MergeSet */ !== t.sa) throw 1 /* UserDataSource.Update */ === t.sa ? t.ha("".concat(this._methodName, "() can only appear at the top level of your update data")) : t.ha("".concat(this._methodName, "() cannot be used with set() unless you pass {merge:true}"));
// No transform to add for a delete, but we need to add it to our
// fieldMask so it gets deleted.
return t.fieldMask.push(t.path), null;
}, n.prototype.isEqual = function(t) {
return t instanceof n;
}, n;
}(hl);
/**
* Creates a child context for parsing SerializableFieldValues.
*
* This is different than calling `ParseContext.contextWith` because it keeps
* the fieldTransforms and fieldMask separate.
*
* The created context has its `dataSource` set to `UserDataSource.Argument`.
* Although these values are used with writes, any elements in these FieldValues
* are not considered writes since they cannot contain any FieldValue sentinels,
* etc.
*
* @param fieldValue - The sentinel FieldValue for which to create a child
* context.
* @param context - The parent context.
* @param arrayElement - Whether or not the FieldValue has an array.
*/ function Tl(t, e, n) {
return new ml({
sa: 3 /* UserDataSource.Argument */ ,
fa: e.settings.fa,
methodName: t._methodName,
oa: n
}, e.databaseId, e.yt, e.ignoreUndefinedProperties);
}
var El = /** @class */ function(e) {
function n() {
return null !== e && e.apply(this, arguments) || this;
}
return t(n, e), n.prototype._toFieldTransform = function(t) {
return new zn(t.path, new Pn);
}, n.prototype.isEqual = function(t) {
return t instanceof n;
}, n;
}(hl), Sl = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this, t) || this)._a = n, r;
}
return t(n, e), n.prototype._toFieldTransform = function(t) {
var e = Tl(this, t,
/*array=*/ !0), n = this._a.map((function(t) {
return Nl(t, e);
})), r = new qn(n);
return new zn(t.path, r);
}, n.prototype.isEqual = function(t) {
// TODO(mrschmidt): Implement isEquals
return this === t;
}, n;
}(hl), _l = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this, t) || this)._a = n, r;
}
return t(n, e), n.prototype._toFieldTransform = function(t) {
var e = Tl(this, t,
/*array=*/ !0), n = this._a.map((function(t) {
return Nl(t, e);
})), r = new Bn(n);
return new zn(t.path, r);
}, n.prototype.isEqual = function(t) {
// TODO(mrschmidt): Implement isEquals
return this === t;
}, n;
}(hl), Dl = /** @class */ function(e) {
function n(t, n) {
var r = this;
return (r = e.call(this, t) || this).wa = n, r;
}
return t(n, e), n.prototype._toFieldTransform = function(t) {
var e = new Kn(t.yt, On(t.yt, this.wa));
return new zn(t.path, e);
}, n.prototype.isEqual = function(t) {
// TODO(mrschmidt): Implement isEquals
return this === t;
}, n;
}(hl);
/** Parse update data from an update() call. */ function xl(t, e, n, r) {
var i = t.da(1 /* UserDataSource.Update */ , e, n);
Ol("Data must be an object, but it was:", i, r);
var o = [], u = en.empty();
Kt(r, (function(t, r) {
var a = Ml(e, t, n);
// For Compat types, we have to "extract" the underlying types before
// performing validation.
r = y(r);
var s = i.ca(a);
if (r instanceof Il)
// Add it to the field mask, but don't add anything to updateData.
o.push(a); else {
var c = Nl(r, s);
null != c && (o.push(a), u.set(a, c));
}
}));
var a = new tn(o);
return new yl(u, a, i.fieldTransforms);
}
/** Parse update data from a list of field/value arguments. */ function Al(t, e, n, r, i, o) {
var u = t.da(1 /* UserDataSource.Update */ , e, n), a = [ Rl(e, r, n) ], s = [ i ];
if (o.length % 2 != 0) throw new j(K.INVALID_ARGUMENT, "Function ".concat(e, "() needs to be called with an even number of arguments that alternate between field names and values."));
for (var c = 0; c < o.length; c += 2) a.push(Rl(e, o[c])), s.push(o[c + 1]);
// We iterate in reverse order to pick the last value for a field if the
// user specified the field multiple times.
for (var l = [], h = en.empty(), f = a.length - 1; f >= 0; --f) if (!Pl(l, a[f])) {
var d = a[f], p = s[f];
// For Compat types, we have to "extract" the underlying types before
// performing validation.
p = y(p);
var v = u.ca(d);
if (p instanceof Il)
// Add it to the field mask, but don't add anything to updateData.
l.push(d); else {
var m = Nl(p, v);
null != m && (l.push(d), h.set(d, m));
}
}
var g = new tn(l);
return new yl(h, g, u.fieldTransforms);
}
/**
* Parse a "query value" (e.g. value in a where filter or a value in a cursor
* bound).
*
* @param allowArrays - Whether the query value is an array that may directly
* contain additional arrays (e.g. the operand of an `in` query).
*/ function Cl(t, e, n, r) {
return void 0 === r && (r = !1), Nl(n, t.da(r ? 4 /* UserDataSource.ArrayArgument */ : 3 /* UserDataSource.Argument */ , e));
}
/**
* Parses user data to Protobuf Values.
*
* @param input - Data to be parsed.
* @param context - A context object representing the current path being parsed,
* the source of the data being parsed, etc.
* @returns The parsed value, or null if the value was a FieldValue sentinel
* that should not be included in the resulting parsed data.
*/ function Nl(t, e) {
if (Fl(
// Unwrap the API type from the Compat SDK. This will return the API type
// from firestore-exp.
t = y(t))) return Ol("Unsupported field value:", e, t), kl(t, e);
if (t instanceof hl)
// FieldValues usually parse into transforms (except deleteField())
// in which case we do not want to include this field in our parsed data
// (as doing so will overwrite the field directly prior to the transform
// trying to transform it). So we don't add this location to
// context.fieldMask and we return null as our parsing result.
/**
* "Parses" the provided FieldValueImpl, adding any necessary transforms to
* context.fieldTransforms.
*/
return function(t, e) {
// Sentinels are only supported with writes, and not within arrays.
if (!vl(e.sa)) throw e.ha("".concat(t._methodName, "() can only be used with update() and set()"));
if (!e.path) throw e.ha("".concat(t._methodName, "() is not currently supported inside arrays"));
var n = t._toFieldTransform(e);
n && e.fieldTransforms.push(n);
}(t, e), null;
if (void 0 === t && e.ignoreUndefinedProperties)
// If the input is undefined it can never participate in the fieldMask, so
// don't handle this below. If `ignoreUndefinedProperties` is false,
// `parseScalarValue` will reject an undefined value.
return null;
if (
// If context.path is null we are inside an array and we don't support
// field mask paths more granular than the top-level array.
e.path && e.fieldMask.push(e.path), t instanceof Array) {
// TODO(b/34871131): Include the path containing the array in the error
// message.
// In the case of IN queries, the parsed data is an array (representing
// the set of values to be included for the IN query) that may directly
// contain additional arrays (each representing an individual field
// value), so we disable this validation.
if (e.settings.oa && 4 /* UserDataSource.ArrayArgument */ !== e.sa) throw e.ha("Nested arrays are not supported");
return function(t, e) {
for (var n = [], r = 0, i = 0, o = t; i < o.length; i++) {
var u = Nl(o[i], e.aa(r));
null == u && (
// Just include nulls in the array for fields being replaced with a
// sentinel.
u = {
nullValue: "NULL_VALUE"
}), n.push(u), r++;
}
return {
arrayValue: {
values: n
}
};
}(t, e);
}
return function(t, e) {
if (null === (t = y(t))) return {
nullValue: "NULL_VALUE"
};
if ("number" == typeof t) return On(e.yt, t);
if ("boolean" == typeof t) return {
booleanValue: t
};
if ("string" == typeof t) return {
stringValue: t
};
if (t instanceof Date) {
var n = ut.fromDate(t);
return {
timestampValue: Br(e.yt, n)
};
}
if (t instanceof ut) {
// Firestore backend truncates precision down to microseconds. To ensure
// offline mode works the same with regards to truncation, perform the
// truncation immediately without waiting for the backend to do that.
var r = new ut(t.seconds, 1e3 * Math.floor(t.nanoseconds / 1e3));
return {
timestampValue: Br(e.yt, r)
};
}
if (t instanceof fl) return {
geoPointValue: {
latitude: t.latitude,
longitude: t.longitude
}
};
if (t instanceof sl) return {
bytesValue: Gr(e.yt, t._byteString)
};
if (t instanceof dc) {
var i = e.databaseId, o = t.firestore._databaseId;
if (!o.isEqual(i)) throw e.ha("Document reference is for database ".concat(o.projectId, "/").concat(o.database, " but should be for database ").concat(i.projectId, "/").concat(i.database));
return {
referenceValue: Qr(t.firestore._databaseId || e.databaseId, t._key.path)
};
}
throw e.ha("Unsupported field value: ".concat(uc(t)));
}(t, e);
}
function kl(t, e) {
var n = {};
return jt(t) ?
// If we encounter an empty object, we explicitly add it to the update
// mask to ensure that the server creates a map entry.
e.path && e.path.length > 0 && e.fieldMask.push(e.path) : Kt(t, (function(t, r) {
var i = Nl(r, e.ra(t));
null != i && (n[t] = i);
})), {
mapValue: {
fields: n
}
};
}
function Fl(t) {
return !("object" != typeof t || null === t || t instanceof Array || t instanceof Date || t instanceof ut || t instanceof fl || t instanceof sl || t instanceof dc || t instanceof hl);
}
function Ol(t, e, n) {
if (!Fl(n) || !function(t) {
return "object" == typeof t && null !== t && (Object.getPrototypeOf(t) === Object.prototype || null === Object.getPrototypeOf(t));
}(n)) {
var r = uc(n);
throw "an object" === r ? e.ha(t + " a custom object") : e.ha(t + " " + r);
}
}
/**
* Helper that calls fromDotSeparatedString() but wraps any error thrown.
*/ function Rl(t, e, n) {
if (
// If required, replace the FieldPath Compat class with with the firestore-exp
// FieldPath.
(e = y(e)) instanceof cl) return e._internalPath;
if ("string" == typeof e) return Ml(t, e);
throw Ll("Field path arguments must be of type string or ", t,
/* hasConverter= */ !1,
/* path= */ void 0, n);
}
/**
* Matches any characters in a field path string that are reserved.
*/ var Vl = new RegExp("[~\\*/\\[\\]]");
/**
* Wraps fromDotSeparatedString with an error message about the method that
* was thrown.
* @param methodName - The publicly visible method name
* @param path - The dot-separated string form of a field path which will be
* split on dots.
* @param targetDoc - The document against which the field path will be
* evaluated.
*/ function Ml(t, e, n) {
if (e.search(Vl) >= 0) throw Ll("Invalid field path (".concat(e, "). Paths must not contain '~', '*', '/', '[', or ']'"), t,
/* hasConverter= */ !1,
/* path= */ void 0, n);
try {
return (new (cl.bind.apply(cl, r([ void 0 ], e.split("."), !1))))._internalPath;
} catch (r) {
throw Ll("Invalid field path (".concat(e, "). Paths must not be empty, begin with '.', end with '.', or contain '..'"), t,
/* hasConverter= */ !1,
/* path= */ void 0, n);
}
}
function Ll(t, e, n, r, i) {
var o = r && !r.isEmpty(), u = void 0 !== i, a = "Function ".concat(e, "() called with invalid data");
n && (a += " (via `toFirestore()`)"), a += ". ";
var s = "";
return (o || u) && (s += " (found", o && (s += " in field ".concat(r)), u && (s += " in document ".concat(i)),
s += ")"), new j(K.INVALID_ARGUMENT, a + t + s)
/** Checks `haystack` if FieldPath `needle` is present. Runs in O(n). */;
}
function Pl(t, e) {
return t.some((function(t) {
return t.isEqual(e);
}));
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A `DocumentSnapshot` contains data read from a document in your Firestore
* database. The data can be extracted with `.data()` or `.get()` to
* get a specific field.
*
* For a `DocumentSnapshot` that points to a non-existing document, any data
* access will return 'undefined'. You can use the `exists()` method to
* explicitly verify a document's existence.
*/ var ql = /** @class */ function() {
// Note: This class is stripped down version of the DocumentSnapshot in
// the legacy SDK. The changes are:
// - No support for SnapshotMetadata.
// - No support for SnapshotOptions.
/** @hideconstructor protected */
function t(t, e, n, r, i) {
this._firestore = t, this._userDataWriter = e, this._key = n, this._document = r,
this._converter = i;
}
return Object.defineProperty(t.prototype, "id", {
/** Property of the `DocumentSnapshot` that provides the document's ID. */ get: function() {
return this._key.path.lastSegment();
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "ref", {
/**
* The `DocumentReference` for the document included in the `DocumentSnapshot`.
*/
get: function() {
return new dc(this._firestore, this._converter, this._key);
},
enumerable: !1,
configurable: !0
}),
/**
* Signals whether or not the document at the snapshot's location exists.
*
* @returns true if the document exists.
*/
t.prototype.exists = function() {
return null !== this._document;
},
/**
* Retrieves all fields in the document as an `Object`. Returns `undefined` if
* the document doesn't exist.
*
* @returns An `Object` containing all fields in the document or `undefined`
* if the document doesn't exist.
*/
t.prototype.data = function() {
if (this._document) {
if (this._converter) {
// We only want to use the converter and create a new DocumentSnapshot
// if a converter has been provided.
var t = new Ul(this._firestore, this._userDataWriter, this._key, this._document,
/* converter= */ null);
return this._converter.fromFirestore(t);
}
return this._userDataWriter.convertValue(this._document.data.value);
}
},
/**
* Retrieves the field specified by `fieldPath`. Returns `undefined` if the
* document or field doesn't exist.
*
* @param fieldPath - The path (for example 'foo' or 'foo.bar') to a specific
* field.
* @returns The data at the specified field location or undefined if no such
* field exists in the document.
*/
// We are using `any` here to avoid an explicit cast by our users.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
t.prototype.get = function(t) {
if (this._document) {
var e = this._document.data.field(Bl("DocumentSnapshot.get", t));
if (null !== e) return this._userDataWriter.convertValue(e);
}
}, t;
}(), Ul = /** @class */ function(e) {
function n() {
return null !== e && e.apply(this, arguments) || this;
}
/**
* Retrieves all fields in the document as an `Object`.
*
* @override
* @returns An `Object` containing all fields in the document.
*/ return t(n, e), n.prototype.data = function() {
return e.prototype.data.call(this);
}, n;
}(ql);
/**
* A `QueryDocumentSnapshot` contains data read from a document in your
* Firestore database as part of a query. The document is guaranteed to exist
* and its data can be extracted with `.data()` or `.get()` to get a
* specific field.
*
* A `QueryDocumentSnapshot` offers the same API surface as a
* `DocumentSnapshot`. Since query results contain only existing documents, the
* `exists` property will always be true and `data()` will never return
* 'undefined'.
*/
/**
* Helper that calls `fromDotSeparatedString()` but wraps any error thrown.
*/
function Bl(t, e) {
return "string" == typeof e ? Ml(t, e) : e instanceof cl ? e._internalPath : e._delegate._internalPath;
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ function Gl(t) {
if ("L" /* LimitType.Last */ === t.limitType && 0 === t.explicitOrderBy.length) throw new j(K.UNIMPLEMENTED, "limitToLast() queries require specifying at least one orderBy() clause");
}
/**
* An `AppliableConstraint` is an abstraction of a constraint that can be applied
* to a Firestore query.
*/ var Kl = function() {}, jl = /** @class */ function(e) {
function n() {
return null !== e && e.apply(this, arguments) || this;
}
return t(n, e), n;
}(Kl);
/**
* A `QueryConstraint` is used to narrow the set of documents returned by a
* Firestore query. `QueryConstraint`s are created by invoking {@link where},
* {@link orderBy}, {@link startAt}, {@link startAfter}, {@link
* endBefore}, {@link endAt}, {@link limit}, {@link limitToLast} and
* can then be passed to {@link query} to create a new query instance that
* also contains this `QueryConstraint`.
*/ function Ql(t, e) {
for (var n = [], r = 2; r < arguments.length; r++) n[r - 2] = arguments[r];
var i = [];
e instanceof Kl && i.push(e), function(t) {
var e = t.filter((function(t) {
return t instanceof Hl;
})).length, n = t.filter((function(t) {
return t instanceof zl;
})).length;
if (e > 1 || e > 0 && n > 0) throw new j(K.INVALID_ARGUMENT, "InvalidQuery. When using composite filters, you cannot use more than one filter at the top level. Consider nesting the multiple filters within an `and(...)` statement. For example: change `query(query, where(...), or(...))` to `query(query, and(where(...), or(...)))`.");
}(i = i.concat(n));
for (var o = 0, u = i; o < u.length; o++) {
var a = u[o];
t = a._apply(t);
}
return t;
}
/**
* A `QueryFieldFilterConstraint` is used to narrow the set of documents returned by
* a Firestore query by filtering on one or more document fields.
* `QueryFieldFilterConstraint`s are created by invoking {@link where} and can then
* be passed to {@link query} to create a new query instance that also contains
* this `QueryFieldFilterConstraint`.
*/ var zl = /** @class */ function(e) {
/**
* @internal
*/
function n(t, n, r) {
var i = this;
return (i = e.call(this) || this)._field = t, i._op = n, i._value = r,
/** The type of this query constraint */
i.type = "where", i;
}
return t(n, e), n._create = function(t, e, r) {
return new n(t, e, r);
}, n.prototype._apply = function(t) {
var e = this._parse(t);
return hh(t._query, e), new pc(t.firestore, t.converter, Tn(t._query, e));
}, n.prototype._parse = function(t) {
var e = wl(t.firestore), n = function(t, e, n, r, i, o, u) {
var a;
if (i.isKeyField()) {
if ("array-contains" /* Operator.ARRAY_CONTAINS */ === o || "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ === o) throw new j(K.INVALID_ARGUMENT, "Invalid Query. You can't perform '".concat(o, "' queries on documentId()."));
if ("in" /* Operator.IN */ === o || "not-in" /* Operator.NOT_IN */ === o) {
lh(u, o);
for (var s = [], c = 0, l = u; c < l.length; c++) {
var h = l[c];
s.push(ch(r, t, h));
}
a = {
arrayValue: {
values: s
}
};
} else a = ch(r, t, u);
} else "in" /* Operator.IN */ !== o && "not-in" /* Operator.NOT_IN */ !== o && "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ !== o || lh(u, o),
a = Cl(n, "where", u,
/* allowArrays= */ "in" /* Operator.IN */ === o || "not-in" /* Operator.NOT_IN */ === o);
return Ae.create(i, o, a);
}(t._query, 0, e, t.firestore._databaseId, this._field, this._op, this._value);
return n;
}, n;
}(jl);
/**
* Creates a {@link QueryFieldFilterConstraint} that enforces that documents
* must contain the specified field and that the value should satisfy the
* relation constraint provided.
*
* @param fieldPath - The path to compare
* @param opStr - The operation string (e.g "<", "<=", "==", "<",
* "<=", "!=").
* @param value - The value for comparison
* @returns The created {@link QueryFieldFilterConstraint}.
*/ function Wl(t, e, n) {
var r = e, i = Bl("where", t);
return zl._create(i, r, n);
}
/**
* A `QueryCompositeFilterConstraint` is used to narrow the set of documents
* returned by a Firestore query by performing the logical OR or AND of multiple
* {@link QueryFieldFilterConstraint}s or {@link QueryCompositeFilterConstraint}s.
* `QueryCompositeFilterConstraint`s are created by invoking {@link or} or
* {@link and} and can then be passed to {@link query} to create a new query
* instance that also contains the `QueryCompositeFilterConstraint`.
* @internal TODO remove this internal tag with OR Query support in the server
*/ var Hl = /** @class */ function(e) {
/**
* @internal
*/
function n(
/** The type of this query constraint */
t, n) {
var r = this;
return (r = e.call(this) || this).type = t, r._queryConstraints = n, r;
}
return t(n, e), n._create = function(t, e) {
return new n(t, e);
}, n.prototype._parse = function(t) {
var e = this._queryConstraints.map((function(e) {
return e._parse(t);
})).filter((function(t) {
return t.getFilters().length > 0;
}));
return 1 === e.length ? e[0] : Ce.create(e, this._getOperator());
}, n.prototype._apply = function(t) {
var e = this._parse(t);
return 0 === e.getFilters().length ? t : (function(t, e) {
for (var n = t, r = 0, i = e.getFlattenedFilters(); r < i.length; r++) {
var o = i[r];
hh(n, o), n = Tn(n, o);
}
}(t._query, e), new pc(t.firestore, t.converter, Tn(t._query, e)));
}, n.prototype._getQueryConstraints = function() {
return this._queryConstraints;
}, n.prototype._getOperator = function() {
return "and" === this.type ? "and" /* CompositeOperator.AND */ : "or" /* CompositeOperator.OR */;
}, n;
}(Kl);
/**
* Creates a new {@link QueryCompositeFilterConstraint} that is a disjunction of
* the given filter constraints. A disjunction filter includes a document if it
* satisfies any of the given filters.
*
* @param queryConstraints - Optional. The list of
* {@link QueryFilterConstraint}s to perform a disjunction for. These must be
* created with calls to {@link where}, {@link or}, or {@link and}.
* @returns The newly created {@link QueryCompositeFilterConstraint}.
* @internal TODO remove this internal tag with OR Query support in the server
*/ function Yl() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
// Only support QueryFilterConstraints
return t.forEach((function(t) {
return dh("or", t);
})), Hl._create("or" /* CompositeOperator.OR */ , t)
/**
* Creates a new {@link QueryCompositeFilterConstraint} that is a conjunction of
* the given filter constraints. A conjunction filter includes a document if it
* satisfies all of the given filters.
*
* @param queryConstraints - Optional. The list of
* {@link QueryFilterConstraint}s to perform a conjunction for. These must be
* created with calls to {@link where}, {@link or}, or {@link and}.
* @returns The newly created {@link QueryCompositeFilterConstraint}.
* @internal TODO remove this internal tag with OR Query support in the server
*/;
}
function Xl() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
// Only support QueryFilterConstraints
return t.forEach((function(t) {
return dh("and", t);
})), Hl._create("and" /* CompositeOperator.AND */ , t)
/**
* A `QueryOrderByConstraint` is used to sort the set of documents returned by a
* Firestore query. `QueryOrderByConstraint`s are created by invoking
* {@link orderBy} and can then be passed to {@link query} to create a new query
* instance that also contains this `QueryOrderByConstraint`.
*
* Note: Documents that do not contain the orderBy field will not be present in
* the query result.
*/;
}
var Zl = /** @class */ function(e) {
/**
* @internal
*/
function n(t, n) {
var r = this;
return (r = e.call(this) || this)._field = t, r._direction = n,
/** The type of this query constraint */
r.type = "orderBy", r;
}
return t(n, e), n._create = function(t, e) {
return new n(t, e);
}, n.prototype._apply = function(t) {
var e = function(t, e, n) {
if (null !== t.startAt) throw new j(K.INVALID_ARGUMENT, "Invalid query. You must not call startAt() or startAfter() before calling orderBy().");
if (null !== t.endAt) throw new j(K.INVALID_ARGUMENT, "Invalid query. You must not call endAt() or endBefore() before calling orderBy().");
var r = new ze(e, n);
return function(t, e) {
if (null === mn(t)) {
// This is the first order by. It must match any inequality.
var n = gn(t);
null !== n && fh(t, n, e.field);
}
}(t, r), r;
}(t._query, this._field, this._direction);
return new pc(t.firestore, t.converter, function(t, e) {
// TODO(dimond): validate that orderBy does not list the same key twice.
var n = t.explicitOrderBy.concat([ e ]);
return new dn(t.path, t.collectionGroup, n, t.filters.slice(), t.limit, t.limitType, t.startAt, t.endAt);
}(t._query, e));
}, n;
}(jl);
/**
* Creates a {@link QueryOrderByConstraint} that sorts the query result by the
* specified field, optionally in descending order instead of ascending.
*
* Note: Documents that do not contain the specified field will not be present
* in the query result.
*
* @param fieldPath - The field to sort by.
* @param directionStr - Optional direction to sort by ('asc' or 'desc'). If
* not specified, order will be ascending.
* @returns The created {@link QueryOrderByConstraint}.
*/ function Jl(t, e) {
void 0 === e && (e = "asc");
var n = e, r = Bl("orderBy", t);
return Zl._create(r, n);
}
/**
* A `QueryLimitConstraint` is used to limit the number of documents returned by
* a Firestore query.
* `QueryLimitConstraint`s are created by invoking {@link limit} or
* {@link limitToLast} and can then be passed to {@link query} to create a new
* query instance that also contains this `QueryLimitConstraint`.
*/ var $l = /** @class */ function(e) {
/**
* @internal
*/
function n(
/** The type of this query constraint */
t, n, r) {
var i = this;
return (i = e.call(this) || this).type = t, i._limit = n, i._limitType = r, i;
}
return t(n, e), n._create = function(t, e, r) {
return new n(t, e, r);
}, n.prototype._apply = function(t) {
return new pc(t.firestore, t.converter, En(t._query, this._limit, this._limitType));
}, n;
}(jl);
/**
* Creates a {@link QueryLimitConstraint} that only returns the first matching
* documents.
*
* @param limit - The maximum number of items to return.
* @returns The created {@link QueryLimitConstraint}.
*/ function th(t) {
return sc("limit", t), $l._create("limit", t, "F" /* LimitType.First */)
/**
* Creates a {@link QueryLimitConstraint} that only returns the last matching
* documents.
*
* You must specify at least one `orderBy` clause for `limitToLast` queries,
* otherwise an exception will be thrown during execution.
*
* @param limit - The maximum number of items to return.
* @returns The created {@link QueryLimitConstraint}.
*/;
}
function eh(t) {
return sc("limitToLast", t), $l._create("limitToLast", t, "L" /* LimitType.Last */)
/**
* A `QueryStartAtConstraint` is used to exclude documents from the start of a
* result set returned by a Firestore query.
* `QueryStartAtConstraint`s are created by invoking {@link (startAt:1)} or
* {@link (startAfter:1)} and can then be passed to {@link query} to create a
* new query instance that also contains this `QueryStartAtConstraint`.
*/;
}
var nh = /** @class */ function(e) {
/**
* @internal
*/
function n(
/** The type of this query constraint */
t, n, r) {
var i = this;
return (i = e.call(this) || this).type = t, i._docOrFields = n, i._inclusive = r,
i;
}
return t(n, e), n._create = function(t, e, r) {
return new n(t, e, r);
}, n.prototype._apply = function(t) {
var e = sh(t, this.type, this._docOrFields, this._inclusive);
return new pc(t.firestore, t.converter, function(t, e) {
return new dn(t.path, t.collectionGroup, t.explicitOrderBy.slice(), t.filters.slice(), t.limit, t.limitType, e, t.endAt);
}(t._query, e));
}, n;
}(jl);
function rh() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
return nh._create("startAt", t,
/*inclusive=*/ !0);
}
function ih() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
return nh._create("startAfter", t,
/*inclusive=*/ !1);
}
/**
* A `QueryEndAtConstraint` is used to exclude documents from the end of a
* result set returned by a Firestore query.
* `QueryEndAtConstraint`s are created by invoking {@link (endAt:1)} or
* {@link (endBefore:1)} and can then be passed to {@link query} to create a new
* query instance that also contains this `QueryEndAtConstraint`.
*/ var oh = /** @class */ function(e) {
/**
* @internal
*/
function n(
/** The type of this query constraint */
t, n, r) {
var i = this;
return (i = e.call(this) || this).type = t, i._docOrFields = n, i._inclusive = r,
i;
}
return t(n, e), n._create = function(t, e, r) {
return new n(t, e, r);
}, n.prototype._apply = function(t) {
var e = sh(t, this.type, this._docOrFields, this._inclusive);
return new pc(t.firestore, t.converter, function(t, e) {
return new dn(t.path, t.collectionGroup, t.explicitOrderBy.slice(), t.filters.slice(), t.limit, t.limitType, t.startAt, e);
}(t._query, e));
}, n;
}(jl);
function uh() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
return oh._create("endBefore", t,
/*inclusive=*/ !1);
}
function ah() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
return oh._create("endAt", t,
/*inclusive=*/ !0);
}
/** Helper function to create a bound from a document or fields */ function sh(t, e, n, r) {
if (n[0] = y(n[0]), n[0] instanceof ql) return function(t, e, n, r, i) {
if (!r) throw new j(K.NOT_FOUND, "Can't use a DocumentSnapshot that doesn't exist for ".concat(n, "()."));
// Because people expect to continue/end a query at the exact document
// provided, we need to use the implicit sort order rather than the explicit
// sort order, because it's guaranteed to contain the document key. That way
// the position becomes unambiguous and the query continues/ends exactly at
// the provided document. Without the key (by using the explicit sort
// orders), multiple documents could match the position, yielding duplicate
// results.
for (var o = [], u = 0, a = bn(t); u < a.length; u++) {
var s = a[u];
if (s.field.isKeyField()) o.push(fe(e, r.key)); else {
var c = r.data.field(s.field);
if (te(c)) throw new j(K.INVALID_ARGUMENT, 'Invalid query. You are trying to start or end a query using a document for which the field "' + s.field + '" is an uncommitted server timestamp. (Since the value of this field is unknown, you cannot start/end a query with it.)');
if (null === c) {
var l = s.field.canonicalString();
throw new j(K.INVALID_ARGUMENT, "Invalid query. You are trying to start or end a query using a document for which the field '".concat(l, "' (used as the orderBy) does not exist."));
}
o.push(c);
}
}
return new Se(o, i);
}(t._query, t.firestore._databaseId, e, n[0]._document, r);
var i = wl(t.firestore);
return function(t, e, n, r, i, o) {
// Use explicit order by's because it has to match the query the user made
var u = t.explicitOrderBy;
if (i.length > u.length) throw new j(K.INVALID_ARGUMENT, "Too many arguments provided to ".concat(r, "(). The number of arguments must be less than or equal to the number of orderBy() clauses"));
for (var a = [], s = 0; s < i.length; s++) {
var c = i[s];
if (u[s].field.isKeyField()) {
if ("string" != typeof c) throw new j(K.INVALID_ARGUMENT, "Invalid query. Expected a string for document ID in ".concat(r, "(), but got a ").concat(typeof c));
if (!wn(t) && -1 !== c.indexOf("/")) throw new j(K.INVALID_ARGUMENT, "Invalid query. When querying a collection and ordering by documentId(), the value passed to ".concat(r, "() must be a plain document ID, but '").concat(c, "' contains a slash."));
var l = t.path.child(ct.fromString(c));
if (!ft.isDocumentKey(l)) throw new j(K.INVALID_ARGUMENT, "Invalid query. When querying a collection group and ordering by documentId(), the value passed to ".concat(r, "() must result in a valid document path, but '").concat(l, "' is not because it contains an odd number of segments."));
var h = new ft(l);
a.push(fe(e, h));
} else {
var f = Cl(n, r, c);
a.push(f);
}
}
return new Se(a, o);
}(t._query, t.firestore._databaseId, i, e, n, r);
}
function ch(t, e, n) {
if ("string" == typeof (n = y(n))) {
if ("" === n) throw new j(K.INVALID_ARGUMENT, "Invalid query. When querying with documentId(), you must provide a valid document ID, but it was an empty string.");
if (!wn(e) && -1 !== n.indexOf("/")) throw new j(K.INVALID_ARGUMENT, "Invalid query. When querying a collection by documentId(), you must provide a plain document ID, but '".concat(n, "' contains a '/' character."));
var r = e.path.child(ct.fromString(n));
if (!ft.isDocumentKey(r)) throw new j(K.INVALID_ARGUMENT, "Invalid query. When querying a collection group by documentId(), the value provided must result in a valid document path, but '".concat(r, "' is not because it has an odd number of segments (").concat(r.length, ")."));
return fe(t, new ft(r));
}
if (n instanceof dc) return fe(t, n._key);
throw new j(K.INVALID_ARGUMENT, "Invalid query. When querying with documentId(), you must provide a valid string or a DocumentReference, but it was: ".concat(uc(n), "."));
}
/**
* Validates that the value passed into a disjunctive filter satisfies all
* array requirements.
*/ function lh(t, e) {
if (!Array.isArray(t) || 0 === t.length) throw new j(K.INVALID_ARGUMENT, "Invalid Query. A non-empty array is required for '".concat(e.toString(), "' filters."));
if (t.length > 10) throw new j(K.INVALID_ARGUMENT, "Invalid Query. '".concat(e.toString(), "' filters support a maximum of 10 elements in the value array."));
}
/**
* Given an operator, returns the set of operators that cannot be used with it.
*
* Operators in a query must adhere to the following set of rules:
* 1. Only one array operator is allowed.
* 2. Only one disjunctive operator is allowed.
* 3. `NOT_EQUAL` cannot be used with another `NOT_EQUAL` operator.
* 4. `NOT_IN` cannot be used with array, disjunctive, or `NOT_EQUAL` operators.
*
* Array operators: `ARRAY_CONTAINS`, `ARRAY_CONTAINS_ANY`
* Disjunctive operators: `IN`, `ARRAY_CONTAINS_ANY`, `NOT_IN`
*/ function hh(t, e) {
if (e.isInequality()) {
var n = gn(t), r = e.field;
if (null !== n && !n.isEqual(r)) throw new j(K.INVALID_ARGUMENT, "Invalid query. All where filters with an inequality (<, <=, !=, not-in, >, or >=) must be on the same field. But you have inequality filters on '".concat(n.toString(), "' and '").concat(r.toString(), "'"));
var i = mn(t);
null !== i && fh(t, r, i);
}
var o = function(t, e) {
for (var n = 0, r = t; n < r.length; n++) for (var i = 0, o = r[n].getFlattenedFilters(); i < o.length; i++) {
var u = o[i];
if (e.indexOf(u.op) >= 0) return u.op;
}
return null;
}(t.filters, function(t) {
switch (t) {
case "!=" /* Operator.NOT_EQUAL */ :
return [ "!=" /* Operator.NOT_EQUAL */ , "not-in" /* Operator.NOT_IN */ ];
case "array-contains" /* Operator.ARRAY_CONTAINS */ :
return [ "array-contains" /* Operator.ARRAY_CONTAINS */ , "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ , "not-in" /* Operator.NOT_IN */ ];
case "in" /* Operator.IN */ :
return [ "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ , "in" /* Operator.IN */ , "not-in" /* Operator.NOT_IN */ ];
case "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ :
return [ "array-contains" /* Operator.ARRAY_CONTAINS */ , "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ , "in" /* Operator.IN */ , "not-in" /* Operator.NOT_IN */ ];
case "not-in" /* Operator.NOT_IN */ :
return [ "array-contains" /* Operator.ARRAY_CONTAINS */ , "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */ , "in" /* Operator.IN */ , "not-in" /* Operator.NOT_IN */ , "!=" /* Operator.NOT_EQUAL */ ];
default:
return [];
}
}(e.op));
if (null !== o)
// Special case when it's a duplicate op to give a slightly clearer error message.
throw o === e.op ? new j(K.INVALID_ARGUMENT, "Invalid query. You cannot use more than one '".concat(e.op.toString(), "' filter.")) : new j(K.INVALID_ARGUMENT, "Invalid query. You cannot use '".concat(e.op.toString(), "' filters with '").concat(o.toString(), "' filters."));
}
function fh(t, e, n) {
if (!n.isEqual(e)) throw new j(K.INVALID_ARGUMENT, "Invalid query. You have a where filter with an inequality (<, <=, !=, not-in, >, or >=) on field '".concat(e.toString(), "' and so you must also use '").concat(e.toString(), "' as your first argument to orderBy(), but your first orderBy() is on field '").concat(n.toString(), "' instead."));
}
function dh(t, e) {
if (!(e instanceof zl || e instanceof Hl)) throw new j(K.INVALID_ARGUMENT, "Function ".concat(t, "() requires AppliableConstraints created with a call to 'where(...)', 'or(...)', or 'and(...)'."));
}
var ph = /** @class */ function() {
function t() {}
return t.prototype.convertValue = function(t, e) {
switch (void 0 === e && (e = "none"), oe(t)) {
case 0 /* TypeOrder.NullValue */ :
return null;
case 1 /* TypeOrder.BooleanValue */ :
return t.booleanValue;
case 2 /* TypeOrder.NumberValue */ :
return Jt(t.integerValue || t.doubleValue);
case 3 /* TypeOrder.TimestampValue */ :
return this.convertTimestamp(t.timestampValue);
case 4 /* TypeOrder.ServerTimestampValue */ :
return this.convertServerTimestamp(t, e);
case 5 /* TypeOrder.StringValue */ :
return t.stringValue;
case 6 /* TypeOrder.BlobValue */ :
return this.convertBytes($t(t.bytesValue));
case 7 /* TypeOrder.RefValue */ :
return this.convertReference(t.referenceValue);
case 8 /* TypeOrder.GeoPointValue */ :
return this.convertGeoPoint(t.geoPointValue);
case 9 /* TypeOrder.ArrayValue */ :
return this.convertArray(t.arrayValue, e);
case 10 /* TypeOrder.ObjectValue */ :
return this.convertObject(t.mapValue, e);
default:
throw q();
}
}, t.prototype.convertObject = function(t, e) {
var n = this, r = {};
return Kt(t.fields, (function(t, i) {
r[t] = n.convertValue(i, e);
})), r;
}, t.prototype.convertGeoPoint = function(t) {
return new fl(Jt(t.latitude), Jt(t.longitude));
}, t.prototype.convertArray = function(t, e) {
var n = this;
return (t.values || []).map((function(t) {
return n.convertValue(t, e);
}));
}, t.prototype.convertServerTimestamp = function(t, e) {
switch (e) {
case "previous":
var n = ee(t);
return null == n ? null : this.convertValue(n, e);
case "estimate":
return this.convertTimestamp(ne(t));
default:
return null;
}
}, t.prototype.convertTimestamp = function(t) {
var e = Zt(t);
return new ut(e.seconds, e.nanos);
}, t.prototype.convertDocumentKey = function(t, e) {
var n = ct.fromString(t);
U(pi(n));
var r = new Bt(n.get(1), n.get(3)), i = new ft(n.popFirst(5));
return r.isEqual(e) ||
// TODO(b/64130202): Somehow support foreign references.
M("Document ".concat(i, " contains a document reference within a different database (").concat(r.projectId, "/").concat(r.database, ") which is not supported. It will be treated as a reference in the current database (").concat(e.projectId, "/").concat(e.database, ") instead.")),
i;
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Converts custom model object of type T into `DocumentData` by applying the
* converter if it exists.
*
* This function is used when converting user objects to `DocumentData`
* because we want to provide the user with a more specific error message if
* their `set()` or fails due to invalid data originating from a `toFirestore()`
* call.
*/ function yh(t, e, n) {
// Cast to `any` in order to satisfy the union type constraint on
// toFirestore().
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return t ? n && (n.merge || n.mergeFields) ? t.toFirestore(e, n) : t.toFirestore(e) : e;
}
var vh = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this) || this).firestore = t, n;
}
return t(n, e), n.prototype.convertBytes = function(t) {
return new sl(t);
}, n.prototype.convertReference = function(t) {
var e = this.convertDocumentKey(t, this.firestore._databaseId);
return new dc(this.firestore, /* converter= */ null, e);
}, n;
}(ph), mh = /** @class */ function() {
/** @hideconstructor */
function t(t, e) {
this.hasPendingWrites = t, this.fromCache = e
/**
* Returns true if this `SnapshotMetadata` is equal to the provided one.
*
* @param other - The `SnapshotMetadata` to compare against.
* @returns true if this `SnapshotMetadata` is equal to the provided one.
*/;
}
return t.prototype.isEqual = function(t) {
return this.hasPendingWrites === t.hasPendingWrites && this.fromCache === t.fromCache;
}, t;
}(), gh = /** @class */ function(e) {
/** @hideconstructor protected */
function n(t, n, r, i, o, u) {
var a = this;
return (a = e.call(this, t, n, r, i, u) || this)._firestore = t, a._firestoreImpl = t,
a.metadata = o, a;
}
/**
* Returns whether or not the data exists. True if the document exists.
*/ return t(n, e), n.prototype.exists = function() {
return e.prototype.exists.call(this);
},
/**
* Retrieves all fields in the document as an `Object`. Returns `undefined` if
* the document doesn't exist.
*
* By default, `serverTimestamp()` values that have not yet been
* set to their final value will be returned as `null`. You can override
* this by passing an options object.
*
* @param options - An options object to configure how data is retrieved from
* the snapshot (for example the desired behavior for server timestamps that
* have not yet been set to their final value).
* @returns An `Object` containing all fields in the document or `undefined` if
* the document doesn't exist.
*/
n.prototype.data = function(t) {
if (void 0 === t && (t = {}), this._document) {
if (this._converter) {
// We only want to use the converter and create a new DocumentSnapshot
// if a converter has been provided.
var e = new wh(this._firestore, this._userDataWriter, this._key, this._document, this.metadata,
/* converter= */ null);
return this._converter.fromFirestore(e, t);
}
return this._userDataWriter.convertValue(this._document.data.value, t.serverTimestamps);
}
},
/**
* Retrieves the field specified by `fieldPath`. Returns `undefined` if the
* document or field doesn't exist.
*
* By default, a `serverTimestamp()` that has not yet been set to
* its final value will be returned as `null`. You can override this by
* passing an options object.
*
* @param fieldPath - The path (for example 'foo' or 'foo.bar') to a specific
* field.
* @param options - An options object to configure how the field is retrieved
* from the snapshot (for example the desired behavior for server timestamps
* that have not yet been set to their final value).
* @returns The data at the specified field location or undefined if no such
* field exists in the document.
*/
// We are using `any` here to avoid an explicit cast by our users.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
n.prototype.get = function(t, e) {
if (void 0 === e && (e = {}), this._document) {
var n = this._document.data.field(Bl("DocumentSnapshot.get", t));
if (null !== n) return this._userDataWriter.convertValue(n, e.serverTimestamps);
}
}, n;
}(ql), wh = /** @class */ function(e) {
function n() {
return null !== e && e.apply(this, arguments) || this;
}
/**
* Retrieves all fields in the document as an `Object`.
*
* By default, `serverTimestamp()` values that have not yet been
* set to their final value will be returned as `null`. You can override
* this by passing an options object.
*
* @override
* @param options - An options object to configure how data is retrieved from
* the snapshot (for example the desired behavior for server timestamps that
* have not yet been set to their final value).
* @returns An `Object` containing all fields in the document.
*/ return t(n, e), n.prototype.data = function(t) {
return void 0 === t && (t = {}), e.prototype.data.call(this, t);
}, n;
}(gh), bh = /** @class */ function() {
/** @hideconstructor */
function t(t, e, n, r) {
this._firestore = t, this._userDataWriter = e, this._snapshot = r, this.metadata = new mh(r.hasPendingWrites, r.fromCache),
this.query = n;
}
return Object.defineProperty(t.prototype, "docs", {
/** An array of all the documents in the `QuerySnapshot`. */ get: function() {
var t = [];
return this.forEach((function(e) {
return t.push(e);
})), t;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "size", {
/** The number of documents in the `QuerySnapshot`. */ get: function() {
return this._snapshot.docs.size;
},
enumerable: !1,
configurable: !0
}), Object.defineProperty(t.prototype, "empty", {
/** True if there are no documents in the `QuerySnapshot`. */ get: function() {
return 0 === this.size;
},
enumerable: !1,
configurable: !0
}),
/**
* Enumerates all of the documents in the `QuerySnapshot`.
*
* @param callback - A callback to be called with a `QueryDocumentSnapshot` for
* each document in the snapshot.
* @param thisArg - The `this` binding for the callback.
*/
t.prototype.forEach = function(t, e) {
var n = this;
this._snapshot.docs.forEach((function(r) {
t.call(e, new wh(n._firestore, n._userDataWriter, r.key, r, new mh(n._snapshot.mutatedKeys.has(r.key), n._snapshot.fromCache), n.query.converter));
}));
},
/**
* Returns an array of the documents changes since the last snapshot. If this
* is the first snapshot, all documents will be in the list as 'added'
* changes.
*
* @param options - `SnapshotListenOptions` that control whether metadata-only
* changes (i.e. only `DocumentSnapshot.metadata` changed) should trigger
* snapshot events.
*/
t.prototype.docChanges = function(t) {
void 0 === t && (t = {});
var e = !!t.includeMetadataChanges;
if (e && this._snapshot.excludesMetadataChanges) throw new j(K.INVALID_ARGUMENT, "To include metadata changes with your document changes, you must also pass { includeMetadataChanges:true } to onSnapshot().");
return this._cachedChanges && this._cachedChangesIncludeMetadataChanges === e || (this._cachedChanges =
/** Calculates the array of `DocumentChange`s for a given `ViewSnapshot`. */
function(t, e) {
if (t._snapshot.oldDocs.isEmpty()) {
var n = 0;
return t._snapshot.docChanges.map((function(e) {
var r = new wh(t._firestore, t._userDataWriter, e.doc.key, e.doc, new mh(t._snapshot.mutatedKeys.has(e.doc.key), t._snapshot.fromCache), t.query.converter);
return e.doc, {
type: "added",
doc: r,
oldIndex: -1,
newIndex: n++
};
}));
}
// A `DocumentSet` that is updated incrementally as changes are applied to use
// to lookup the index of a document.
var r = t._snapshot.oldDocs;
return t._snapshot.docChanges.filter((function(t) {
return e || 3 /* ChangeType.Metadata */ !== t.type;
})).map((function(e) {
var n = new wh(t._firestore, t._userDataWriter, e.doc.key, e.doc, new mh(t._snapshot.mutatedKeys.has(e.doc.key), t._snapshot.fromCache), t.query.converter), i = -1, o = -1;
return 0 /* ChangeType.Added */ !== e.type && (i = r.indexOf(e.doc.key), r = r.delete(e.doc.key)),
1 /* ChangeType.Removed */ !== e.type && (o = (r = r.add(e.doc)).indexOf(e.doc.key)),
{
type: Ih(e.type),
doc: n,
oldIndex: i,
newIndex: o
};
}));
}(this, e), this._cachedChangesIncludeMetadataChanges = e), this._cachedChanges;
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Metadata about a snapshot, describing the state of the snapshot.
*/ function Ih(t) {
switch (t) {
case 0 /* ChangeType.Added */ :
return "added";
case 2 /* ChangeType.Modified */ :
case 3 /* ChangeType.Metadata */ :
return "modified";
case 1 /* ChangeType.Removed */ :
return "removed";
default:
return q();
}
}
// TODO(firestoreexp): Add tests for snapshotEqual with different snapshot
// metadata
/**
* Returns true if the provided snapshots are equal.
*
* @param left - A snapshot to compare.
* @param right - A snapshot to compare.
* @returns true if the snapshots are equal.
*/ function Th(t, e) {
return t instanceof gh && e instanceof gh ? t._firestore === e._firestore && t._key.isEqual(e._key) && (null === t._document ? null === e._document : t._document.isEqual(e._document)) && t._converter === e._converter : t instanceof bh && e instanceof bh && t._firestore === e._firestore && bc(t.query, e.query) && t.metadata.isEqual(e.metadata) && t._snapshot.isEqual(e._snapshot);
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Reads the document referred to by this `DocumentReference`.
*
* Note: `getDoc()` attempts to provide up-to-date data when possible by waiting
* for data from the server, but it may return cached data or fail if you are
* offline and the server cannot be reached. To specify this behavior, invoke
* {@link getDocFromCache} or {@link getDocFromServer}.
*
* @param reference - The reference of the document to fetch.
* @returns A Promise resolved with a `DocumentSnapshot` containing the
* current document contents.
*/ function Eh(t) {
t = ac(t, dc);
var e = ac(t.firestore, zc);
return Uc(Yc(e), t._key).then((function(n) {
return Lh(e, t, n);
}));
}
var Sh = /** @class */ function(e) {
function n(t) {
var n = this;
return (n = e.call(this) || this).firestore = t, n;
}
return t(n, e), n.prototype.convertBytes = function(t) {
return new sl(t);
}, n.prototype.convertReference = function(t) {
var e = this.convertDocumentKey(t, this.firestore._databaseId);
return new dc(this.firestore, /* converter= */ null, e);
}, n;
}(ph);
/**
* Reads the document referred to by this `DocumentReference` from cache.
* Returns an error if the document is not currently cached.
*
* @returns A `Promise` resolved with a `DocumentSnapshot` containing the
* current document contents.
*/ function _h(t) {
t = ac(t, dc);
var r = ac(t.firestore, zc), i = Yc(r), o = new Sh(r);
return function(t, r) {
var i = this, o = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(i, void 0, void 0, (function() {
var i;
return n(this, (function(u) {
switch (u.label) {
case 0:
return i = function(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u;
return n(this, (function(n) {
switch (n.label) {
case 0:
return n.trys.push([ 0, 2, , 3 ]), [ 4 /*yield*/ , function(t, e) {
var n = G(t);
return n.persistence.runTransaction("read document", "readonly", (function(t) {
return n.localDocuments.getDocument(t, e);
}));
}(t, r) ];
case 1:
return (e = n.sent()).isFoundDocument() ? i.resolve(e) : e.isNoDocument() ? i.resolve(null) : i.reject(new j(K.UNAVAILABLE, "Failed to get document from cache. (However, this document may exist on the server. Run again without setting 'source' in the GetOptions to attempt to retrieve the document from the server.)")),
[ 3 /*break*/ , 3 ];
case 2:
return o = n.sent(), u = Ja(o, "Failed to get document '".concat(r, " from cache")),
i.reject(u), [ 3 /*break*/ , 3 ];
case 3:
return [ 2 /*return*/ ];
}
}));
}));
}, [ 4 /*yield*/ , Vc(t) ];
case 1:
return [ 2 /*return*/ , i.apply(void 0, [ u.sent(), r, o ]) ];
}
}));
}));
})), o.promise;
}(i, t._key).then((function(e) {
return new gh(r, o, t._key, e, new mh(null !== e && e.hasLocalMutations,
/* fromCache= */ !0), t.converter);
}));
}
/**
* Reads the document referred to by this `DocumentReference` from the server.
* Returns an error if the network is not available.
*
* @returns A `Promise` resolved with a `DocumentSnapshot` containing the
* current document contents.
*/ function Dh(t) {
t = ac(t, dc);
var e = ac(t.firestore, zc);
return Uc(Yc(e), t._key, {
source: "server"
}).then((function(n) {
return Lh(e, t, n);
}));
}
/**
* Executes the query and returns the results as a `QuerySnapshot`.
*
* Note: `getDocs()` attempts to provide up-to-date data when possible by
* waiting for data from the server, but it may return cached data or fail if
* you are offline and the server cannot be reached. To specify this behavior,
* invoke {@link getDocsFromCache} or {@link getDocsFromServer}.
*
* @returns A `Promise` that will be resolved with the results of the query.
*/ function xh(t) {
t = ac(t, pc);
var e = ac(t.firestore, zc), n = Yc(e), r = new Sh(e);
return Gl(t._query), Bc(n, t._query).then((function(n) {
return new bh(e, r, t, n);
}))
/**
* Executes the query and returns the results as a `QuerySnapshot` from cache.
* Returns an empty result set if no documents matching the query are currently
* cached.
*
* @returns A `Promise` that will be resolved with the results of the query.
*/;
}
function Ah(t) {
t = ac(t, pc);
var r = ac(t.firestore, zc), i = Yc(r), o = new Sh(r);
return function(t, r) {
var i = this, o = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(i, void 0, void 0, (function() {
var i;
return n(this, (function(u) {
switch (u.label) {
case 0:
return i = function(t, r, i) {
return e(this, void 0, void 0, (function() {
var e, o, u, a, s, c;
return n(this, (function(n) {
switch (n.label) {
case 0:
return n.trys.push([ 0, 2, , 3 ]), [ 4 /*yield*/ , Hu(t, r,
/* usePreviousResults= */ !0) ];
case 1:
return e = n.sent(), o = new vs(r, e.Hi), u = o.Wu(e.documents), a = o.applyChanges(u,
/* updateLimboDocuments= */ !1), i.resolve(a.snapshot), [ 3 /*break*/ , 3 ];
case 2:
return s = n.sent(), c = Ja(s, "Failed to execute query '".concat(r, " against cache")),
i.reject(c), [ 3 /*break*/ , 3 ];
case 3:
return [ 2 /*return*/ ];
}
}));
}));
}, [ 4 /*yield*/ , Vc(t) ];
case 1:
return [ 2 /*return*/ , i.apply(void 0, [ u.sent(), r, o ]) ];
}
}));
}));
})), o.promise;
}(i, t._query).then((function(e) {
return new bh(r, o, t, e);
}));
}
/**
* Executes the query and returns the results as a `QuerySnapshot` from the
* server. Returns an error if the network is not available.
*
* @returns A `Promise` that will be resolved with the results of the query.
*/ function Ch(t) {
t = ac(t, pc);
var e = ac(t.firestore, zc), n = Yc(e), r = new Sh(e);
return Bc(n, t._query, {
source: "server"
}).then((function(n) {
return new bh(e, r, t, n);
}));
}
function Nh(t, e, n) {
t = ac(t, dc);
var r = ac(t.firestore, zc), i = yh(t.converter, e, n);
return Mh(r, [ bl(wl(r), "setDoc", t._key, i, null !== t.converter, n).toMutation(t._key, Hn.none()) ]);
}
function kh(t, e, n) {
for (var r = [], i = 3; i < arguments.length; i++) r[i - 3] = arguments[i];
t = ac(t, dc);
var o = ac(t.firestore, zc), u = wl(o);
return Mh(o, [ ("string" == typeof (
// For Compat types, we have to "extract" the underlying types before
// performing validation.
e = y(e)) || e instanceof cl ? Al(u, "updateDoc", t._key, e, n, r) : xl(u, "updateDoc", t._key, e)).toMutation(t._key, Hn.exists(!0)) ]);
}
/**
* Deletes the document referred to by the specified `DocumentReference`.
*
* @param reference - A reference to the document to delete.
* @returns A Promise resolved once the document has been successfully
* deleted from the backend (note that it won't resolve while you're offline).
*/ function Fh(t) {
return Mh(ac(t.firestore, zc), [ new cr(t._key, Hn.none()) ]);
}
/**
* Add a new document to specified `CollectionReference` with the given data,
* assigning it a document ID automatically.
*
* @param reference - A reference to the collection to add this document to.
* @param data - An Object containing the data for the new document.
* @returns A `Promise` resolved with a `DocumentReference` pointing to the
* newly created document after it has been written to the backend (Note that it
* won't resolve while you're offline).
*/ function Oh(t, e) {
var n = ac(t.firestore, zc), r = gc(t), i = yh(t.converter, e);
return Mh(n, [ bl(wl(t.firestore), "addDoc", r._key, i, null !== t.converter, {}).toMutation(r._key, Hn.exists(!1)) ]).then((function() {
return r;
}));
}
function Rh(t) {
for (var r, i, o, u = [], a = 1; a < arguments.length; a++) u[a - 1] = arguments[a];
t = y(t);
var s = {
includeMetadataChanges: !1
}, c = 0;
"object" != typeof u[c] || Kc(u[c]) || (s = u[c], c++);
var l, h, f, d = {
includeMetadataChanges: s.includeMetadataChanges
};
if (Kc(u[c])) {
var p = u[c];
u[c] = null === (r = p.next) || void 0 === r ? void 0 : r.bind(p), u[c + 1] = null === (i = p.error) || void 0 === i ? void 0 : i.bind(p),
u[c + 2] = null === (o = p.complete) || void 0 === o ? void 0 : o.bind(p);
}
if (t instanceof dc) h = ac(t.firestore, zc), f = yn(t._key.path), l = {
next: function(e) {
u[c] && u[c](Lh(h, t, e));
},
error: u[c + 1],
complete: u[c + 2]
}; else {
var v = ac(t, pc);
h = ac(v.firestore, zc), f = v._query;
var m = new Sh(h);
l = {
next: function(t) {
u[c] && u[c](new bh(h, m, v, t));
},
error: u[c + 1],
complete: u[c + 2]
}, Gl(t._query);
}
return function(t, r, i, o) {
var u = this, a = new Tc(o), s = new cs(r, a, i);
return t.asyncQueue.enqueueAndForget((function() {
return e(u, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = is, [ 4 /*yield*/ , qc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), s ]) ];
}
}));
}));
})), function() {
a.bc(), t.asyncQueue.enqueueAndForget((function() {
return e(u, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = os, [ 4 /*yield*/ , qc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), s ]) ];
}
}));
}));
}));
};
}(Yc(h), f, d, l);
}
function Vh(t, r) {
return function(t, r) {
var i = this, o = new Tc(r);
return t.asyncQueue.enqueueAndForget((function() {
return e(i, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = function(t, e) {
G(t).Ru.add(e),
// Immediately fire an initial event, indicating all existing listeners
// are in-sync.
e.next();
}, [ 4 /*yield*/ , qc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), o ]) ];
}
}));
}));
})), function() {
o.bc(), t.asyncQueue.enqueueAndForget((function() {
return e(i, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = function(t, e) {
G(t).Ru.delete(e);
}, [ 4 /*yield*/ , qc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), o ]) ];
}
}));
}));
}));
};
}(Yc(t = ac(t, zc)), Kc(r) ? r : {
next: r
});
}
/**
* Locally writes `mutations` on the async queue.
* @internal
*/ function Mh(t, r) {
return function(t, r) {
var i = this, o = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(i, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return e = Es, [ 4 /*yield*/ , Lc(t) ];
case 1:
return [ 2 /*return*/ , e.apply(void 0, [ n.sent(), r, o ]) ];
}
}));
}));
})), o.promise;
}(Yc(t), r);
}
/**
* Converts a {@link ViewSnapshot} that contains the single document specified by `ref`
* to a {@link DocumentSnapshot}.
*/ function Lh(t, e, n) {
var r = n.docs.get(e._key), i = new Sh(t);
return new gh(t, i, e._key, r, new mh(n.hasPendingWrites, n.fromCache), e.converter);
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Compares two `AggregateQuerySnapshot` instances for equality.
*
* Two `AggregateQuerySnapshot` instances are considered "equal" if they have
* underlying queries that compare equal, and the same data.
*
* @param left - The first `AggregateQuerySnapshot` to compare.
* @param right - The second `AggregateQuerySnapshot` to compare.
*
* @returns `true` if the objects are "equal", as defined above, or `false`
* otherwise.
*/ function Ph(t, e) {
return bc(t.query, e.query) && g(t.data(), e.data());
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Calculates the number of documents in the result set of the given query,
* without actually downloading the documents.
*
* Using this function to count the documents is efficient because only the
* final count, not the documents' data, is downloaded. This function can even
* count the documents if the result set would be prohibitively large to
* download entirely (e.g. thousands of documents).
*
* The result received from the server is presented, unaltered, without
* considering any local state. That is, documents in the local cache are not
* taken into consideration, neither are local modifications not yet
* synchronized with the server. Previously-downloaded results, if any, are not
* used: every request using this source necessarily involves a round trip to
* the server.
*
* @param query - The query whose result set size to calculate.
* @returns A Promise that will be resolved with the count; the count can be
* retrieved from `snapshot.data().count`, where `snapshot` is the
* `AggregateQuerySnapshot` to which the returned Promise resolves.
*/ function qh(t) {
var r = ac(t.firestore, zc);
return function(t, r, i) {
var o = this, u = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(o, void 0, void 0, (function() {
var e, o, a, s;
return n(this, (function(n) {
switch (n.label) {
case 0:
return n.trys.push([ 0, 5, , 6 ]), e = Oa, [ 4 /*yield*/ , Mc(t) ];
case 1:
return e.apply(void 0, [ n.sent() ]) ? [ 4 /*yield*/ , Pc(t) ] : [ 3 /*break*/ , 3 ];
case 2:
return o = n.sent(), a = new Dc(r, o, i).run(), u.resolve(a), [ 3 /*break*/ , 4 ];
case 3:
u.reject(new j(K.UNAVAILABLE, "Failed to get count result because the client is offline.")),
n.label = 4;
case 4:
return [ 3 /*break*/ , 6 ];
case 5:
return s = n.sent(), u.reject(s), [ 3 /*break*/ , 6 ];
case 6:
return [ 2 /*return*/ ];
}
}));
}));
})), u.promise;
}(Yc(r), t, new Sh(r));
}
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ var Uh = {
maxAttempts: 5
}, Bh = /** @class */ function() {
/** @hideconstructor */
function t(t, e) {
this._firestore = t, this._commitHandler = e, this._mutations = [], this._committed = !1,
this._dataReader = wl(t);
}
return t.prototype.set = function(t, e, n) {
this._verifyNotCommitted();
var r = Gh(t, this._firestore), i = yh(r.converter, e, n), o = bl(this._dataReader, "WriteBatch.set", r._key, i, null !== r.converter, n);
return this._mutations.push(o.toMutation(r._key, Hn.none())), this;
}, t.prototype.update = function(t, e, n) {
for (var r = [], i = 3; i < arguments.length; i++) r[i - 3] = arguments[i];
this._verifyNotCommitted();
var o, u = Gh(t, this._firestore);
// For Compat types, we have to "extract" the underlying types before
// performing validation.
return o = "string" == typeof (e = y(e)) || e instanceof cl ? Al(this._dataReader, "WriteBatch.update", u._key, e, n, r) : xl(this._dataReader, "WriteBatch.update", u._key, e),
this._mutations.push(o.toMutation(u._key, Hn.exists(!0))), this;
},
/**
* Deletes the document referred to by the provided {@link DocumentReference}.
*
* @param documentRef - A reference to the document to be deleted.
* @returns This `WriteBatch` instance. Used for chaining method calls.
*/
t.prototype.delete = function(t) {
this._verifyNotCommitted();
var e = Gh(t, this._firestore);
return this._mutations = this._mutations.concat(new cr(e._key, Hn.none())), this;
},
/**
* Commits all of the writes in this write batch as a single atomic unit.
*
* The result of these writes will only be reflected in document reads that
* occur after the returned promise resolves. If the client is offline, the
* write fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @returns A `Promise` resolved once all of the writes in the batch have been
* successfully written to the backend as an atomic unit (note that it won't
* resolve while you're offline).
*/
t.prototype.commit = function() {
return this._verifyNotCommitted(), this._committed = !0, this._mutations.length > 0 ? this._commitHandler(this._mutations) : Promise.resolve();
}, t.prototype._verifyNotCommitted = function() {
if (this._committed) throw new j(K.FAILED_PRECONDITION, "A write batch can no longer be used after commit() has been called.");
}, t;
}();
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A write batch, used to perform multiple writes as a single atomic unit.
*
* A `WriteBatch` object can be acquired by calling {@link writeBatch}. It
* provides methods for adding writes to the write batch. None of the writes
* will be committed (or visible locally) until {@link WriteBatch.commit} is
* called.
*/ function Gh(t, e) {
if ((t = y(t)).firestore !== e) throw new j(K.INVALID_ARGUMENT, "Provided document reference is from a different Firestore instance.");
return t;
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// TODO(mrschmidt) Consider using `BaseTransaction` as the base class in the
// legacy SDK.
/**
* A reference to a transaction.
*
* The `Transaction` object passed to a transaction's `updateFunction` provides
* the methods to read and write data within the transaction context. See
* {@link runTransaction}.
*/
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A reference to a transaction.
*
* The `Transaction` object passed to a transaction's `updateFunction` provides
* the methods to read and write data within the transaction context. See
* {@link runTransaction}.
*/ var Kh, jh = /** @class */ function(e) {
// This class implements the same logic as the Transaction API in the Lite SDK
// but is subclassed in order to return its own DocumentSnapshot types.
/** @hideconstructor */
function n(t, n) {
var r = this;
return (r = e.call(this, t, n) || this)._firestore = t, r;
}
/**
* Reads the document referenced by the provided {@link DocumentReference}.
*
* @param documentRef - A reference to the document to be read.
* @returns A `DocumentSnapshot` with the read data.
*/ return t(n, e), n.prototype.get = function(t) {
var n = this, r = Gh(t, this._firestore), i = new Sh(this._firestore);
return e.prototype.get.call(this, t).then((function(t) {
return new gh(n._firestore, i, r._key, t._document, new mh(
/* hasPendingWrites= */ !1,
/* fromCache= */ !1), r.converter);
}));
}, n;
}(/** @class */ function() {
/** @hideconstructor */
function t(t, e) {
this._firestore = t, this._transaction = e, this._dataReader = wl(t)
/**
* Reads the document referenced by the provided {@link DocumentReference}.
*
* @param documentRef - A reference to the document to be read.
* @returns A `DocumentSnapshot` with the read data.
*/;
}
return t.prototype.get = function(t) {
var e = this, n = Gh(t, this._firestore), r = new vh(this._firestore);
return this._transaction.lookup([ n._key ]).then((function(t) {
if (!t || 1 !== t.length) return q();
var i = t[0];
if (i.isFoundDocument()) return new ql(e._firestore, r, i.key, i, n.converter);
if (i.isNoDocument()) return new ql(e._firestore, r, n._key, null, n.converter);
throw q();
}));
}, t.prototype.set = function(t, e, n) {
var r = Gh(t, this._firestore), i = yh(r.converter, e, n), o = bl(this._dataReader, "Transaction.set", r._key, i, null !== r.converter, n);
return this._transaction.set(r._key, o), this;
}, t.prototype.update = function(t, e, n) {
for (var r = [], i = 3; i < arguments.length; i++) r[i - 3] = arguments[i];
var o, u = Gh(t, this._firestore);
// For Compat types, we have to "extract" the underlying types before
// performing validation.
return o = "string" == typeof (e = y(e)) || e instanceof cl ? Al(this._dataReader, "Transaction.update", u._key, e, n, r) : xl(this._dataReader, "Transaction.update", u._key, e),
this._transaction.update(u._key, o), this;
},
/**
* Deletes the document referred to by the provided {@link DocumentReference}.
*
* @param documentRef - A reference to the document to be deleted.
* @returns This `Transaction` instance. Used for chaining method calls.
*/
t.prototype.delete = function(t) {
var e = Gh(t, this._firestore);
return this._transaction.delete(e._key), this;
}, t;
}());
/**
* Executes the given `updateFunction` and then attempts to commit the changes
* applied within the transaction. If any document read within the transaction
* has changed, Cloud Firestore retries the `updateFunction`. If it fails to
* commit after 5 attempts, the transaction fails.
*
* The maximum number of writes allowed in a single transaction is 500.
*
* @param firestore - A reference to the Firestore database to run this
* transaction against.
* @param updateFunction - The function to execute within the transaction
* context.
* @param options - An options object to configure maximum number of attempts to
* commit.
* @returns If the transaction completed successfully or was explicitly aborted
* (the `updateFunction` returned a failed promise), the promise returned by the
* `updateFunction `is returned here. Otherwise, if the transaction failed, a
* rejected promise with the corresponding failure error is returned.
*/ function Qh(t, r, i) {
t = ac(t, zc);
var o = Object.assign(Object.assign({}, Uh), i);
return function(t) {
if (t.maxAttempts < 1) throw new j(K.INVALID_ARGUMENT, "Max attempts must be at least 1");
}(o), function(t, r, i) {
var o = this, u = new Q;
return t.asyncQueue.enqueueAndForget((function() {
return e(o, void 0, void 0, (function() {
var e;
return n(this, (function(n) {
switch (n.label) {
case 0:
return [ 4 /*yield*/ , Pc(t) ];
case 1:
return e = n.sent(), new Ac(t.asyncQueue, e, i, r, u).run(), [ 2 /*return*/ ];
}
}));
}));
})), u.promise;
}(Yc(t), (function(e) {
return r(new jh(t, e));
}), o);
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Returns a sentinel for use with {@link @firebase/firestore/lite#(updateDoc:1)} or
* {@link @firebase/firestore/lite#(setDoc:1)} with `{merge: true}` to mark a field for deletion.
*/ function zh() {
return new Il("deleteField");
}
/**
* Returns a sentinel used with {@link @firebase/firestore/lite#(setDoc:1)} or {@link @firebase/firestore/lite#(updateDoc:1)} to
* include a server-generated timestamp in the written data.
*/ function Wh() {
return new El("serverTimestamp");
}
/**
* Returns a special value that can be used with {@link @firebase/firestore/lite#(setDoc:1)} or {@link
* @firebase/firestore/lite#(updateDoc:1)} that tells the server to union the given elements with any array
* value that already exists on the server. Each specified element that doesn't
* already exist in the array will be added to the end. If the field being
* modified is not already an array it will be overwritten with an array
* containing exactly the specified elements.
*
* @param elements - The elements to union into the array.
* @returns The `FieldValue` sentinel for use in a call to `setDoc()` or
* `updateDoc()`.
*/ function Hh() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
// NOTE: We don't actually parse the data until it's used in set() or
// update() since we'd need the Firestore instance to do this.
return new Sl("arrayUnion", t);
}
/**
* Returns a special value that can be used with {@link (setDoc:1)} or {@link
* updateDoc:1} that tells the server to remove the given elements from any
* array value that already exists on the server. All instances of each element
* specified will be removed from the array. If the field being modified is not
* already an array it will be overwritten with an empty array.
*
* @param elements - The elements to remove from the array.
* @returns The `FieldValue` sentinel for use in a call to `setDoc()` or
* `updateDoc()`
*/ function Yh() {
for (var t = [], e = 0; e < arguments.length; e++) t[e] = arguments[e];
// NOTE: We don't actually parse the data until it's used in set() or
// update() since we'd need the Firestore instance to do this.
return new _l("arrayRemove", t);
}
/**
* Returns a special value that can be used with {@link @firebase/firestore/lite#(setDoc:1)} or {@link
* @firebase/firestore/lite#(updateDoc:1)} that tells the server to increment the field's current value by
* the given value.
*
* If either the operand or the current field value uses floating point
* precision, all arithmetic follows IEEE 754 semantics. If both values are
* integers, values outside of JavaScript's safe number range
* (`Number.MIN_SAFE_INTEGER` to `Number.MAX_SAFE_INTEGER`) are also subject to
* precision loss. Furthermore, once processed by the Firestore backend, all
* integer operations are capped between -2^63 and 2^63-1.
*
* If the current field value is not of type `number`, or if the field does not
* yet exist, the transformation sets the field to the given value.
*
* @param n - The value to increment by.
* @returns The `FieldValue` sentinel for use in a call to `setDoc()` or
* `updateDoc()`
*/ function Xh(t) {
return new Dl("increment", t);
}
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Creates a write batch, used for performing multiple writes as a single
* atomic operation. The maximum number of writes allowed in a single {@link WriteBatch}
* is 500.
*
* Unlike transactions, write batches are persisted offline and therefore are
* preferable when you don't need to condition your writes on read data.
*
* @returns A {@link WriteBatch} that can be used to atomically execute multiple
* writes.
*/ function Zh(t) {
return Yc(t = ac(t, zc)), new Bh(t, (function(e) {
return Mh(t, e);
}))
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/;
}
function Jh(t, i) {
var o, u = Yc(t = ac(t, zc));
// PORTING NOTE: We don't return an error if the user has not enabled
// persistence since `enableIndexeddbPersistence()` can fail on the Web.
if (!(null === (o = u.offlineComponents) || void 0 === o ? void 0 : o.indexBackfillerScheduler)) return L("Cannot enable indexes when persistence is disabled"),
Promise.resolve();
var a = function(t) {
var e = "string" == typeof t ? function(t) {
try {
return JSON.parse(t);
} catch (t) {
throw new j(K.INVALID_ARGUMENT, "Failed to parse JSON: " + (null == t ? void 0 : t.message));
}
}(t) : t, n = [];
if (Array.isArray(e.indexes)) for (var r = 0, i = e.indexes; r < i.length; r++) {
var o = i[r], u = $h(o, "collectionGroup"), a = [];
if (Array.isArray(o.fields)) for (var s = 0, c = o.fields; s < c.length; s++) {
var l = c[s], h = Ml("setIndexConfiguration", $h(l, "fieldPath"));
"CONTAINS" === l.arrayConfig ? a.push(new mt(h, 2 /* IndexKind.CONTAINS */)) : "ASCENDING" === l.order ? a.push(new mt(h, 0 /* IndexKind.ASCENDING */)) : "DESCENDING" === l.order && a.push(new mt(h, 1 /* IndexKind.DESCENDING */));
}
n.push(new dt(dt.UNKNOWN_ID, u, a, wt.empty()));
}
return n;
}(i);
return Vc(u).then((function(t) {
return function(t, i) {
return e(this, void 0, void 0, (function() {
var e, o, u;
return n(this, (function(n) {
return e = G(t), o = e.indexManager, u = [], [ 2 /*return*/ , e.persistence.runTransaction("Configure indexes", "readwrite", (function(t) {
return o.getFieldIndexes(t).next((function(e) {
return function(t, e, n, i, o) {
t = r([], t, !0), e = r([], e, !0), t.sort(n), e.sort(n);
for (var u = t.length, a = e.length, s = 0, c = 0; s < a && c < u; ) {
var l = n(t[c], e[s]);
l < 0 ?
// The element was removed if the next element in our ordered
// walkthrough is only in `before`.
o(t[c++]) : l > 0 ?
// The element was added if the next element in our ordered walkthrough
// is only in `after`.
i(e[s++]) : (s++, c++);
}
for (;s < a; ) i(e[s++]);
for (;c < u; ) o(t[c++]);
}(e, i, vt, (function(e) {
u.push(o.addFieldIndex(t, e));
}), (function(e) {
u.push(o.deleteFieldIndex(t, e));
}));
})).next((function() {
return xt.waitFor(u);
}));
})) ];
}));
}));
}(t, a);
}));
}
function $h(t, e) {
if ("string" != typeof t[e]) throw new j(K.INVALID_ARGUMENT, "Missing string value for: " + e);
return t[e];
}
/**
* Cloud Firestore
*
* @packageDocumentation
*/ void 0 === Kh && (Kh = !0), k = i, o(new c("firestore", (function(t, e) {
var n = e.instanceIdentifier, r = e.options, i = t.getProvider("app").getImmediate(), o = new zc(new Y(t.getProvider("auth-internal")), new $(t.getProvider("app-check-internal")), function(t, e) {
if (!Object.prototype.hasOwnProperty.apply(t.options, [ "projectId" ])) throw new j(K.INVALID_ARGUMENT, '"projectId" not provided in firebase.initializeApp.');
return new Bt(t.options.projectId, e);
}(i, n), i);
return r = Object.assign({
useFetchStreams: Kh
}, r), o._setSettings(r), o;
}), "PUBLIC").setMultipleInstances(!0)), u(C, "3.8.1", void 0),
// BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
u(C, "3.8.1", "esm5");
export { ph as AbstractUserDataWriter, Sc as AggregateField, _c as AggregateQuerySnapshot, sl as Bytes, Qc as CACHE_SIZE_UNLIMITED, yc as CollectionReference, dc as DocumentReference, gh as DocumentSnapshot, cl as FieldPath, hl as FieldValue, zc as Firestore, j as FirestoreError, fl as GeoPoint, jc as LoadBundleTask, pc as Query, Hl as QueryCompositeFilterConstraint, jl as QueryConstraint, wh as QueryDocumentSnapshot, oh as QueryEndAtConstraint, zl as QueryFieldFilterConstraint, $l as QueryLimitConstraint, Zl as QueryOrderByConstraint, bh as QuerySnapshot, nh as QueryStartAtConstraint, mh as SnapshotMetadata, ut as Timestamp, jh as Transaction, Bh as WriteBatch, Bt as _DatabaseId, ft as _DocumentKey, tt as _EmptyAppCheckTokenProvider, W as _EmptyAuthCredentialsProvider, ht as _FieldPath, ac as _cast, B as _debugAssert, Ht as _isBase64Available, L as _logWarn, rc as _validateIsNotUsedTogether, Oh as addDoc, Ph as aggregateQuerySnapshotEqual, Xl as and, Yh as arrayRemove, Hh as arrayUnion, tl as clearIndexedDbPersistence, vc as collection, mc as collectionGroup, fc as connectFirestoreEmulator, Fh as deleteDoc, zh as deleteField, rl as disableNetwork, gc as doc, ll as documentId, Zc as enableIndexedDbPersistence, Jc as enableMultiTabIndexedDbPersistence, nl as enableNetwork, ah as endAt, uh as endBefore, Yc as ensureFirestoreConfigured, Mh as executeWrite, qh as getCountFromServer, Eh as getDoc, _h as getDocFromCache, Dh as getDocFromServer, xh as getDocs, Ah as getDocsFromCache, Ch as getDocsFromServer, Hc as getFirestore, Xh as increment, Wc as initializeFirestore, th as limit, eh as limitToLast, ol as loadBundle, ul as namedQuery, Rh as onSnapshot, Vh as onSnapshotsInSync, Yl as or, Jl as orderBy, Ql as query, bc as queryEqual, wc as refEqual, Qh as runTransaction, Wh as serverTimestamp, Nh as setDoc, Jh as setIndexConfiguration, R as setLogLevel, Th as snapshotEqual, ih as startAfter, rh as startAt, il as terminate, kh as updateDoc, el as waitForPendingWrites, Wl as where, Zh as writeBatch };
//# sourceMappingURL=index.esm5.js.map