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

experiment(core): Defer teardown operations #3671

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
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
18 changes: 12 additions & 6 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
}
}

function flushOperations(operation?: Operation | void) {
if (!isOperationBatchActive) {
Promise.resolve(operation).then(dispatchOperation);
} else if (operation) {
queue.unshift(operation);
}
}

/** Defines how result streams are created */
const makeResultSource = (operation: Operation) => {
let result$ = pipe(
Expand Down Expand Up @@ -667,13 +675,11 @@ 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(
flushOperations(
makeOperation('teardown', operation, operation.context)
);
})
Expand Down Expand Up @@ -704,7 +710,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++) {
Expand All @@ -720,10 +726,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();
}
}
},
Expand Down
Loading