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

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

Jovi De Croock and committed by
GitHub
d6e435c5 f7684a0a

+19 -8
+5
.changeset/stale-eagles-hide.md
··· 1 + --- 2 + '@urql/exchange-graphcache': minor 3 + --- 4 + 5 + Add `onCacheHydrated` as an option for the `StorageAdapter`
+8 -7
docs/api/graphcache.md
··· 178 178 > **NOTE:** Offline Support is currently experimental! It hasn't been extensively tested yet and 179 179 > may not always behave as expected. Please try it out with caution! 180 180 181 - | Method | Type | Description | 182 - | --------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 183 - | `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. | 184 - | `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. | 185 - | `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. | 186 - | `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. | 187 - | `onOnline` | `(cb: () => void) => void` | This method must be able to accept a callback that is called when the user's device comes back online. | 181 + | Method | Type | Description | 182 + | ----------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 183 + | `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. | 184 + | `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. | 185 + | `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. | 186 + | `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. | 187 + | `onOnline` | `(cb: () => void) => void` | This method must be able to accept a callback that is called when the user's device comes back online. | 188 + | `onCacheHydrated` | `() => void` | This method will be called when the `cacheExchange` has finished hydrating the data coming from storage. | 188 189 189 190 These options are split into three parts: 190 191
+1
exchanges/graphcache/src/cacheExchange.ts
··· 73 73 store.data.hydrating = true; 74 74 opts.storage.readData().then(entries => { 75 75 hydrateData(store.data, opts!.storage!, entries); 76 + if (opts.storage!.onCacheHydrated) opts.storage!.onCacheHydrated(); 76 77 }); 77 78 } 78 79
+3 -1
exchanges/graphcache/src/default-storage/index.ts
··· 35 35 * @defaultValue `7` days 36 36 */ 37 37 maxAge?: number; 38 + /** Gets Called when the exchange has hydrated the data from storage. */ 39 + onCacheHydrated?: () => void; 38 40 } 39 41 40 42 /** Sample storage adapter persisting to IndexedDB. */ ··· 219 221 () => batch 220 222 ); 221 223 }, 222 - 224 + onCacheHydrated: opts.onCacheHydrated, 223 225 onOnline(cb: () => void) { 224 226 if (callback) { 225 227 window.removeEventListener('online', callback);
+2
exchanges/graphcache/src/types.ts
··· 980 980 * will cause all failed mutations in the queue to be retried. 981 981 */ 982 982 onOnline?(cb: () => void): any; 983 + /** Called when the cache has been hydrated with the data from `readData` */ 984 + onCacheHydrated?(): any; 983 985 } 984 986 985 987 /** Set of keys that have been modified or accessed.