Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/src/pages/plugins/createAsyncStoragePersistor.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This utility comes packaged with `react-query` and is available under the `react
- Pass it to the [`persistQueryClient`](../persistQueryClient) function

```ts
import AsyncStorage from '@react-native-community/async-storage'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental'

Expand Down Expand Up @@ -56,7 +56,7 @@ interface CreateAsyncStoragePersistorOptions {
/** The storage client used for setting an retrieving items from cache */
storage: AsyncStorage
/** The key to use when storing the cache to localstorage */
asyncStorageKey?: string
key?: string
/** To avoid localstorage spamming,
* pass a time in ms to throttle saving the cache to disk */
throttleTime?: number
Expand All @@ -73,7 +73,7 @@ The default options are:

```js
{
asyncStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
key = `REACT_QUERY_OFFLINE_CACHE`,
throttleTime = 1000,
}
```
14 changes: 8 additions & 6 deletions src/createAsyncStoragePersistor-experimental/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PersistedClient, Persistor } from '../persistQueryClient-experimental'

interface AsyncStorage {
getItem: (key: string) => Promise<string>
setItem: (key: string, value: string) => Promise<unknown>
removeItem: (key: string) => Promise<unknown>
getItem: (key: string) => Promise<string | null>
setItem: (key: string, value: string) => Promise<void>
removeItem: (key: string) => Promise<void>
}

interface CreateAsyncStoragePersistorOptions {
Expand All @@ -14,11 +16,11 @@ interface CreateAsyncStoragePersistorOptions {
throttleTime?: number
}

export const asyncStoragePersistor = ({
export const createAsyncStoragePersistor = ({
storage,
key = `REACT_QUERY_OFFLINE_CACHE`,
throttleTime = 1000,
}: CreateAsyncStoragePersistorOptions) => {
}: CreateAsyncStoragePersistorOptions): Persistor => {
return {
persistClient: asyncThrottle(
persistedClient => storage.setItem(key, JSON.stringify(persistedClient)),
Expand All @@ -31,7 +33,7 @@ export const asyncStoragePersistor = ({
return
}

return JSON.parse(cacheString)
return JSON.parse(cacheString) as PersistedClient
},
removeClient: () => storage.removeItem(key),
}
Expand Down