Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

feat(graphcache): add onCacheHydrated event (#3428)

Changed files
+19 -8
.changeset
docs
exchanges
graphcache
src
+5
.changeset/stale-eagles-hide.md
···
···
+
---
+
'@urql/exchange-graphcache': minor
+
---
+
+
Add `onCacheHydrated` as an option for the `StorageAdapter`
+8 -7
docs/api/graphcache.md
···
> **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. |
These options are split into three parts:
···
> **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. |
+
| `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:
+1
exchanges/graphcache/src/cacheExchange.ts
···
store.data.hydrating = true;
opts.storage.readData().then(entries => {
hydrateData(store.data, opts!.storage!, entries);
});
}
···
store.data.hydrating = true;
opts.storage.readData().then(entries => {
hydrateData(store.data, opts!.storage!, entries);
+
if (opts.storage!.onCacheHydrated) opts.storage!.onCacheHydrated();
});
}
+3 -1
exchanges/graphcache/src/default-storage/index.ts
···
* @defaultValue `7` days
*/
maxAge?: number;
}
/** Sample storage adapter persisting to IndexedDB. */
···
() => batch
);
},
-
onOnline(cb: () => void) {
if (callback) {
window.removeEventListener('online', callback);
···
* @defaultValue `7` days
*/
maxAge?: number;
+
/** Gets Called when the exchange has hydrated the data from storage. */
+
onCacheHydrated?: () => void;
}
/** Sample storage adapter persisting to IndexedDB. */
···
() => batch
);
},
+
onCacheHydrated: opts.onCacheHydrated,
onOnline(cb: () => void) {
if (callback) {
window.removeEventListener('online', callback);
+2
exchanges/graphcache/src/types.ts
···
* will cause all failed mutations in the queue to be retried.
*/
onOnline?(cb: () => void): any;
}
/** Set of keys that have been modified or accessed.
···
* 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.