@gluonjs/store / packages/store/src / AsyncStorageLike
Interface: AsyncStorageLike
Promise-based persistence adapter. The synchronous StorageLike contract is intentionally unchanged.
Methods
getItem()
getItem(
key,signal?):Promise<string|null>
Parameters
key
string
signal?
Returns
Promise<string | null>
removeItem()?
optionalremoveItem(key,signal?):Promise<void>
Parameters
key
string
signal?
Returns
Promise<void>
setItem()
setItem(
key,value,signal?):Promise<void>
Parameters
key
string
value
string
signal?
Returns
Promise<void>
Example
Implement a DOM-free promise-based persistence adapter without changing the synchronous StorageLike contract:
import {
createAsyncPersistencePlugin,
createStoreManager,
defineStore,
type AsyncPersistenceLifecycle,
type AsyncPersistencePlugin,
type AsyncPersistencePluginOptions,
type AsyncPersistenceStatus,
type AsyncStorageLike,
type StoreAbortSignal,
} from '@gluonjs/store';
declare function indexedDbGet(key: string, signal?: StoreAbortSignal): Promise<string | null>;
declare function indexedDbSet(key: string, value: string, signal?: StoreAbortSignal): Promise<void>;
const storage: AsyncStorageLike = {
async getItem(key, signal) { return await indexedDbGet(key, signal); },
async setItem(key, value, signal) { await indexedDbSet(key, value, signal); },
};
const signal: StoreAbortSignal | undefined = undefined;
const pluginOptions: AsyncPersistencePluginOptions = { storage, signal };
const persistence: AsyncPersistencePlugin = createAsyncPersistencePlugin(pluginOptions);
const manager = createStoreManager({ plugins: [persistence] });
const cart = manager.use(defineStore({ id: 'cart', state: () => ({ items: [] as string[] }), persist: true }));
const lifecycle: AsyncPersistenceLifecycle = persistence.lifecycle;
const status: AsyncPersistenceStatus = lifecycle.status;
await lifecycle.ready;
if (lifecycle.status === 'ready') console.log('bootstrap restored store', cart.items);