@tanstack/store
Version:
Framework agnostic type-safe store w/ reactive framework adapters
108 lines (107 loc) • 3.33 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const derived = require("./derived.cjs");
const __storeToDerived = /* @__PURE__ */ new WeakMap();
const __derivedToStore = /* @__PURE__ */ new WeakMap();
const __depsThatHaveWrittenThisTick = {
current: []
};
let __isFlushing = false;
let __batchDepth = 0;
const __pendingUpdates = /* @__PURE__ */ new Set();
const __initialBatchValues = /* @__PURE__ */ new Map();
function __flush_internals(relatedVals) {
const sorted = Array.from(relatedVals).sort((a, b) => {
if (a instanceof derived.Derived && a.options.deps.includes(b)) return 1;
if (b instanceof derived.Derived && b.options.deps.includes(a)) return -1;
return 0;
});
for (const derived2 of sorted) {
if (__depsThatHaveWrittenThisTick.current.includes(derived2)) {
continue;
}
__depsThatHaveWrittenThisTick.current.push(derived2);
derived2.recompute();
const stores = __derivedToStore.get(derived2);
if (stores) {
for (const store of stores) {
const relatedLinkedDerivedVals = __storeToDerived.get(store);
if (!relatedLinkedDerivedVals) continue;
__flush_internals(relatedLinkedDerivedVals);
}
}
}
}
function __notifyListeners(store) {
store.listeners.forEach(
(listener) => listener({
prevVal: store.prevState,
currentVal: store.state
})
);
}
function __notifyDerivedListeners(derived2) {
derived2.listeners.forEach(
(listener) => listener({
prevVal: derived2.prevState,
currentVal: derived2.state
})
);
}
function __flush(store) {
if (__batchDepth > 0 && !__initialBatchValues.has(store)) {
__initialBatchValues.set(store, store.prevState);
}
__pendingUpdates.add(store);
if (__batchDepth > 0) return;
if (__isFlushing) return;
try {
__isFlushing = true;
while (__pendingUpdates.size > 0) {
const stores = Array.from(__pendingUpdates);
__pendingUpdates.clear();
for (const store2 of stores) {
const prevState = __initialBatchValues.get(store2) ?? store2.prevState;
store2.prevState = prevState;
__notifyListeners(store2);
}
for (const store2 of stores) {
const derivedVals = __storeToDerived.get(store2);
if (!derivedVals) continue;
__depsThatHaveWrittenThisTick.current.push(store2);
__flush_internals(derivedVals);
}
for (const store2 of stores) {
const derivedVals = __storeToDerived.get(store2);
if (!derivedVals) continue;
for (const derived2 of derivedVals) {
__notifyDerivedListeners(derived2);
}
}
}
} finally {
__isFlushing = false;
__depsThatHaveWrittenThisTick.current = [];
__initialBatchValues.clear();
}
}
function batch(fn) {
__batchDepth++;
try {
fn();
} finally {
__batchDepth--;
if (__batchDepth === 0) {
const pendingUpdateToFlush = Array.from(__pendingUpdates)[0];
if (pendingUpdateToFlush) {
__flush(pendingUpdateToFlush);
}
}
}
}
exports.__depsThatHaveWrittenThisTick = __depsThatHaveWrittenThisTick;
exports.__derivedToStore = __derivedToStore;
exports.__flush = __flush;
exports.__storeToDerived = __storeToDerived;
exports.batch = batch;
//# sourceMappingURL=scheduler.cjs.map
;