Skip to content

Commit

Permalink
Defer client's teardown operations
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Sep 13, 2024
1 parent 150c94d commit afffed6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('promisified methods', () => {
});

describe('synchronous methods', () => {
it('readQuery', () => {
it('readQuery', async () => {
const result = client.readQuery(
gql`
{
Expand All @@ -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,
Expand All @@ -204,6 +203,9 @@ describe('synchronous methods', () => {
kind: 'query',
},
});

await Promise.resolve();
expect(receivedOps[1].kind).toBe('teardown');
});
});

Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit afffed6

Please sign in to comment.