@stainless-code/persist
Version:
Hydration-aware persistence for any reactive store — zero-dep persistSource core; codecs, backends, cross-tab transport, source + framework hydration adapters ship as opt-in recipes
30 lines (29 loc) • 874 B
JavaScript
import { l as persistSource } from "../persist-core-B5VikEft.mjs";
//#region src/adapters/sources/jotai.ts
/**
* Persist a writable jotai atom via a jotai `Store`. `get`/`set`/`sub` map
* to `PersistableSource` for the atom's value.
*
* @example
* ```ts
* import { atom, createStore } from "jotai";
* import { persistAtom } from "@stainless-code/persist/sources/jotai";
* const countAtom = atom(0);
* const store = createStore();
* const persist = persistAtom(store, countAtom, { name: "count" });
* ```
*/
function persistAtom(store, atom, options) {
return persistSource({
getState: () => store.get(atom),
setState: (updater) => store.set(atom, updater),
subscribe: (listener) => {
return { unsubscribe: store.sub(atom, () => listener()) };
}
}, {
...options,
merge: options.merge ?? ((persisted) => persisted)
});
}
//#endregion
export { persistAtom };