UNPKG

mockbase

Version:
141 lines 6.01 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.MockDocumentReference = exports.SNAPSHOT_ERROR_EVENT = exports.SNAPSHOT_NEXT_EVENT = void 0; const firebase = require("firebase"); const util_1 = require("../util"); const document_snapshot_1 = require("./document-snapshot"); exports.SNAPSHOT_NEXT_EVENT = "snapshot:next"; exports.SNAPSHOT_ERROR_EVENT = "snapshot:error"; class MockDocumentReference { constructor(firestore, id, parent, converter, emitEvents = true) { this.firestore = firestore; this.id = id; this.parent = parent; this.converter = converter; this.emitEvents = emitEvents; } get path() { return this.parent.path + "/" + this.id; } get emitter() { const emitter = this.firestore.documentEvents.get(this.path) || new util_1.EventEmitter(); this.firestore.documentEvents.set(this.path, emitter); return emitter; } get currentData() { return this.firestore.documentData.get(this.path); } emitChange() { return __awaiter(this, void 0, void 0, function* () { if (!this.emitEvents) { return; } const snapshot = yield this.get(); this.emitter.emit(exports.SNAPSHOT_NEXT_EVENT, [snapshot]); this.parent.emitChange(); }); } collection(collectionPath) { return this.firestore.collection(this.path + "/" + collectionPath); } isEqual(other) { return (other.firestore === this.firestore && other.path === this.path && other instanceof MockDocumentReference && other.converter === this.converter); } set(data, options = {}) { if (options.mergeFields && options.mergeFields.length) { throw new Error("Option mergeFields is not supported"); } const parsedData = this.converter.toFirestore(data); this.firestore.writeDocument(this, Object.assign(options.merge ? this.currentData : {}, parsedData)); return this.emitChange(); } update(data, ...rest) { return __awaiter(this, void 0, void 0, function* () { if (typeof data === "string" || data instanceof firebase.firestore.FieldPath) { throw new Error("Document updating by field is not supported"); } if (this.currentData === undefined) { throw new Error(`Document doesn't exist: ${this.path}`); } const currentData = Object.assign({}, this.currentData); let changed = false; Object.keys(data).forEach((key) => { key.split(".").reduce((obj, part, index, path) => { if (path.length === index + 1) { changed = changed || data[key] !== obj[part] || obj[part] === undefined; obj[part] = data[key]; } else { changed = changed || obj[part] === undefined; obj[part] = typeof obj[part] === "object" ? obj[part] : {}; } return obj[part]; }, currentData); }); if (changed) { this.firestore.writeDocument(this, currentData); yield this.emitChange(); } }); } delete() { return __awaiter(this, void 0, void 0, function* () { const existed = !!this.currentData; if (existed) { this.firestore.writeDocument(this, undefined); yield this.emitChange(); } }); } get(options) { return Promise.resolve(new document_snapshot_1.MockDocumentSnapshot(this, this.currentData ? Object.assign(this.currentData) : undefined)); } onSnapshot(options, onNext, onError, onCompletion) { let actualListeners; if (typeof options === "object") { if (typeof onNext === "object") { actualListeners = onNext; } else if (typeof onNext === "function") { actualListeners = { next: onNext, error: onError, }; } else { actualListeners = options; } } else { actualListeners = { next: options, error: onNext, }; } this.emitter.on(exports.SNAPSHOT_NEXT_EVENT, actualListeners.next); actualListeners.error && this.emitter.on(exports.SNAPSHOT_ERROR_EVENT, actualListeners.error); // Don't emit SNAPSHOT_NEXT_EVENT otherwise every listener will get it this.get().then((snapshot) => actualListeners.next(snapshot)); return () => { this.emitter.off(exports.SNAPSHOT_NEXT_EVENT, actualListeners.next); actualListeners.error && this.emitter.off(exports.SNAPSHOT_ERROR_EVENT, actualListeners.error); }; } withConverter(converter) { return new MockDocumentReference(this.firestore, this.id, this.parent.withConverter(converter), converter); } } exports.MockDocumentReference = MockDocumentReference; //# sourceMappingURL=document-reference.js.map