@tanstack/store
Version:
Framework agnostic type-safe store w/ reactive framework adapters
346 lines (345 loc) • 8.52 kB
JavaScript
var ReactiveFlags = /* @__PURE__ */ ((ReactiveFlags2) => {
ReactiveFlags2[ReactiveFlags2["None"] = 0] = "None";
ReactiveFlags2[ReactiveFlags2["Mutable"] = 1] = "Mutable";
ReactiveFlags2[ReactiveFlags2["Watching"] = 2] = "Watching";
ReactiveFlags2[ReactiveFlags2["RecursedCheck"] = 4] = "RecursedCheck";
ReactiveFlags2[ReactiveFlags2["Recursed"] = 8] = "Recursed";
ReactiveFlags2[ReactiveFlags2["Dirty"] = 16] = "Dirty";
ReactiveFlags2[ReactiveFlags2["Pending"] = 32] = "Pending";
return ReactiveFlags2;
})(ReactiveFlags || {});
function createReactiveSystem({
update,
notify,
unwatched
}) {
return {
link: link2,
unlink: unlink2,
propagate: propagate2,
checkDirty: checkDirty2,
shallowPropagate: shallowPropagate2
};
function link2(dep, sub, version) {
const prevDep = sub.depsTail;
if (prevDep !== void 0 && prevDep.dep === dep) {
return;
}
const nextDep = prevDep !== void 0 ? prevDep.nextDep : sub.deps;
if (nextDep !== void 0 && nextDep.dep === dep) {
nextDep.version = version;
sub.depsTail = nextDep;
return;
}
const prevSub = dep.subsTail;
if (prevSub !== void 0 && prevSub.version === version && prevSub.sub === sub) {
return;
}
const newLink = sub.depsTail = dep.subsTail = {
version,
dep,
sub,
prevDep,
nextDep,
prevSub,
nextSub: void 0
};
if (nextDep !== void 0) {
nextDep.prevDep = newLink;
}
if (prevDep !== void 0) {
prevDep.nextDep = newLink;
} else {
sub.deps = newLink;
}
if (prevSub !== void 0) {
prevSub.nextSub = newLink;
} else {
dep.subs = newLink;
}
}
function unlink2(link3, sub = link3.sub) {
const dep = link3.dep;
const prevDep = link3.prevDep;
const nextDep = link3.nextDep;
const nextSub = link3.nextSub;
const prevSub = link3.prevSub;
if (nextDep !== void 0) {
nextDep.prevDep = prevDep;
} else {
sub.depsTail = prevDep;
}
if (prevDep !== void 0) {
prevDep.nextDep = nextDep;
} else {
sub.deps = nextDep;
}
if (nextSub !== void 0) {
nextSub.prevSub = prevSub;
} else {
dep.subsTail = prevSub;
}
if (prevSub !== void 0) {
prevSub.nextSub = nextSub;
} else if ((dep.subs = nextSub) === void 0) {
unwatched(dep);
}
return nextDep;
}
function propagate2(link3) {
let next = link3.nextSub;
let stack;
top: do {
const sub = link3.sub;
let flags = sub.flags;
if (!(flags & (4 | 8 | 16 | 32))) {
sub.flags = flags | 32;
} else if (!(flags & (4 | 8))) {
flags = 0;
} else if (!(flags & 4)) {
sub.flags = flags & -9 | 32;
} else if (!(flags & (16 | 32)) && isValidLink(link3, sub)) {
sub.flags = flags | (8 | 32);
flags &= 1;
} else {
flags = 0;
}
if (flags & 2) {
notify(sub);
}
if (flags & 1) {
const subSubs = sub.subs;
if (subSubs !== void 0) {
const nextSub = (link3 = subSubs).nextSub;
if (nextSub !== void 0) {
stack = { value: next, prev: stack };
next = nextSub;
}
continue;
}
}
if ((link3 = next) !== void 0) {
next = link3.nextSub;
continue;
}
while (stack !== void 0) {
link3 = stack.value;
stack = stack.prev;
if (link3 !== void 0) {
next = link3.nextSub;
continue top;
}
}
break;
} while (true);
}
function checkDirty2(link3, sub) {
let stack;
let checkDepth = 0;
let dirty = false;
top: do {
const dep = link3.dep;
const flags = dep.flags;
if (sub.flags & 16) {
dirty = true;
} else if ((flags & (1 | 16)) === (1 | 16)) {
if (update(dep)) {
const subs = dep.subs;
if (subs.nextSub !== void 0) {
shallowPropagate2(subs);
}
dirty = true;
}
} else if ((flags & (1 | 32)) === (1 | 32)) {
if (link3.nextSub !== void 0 || link3.prevSub !== void 0) {
stack = { value: link3, prev: stack };
}
link3 = dep.deps;
sub = dep;
++checkDepth;
continue;
}
if (!dirty) {
const nextDep = link3.nextDep;
if (nextDep !== void 0) {
link3 = nextDep;
continue;
}
}
while (checkDepth--) {
const firstSub = sub.subs;
const hasMultipleSubs = firstSub.nextSub !== void 0;
if (hasMultipleSubs) {
link3 = stack.value;
stack = stack.prev;
} else {
link3 = firstSub;
}
if (dirty) {
if (update(sub)) {
if (hasMultipleSubs) {
shallowPropagate2(firstSub);
}
sub = link3.sub;
continue;
}
dirty = false;
} else {
sub.flags &= -33;
}
sub = link3.sub;
const nextDep = link3.nextDep;
if (nextDep !== void 0) {
link3 = nextDep;
continue top;
}
}
return dirty;
} while (true);
}
function shallowPropagate2(link3) {
do {
const sub = link3.sub;
const flags = sub.flags;
if ((flags & (32 | 16)) === 32) {
sub.flags = flags | 16;
if ((flags & (2 | 4)) === 2) {
notify(sub);
}
}
} while ((link3 = link3.nextSub) !== void 0);
}
function isValidLink(checkLink, sub) {
let link3 = sub.depsTail;
while (link3 !== void 0) {
if (link3 === checkLink) {
return true;
}
link3 = link3.prevDep;
}
return false;
}
}
let batchDepth = 0;
let notifyIndex = 0;
let queuedLength = 0;
const queued = [];
const { link, unlink, propagate, checkDirty, shallowPropagate } = createReactiveSystem({
update(node) {
if (node.depsTail !== void 0) {
return updateComputed(node);
} else {
return updateSignal(node);
}
},
notify(effect2) {
let insertIndex = queuedLength;
let firstInsertedIndex = insertIndex;
do {
queued[insertIndex++] = effect2;
effect2.flags &= -3;
effect2 = effect2.subs?.sub;
if (effect2 === void 0 || !(effect2.flags & 2)) {
break;
}
} while (true);
queuedLength = insertIndex;
while (firstInsertedIndex < --insertIndex) {
const left = queued[firstInsertedIndex];
queued[firstInsertedIndex++] = queued[insertIndex];
queued[insertIndex] = left;
}
},
unwatched(node) {
if (!(node.flags & 1)) {
effectScopeOper.call(node);
} else if (node.depsTail !== void 0) {
node.depsTail = void 0;
node.flags = 1 | 16;
purgeDeps(node);
}
}
});
function getBatchDepth() {
return batchDepth;
}
function startBatch() {
++batchDepth;
}
function endBatch() {
if (!--batchDepth) {
flush();
}
}
function updateComputed(c) {
c.depsTail = void 0;
c.flags = 1 | 4;
try {
const oldValue = c.value;
return oldValue !== (c.value = c.getter(oldValue));
} finally {
c.flags &= -5;
purgeDeps(c);
}
}
function updateSignal(s) {
s.flags = 1;
return s.currentValue !== (s.currentValue = s.pendingValue);
}
function run(e) {
const flags = e.flags;
if (flags & 16 || flags & 32 && checkDirty(e.deps, e)) {
e.depsTail = void 0;
e.flags = 2 | 4;
try {
;
e.fn();
} finally {
e.flags &= -5;
purgeDeps(e);
}
} else {
e.flags = 2;
}
}
function flush() {
try {
while (notifyIndex < queuedLength) {
const effect2 = queued[notifyIndex];
queued[notifyIndex++] = void 0;
run(effect2);
}
} finally {
while (notifyIndex < queuedLength) {
const effect2 = queued[notifyIndex];
queued[notifyIndex++] = void 0;
effect2.flags |= 2 | 8;
}
notifyIndex = 0;
queuedLength = 0;
}
}
function effectScopeOper() {
this.depsTail = void 0;
this.flags = 0;
purgeDeps(this);
const sub = this.subs;
if (sub !== void 0) {
unlink(sub);
}
}
function purgeDeps(sub) {
const depsTail = sub.depsTail;
let dep = depsTail !== void 0 ? depsTail.nextDep : sub.deps;
while (dep !== void 0) {
dep = unlink(dep, sub);
}
}
export {
ReactiveFlags,
createReactiveSystem,
endBatch,
getBatchDepth,
startBatch
};
//# sourceMappingURL=alien.js.map