UNPKG

mockbase

Version:
78 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockQuerySnapshot = void 0; class MockQuerySnapshot { constructor(query, docs, previousSnapshot) { this.query = query; this.docs = docs; this.previousSnapshot = previousSnapshot; this.metadata = { fromCache: true, hasPendingWrites: false, isEqual: () => true, }; } get size() { return this.docs.length; } get empty() { return this.size === 0; } docChanges(options) { if (!this.previousSnapshot) { // Short circuit: this is the first snapshot, so all items were added. return this.docs.map((doc, newIndex) => ({ type: "added", oldIndex: -1, newIndex, doc, })); } const checkedPaths = new Set(); const previousDocs = this.previousSnapshot.docs; const changes = []; for (const [newIndex, doc] of this.docs.entries()) { const oldIndex = previousDocs.findIndex(({ ref }) => ref.path === doc.ref.path); const previousDoc = previousDocs[oldIndex]; if (!previousDoc) { changes.push({ type: "added", oldIndex, newIndex, doc, }); } else if (newIndex !== oldIndex || !previousDoc.isEqual(doc)) { changes.push({ type: "modified", oldIndex, newIndex, doc, }); } checkedPaths.add(doc.ref.path); } for (const [oldIndex, doc] of previousDocs.entries()) { if (!checkedPaths.has(doc.ref.path)) { changes.push({ type: "removed", oldIndex, newIndex: -1, doc, }); checkedPaths.add(doc.ref.path); } } return changes; } forEach(callback, thisArg) { this.docs.forEach(callback, thisArg); } isEqual(other) { return (other instanceof MockQuerySnapshot && other.size === this.size && other.docs.every((doc, index) => this.docs[index].isEqual(doc))); } } exports.MockQuerySnapshot = MockQuerySnapshot; //# sourceMappingURL=query-snapshot.js.map