alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
281 lines (278 loc) • 8.28 kB
JavaScript
import {
atom
} from "./chunk-OBOPLPUQ.js";
// node_modules/jotai/esm/vanilla/utils.mjs
var RESET = Symbol();
function atomFamily(initializeAtom, areEqual) {
let shouldRemove = null;
const atoms = /* @__PURE__ */ new Map();
const createAtom = (param) => {
let item;
if (areEqual === void 0) {
item = atoms.get(param);
} else {
for (const [key, value] of atoms) {
if (areEqual(key, param)) {
item = value;
break;
}
}
}
if (item !== void 0) {
if (shouldRemove == null ? void 0 : shouldRemove(item[1], param)) {
createAtom.remove(param);
} else {
return item[0];
}
}
const newAtom = initializeAtom(param);
atoms.set(param, [newAtom, Date.now()]);
return newAtom;
};
createAtom.remove = (param) => {
if (areEqual === void 0) {
atoms.delete(param);
} else {
for (const [key] of atoms) {
if (areEqual(key, param)) {
atoms.delete(key);
break;
}
}
}
};
createAtom.setShouldRemove = (fn) => {
shouldRemove = fn;
if (!shouldRemove)
return;
for (const [key, value] of atoms) {
if (shouldRemove(value[1], key)) {
atoms.delete(key);
}
}
};
return createAtom;
}
var isPromiseLike = (x) => typeof (x == null ? void 0 : x.then) === "function";
function createJSONStorage(getStringStorage) {
let lastStr;
let lastValue;
const storage = {
getItem: (key, initialValue) => {
var _a, _b;
const parse = (str2) => {
str2 = str2 || "";
if (lastStr !== str2) {
try {
lastValue = JSON.parse(str2);
} catch {
return initialValue;
}
lastStr = str2;
}
return lastValue;
};
const str = (_b = (_a = getStringStorage()) == null ? void 0 : _a.getItem(key)) != null ? _b : null;
if (isPromiseLike(str)) {
return str.then(parse);
}
return parse(str);
},
setItem: (key, newValue) => {
var _a;
return (_a = getStringStorage()) == null ? void 0 : _a.setItem(key, JSON.stringify(newValue));
},
removeItem: (key) => {
var _a;
return (_a = getStringStorage()) == null ? void 0 : _a.removeItem(key);
}
};
if (typeof window !== "undefined" && typeof window.addEventListener === "function" && window.Storage) {
storage.subscribe = (key, callback, initialValue) => {
if (!(getStringStorage() instanceof window.Storage)) {
return () => {
};
}
const storageEventCallback = (e) => {
if (e.storageArea === getStringStorage() && e.key === key) {
let newValue;
try {
newValue = JSON.parse(e.newValue || "");
} catch {
newValue = initialValue;
}
callback(newValue);
}
};
window.addEventListener("storage", storageEventCallback);
return () => {
window.removeEventListener("storage", storageEventCallback);
};
};
}
return storage;
}
var defaultStorage = createJSONStorage(
() => typeof window !== "undefined" ? window.localStorage : void 0
);
function atomWithStorage(key, initialValue, storage = defaultStorage, unstable_options) {
const getOnInit = unstable_options == null ? void 0 : unstable_options.unstable_getOnInit;
const baseAtom = atom(
getOnInit ? storage.getItem(key, initialValue) : initialValue
);
if ((import.meta.env ? "production" : void 0) !== "production") {
baseAtom.debugPrivate = true;
}
baseAtom.onMount = (setAtom) => {
if (!getOnInit) {
setAtom(storage.getItem(key, initialValue));
}
let unsub;
if (storage.subscribe) {
unsub = storage.subscribe(key, setAtom, initialValue);
}
return unsub;
};
const anAtom = atom(
(get) => get(baseAtom),
(get, set, update) => {
const nextValue = typeof update === "function" ? update(get(baseAtom)) : update;
if (nextValue === RESET) {
set(baseAtom, initialValue);
return storage.removeItem(key);
}
if (nextValue instanceof Promise) {
return nextValue.then((resolvedValue) => {
set(baseAtom, resolvedValue);
return storage.setItem(key, resolvedValue);
});
}
set(baseAtom, nextValue);
return storage.setItem(key, nextValue);
}
);
return anAtom;
}
var cache1$1 = /* @__PURE__ */ new WeakMap();
var memo1 = (create, dep1) => (cache1$1.has(dep1) ? cache1$1 : cache1$1.set(dep1, create())).get(dep1);
var LOADING = { state: "loading" };
function loadable(anAtom) {
return memo1(() => {
const loadableCache = /* @__PURE__ */ new WeakMap();
const refreshAtom = atom(0);
if ((import.meta.env ? "production" : void 0) !== "production") {
refreshAtom.debugPrivate = true;
}
const derivedAtom = atom(
(get, { setSelf }) => {
get(refreshAtom);
let value;
try {
value = get(anAtom);
} catch (error) {
return { state: "hasError", error };
}
if (!(value instanceof Promise)) {
return { state: "hasData", data: value };
}
const promise = value;
const cached = loadableCache.get(promise);
if (cached) {
return cached;
}
loadableCache.set(promise, LOADING);
promise.then(
(data) => {
loadableCache.set(promise, { state: "hasData", data });
},
(error) => {
loadableCache.set(promise, { state: "hasError", error });
}
).finally(setSelf);
return LOADING;
},
(_get, set) => {
set(refreshAtom, (c) => c + 1);
}
);
if ((import.meta.env ? "production" : void 0) !== "production") {
derivedAtom.debugPrivate = true;
}
return atom((get) => get(derivedAtom));
}, anAtom);
}
var getCached = (c, m, k) => (m.has(k) ? m : m.set(k, c())).get(k);
var cache1 = /* @__PURE__ */ new WeakMap();
var memo2 = (create, dep1, dep2) => {
const cache2 = getCached(() => /* @__PURE__ */ new WeakMap(), cache1, dep1);
return getCached(create, cache2, dep2);
};
var defaultFallback = () => void 0;
function unwrap(anAtom, fallback = defaultFallback) {
return memo2(
() => {
const promiseErrorCache = /* @__PURE__ */ new WeakMap();
const promiseResultCache = /* @__PURE__ */ new WeakMap();
const refreshAtom = atom(0);
if ((import.meta.env ? "production" : void 0) !== "production") {
refreshAtom.debugPrivate = true;
}
const promiseAndValueAtom = atom(
(get, { setSelf }) => {
get(refreshAtom);
const prev = get(promiseAndValueAtom);
const promise = get(anAtom);
if (!(promise instanceof Promise)) {
return { v: promise };
}
if (promise === (prev == null ? void 0 : prev.p)) {
if (promiseErrorCache.has(promise)) {
throw promiseErrorCache.get(promise);
}
if (promiseResultCache.has(promise)) {
return {
p: promise,
v: promiseResultCache.get(promise)
};
}
}
if (promise !== (prev == null ? void 0 : prev.p)) {
promise.then(
(v) => promiseResultCache.set(promise, v),
(e) => promiseErrorCache.set(promise, e)
).finally(setSelf);
}
if (prev && "v" in prev) {
return { p: promise, f: fallback(prev.v) };
}
return { p: promise, f: fallback() };
},
(_get, set) => {
set(refreshAtom, (c) => c + 1);
}
);
promiseAndValueAtom.init = void 0;
if ((import.meta.env ? "production" : void 0) !== "production") {
promiseAndValueAtom.debugPrivate = true;
}
return atom(
(get) => {
const state = get(promiseAndValueAtom);
if ("v" in state) {
return state.v;
}
return state.f;
},
anAtom.write
);
},
anAtom,
fallback
);
}
export {
atomFamily,
atomWithStorage,
loadable,
unwrap
};