Skip to content

Commit 0bfb4c2

Browse files
committed
Merge into single restoreQueries function
1 parent d33e386 commit 0bfb4c2

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

docs/framework/react/plugins/createPersister.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,17 @@ This function can be used to sporadically clean up stoage from `expired`, `buste
111111
For this function to work, your storage must expose `entries` method that would return a `key-value tuple array`.
112112
For example `Object.entries(localStorage)` for `localStorage` or `entries` from `idb-keyval`.
113113

114-
### `persisterRestoreAll(queryClient: QueryClient): Promise<void>`
114+
### `restoreQueries(queryClient: QueryClient, filters): Promise<void>`
115115

116-
This function can be used to restore all queries that are currently stored by persister in one go.
117-
For example when your app is starting up in offline mode, or you want all data from previous session to be immediately available without intermediate `loading` state.
116+
This function can be used to restore queries that are currently stored by persister.
117+
For example when your app is starting up in offline mode, or you want all or only specific data from previous session to be immediately available without intermediate `loading` state.
118118

119-
For this function to work, your storage must expose `entries` method that would return a `key-value tuple array`.
120-
For example `Object.entries(localStorage)` for `localStorage` or `entries` from `idb-keyval`.
121-
122-
### `persisterRestoreByKey(queryClient: QueryClient, queryKey: QueryKey, exact: boolean = false): Promise<void>`
119+
The filter object supports the following properties:
123120

124-
This function can be used to restore a specific query or multiple queries that partial match the provided queryKey.
125-
For example when your app is starting up in offline mode, or you want only specific data from previous session to be immediately available without intermediate `loading` state.
121+
- `queryKey?: QueryKey`
122+
- Set this property to define a query key to match on.
123+
- `exact?: boolean`
124+
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed.
126125

127126
For this function to work, your storage must expose `entries` method that would return a `key-value tuple array`.
128127
For example `Object.entries(localStorage)` for `localStorage` or `entries` from `idb-keyval`.

packages/query-persist-client-core/src/__tests__/createPersister.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ describe('createPersister', () => {
497497
})
498498
})
499499

500-
describe('persisterRestoreAll', () => {
500+
describe('restoreQueries', () => {
501501
test('should properly clean storage from busted entries', async () => {
502502
const storage = getFreshStorage()
503503
const { persister, client, query, queryKey } = setupPersister(['foo'], {
@@ -513,11 +513,11 @@ describe('createPersister', () => {
513513

514514
expect(await storage.entries()).toHaveLength(1)
515515

516-
await persister.persisterRestoreAll(client)
516+
await persister.restoreQueries(client)
517517
expect(await storage.entries()).toHaveLength(0)
518518
})
519519

520-
test('should properly restore queries from cache', async () => {
520+
test('should properly restore queries from cache without filters', async () => {
521521
const storage = getFreshStorage()
522522
const { persister, client, queryKey } = setupPersister(['foo'], {
523523
storage,
@@ -530,14 +530,12 @@ describe('createPersister', () => {
530530
client.clear()
531531
expect(client.getQueryCache().getAll()).toHaveLength(0)
532532

533-
await persister.persisterRestoreAll(client)
533+
await persister.restoreQueries(client)
534534
expect(client.getQueryCache().getAll()).toHaveLength(1)
535535

536536
expect(client.getQueryData(queryKey)).toEqual('foo')
537537
})
538-
})
539538

540-
describe('persisterRestoreByKey', () => {
541539
test('should properly restore queries from cache', async () => {
542540
const storage = getFreshStorage()
543541
const { persister, client, queryKey } = setupPersister(['foo', 'bar'], {
@@ -551,7 +549,7 @@ describe('createPersister', () => {
551549
client.clear()
552550
expect(client.getQueryCache().getAll()).toHaveLength(0)
553551

554-
await persister.persisterRestoreByKey(client, queryKey)
552+
await persister.restoreQueries(client, { queryKey })
555553
expect(client.getQueryCache().getAll()).toHaveLength(1)
556554

557555
expect(client.getQueryData(queryKey)).toEqual('foo')
@@ -570,7 +568,7 @@ describe('createPersister', () => {
570568
client.clear()
571569
expect(client.getQueryCache().getAll()).toHaveLength(0)
572570

573-
await persister.persisterRestoreByKey(client, ['bar'])
571+
await persister.restoreQueries(client, { queryKey: ['bar'] })
574572
expect(client.getQueryCache().getAll()).toHaveLength(0)
575573
})
576574

@@ -587,7 +585,7 @@ describe('createPersister', () => {
587585
client.clear()
588586
expect(client.getQueryCache().getAll()).toHaveLength(0)
589587

590-
await persister.persisterRestoreByKey(client, ['foo'])
588+
await persister.restoreQueries(client, { queryKey: ['foo'] })
591589
expect(client.getQueryCache().getAll()).toHaveLength(1)
592590

593591
expect(client.getQueryData(queryKey)).toEqual('foo')
@@ -606,7 +604,7 @@ describe('createPersister', () => {
606604
client.clear()
607605
expect(client.getQueryCache().getAll()).toHaveLength(0)
608606

609-
await persister.persisterRestoreByKey(client, ['foo'], true)
607+
await persister.restoreQueries(client, { queryKey: ['foo'], exact: true })
610608
expect(client.getQueryCache().getAll()).toHaveLength(0)
611609
})
612610

@@ -623,7 +621,10 @@ describe('createPersister', () => {
623621
client.clear()
624622
expect(client.getQueryCache().getAll()).toHaveLength(0)
625623

626-
await persister.persisterRestoreByKey(client, queryKey, true)
624+
await persister.restoreQueries(client, {
625+
queryKey: queryKey,
626+
exact: true,
627+
})
627628
expect(client.getQueryCache().getAll()).toHaveLength(1)
628629
})
629630
})

packages/query-persist-client-core/src/createPersister.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,12 @@ export function experimental_createQueryPersister<TStorageValue = string>({
240240
}
241241
}
242242

243-
async function persisterRestoreAll(queryClient: QueryClient) {
244-
return persisterRestoreByKey(queryClient, [])
245-
}
246-
247-
async function persisterRestoreByKey(
243+
async function restoreQueries(
248244
queryClient: QueryClient,
249-
queryKey: QueryKey,
250-
exact: boolean = false,
245+
filters: Pick<QueryFilters, 'queryKey' | 'exact'> = {},
251246
): Promise<void> {
247+
const { exact, queryKey } = filters
248+
252249
if (storage?.entries) {
253250
const entries = await storage.entries()
254251
for (const [key, value] of entries) {
@@ -260,12 +257,14 @@ export function experimental_createQueryPersister<TStorageValue = string>({
260257
continue
261258
}
262259

263-
if (exact) {
264-
if (persistedQuery.queryHash !== hashKey(queryKey)) {
260+
if (queryKey) {
261+
if (exact) {
262+
if (persistedQuery.queryHash !== hashKey(queryKey)) {
263+
continue
264+
}
265+
} else if (!partialMatchKey(persistedQuery.queryKey, queryKey)) {
265266
continue
266267
}
267-
} else if (!partialMatchKey(persistedQuery.queryKey, queryKey)) {
268-
continue
269268
}
270269

271270
queryClient.setQueryData(
@@ -290,7 +289,6 @@ export function experimental_createQueryPersister<TStorageValue = string>({
290289
persistQueryByKey,
291290
retrieveQuery,
292291
persisterGc,
293-
persisterRestoreAll,
294-
persisterRestoreByKey,
292+
restoreQueries,
295293
}
296294
}

0 commit comments

Comments
 (0)