Skip to content

Commit

Permalink
feat(graphcache): add onCacheHydrated event (#3428)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Dec 1, 2023
1 parent f7684a0 commit d6e435c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-eagles-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': minor
---

Add `onCacheHydrated` as an option for the `StorageAdapter`
15 changes: 8 additions & 7 deletions docs/api/graphcache.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ the cache's data to persisted storage on the user's device. it
> **NOTE:** Offline Support is currently experimental! It hasn't been extensively tested yet and
> may not always behave as expected. Please try it out with caution!
| Method | Type | Description |
| --------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `writeData` | `(delta: SerializedEntries) => Promise<void>` | This provided method must be able to accept an object of key-value entries that will be persisted to the storage. This method is called as a batch of updated entries becomes ready. |
| `readData` | `() => Promise<SerializedEntries>` | This provided method must be able to return a single combined object of previous key-value entries that have been previously preserved using `writeData`. It's only called on startup. |
| `writeMetadata` | `(json: SerializedRequest[]) => void` | This provided method must be able to persist metadata for the cache. For backwards compatibility it should be able to accept any JSON data. |
| `readMetadata` | `() => Promise<null \| SerializedRequest[]>` | This provided method must be able to read the persisted metadata that has previously been written using `writeMetadata`. It's only called on startup. |
| `onOnline` | `(cb: () => void) => void` | This method must be able to accept a callback that is called when the user's device comes back online. |
| Method | Type | Description |
| ----------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `writeData` | `(delta: SerializedEntries) => Promise<void>` | This provided method must be able to accept an object of key-value entries that will be persisted to the storage. This method is called as a batch of updated entries becomes ready. |
| `readData` | `() => Promise<SerializedEntries>` | This provided method must be able to return a single combined object of previous key-value entries that have been previously preserved using `writeData`. It's only called on startup. |
| `writeMetadata` | `(json: SerializedRequest[]) => void` | This provided method must be able to persist metadata for the cache. For backwards compatibility it should be able to accept any JSON data. |
| `readMetadata` | `() => Promise<null \| SerializedRequest[]>` | This provided method must be able to read the persisted metadata that has previously been written using `writeMetadata`. It's only called on startup. |
| `onOnline` | `(cb: () => void) => void` | This method must be able to accept a callback that is called when the user's device comes back online. |
| `onCacheHydrated` | `() => void` | This method will be called when the `cacheExchange` has finished hydrating the data coming from storage. |

These options are split into three parts:

Expand Down
1 change: 1 addition & 0 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const cacheExchange =
store.data.hydrating = true;
opts.storage.readData().then(entries => {
hydrateData(store.data, opts!.storage!, entries);
if (opts.storage!.onCacheHydrated) opts.storage!.onCacheHydrated();
});
}

Expand Down
4 changes: 3 additions & 1 deletion exchanges/graphcache/src/default-storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface StorageOptions {
* @defaultValue `7` days
*/
maxAge?: number;
/** Gets Called when the exchange has hydrated the data from storage. */
onCacheHydrated?: () => void;
}

/** Sample storage adapter persisting to IndexedDB. */
Expand Down Expand Up @@ -219,7 +221,7 @@ export const makeDefaultStorage = (opts?: StorageOptions): DefaultStorage => {
() => batch
);
},

onCacheHydrated: opts.onCacheHydrated,
onOnline(cb: () => void) {
if (callback) {
window.removeEventListener('online', callback);
Expand Down
2 changes: 2 additions & 0 deletions exchanges/graphcache/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,8 @@ export interface StorageAdapter {
* will cause all failed mutations in the queue to be retried.
*/
onOnline?(cb: () => void): any;
/** Called when the cache has been hydrated with the data from `readData` */
onCacheHydrated?(): any;
}

/** Set of keys that have been modified or accessed.
Expand Down

0 comments on commit d6e435c

Please sign in to comment.