@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
49 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowrCache = void 0;
const assert_1 = require("../../util/assert");
/**
* Central class for caching analysis results in FlowR.
*/
class FlowrCache {
value = undefined;
dependents = [];
registerDependent(dependent) {
this.dependents.push(dependent);
}
removeDependent(dependent) {
this.dependents = this.dependents.filter(d => d !== dependent);
}
receive(event) {
/* we will update this as soon as we support incremental update patterns */
switch (event.type) {
case "full" /* CacheInvalidationEventType.Full */:
this.value = undefined;
break;
default:
(0, assert_1.assertUnreachable)(event.type);
}
/* in the future we want to defer this *after* the dataflow is re-computed, then all receivers can decide whether they need to update */
this.notifyDependents(event);
}
/**
* Notify all dependents of a cache invalidation event.
*/
notifyDependents(event) {
for (const dependent of this.dependents) {
dependent.receive(event);
}
}
/**
* Get the cached value or compute it if not present.
* This will, by default, not trigger any {@link notifyDependents} calls, as this is only a cache retrieval.
*/
computeIfAbsent(force, compute) {
if (this.value === undefined || force) {
this.value = compute();
}
return this.value;
}
}
exports.FlowrCache = FlowrCache;
//# sourceMappingURL=flowr-cache.js.map