UNPKG

mockbase

Version:
53 lines 2.43 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockWriteBatch = void 0; const document_reference_1 = require("./document-reference"); class MockWriteBatch { constructor(firestore) { this.firestore = firestore; this.operations = []; } set(doc, data, options) { this.operations.push({ doc, execute: (doc) => doc.set(data, options) }); return this; } update(doc, data, value, ...rest) { this.operations.push({ doc, execute: (doc) => doc.update(data, value, ...rest) }); return this; } delete(doc) { this.operations.push({ doc, execute: (doc) => doc.delete() }); return this; } commit() { return __awaiter(this, void 0, void 0, function* () { const affectedDocs = new Set(); while (this.operations.length) { const { doc, execute } = this.operations.shift(); const beforeOperation = yield doc.get(); const surrogate = new document_reference_1.MockDocumentReference(doc.firestore, doc.id, doc.parent, doc.converter, false); yield execute(surrogate); const afterOperation = yield doc.get(); // If state before and after changed, then it's a path which should have events emitted // after all operations are completed if (!afterOperation.isEqual(beforeOperation)) { affectedDocs.add(doc.path); } } for (const path of affectedDocs) { this.firestore.doc(path).emitChange(); } }); } } exports.MockWriteBatch = MockWriteBatch; //# sourceMappingURL=write-batch.js.map