-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
asyncStoragePersistor for React Native #2360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TkDodo
merged 16 commits into
TanStack:master
from
Aung-Myint-Thein:asyncStoragePersistor
Jun 28, 2021
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6b96263
added a new file for asyncStoragePersistor
Aung-Myint-Thein 490c236
added doc
Aung-Myint-Thein a39f57b
Merge branch 'tannerlinsley:master' into asyncStoragePersistor
Aung-Myint-Thein 8b0d824
added in doc manifest
Aung-Myint-Thein 809e22b
add createAsyncStoragePersisor to list of officially supported plugins
TkDodo 7a93b84
add rollup config and separate package
TkDodo a11e80b
move createAsyncStoragePersistor to typeScript
TkDodo ca037b1
run prettier on createAsyncStoragePersistor
TkDodo 6ec1528
use the same throttle function as the webStoragePersistor
TkDodo 37f6a32
add interface
TkDodo 0612612
get rid of AsyncStorage dependency
TkDodo 1637715
fix typo
TkDodo 9cf8bd4
yay, typo again
TkDodo 73a20c4
use asyncThrottle instead of sync throttle
TkDodo f53d7c2
default throttleTime to 1000
TkDodo 279faaa
Update src/createAsyncStoragePersistor-experimental/index.ts
TkDodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.