Files
infocom-systems-design/node_modules/jotai/esm/vanilla.mjs
2025-10-03 22:27:28 +03:00

60 lines
1.6 KiB
JavaScript

import { INTERNAL_buildStoreRev2 } from 'jotai/vanilla/internals';
let 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
);
}
let overiddenCreateStore;
function INTERNAL_overrideCreateStore(fn) {
overiddenCreateStore = fn(overiddenCreateStore);
}
function createStore() {
if (overiddenCreateStore) {
return overiddenCreateStore();
}
return INTERNAL_buildStoreRev2();
}
let 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;
}
export { INTERNAL_overrideCreateStore, atom, createStore, getDefaultStore };