jinaga
Version:
Data management for web and mobile applications.
71 lines • 3.49 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.PurgeManager = void 0;
const purgeCompliance_1 = require("../purge/purgeCompliance");
const inverse_1 = require("../specification/inverse");
const trace_1 = require("../util/trace");
class PurgeManager {
constructor(store, purgeConditions) {
this.store = store;
this.purgeConditions = purgeConditions;
this.purgeInverses = purgeConditions.map(pc => (0, inverse_1.invertSpecification)(pc)).flat();
}
purge() {
return __awaiter(this, void 0, void 0, function* () {
const count = yield this.store.purge(this.purgeConditions);
if (count > 0) {
trace_1.Trace.counter("facts_purged", count);
}
});
}
triggerPurge(factsAdded) {
return __awaiter(this, void 0, void 0, function* () {
for (const envelope of factsAdded) {
const fact = envelope.fact;
for (const purgeInverse of this.purgeInverses) {
// Only run the purge inverse if the given type matches the fact type
if (purgeInverse.inverseSpecification.given[0].type !== fact.type) {
continue;
}
const givenReference = {
type: fact.type,
hash: fact.hash
};
const results = yield this.store.read([givenReference], purgeInverse.inverseSpecification);
for (const result of results) {
const givenName = purgeInverse.givenSubset[0];
// The given is the purge root
const purgeRoot = result.tuple[givenName];
// All other members of the result tuple are triggers
const triggers = Object.keys(result.tuple)
.filter(k => k !== givenName)
.map(k => result.tuple[k]);
// Purge all descendants of the purge root except for the triggers
const count = yield this.store.purgeDescendants(purgeRoot, triggers);
if (count > 0) {
trace_1.Trace.counter("facts_purged", count);
}
}
}
}
});
}
checkCompliance(specification) {
const failures = (0, purgeCompliance_1.testSpecificationForCompliance)(specification, this.purgeConditions);
if (failures.length > 0) {
const message = failures.join("\n");
throw new Error(message);
}
}
}
exports.PurgeManager = PurgeManager;
//# sourceMappingURL=PurgeManager.js.map