forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: asyncStoragePersistor for React Native (TanStack#2360)
- Loading branch information
1 parent
ddbd755
commit 7a628d6
Showing
8 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"internal": true, | ||
"main": "../lib/createAsyncStoragePersistor-experimental/index.js", | ||
"module": "../es/createAsyncStoragePersistor-experimental/index.js", | ||
"types": "../types/createAsyncStoragePersistor-experimental/index.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
id: createAsyncStoragePersistor | ||
title: createAsyncStoragePersistor for React Native (Experimental) | ||
--- | ||
|
||
> VERY IMPORTANT: This utility is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Use at your own risk. If you choose to rely on this in production in an experimental stage, please lock your version to a patch-level version to avoid unexpected breakages. | ||
## Installation | ||
|
||
This utility comes packaged with `react-query` and is available under the `react-query/createAsyncStoragePersistor-experimental` import. | ||
|
||
## Usage | ||
|
||
- Import the `createAsyncStoragePersistor` function | ||
- Create a new asyncStoragePersistor | ||
- Pass it to the [`persistQueryClient`](../persistQueryClient) function | ||
|
||
```ts | ||
import { persistQueryClient } from 'react-query/persistQueryClient-experimental' | ||
import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental' | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
cacheTime: 1000 * 60 * 60 * 24, // 24 hours | ||
}, | ||
}, | ||
}) | ||
|
||
const asyncStoragePersistor = createAsyncStoragePersistor() | ||
|
||
persistQueryClient({ | ||
queryClient, | ||
persistor: asyncStoragePersistor, | ||
}) | ||
``` | ||
|
||
## API | ||
|
||
### `createAsyncStoragePersistor` | ||
|
||
Call this function (with an optional options object) to create a asyncStoragePersistor that you can use later with `persisteQueryClient`. | ||
|
||
```js | ||
createAsyncStoragePersistor(options?: CreateAsyncStoragePersistorOptions) | ||
``` | ||
|
||
### `Options` | ||
|
||
An optional object of options: | ||
|
||
```js | ||
interface CreateAsyncStoragePersistorOptions { | ||
/** The key to use when storing the cache to localstorage */ | ||
asyncStorageKey?: string | ||
/** To avoid localstorage spamming, | ||
* pass a time in ms to throttle saving the cache to disk */ | ||
throttleTime?: number | ||
} | ||
``` | ||
|
||
The default options are: | ||
|
||
```js | ||
{ | ||
asyncStorageKey = `REACT_QUERY_OFFLINE_CACHE`, | ||
throttleTime = 1000, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
interface AsyncStorage { | ||
getItem: (key: string) => Promise<string> | ||
setItem: (key: string, value: string) => Promise<unknown> | ||
removeItem: (key: string) => Promise<unknown> | ||
} | ||
|
||
interface CreateAsyncStoragePersistorOptions { | ||
/** The storage client used for setting an retrieving items from cache */ | ||
storage: AsyncStorage | ||
/** The key to use when storing the cache */ | ||
key?: string | ||
/** To avoid spamming, | ||
* pass a time in ms to throttle saving the cache to disk */ | ||
throttleTime?: number | ||
} | ||
|
||
export const asyncStoragePersistor = ({ | ||
storage, | ||
key = `REACT_QUERY_OFFLINE_CACHE`, | ||
throttleTime = 1000, | ||
}: CreateAsyncStoragePersistorOptions) => { | ||
return { | ||
persistClient: asyncThrottle( | ||
persistedClient => storage.setItem(key, JSON.stringify(persistedClient)), | ||
{ interval: throttleTime } | ||
), | ||
restoreClient: async () => { | ||
const cacheString = await storage.getItem(key) | ||
|
||
if (!cacheString) { | ||
return | ||
} | ||
|
||
return JSON.parse(cacheString) | ||
}, | ||
removeClient: () => storage.removeItem(key), | ||
} | ||
} | ||
|
||
function asyncThrottle<T>( | ||
func: (...args: ReadonlyArray<unknown>) => Promise<T>, | ||
{ interval = 1000, limit = 1 }: { interval?: number; limit?: number } = {} | ||
) { | ||
if (typeof func !== 'function') throw new Error('argument is not function.') | ||
const running = { current: false } | ||
let lastTime = 0 | ||
let timeout: number | ||
const queue: Array<any[]> = [] | ||
return (...args: any) => | ||
(async () => { | ||
if (running.current) { | ||
lastTime = Date.now() | ||
if (queue.length > limit) { | ||
queue.shift() | ||
} | ||
|
||
queue.push(args) | ||
clearTimeout(timeout) | ||
} | ||
if (Date.now() - lastTime > interval) { | ||
running.current = true | ||
await func(...args) | ||
lastTime = Date.now() | ||
running.current = false | ||
} else { | ||
if (queue.length > 0) { | ||
const lastArgs = queue[queue.length - 1]! | ||
timeout = setTimeout(async () => { | ||
if (!running.current) { | ||
running.current = true | ||
await func(...lastArgs) | ||
running.current = false | ||
} | ||
}, interval) | ||
} | ||
} | ||
})() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters