From 150c94d47f1f85360d9f7a63045bbe8531fa1074 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 13 Sep 2024 08:47:45 +0100 Subject: [PATCH 1/2] Defer teardown operations --- packages/core/src/client.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index 0cf9b74fc3..0917c960d0 100755 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -586,6 +586,12 @@ export const Client: new (opts: ClientOptions) => Client = function Client( } } + function flushOperations() { + if (!isOperationBatchActive) { + Promise.resolve().then(dispatchOperation); + } + } + /** Defines how result streams are created */ const makeResultSource = (operation: Operation) => { let result$ = pipe( @@ -667,15 +673,14 @@ export const Client: new (opts: ClientOptions) => Client = function Client( dispatched.delete(operation.key); replays.delete(operation.key); active.delete(operation.key); - // Interrupt active queue - isOperationBatchActive = false; // Delete all queued up operations of the same key on end for (let i = queue.length - 1; i >= 0; i--) if (queue[i].key === operation.key) queue.splice(i, 1); // Dispatch a teardown signal for the stopped operation - nextOperation( + queue.unshift( makeOperation('teardown', operation, operation.context) ); + flushOperations(); }) ); } else { @@ -704,7 +709,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client( dispatchOperation(operation); } else if (operation.kind === 'mutation') { queue.push(operation); - Promise.resolve().then(dispatchOperation); + flushOperations(); } else if (active.has(operation.key)) { let queued = false; for (let i = 0; i < queue.length; i++) { @@ -720,10 +725,10 @@ export const Client: new (opts: ClientOptions) => Client = function Client( operation.context.requestPolicy === 'network-only') ) { queue.push(operation); - Promise.resolve().then(dispatchOperation); + flushOperations(); } else { dispatched.delete(operation.key); - Promise.resolve().then(dispatchOperation); + flushOperations(); } } }, From afffed652b18adb6e9ff274b749bf89dde98d452 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 13 Sep 2024 08:50:42 +0100 Subject: [PATCH 2/2] Defer client's teardown operations --- packages/core/src/client.test.ts | 8 +++++--- packages/core/src/client.ts | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/core/src/client.test.ts b/packages/core/src/client.test.ts index 956851baf6..072df20edf 100755 --- a/packages/core/src/client.test.ts +++ b/packages/core/src/client.test.ts @@ -181,7 +181,7 @@ describe('promisified methods', () => { }); describe('synchronous methods', () => { - it('readQuery', () => { + it('readQuery', async () => { const result = client.readQuery( gql` { @@ -193,9 +193,8 @@ describe('synchronous methods', () => { { example: 1234 } ); - expect(receivedOps.length).toBe(2); + expect(receivedOps.length).toBe(1); expect(receivedOps[0].kind).toBe('query'); - expect(receivedOps[1].kind).toBe('teardown'); expect(result).toEqual({ operation: { ...query, @@ -204,6 +203,9 @@ describe('synchronous methods', () => { kind: 'query', }, }); + + await Promise.resolve(); + expect(receivedOps[1].kind).toBe('teardown'); }); }); diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index 0917c960d0..b6ee95b98c 100755 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -586,9 +586,11 @@ export const Client: new (opts: ClientOptions) => Client = function Client( } } - function flushOperations() { + function flushOperations(operation?: Operation | void) { if (!isOperationBatchActive) { - Promise.resolve().then(dispatchOperation); + Promise.resolve(operation).then(dispatchOperation); + } else if (operation) { + queue.unshift(operation); } } @@ -677,10 +679,9 @@ export const Client: new (opts: ClientOptions) => Client = function Client( for (let i = queue.length - 1; i >= 0; i--) if (queue[i].key === operation.key) queue.splice(i, 1); // Dispatch a teardown signal for the stopped operation - queue.unshift( + flushOperations( makeOperation('teardown', operation, operation.context) ); - flushOperations(); }) ); } else {