1001 lines
31 KiB
JavaScript
1001 lines
31 KiB
JavaScript
import {
|
|
require_react
|
|
} from "./chunk-7ZNOTH45.js";
|
|
import {
|
|
__toESM
|
|
} from "./chunk-V4OQ3NZ2.js";
|
|
|
|
// node_modules/jotai/esm/vanilla/internals.mjs
|
|
function hasInitialValue(atom2) {
|
|
return "init" in atom2;
|
|
}
|
|
function isActuallyWritableAtom(atom2) {
|
|
return !!atom2.write;
|
|
}
|
|
function hasOnMount(atom2) {
|
|
return !!atom2.onMount;
|
|
}
|
|
function isAtomStateInitialized(atomState) {
|
|
return "v" in atomState || "e" in atomState;
|
|
}
|
|
function returnAtomValue(atomState) {
|
|
if ("e" in atomState) {
|
|
throw atomState.e;
|
|
}
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !("v" in atomState)) {
|
|
throw new Error("[Bug] atom state is not initialized");
|
|
}
|
|
return atomState.v;
|
|
}
|
|
function isPromiseLike(p) {
|
|
return typeof (p == null ? void 0 : p.then) === "function";
|
|
}
|
|
function addPendingPromiseToDependency(atom2, promise, dependencyAtomState) {
|
|
if (!dependencyAtomState.p.has(atom2)) {
|
|
dependencyAtomState.p.add(atom2);
|
|
const cleanup = () => dependencyAtomState.p.delete(atom2);
|
|
promise.then(cleanup, cleanup);
|
|
}
|
|
}
|
|
function getMountedOrPendingDependents(atom2, atomState, mountedMap) {
|
|
const mounted = mountedMap.get(atom2);
|
|
const mountedDependents = mounted == null ? void 0 : mounted.t;
|
|
const pendingDependents = atomState.p;
|
|
if (!(mountedDependents == null ? void 0 : mountedDependents.size)) {
|
|
return pendingDependents;
|
|
}
|
|
if (!pendingDependents.size) {
|
|
return mountedDependents;
|
|
}
|
|
const dependents = new Set(mountedDependents);
|
|
for (const a of pendingDependents) {
|
|
dependents.add(a);
|
|
}
|
|
return dependents;
|
|
}
|
|
var BUILDING_BLOCK_atomRead = (_store, atom2, ...params) => atom2.read(...params);
|
|
var BUILDING_BLOCK_atomWrite = (_store, atom2, ...params) => atom2.write(...params);
|
|
var BUILDING_BLOCK_atomOnInit = (store, atom2) => {
|
|
var _a;
|
|
return (_a = atom2.INTERNAL_onInit) == null ? void 0 : _a.call(atom2, store);
|
|
};
|
|
var BUILDING_BLOCK_atomOnMount = (_store, atom2, setAtom) => {
|
|
var _a;
|
|
return (_a = atom2.onMount) == null ? void 0 : _a.call(atom2, setAtom);
|
|
};
|
|
var BUILDING_BLOCK_ensureAtomState = (store, atom2) => {
|
|
var _a;
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const atomStateMap = buildingBlocks[0];
|
|
const storeHooks = buildingBlocks[6];
|
|
const atomOnInit = buildingBlocks[9];
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !atom2) {
|
|
throw new Error("Atom is undefined or null");
|
|
}
|
|
let atomState = atomStateMap.get(atom2);
|
|
if (!atomState) {
|
|
atomState = { d: /* @__PURE__ */ new Map(), p: /* @__PURE__ */ new Set(), n: 0 };
|
|
atomStateMap.set(atom2, atomState);
|
|
(_a = storeHooks.i) == null ? void 0 : _a.call(storeHooks, atom2);
|
|
atomOnInit == null ? void 0 : atomOnInit(store, atom2);
|
|
}
|
|
return atomState;
|
|
};
|
|
var BUILDING_BLOCK_flushCallbacks = (store) => {
|
|
var _a;
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const changedAtoms = buildingBlocks[3];
|
|
const mountCallbacks = buildingBlocks[4];
|
|
const unmountCallbacks = buildingBlocks[5];
|
|
const storeHooks = buildingBlocks[6];
|
|
const recomputeInvalidatedAtoms = buildingBlocks[13];
|
|
if (!storeHooks.f && !changedAtoms.size && !mountCallbacks.size && !unmountCallbacks.size) {
|
|
return;
|
|
}
|
|
const errors = [];
|
|
const call = (fn) => {
|
|
try {
|
|
fn();
|
|
} catch (e) {
|
|
errors.push(e);
|
|
}
|
|
};
|
|
do {
|
|
if (storeHooks.f) {
|
|
call(storeHooks.f);
|
|
}
|
|
const callbacks = /* @__PURE__ */ new Set();
|
|
for (const atom2 of changedAtoms) {
|
|
const listeners = (_a = mountedMap.get(atom2)) == null ? void 0 : _a.l;
|
|
if (listeners) {
|
|
for (const listener of listeners) {
|
|
callbacks.add(listener);
|
|
}
|
|
}
|
|
}
|
|
changedAtoms.clear();
|
|
for (const fn of unmountCallbacks) {
|
|
callbacks.add(fn);
|
|
}
|
|
unmountCallbacks.clear();
|
|
for (const fn of mountCallbacks) {
|
|
callbacks.add(fn);
|
|
}
|
|
mountCallbacks.clear();
|
|
for (const fn of callbacks) {
|
|
call(fn);
|
|
}
|
|
if (changedAtoms.size) {
|
|
recomputeInvalidatedAtoms(store);
|
|
}
|
|
} while (changedAtoms.size || unmountCallbacks.size || mountCallbacks.size);
|
|
if (errors.length) {
|
|
throw new AggregateError(errors);
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_recomputeInvalidatedAtoms = (store) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const invalidatedAtoms = buildingBlocks[2];
|
|
const changedAtoms = buildingBlocks[3];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const readAtomState = buildingBlocks[14];
|
|
const mountDependencies = buildingBlocks[17];
|
|
if (!changedAtoms.size) {
|
|
return;
|
|
}
|
|
const sortedReversedAtoms = [];
|
|
const sortedReversedStates = [];
|
|
const visiting = /* @__PURE__ */ new WeakSet();
|
|
const visited = /* @__PURE__ */ new WeakSet();
|
|
const stackAtoms = [];
|
|
const stackStates = [];
|
|
for (const atom2 of changedAtoms) {
|
|
stackAtoms.push(atom2);
|
|
stackStates.push(ensureAtomState(store, atom2));
|
|
}
|
|
while (stackAtoms.length) {
|
|
const top = stackAtoms.length - 1;
|
|
const a = stackAtoms[top];
|
|
const aState = stackStates[top];
|
|
if (visited.has(a)) {
|
|
stackAtoms.pop();
|
|
stackStates.pop();
|
|
continue;
|
|
}
|
|
if (visiting.has(a)) {
|
|
if (invalidatedAtoms.get(a) === aState.n) {
|
|
sortedReversedAtoms.push(a);
|
|
sortedReversedStates.push(aState);
|
|
} else if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && invalidatedAtoms.has(a)) {
|
|
throw new Error("[Bug] invalidated atom exists");
|
|
}
|
|
visited.add(a);
|
|
stackAtoms.pop();
|
|
stackStates.pop();
|
|
continue;
|
|
}
|
|
visiting.add(a);
|
|
for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
|
|
if (!visiting.has(d)) {
|
|
stackAtoms.push(d);
|
|
stackStates.push(ensureAtomState(store, d));
|
|
}
|
|
}
|
|
}
|
|
for (let i = sortedReversedAtoms.length - 1; i >= 0; --i) {
|
|
const a = sortedReversedAtoms[i];
|
|
const aState = sortedReversedStates[i];
|
|
let hasChangedDeps = false;
|
|
for (const dep of aState.d.keys()) {
|
|
if (dep !== a && changedAtoms.has(dep)) {
|
|
hasChangedDeps = true;
|
|
break;
|
|
}
|
|
}
|
|
if (hasChangedDeps) {
|
|
invalidatedAtoms.set(a, aState.n);
|
|
readAtomState(store, a);
|
|
mountDependencies(store, a);
|
|
}
|
|
invalidatedAtoms.delete(a);
|
|
}
|
|
};
|
|
var storeMutationSet = /* @__PURE__ */ new WeakSet();
|
|
var BUILDING_BLOCK_readAtomState = (store, atom2) => {
|
|
var _a, _b;
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const invalidatedAtoms = buildingBlocks[2];
|
|
const changedAtoms = buildingBlocks[3];
|
|
const storeHooks = buildingBlocks[6];
|
|
const atomRead = buildingBlocks[7];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const flushCallbacks = buildingBlocks[12];
|
|
const recomputeInvalidatedAtoms = buildingBlocks[13];
|
|
const readAtomState = buildingBlocks[14];
|
|
const writeAtomState = buildingBlocks[16];
|
|
const mountDependencies = buildingBlocks[17];
|
|
const setAtomStateValueOrPromise = buildingBlocks[20];
|
|
const registerAbortHandler = buildingBlocks[26];
|
|
const storeEpochHolder = buildingBlocks[28];
|
|
const atomState = ensureAtomState(store, atom2);
|
|
const storeEpochNumber = storeEpochHolder[0];
|
|
if (isAtomStateInitialized(atomState)) {
|
|
if (
|
|
// If the atom is mounted, we can use cached atom state,
|
|
// because it should have been updated by dependencies.
|
|
// We can't use the cache if the atom is invalidated.
|
|
mountedMap.has(atom2) && invalidatedAtoms.get(atom2) !== atomState.n || // If atom is not mounted, we can use cached atom state,
|
|
// only if store hasn't been mutated.
|
|
atomState.m === storeEpochNumber
|
|
) {
|
|
atomState.m = storeEpochNumber;
|
|
return atomState;
|
|
}
|
|
let hasChangedDeps = false;
|
|
for (const [a, n] of atomState.d) {
|
|
if (readAtomState(store, a).n !== n) {
|
|
hasChangedDeps = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!hasChangedDeps) {
|
|
atomState.m = storeEpochNumber;
|
|
return atomState;
|
|
}
|
|
}
|
|
let isSync = true;
|
|
const prevDeps = new Set(atomState.d.keys());
|
|
const pruneDependencies = () => {
|
|
for (const a of prevDeps) {
|
|
atomState.d.delete(a);
|
|
}
|
|
};
|
|
const mountDependenciesIfAsync = () => {
|
|
if (mountedMap.has(atom2)) {
|
|
const shouldRecompute = !changedAtoms.size;
|
|
mountDependencies(store, atom2);
|
|
if (shouldRecompute) {
|
|
recomputeInvalidatedAtoms(store);
|
|
flushCallbacks(store);
|
|
}
|
|
}
|
|
};
|
|
const getter = (a) => {
|
|
var _a2;
|
|
if (a === atom2) {
|
|
const aState2 = ensureAtomState(store, a);
|
|
if (!isAtomStateInitialized(aState2)) {
|
|
if (hasInitialValue(a)) {
|
|
setAtomStateValueOrPromise(store, a, a.init);
|
|
} else {
|
|
throw new Error("no atom init");
|
|
}
|
|
}
|
|
return returnAtomValue(aState2);
|
|
}
|
|
const aState = readAtomState(store, a);
|
|
try {
|
|
return returnAtomValue(aState);
|
|
} finally {
|
|
prevDeps.delete(a);
|
|
atomState.d.set(a, aState.n);
|
|
if (isPromiseLike(atomState.v)) {
|
|
addPendingPromiseToDependency(atom2, atomState.v, aState);
|
|
}
|
|
if (mountedMap.has(atom2)) {
|
|
(_a2 = mountedMap.get(a)) == null ? void 0 : _a2.t.add(atom2);
|
|
}
|
|
if (!isSync) {
|
|
mountDependenciesIfAsync();
|
|
}
|
|
}
|
|
};
|
|
let controller;
|
|
let setSelf;
|
|
const options = {
|
|
get signal() {
|
|
if (!controller) {
|
|
controller = new AbortController();
|
|
}
|
|
return controller.signal;
|
|
},
|
|
get setSelf() {
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
|
|
console.warn(
|
|
"[DEPRECATED] setSelf is deprecated and will be removed in v3."
|
|
);
|
|
}
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !isActuallyWritableAtom(atom2)) {
|
|
console.warn("setSelf function cannot be used with read-only atom");
|
|
}
|
|
if (!setSelf && isActuallyWritableAtom(atom2)) {
|
|
setSelf = (...args) => {
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && isSync) {
|
|
console.warn("setSelf function cannot be called in sync");
|
|
}
|
|
if (!isSync) {
|
|
try {
|
|
return writeAtomState(store, atom2, ...args);
|
|
} finally {
|
|
recomputeInvalidatedAtoms(store);
|
|
flushCallbacks(store);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
return setSelf;
|
|
}
|
|
};
|
|
const prevEpochNumber = atomState.n;
|
|
const prevInvalidated = invalidatedAtoms.get(atom2) === prevEpochNumber;
|
|
try {
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
|
|
storeMutationSet.delete(store);
|
|
}
|
|
const valueOrPromise = atomRead(store, atom2, getter, options);
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && storeMutationSet.has(store)) {
|
|
console.warn(
|
|
"Detected store mutation during atom read. This is not supported."
|
|
);
|
|
}
|
|
setAtomStateValueOrPromise(store, atom2, valueOrPromise);
|
|
if (isPromiseLike(valueOrPromise)) {
|
|
registerAbortHandler(store, valueOrPromise, () => controller == null ? void 0 : controller.abort());
|
|
const settle = () => {
|
|
pruneDependencies();
|
|
mountDependenciesIfAsync();
|
|
};
|
|
valueOrPromise.then(settle, settle);
|
|
} else {
|
|
pruneDependencies();
|
|
}
|
|
(_a = storeHooks.r) == null ? void 0 : _a.call(storeHooks, atom2);
|
|
atomState.m = storeEpochNumber;
|
|
return atomState;
|
|
} catch (error) {
|
|
delete atomState.v;
|
|
atomState.e = error;
|
|
++atomState.n;
|
|
atomState.m = storeEpochNumber;
|
|
return atomState;
|
|
} finally {
|
|
isSync = false;
|
|
if (atomState.n !== prevEpochNumber && prevInvalidated) {
|
|
invalidatedAtoms.set(atom2, atomState.n);
|
|
changedAtoms.add(atom2);
|
|
(_b = storeHooks.c) == null ? void 0 : _b.call(storeHooks, atom2);
|
|
}
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_invalidateDependents = (store, atom2) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const invalidatedAtoms = buildingBlocks[2];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const stack = [atom2];
|
|
while (stack.length) {
|
|
const a = stack.pop();
|
|
const aState = ensureAtomState(store, a);
|
|
for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
|
|
const dState = ensureAtomState(store, d);
|
|
if (invalidatedAtoms.get(d) !== dState.n) {
|
|
invalidatedAtoms.set(d, dState.n);
|
|
stack.push(d);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_writeAtomState = (store, atom2, ...args) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const changedAtoms = buildingBlocks[3];
|
|
const storeHooks = buildingBlocks[6];
|
|
const atomWrite = buildingBlocks[8];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const flushCallbacks = buildingBlocks[12];
|
|
const recomputeInvalidatedAtoms = buildingBlocks[13];
|
|
const readAtomState = buildingBlocks[14];
|
|
const invalidateDependents = buildingBlocks[15];
|
|
const writeAtomState = buildingBlocks[16];
|
|
const mountDependencies = buildingBlocks[17];
|
|
const setAtomStateValueOrPromise = buildingBlocks[20];
|
|
const storeEpochHolder = buildingBlocks[28];
|
|
let isSync = true;
|
|
const getter = (a) => returnAtomValue(readAtomState(store, a));
|
|
const setter = (a, ...args2) => {
|
|
var _a;
|
|
const aState = ensureAtomState(store, a);
|
|
try {
|
|
if (a === atom2) {
|
|
if (!hasInitialValue(a)) {
|
|
throw new Error("atom not writable");
|
|
}
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
|
|
storeMutationSet.add(store);
|
|
}
|
|
const prevEpochNumber = aState.n;
|
|
const v = args2[0];
|
|
setAtomStateValueOrPromise(store, a, v);
|
|
mountDependencies(store, a);
|
|
if (prevEpochNumber !== aState.n) {
|
|
++storeEpochHolder[0];
|
|
changedAtoms.add(a);
|
|
invalidateDependents(store, a);
|
|
(_a = storeHooks.c) == null ? void 0 : _a.call(storeHooks, a);
|
|
}
|
|
return void 0;
|
|
} else {
|
|
return writeAtomState(store, a, ...args2);
|
|
}
|
|
} finally {
|
|
if (!isSync) {
|
|
recomputeInvalidatedAtoms(store);
|
|
flushCallbacks(store);
|
|
}
|
|
}
|
|
};
|
|
try {
|
|
return atomWrite(store, atom2, getter, setter, ...args);
|
|
} finally {
|
|
isSync = false;
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_mountDependencies = (store, atom2) => {
|
|
var _a;
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const changedAtoms = buildingBlocks[3];
|
|
const storeHooks = buildingBlocks[6];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const invalidateDependents = buildingBlocks[15];
|
|
const mountAtom = buildingBlocks[18];
|
|
const unmountAtom = buildingBlocks[19];
|
|
const atomState = ensureAtomState(store, atom2);
|
|
const mounted = mountedMap.get(atom2);
|
|
if (mounted && atomState.d.size > 0) {
|
|
for (const [a, n] of atomState.d) {
|
|
if (!mounted.d.has(a)) {
|
|
const aState = ensureAtomState(store, a);
|
|
const aMounted = mountAtom(store, a);
|
|
aMounted.t.add(atom2);
|
|
mounted.d.add(a);
|
|
if (n !== aState.n) {
|
|
changedAtoms.add(a);
|
|
invalidateDependents(store, a);
|
|
(_a = storeHooks.c) == null ? void 0 : _a.call(storeHooks, a);
|
|
}
|
|
}
|
|
}
|
|
for (const a of mounted.d) {
|
|
if (!atomState.d.has(a)) {
|
|
mounted.d.delete(a);
|
|
const aMounted = unmountAtom(store, a);
|
|
aMounted == null ? void 0 : aMounted.t.delete(atom2);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_mountAtom = (store, atom2) => {
|
|
var _a;
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const mountCallbacks = buildingBlocks[4];
|
|
const storeHooks = buildingBlocks[6];
|
|
const atomOnMount = buildingBlocks[10];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const flushCallbacks = buildingBlocks[12];
|
|
const recomputeInvalidatedAtoms = buildingBlocks[13];
|
|
const readAtomState = buildingBlocks[14];
|
|
const writeAtomState = buildingBlocks[16];
|
|
const mountAtom = buildingBlocks[18];
|
|
const atomState = ensureAtomState(store, atom2);
|
|
let mounted = mountedMap.get(atom2);
|
|
if (!mounted) {
|
|
readAtomState(store, atom2);
|
|
for (const a of atomState.d.keys()) {
|
|
const aMounted = mountAtom(store, a);
|
|
aMounted.t.add(atom2);
|
|
}
|
|
mounted = {
|
|
l: /* @__PURE__ */ new Set(),
|
|
d: new Set(atomState.d.keys()),
|
|
t: /* @__PURE__ */ new Set()
|
|
};
|
|
mountedMap.set(atom2, mounted);
|
|
if (isActuallyWritableAtom(atom2) && hasOnMount(atom2)) {
|
|
const processOnMount = () => {
|
|
let isSync = true;
|
|
const setAtom = (...args) => {
|
|
try {
|
|
return writeAtomState(store, atom2, ...args);
|
|
} finally {
|
|
if (!isSync) {
|
|
recomputeInvalidatedAtoms(store);
|
|
flushCallbacks(store);
|
|
}
|
|
}
|
|
};
|
|
try {
|
|
const onUnmount = atomOnMount(store, atom2, setAtom);
|
|
if (onUnmount) {
|
|
mounted.u = () => {
|
|
isSync = true;
|
|
try {
|
|
onUnmount();
|
|
} finally {
|
|
isSync = false;
|
|
}
|
|
};
|
|
}
|
|
} finally {
|
|
isSync = false;
|
|
}
|
|
};
|
|
mountCallbacks.add(processOnMount);
|
|
}
|
|
(_a = storeHooks.m) == null ? void 0 : _a.call(storeHooks, atom2);
|
|
}
|
|
return mounted;
|
|
};
|
|
var BUILDING_BLOCK_unmountAtom = (store, atom2) => {
|
|
var _a, _b;
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const mountedMap = buildingBlocks[1];
|
|
const unmountCallbacks = buildingBlocks[5];
|
|
const storeHooks = buildingBlocks[6];
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const unmountAtom = buildingBlocks[19];
|
|
const atomState = ensureAtomState(store, atom2);
|
|
let mounted = mountedMap.get(atom2);
|
|
if (!mounted || mounted.l.size) {
|
|
return mounted;
|
|
}
|
|
let isDependent = false;
|
|
for (const a of mounted.t) {
|
|
if ((_a = mountedMap.get(a)) == null ? void 0 : _a.d.has(atom2)) {
|
|
isDependent = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!isDependent) {
|
|
if (mounted.u) {
|
|
unmountCallbacks.add(mounted.u);
|
|
}
|
|
mounted = void 0;
|
|
mountedMap.delete(atom2);
|
|
for (const a of atomState.d.keys()) {
|
|
const aMounted = unmountAtom(store, a);
|
|
aMounted == null ? void 0 : aMounted.t.delete(atom2);
|
|
}
|
|
(_b = storeHooks.u) == null ? void 0 : _b.call(storeHooks, atom2);
|
|
return void 0;
|
|
}
|
|
return mounted;
|
|
};
|
|
var BUILDING_BLOCK_setAtomStateValueOrPromise = (store, atom2, valueOrPromise) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const ensureAtomState = buildingBlocks[11];
|
|
const abortPromise = buildingBlocks[27];
|
|
const atomState = ensureAtomState(store, atom2);
|
|
const hasPrevValue = "v" in atomState;
|
|
const prevValue = atomState.v;
|
|
if (isPromiseLike(valueOrPromise)) {
|
|
for (const a of atomState.d.keys()) {
|
|
addPendingPromiseToDependency(
|
|
atom2,
|
|
valueOrPromise,
|
|
ensureAtomState(store, a)
|
|
);
|
|
}
|
|
}
|
|
atomState.v = valueOrPromise;
|
|
delete atomState.e;
|
|
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
|
|
++atomState.n;
|
|
if (isPromiseLike(prevValue)) {
|
|
abortPromise(store, prevValue);
|
|
}
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_storeGet = (store, atom2) => {
|
|
const readAtomState = getInternalBuildingBlocks(store)[14];
|
|
return returnAtomValue(readAtomState(store, atom2));
|
|
};
|
|
var BUILDING_BLOCK_storeSet = (store, atom2, ...args) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const changedAtoms = buildingBlocks[3];
|
|
const flushCallbacks = buildingBlocks[12];
|
|
const recomputeInvalidatedAtoms = buildingBlocks[13];
|
|
const writeAtomState = buildingBlocks[16];
|
|
const prevChangedAtomsSize = changedAtoms.size;
|
|
try {
|
|
return writeAtomState(store, atom2, ...args);
|
|
} finally {
|
|
if (changedAtoms.size !== prevChangedAtomsSize) {
|
|
recomputeInvalidatedAtoms(store);
|
|
flushCallbacks(store);
|
|
}
|
|
}
|
|
};
|
|
var BUILDING_BLOCK_storeSub = (store, atom2, listener) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const flushCallbacks = buildingBlocks[12];
|
|
const mountAtom = buildingBlocks[18];
|
|
const unmountAtom = buildingBlocks[19];
|
|
const mounted = mountAtom(store, atom2);
|
|
const listeners = mounted.l;
|
|
listeners.add(listener);
|
|
flushCallbacks(store);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
unmountAtom(store, atom2);
|
|
flushCallbacks(store);
|
|
};
|
|
};
|
|
var BUILDING_BLOCK_registerAbortHandler = (store, promise, abortHandler) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const abortHandlersMap = buildingBlocks[25];
|
|
let abortHandlers = abortHandlersMap.get(promise);
|
|
if (!abortHandlers) {
|
|
abortHandlers = /* @__PURE__ */ new Set();
|
|
abortHandlersMap.set(promise, abortHandlers);
|
|
const cleanup = () => abortHandlersMap.delete(promise);
|
|
promise.then(cleanup, cleanup);
|
|
}
|
|
abortHandlers.add(abortHandler);
|
|
};
|
|
var BUILDING_BLOCK_abortPromise = (store, promise) => {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const abortHandlersMap = buildingBlocks[25];
|
|
const abortHandlers = abortHandlersMap.get(promise);
|
|
abortHandlers == null ? void 0 : abortHandlers.forEach((fn) => fn());
|
|
};
|
|
var buildingBlockMap = /* @__PURE__ */ new WeakMap();
|
|
var getInternalBuildingBlocks = (store) => {
|
|
const buildingBlocks = buildingBlockMap.get(store);
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !buildingBlocks) {
|
|
throw new Error(
|
|
"Store must be created by buildStore to read its building blocks"
|
|
);
|
|
}
|
|
return buildingBlocks;
|
|
};
|
|
function getBuildingBlocks(store) {
|
|
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
const enhanceBuildingBlocks = buildingBlocks[24];
|
|
if (enhanceBuildingBlocks) {
|
|
return enhanceBuildingBlocks(buildingBlocks);
|
|
}
|
|
return buildingBlocks;
|
|
}
|
|
function buildStore(...buildArgs) {
|
|
const store = {
|
|
get(atom2) {
|
|
const storeGet = getInternalBuildingBlocks(store)[21];
|
|
return storeGet(store, atom2);
|
|
},
|
|
set(atom2, ...args) {
|
|
const storeSet = getInternalBuildingBlocks(store)[22];
|
|
return storeSet(store, atom2, ...args);
|
|
},
|
|
sub(atom2, listener) {
|
|
const storeSub = getInternalBuildingBlocks(store)[23];
|
|
return storeSub(store, atom2, listener);
|
|
}
|
|
};
|
|
const buildingBlocks = [
|
|
// store state
|
|
/* @__PURE__ */ new WeakMap(),
|
|
// atomStateMap
|
|
/* @__PURE__ */ new WeakMap(),
|
|
// mountedMap
|
|
/* @__PURE__ */ new WeakMap(),
|
|
// invalidatedAtoms
|
|
/* @__PURE__ */ new Set(),
|
|
// changedAtoms
|
|
/* @__PURE__ */ new Set(),
|
|
// mountCallbacks
|
|
/* @__PURE__ */ new Set(),
|
|
// unmountCallbacks
|
|
{},
|
|
// storeHooks
|
|
// atom interceptors
|
|
BUILDING_BLOCK_atomRead,
|
|
BUILDING_BLOCK_atomWrite,
|
|
BUILDING_BLOCK_atomOnInit,
|
|
BUILDING_BLOCK_atomOnMount,
|
|
// building-block functions
|
|
BUILDING_BLOCK_ensureAtomState,
|
|
BUILDING_BLOCK_flushCallbacks,
|
|
BUILDING_BLOCK_recomputeInvalidatedAtoms,
|
|
BUILDING_BLOCK_readAtomState,
|
|
BUILDING_BLOCK_invalidateDependents,
|
|
BUILDING_BLOCK_writeAtomState,
|
|
BUILDING_BLOCK_mountDependencies,
|
|
BUILDING_BLOCK_mountAtom,
|
|
BUILDING_BLOCK_unmountAtom,
|
|
BUILDING_BLOCK_setAtomStateValueOrPromise,
|
|
BUILDING_BLOCK_storeGet,
|
|
BUILDING_BLOCK_storeSet,
|
|
BUILDING_BLOCK_storeSub,
|
|
void 0,
|
|
// abortable promise support
|
|
/* @__PURE__ */ new WeakMap(),
|
|
// abortHandlersMap
|
|
BUILDING_BLOCK_registerAbortHandler,
|
|
BUILDING_BLOCK_abortPromise,
|
|
// store epoch
|
|
[0]
|
|
].map((fn, i) => buildArgs[i] || fn);
|
|
buildingBlockMap.set(store, Object.freeze(buildingBlocks));
|
|
return store;
|
|
}
|
|
|
|
// node_modules/jotai/esm/vanilla.mjs
|
|
var keyCount = 0;
|
|
function atom(read, write) {
|
|
const key = `atom${++keyCount}`;
|
|
const config = {
|
|
toString() {
|
|
return (import.meta.env ? import.meta.env.MODE : void 0) !== "production" && this.debugLabel ? key + ":" + this.debugLabel : key;
|
|
}
|
|
};
|
|
if (typeof read === "function") {
|
|
config.read = read;
|
|
} else {
|
|
config.init = read;
|
|
config.read = defaultRead;
|
|
config.write = defaultWrite;
|
|
}
|
|
if (write) {
|
|
config.write = write;
|
|
}
|
|
return config;
|
|
}
|
|
function defaultRead(get) {
|
|
return get(this);
|
|
}
|
|
function defaultWrite(get, set, arg) {
|
|
return set(
|
|
this,
|
|
typeof arg === "function" ? arg(get(this)) : arg
|
|
);
|
|
}
|
|
var overriddenCreateStore;
|
|
function INTERNAL_overrideCreateStore(fn) {
|
|
overriddenCreateStore = fn(overriddenCreateStore);
|
|
}
|
|
function createStore() {
|
|
if (overriddenCreateStore) {
|
|
return overriddenCreateStore();
|
|
}
|
|
return buildStore();
|
|
}
|
|
var defaultStore;
|
|
function getDefaultStore() {
|
|
if (!defaultStore) {
|
|
defaultStore = createStore();
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
|
|
globalThis.__JOTAI_DEFAULT_STORE__ || (globalThis.__JOTAI_DEFAULT_STORE__ = defaultStore);
|
|
if (globalThis.__JOTAI_DEFAULT_STORE__ !== defaultStore) {
|
|
console.warn(
|
|
"Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return defaultStore;
|
|
}
|
|
|
|
// node_modules/jotai/esm/react.mjs
|
|
var import_react = __toESM(require_react(), 1);
|
|
var StoreContext = (0, import_react.createContext)(
|
|
void 0
|
|
);
|
|
function useStore(options) {
|
|
const store = (0, import_react.useContext)(StoreContext);
|
|
return (options == null ? void 0 : options.store) || store || getDefaultStore();
|
|
}
|
|
function Provider({
|
|
children,
|
|
store
|
|
}) {
|
|
const storeRef = (0, import_react.useRef)(null);
|
|
if (store) {
|
|
return (0, import_react.createElement)(StoreContext.Provider, { value: store }, children);
|
|
}
|
|
if (storeRef.current === null) {
|
|
storeRef.current = createStore();
|
|
}
|
|
return (0, import_react.createElement)(
|
|
StoreContext.Provider,
|
|
{
|
|
// TODO: If this is not a false positive, consider using useState instead of useRef like https://github.com/pmndrs/jotai/pull/2771
|
|
// eslint-disable-next-line react-hooks/refs
|
|
value: storeRef.current
|
|
},
|
|
children
|
|
);
|
|
}
|
|
var isPromiseLike2 = (x) => typeof (x == null ? void 0 : x.then) === "function";
|
|
var attachPromiseStatus = (promise) => {
|
|
if (!promise.status) {
|
|
promise.status = "pending";
|
|
promise.then(
|
|
(v) => {
|
|
promise.status = "fulfilled";
|
|
promise.value = v;
|
|
},
|
|
(e) => {
|
|
promise.status = "rejected";
|
|
promise.reason = e;
|
|
}
|
|
);
|
|
}
|
|
};
|
|
var use = import_react.default.use || // A shim for older React versions
|
|
((promise) => {
|
|
if (promise.status === "pending") {
|
|
throw promise;
|
|
} else if (promise.status === "fulfilled") {
|
|
return promise.value;
|
|
} else if (promise.status === "rejected") {
|
|
throw promise.reason;
|
|
} else {
|
|
attachPromiseStatus(promise);
|
|
throw promise;
|
|
}
|
|
});
|
|
var continuablePromiseMap = /* @__PURE__ */ new WeakMap();
|
|
var createContinuablePromise = (store, promise, getValue) => {
|
|
const buildingBlocks = getBuildingBlocks(store);
|
|
const registerAbortHandler = buildingBlocks[26];
|
|
let continuablePromise = continuablePromiseMap.get(promise);
|
|
if (!continuablePromise) {
|
|
continuablePromise = new Promise((resolve, reject) => {
|
|
let curr = promise;
|
|
const onFulfilled = (me) => (v) => {
|
|
if (curr === me) {
|
|
resolve(v);
|
|
}
|
|
};
|
|
const onRejected = (me) => (e) => {
|
|
if (curr === me) {
|
|
reject(e);
|
|
}
|
|
};
|
|
const onAbort = () => {
|
|
try {
|
|
const nextValue = getValue();
|
|
if (isPromiseLike2(nextValue)) {
|
|
continuablePromiseMap.set(nextValue, continuablePromise);
|
|
curr = nextValue;
|
|
nextValue.then(onFulfilled(nextValue), onRejected(nextValue));
|
|
registerAbortHandler(store, nextValue, onAbort);
|
|
} else {
|
|
resolve(nextValue);
|
|
}
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
};
|
|
promise.then(onFulfilled(promise), onRejected(promise));
|
|
registerAbortHandler(store, promise, onAbort);
|
|
});
|
|
continuablePromiseMap.set(promise, continuablePromise);
|
|
}
|
|
return continuablePromise;
|
|
};
|
|
function useAtomValue(atom2, options) {
|
|
const { delay, unstable_promiseStatus: promiseStatus = !import_react.default.use } = options || {};
|
|
const store = useStore(options);
|
|
const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] = (0, import_react.useReducer)(
|
|
(prev) => {
|
|
const nextValue = store.get(atom2);
|
|
if (Object.is(prev[0], nextValue) && prev[1] === store && prev[2] === atom2) {
|
|
return prev;
|
|
}
|
|
return [nextValue, store, atom2];
|
|
},
|
|
void 0,
|
|
() => [store.get(atom2), store, atom2]
|
|
);
|
|
let value = valueFromReducer;
|
|
if (storeFromReducer !== store || atomFromReducer !== atom2) {
|
|
rerender();
|
|
value = store.get(atom2);
|
|
}
|
|
(0, import_react.useEffect)(() => {
|
|
const unsub = store.sub(atom2, () => {
|
|
if (promiseStatus) {
|
|
try {
|
|
const value2 = store.get(atom2);
|
|
if (isPromiseLike2(value2)) {
|
|
attachPromiseStatus(
|
|
createContinuablePromise(store, value2, () => store.get(atom2))
|
|
);
|
|
}
|
|
} catch (e) {
|
|
}
|
|
}
|
|
if (typeof delay === "number") {
|
|
console.warn(`[DEPRECATED] delay option is deprecated and will be removed in v3.
|
|
|
|
Migration guide:
|
|
|
|
Create a custom hook like the following.
|
|
|
|
function useAtomValueWithDelay<Value>(
|
|
atom: Atom<Value>,
|
|
options: { delay: number },
|
|
): Value {
|
|
const { delay } = options
|
|
const store = useStore(options)
|
|
const [value, setValue] = useState(() => store.get(atom))
|
|
useEffect(() => {
|
|
const unsub = store.sub(atom, () => {
|
|
setTimeout(() => setValue(store.get(atom)), delay)
|
|
})
|
|
return unsub
|
|
}, [store, atom, delay])
|
|
return value
|
|
}
|
|
`);
|
|
setTimeout(rerender, delay);
|
|
return;
|
|
}
|
|
rerender();
|
|
});
|
|
rerender();
|
|
return unsub;
|
|
}, [store, atom2, delay, promiseStatus]);
|
|
(0, import_react.useDebugValue)(value);
|
|
if (isPromiseLike2(value)) {
|
|
const promise = createContinuablePromise(
|
|
store,
|
|
value,
|
|
() => store.get(atom2)
|
|
);
|
|
if (promiseStatus) {
|
|
attachPromiseStatus(promise);
|
|
}
|
|
return use(promise);
|
|
}
|
|
return value;
|
|
}
|
|
function useSetAtom(atom2, options) {
|
|
const store = useStore(options);
|
|
const setAtom = (0, import_react.useCallback)(
|
|
(...args) => {
|
|
if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !("write" in atom2)) {
|
|
throw new Error("not writable atom");
|
|
}
|
|
return store.set(atom2, ...args);
|
|
},
|
|
[store, atom2]
|
|
);
|
|
return setAtom;
|
|
}
|
|
function useAtom(atom2, options) {
|
|
return [
|
|
useAtomValue(atom2, options),
|
|
// We do wrong type assertion here, which results in throwing an error.
|
|
useSetAtom(atom2, options)
|
|
];
|
|
}
|
|
|
|
export {
|
|
atom,
|
|
INTERNAL_overrideCreateStore,
|
|
createStore,
|
|
getDefaultStore,
|
|
useStore,
|
|
Provider,
|
|
useAtomValue,
|
|
useSetAtom,
|
|
useAtom
|
|
};
|
|
//# sourceMappingURL=chunk-DOMCSPSI.js.map
|