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

(core) - Fix Client hang on promisified sources #1634

Merged
merged 1 commit into from
May 12, 2021
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/empty-zoos-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix a condition under which the `Client` would hang when a query is started and consumed with `toPromise()`
27 changes: 27 additions & 0 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
subscribe,
filter,
toArray,
toPromise,
tap,
take,
} from 'wonka';
Expand Down Expand Up @@ -893,4 +894,30 @@ describe('shared sources behavior', () => {
);
expect(resultTwo).toHaveBeenCalledTimes(0);
});

it('supports promisified sources', async () => {
const exchange: Exchange = () => ops$ => {
return pipe(
ops$,
map(op => ({ stale: true, data: 1, operation: op }))
);
};

const client = createClient({
url: 'test',
exchanges: [exchange],
});

const resultOne = await pipe(
client.executeRequestOperation(queryOperation),
take(1),
toPromise
);

expect(resultOne).toEqual({
data: 1,
operation: queryOperation,
stale: true,
});
});
});
49 changes: 28 additions & 21 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {
filter,
make,
makeSubject,
onEnd,
onPush,
Expand Down Expand Up @@ -285,31 +286,37 @@ export class Client {
};
}

const prevReplay = activeOperation.replay;
const subject = makeSubject<OperationResult>();
const isNetworkOperation =
operation.context.requestPolicy === 'cache-and-network' ||
operation.context.requestPolicy === 'network-only';

return pipe(
merge([subject.source, activeOperation.source]),
onStart(() => {
this.activeOperations.set(operation.key, activeOperation!);
if (operation.kind === 'subscription') {
return this.dispatchOperation(operation);
} else if (isNetworkOperation) {
this.dispatchOperation(operation);
}

if (prevReplay != null && prevReplay === activeOperation!.replay) {
subject.next(
isNetworkOperation ? { ...prevReplay, stale: true } : prevReplay
);
} else if (!isNetworkOperation) {
this.dispatchOperation(operation);
}
})
);
return make(observer => {
activeOperation =
this.activeOperations.get(operation.key) || activeOperation!;
const prevReplay = activeOperation.replay;

return pipe(
activeOperation.source,
onStart(() => {
this.activeOperations.set(operation.key, activeOperation!);
if (operation.kind === 'subscription') {
return this.dispatchOperation(operation);
} else if (isNetworkOperation) {
this.dispatchOperation(operation);
}

if (prevReplay != null && prevReplay === activeOperation!.replay) {
observer.next(
isNetworkOperation ? { ...prevReplay, stale: true } : prevReplay
);
} else if (!isNetworkOperation) {
this.dispatchOperation(operation);
}
}),
onEnd(observer.complete),
subscribe(observer.next)
).unsubscribe;
});
}

query<Data = any, Variables extends object = {}>(
Expand Down