mockbase
Version:
Firebase v7+ mock.
91 lines • 3.78 kB
JavaScript
;
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.MockCollectionGroup = void 0;
const query_1 = require("./query");
class MockCollectionGroup extends query_1.MockQuery {
constructor(firestore, id, converter) {
super(firestore, id, converter);
// A list of onSnapshot calls that have been proxied to other collections and the disposers.
this.activeListeners = [];
firestore.onNewCollection(this.maybeHookNewCollection.bind(this));
}
emitChange() {
return __awaiter(this, void 0, void 0, function* () {
// No-op
});
}
clone() {
const group = new MockCollectionGroup(this.firestore, this.path, this.converter);
Object.assign(group, this);
group.filters = Object.assign({}, this.filters);
return group;
}
maybeHookNewCollection(path) {
if (!path.endsWith("/" + this.path)) {
return;
}
const collection = this.firestore.collection(path).setNoInitialSnapshot();
this.activeListeners.forEach((listener) => {
listener.disposers.push(collection.onSnapshot(listener.listener));
});
}
getCandidateCollections() {
return Array.from(this.firestore.collectionDocuments.keys()).filter((path) => path.endsWith("/" + this.path));
}
getCandidateDocKeys() {
return new Set(this.getCandidateCollections().reduce((docs, path) => {
return docs.concat(Array.from(this.firestore.collectionDocuments.get(path)));
}, []));
}
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,
};
}
const listenerIndex = this.activeListeners.length;
this.activeListeners.push({
listener: actualListeners,
disposers: this.getCandidateCollections().map((path) => {
return this.firestore
.collection(path)
.setNoInitialSnapshot()
.onSnapshot(() => this.get().then((snapshot) => {
actualListeners.next(snapshot);
}), actualListeners.error);
}),
});
this.get().then((snapshot) => actualListeners.next(snapshot));
return () => {
this.activeListeners[listenerIndex].disposers.forEach((disposer) => disposer());
this.activeListeners.splice(listenerIndex, 1);
};
}
}
exports.MockCollectionGroup = MockCollectionGroup;
//# sourceMappingURL=collection-group.js.map