Skip to content
Open
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/brave-moons-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix `client.reexecuteOperation` letting every second reexecute of an in-flight operation through. The in-flight block cleared the operation's `dispatched` flag, so the next reexecute during the same request passed the deduplication check and sent a second network request. The flag is now only cleared when the reexecute replaced an operation that was already waiting in the queue.
55 changes: 55 additions & 0 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,61 @@ describe('deduplication behavior', () => {
expect(onOperation).toHaveBeenCalledTimes(2);
expect(onResult).toHaveBeenCalledTimes(2);
});

it('blocks repeated reexecutes of operations that are in-flight', async () => {
const onOperation = vi.fn();
const onResult = vi.fn();

let resolve;
const exchange: Exchange = () => ops$ =>
pipe(
ops$,
filter(op => op.kind !== 'teardown'),
onPush(onOperation),
mergeMap(op =>
fromPromise(
new Promise<void>(res => {
resolve = res;
}).then(() => ({
hasNext: false,
stale: false,
data: 'test',
operation: op,
}))
)
)
);

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

const operation = makeOperation('query', queryOperation, {
...queryOperation.context,
requestPolicy: 'cache-first',
});

pipe(client.executeRequestOperation(operation), subscribe(onResult));
expect(onOperation).toHaveBeenCalledTimes(1);

client.reexecuteOperation(operation);
await Promise.resolve();
client.reexecuteOperation(operation);
await Promise.resolve();
client.reexecuteOperation(operation);
await Promise.resolve();

expect(onOperation).toHaveBeenCalledTimes(1);

resolve();
await Promise.resolve();
await Promise.resolve();
await Promise.resolve();
await Promise.resolve();
expect(onOperation).toHaveBeenCalledTimes(1);
expect(onResult).toHaveBeenCalledTimes(1);
});
});

describe('shared sources behavior', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
queue.push(operation);
Promise.resolve().then(dispatchOperation);
} else {
dispatched.delete(operation.key);
if (queued) dispatched.delete(operation.key);
Promise.resolve().then(dispatchOperation);
}
}
Expand Down
Loading