Skip to content
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

(graphcache) - Fix offlineExchange timing after rehydration #1019

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-plants-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Fix a case where the `offlineExchange` would not start processing operations after hydrating persisted data when no operations arrived in time by the time the persisted data was restored. This would be more evident in Preact and Svelte due to their internal short timings.
14 changes: 10 additions & 4 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import {

import {
filter,
combine,
scan,
map,
merge,
pipe,
share,
fromPromise,
fromArray,
buffer,
take,
mergeMap,
concat,
Expand Down Expand Up @@ -249,10 +250,15 @@ export const cacheExchange = (opts?: CacheExchangeOpts): Exchange => ({
// If no hydration takes place we replace this stream with an empty one
const bufferedOps$ = hydration
? pipe(
sharedOps$,
buffer(fromPromise(hydration)),
combine(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works because combine can retroactively still resolve an operation synchronously once the first operation comes in. This lets bufferedOps$ hang in its active state for as long or short as needed.

buffer on the other hand may not emit when its list of values is empty, but would wait for the next signal if then a new event comes in. Because we'd before only wait for one buffer output though and have one input event, there are conditions where bufferedOps$ would stay active indefinitely.

pipe(
sharedOps$,
scan((acc: Operation[], x) => (acc.push(x), acc), [])
),
fromPromise(hydration)
),
take(1),
mergeMap(fromArray)
mergeMap(zip => fromArray(zip[0]))
)
: (empty as Source<Operation>);

Expand Down