You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.5 KiB
40 lines
1.5 KiB
4 months ago
|
import { parentEntrySlot } from "./context.js";
|
||
|
import { hasOwnProperty, maybeUnsubscribe, arrayFromSet, } from "./helpers.js";
|
||
|
const EntryMethods = {
|
||
|
setDirty: true,
|
||
|
dispose: true,
|
||
|
forget: true, // Fully remove parent Entry from LRU cache and computation graph
|
||
|
};
|
||
|
export function dep(options) {
|
||
|
const depsByKey = new Map();
|
||
|
const subscribe = options && options.subscribe;
|
||
|
function depend(key) {
|
||
|
const parent = parentEntrySlot.getValue();
|
||
|
if (parent) {
|
||
|
let dep = depsByKey.get(key);
|
||
|
if (!dep) {
|
||
|
depsByKey.set(key, dep = new Set);
|
||
|
}
|
||
|
parent.dependOn(dep);
|
||
|
if (typeof subscribe === "function") {
|
||
|
maybeUnsubscribe(dep);
|
||
|
dep.unsubscribe = subscribe(key);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
depend.dirty = function dirty(key, entryMethodName) {
|
||
|
const dep = depsByKey.get(key);
|
||
|
if (dep) {
|
||
|
const m = (entryMethodName &&
|
||
|
hasOwnProperty.call(EntryMethods, entryMethodName)) ? entryMethodName : "setDirty";
|
||
|
// We have to use arrayFromSet(dep).forEach instead of dep.forEach,
|
||
|
// because modifying a Set while iterating over it can cause elements in
|
||
|
// the Set to be removed from the Set before they've been iterated over.
|
||
|
arrayFromSet(dep).forEach(entry => entry[m]());
|
||
|
depsByKey.delete(key);
|
||
|
maybeUnsubscribe(dep);
|
||
|
}
|
||
|
};
|
||
|
return depend;
|
||
|
}
|
||
|
//# sourceMappingURL=dep.js.map
|