UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

24 lines (22 loc) 627 B
import { asyncThrottle } from "./asyncThrottle.mjs"; export const createAsyncStoragePersister = ({ storage, key = "REACT_QUERY_OFFLINE_CACHE", throttleTime = 1000, serialize = JSON.stringify, deserialize = JSON.parse }) => { return { persistClient: asyncThrottle(persistedClient => storage.setItem(key, serialize(persistedClient)), { interval: throttleTime }), restoreClient: async () => { const cacheString = await storage.getItem(key); if (!cacheString) { return; } return deserialize(cacheString); }, removeClient: () => storage.removeItem(key) }; };