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

fix(core): Add missing dedupe operation logic #3101

Merged
merged 1 commit into from
Mar 27, 2023
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/afraid-geckos-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Deduplicate operations as the `dedupExchange` did; by filtering out duplicate operations until either the original operation has been cancelled (teardown) or a first result (without `hasNext: true`) has come in.
10 changes: 8 additions & 2 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe('queuing behavior', () => {
output.push(op);
if (
op.key === queryOperation.key &&
op.context.requestPolicy === 'cache-first'
op.context.requestPolicy !== 'network-only'
) {
client.reexecuteOperation({
...op,
Expand Down Expand Up @@ -840,10 +840,15 @@ describe('shared sources behavior', () => {
});

it('does nothing when no operation result has been emitted yet', () => {
const dispatched = vi.fn();

const exchange: Exchange = () => ops$ => {
return pipe(
ops$,
map(op => ({ hasNext: false, stale: false, data: 1, operation: op })),
map(op => {
dispatched(op);
return { hasNext: false, stale: false, data: 1, operation: op };
}),
filter(() => false)
);
};
Expand All @@ -862,6 +867,7 @@ describe('shared sources behavior', () => {

expect(resultOne).toHaveBeenCalledTimes(0);
expect(resultTwo).toHaveBeenCalledTimes(0);
expect(dispatched).toHaveBeenCalledTimes(1);
});

it('skips replaying results when a result is emitted immediately (network-only)', () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client(

const replays = new Map<number, OperationResult>();
const active: Map<number, Source<OperationResult>> = new Map();
const dispatched = new Set<number>();
const queue: Operation[] = [];

const baseOpts = {
Expand All @@ -569,8 +570,18 @@ export const Client: new (opts: ClientOptions) => Client = function Client(

function nextOperation(operation: Operation) {
const prevReplay = replays.get(operation.key);
if (operation.kind === 'mutation' || !prevReplay || !prevReplay.hasNext)
if (
operation.kind === 'mutation' ||
operation.kind === 'teardown' ||
(prevReplay ? !prevReplay.hasNext : !dispatched.has(operation.key))
) {
if (operation.kind === 'teardown') {
dispatched.delete(operation.key);
} else if (operation.kind !== 'mutation') {
dispatched.add(operation.key);
}
operations.next(operation);
}
}

// We define a queued dispatcher on the subject, which empties the queue when it's
Expand Down Expand Up @@ -660,10 +671,12 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
]);
}),
onPush(result => {
dispatched.delete(operation.key);
replays.set(operation.key, result);
}),
onEnd(() => {
// Delete the active operation handle
dispatched.delete(operation.key);
replays.delete(operation.key);
active.delete(operation.key);
// Delete all queued up operations of the same key on end
Expand Down