@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
42 lines (41 loc) • 1.6 kB
JavaScript
import { n as createJSONStorage } from "../persist-core-B5VikEft.mjs";
import AsyncStorage$1 from "@react-native-async-storage/async-storage";
//#region src/adapters/backends/async-storage.ts
/**
* `StateStorage` over React Native `AsyncStorage` — fully async, string-wire.
* Pass a custom `AsyncStorage` instance (e.g. `getLegacyStorage()`) to
* namespace away from the default; defaults to the module singleton.
*
* @example
* ```ts
* import { asyncStorageStateStorage } from "@stainless-code/persist/backends/async-storage";
*
* const storage = asyncStorageStateStorage();
* ```
*/
function asyncStorageStateStorage(storage = AsyncStorage$1) {
return {
getItem: (name) => storage.getItem(name),
setItem: (name, value) => storage.setItem(name, value),
removeItem: (name) => storage.removeItem(name)
};
}
/**
* Build a JSON-encoded `PersistStorage` over React Native `AsyncStorage`.
* Async backend → hydration can't settle before first render, so `useHydrated`
* gating is mandatory (the `./frameworks/react` / `./frameworks/solid` / `./frameworks/vue` / `./frameworks/svelte` adapters).
*
* @example
* ```ts
* import { createAsyncStorage } from "@stainless-code/persist/backends/async-storage";
* import { persistStore } from "@stainless-code/persist/sources/tanstack-store";
*
* const storage = createAsyncStorage<Prefs>()!;
* persistStore(store, { name: "app:prefs:v1", storage });
* ```
*/
function createAsyncStorage(storage = AsyncStorage$1) {
return createJSONStorage(() => asyncStorageStateStorage(storage));
}
//#endregion
export { asyncStorageStateStorage, createAsyncStorage };