Package Exports
- @wora/cache-persist
- @wora/cache-persist/lib/idbstorage
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@wora/cache-persist) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@wora/cache-persist
Installation
Install @wora/cache-persist using yarn or npm:
yarn add @wora/cache-persistOptions
CacheOptions { storage?: Cache, prefix?: string, serialize?: boolean }
storage: custom storage, localStorage is used as the default react web persistence, while AsyncStorage is used for react-native.
prefix: prefix keys
serialize: if it is true, the data will be serialized and deserialized JSON
Cache
isRehydrated(): boolean; // true if restored
replace(data: any):
restore(): Promise
getStorageName(): string; // storage name
purge(): Promise
clear(): Promise
getState(): Readonly<{v[key: string]: any; }>; // return in memory state
toObject(): Readonly<{v[key: string]: any; }>; // return in memory state
get(key: string): any; // get value from in memory state
getAllKeys(): Array
set(key: string, value: any): Promise
delete(key: string): Promise
remove(key: string): Promise
subscribe( callback: (message: string, state: any) => void, ): () => void // subscription management
notify(message: string = "notify data"): void // notification of the message and status to all subscriptions
Usage default
import { Cache } from "@wora/cache-persist";
const cache = new Cache();
cache.restore().then(() => {
const state = cache.getState();
});Usage indexedDB
import Cache, { CacheStorage, CacheOptions } from "@wora/cache-persist";
import IDBStorage from '@wora/cache-persist/lib/idbstorage';
const idbStorages: CacheStorage[] = IDBStorage.create("cache", ["persist", "persist2"]);
const idb: CacheOptions = {
storage: idbStorages[0],
serialize: false,
}
const idb1: CacheOptions = {
storage: idbStorages[1],
serialize: false,
}
const cacheidb = new Cache(idb);
cacheidb.restore().then(() => {
const state = cacheidb.getState();
});
const cacheidb1 = new Cache(idb1);
cacheidb1.restore().then(() => {
const state = cacheidb1.getState();
});Hooks example
import * as React from 'react';
import { useEffect, useState } from 'react';
import { DataCache } from '@wora/cache-persist';
const [result, setResult] = useState<{loading: boolean, data: DataCache}>({loading: true, data: {}});
useEffect(() => {
const dispose = cache.subscribe((message, state) => {
setResult({loading: false, data: state});
});
cache.restore().then(() => {
cache.notify("restored");
})
return () => dispose();
},
[]);