Skip to content

Commit

Permalink
Do not flick .$status during cancellation (#411)
Browse files Browse the repository at this point in the history
* Add test case

* Do not flick `.$status` during cancellation

* Add fix
  • Loading branch information
igorkamyshev committed Nov 30, 2023
1 parent 036adea commit 730ef73
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-scissors-collect.md
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Do not flick `.$status` during cancellation
Expand Up @@ -188,4 +188,48 @@ describe('createJsonQuery concurrency.strategy', () => {
expect.objectContaining({ error: abortError() })
);
});

test('do not flick $status', async () => {
const query = createJsonQuery({
request: { method: 'GET', url: 'https://api.salo.com' },
response: { contract: unknownContract },
concurrency: { strategy: 'TAKE_LATEST' },
});

const statusListener = vi.fn();
const abortedListener = vi.fn();

const scope = fork({
handlers: [
[
// We have to mock fetchFx because executeFx contains cancellation logic
fetchFx,
vi.fn().mockImplementation(async () => {
await setTimeout(100);
throw new Error('cannot');
}),
],
],
});

createWatch({ unit: query.$status, fn: statusListener, scope });
createWatch({ unit: query.aborted, fn: abortedListener, scope });

allSettled(query.start, { scope });
allSettled(query.start, { scope });

await allSettled(scope);

expect(abortedListener).toBeCalledTimes(1);
expect(statusListener.mock.calls).toMatchInlineSnapshot(`
[
[
"pending",
],
[
"fail",
],
]
`);
});
});
13 changes: 11 additions & 2 deletions packages/core/src/remote_operation/create_remote_operation.ts
Expand Up @@ -214,8 +214,17 @@ export function createRemoteOperation<
finished.failure.map(() => 'fail' as const),
sample({
clock: aborted,
source: $statusHistory,
fn: (history) => history[history.length - 2] ?? 'initial',
source: {
history: $statusHistory,
retrieveDataPengind: retrieveDataFx.pending,
},
fn: ({ history, retrieveDataPengind }) => {
if (retrieveDataPengind) {
return 'pending';
}

return history[history.length - 2] ?? 'initial';
},
}),
],
target: $status,
Expand Down

0 comments on commit 730ef73

Please sign in to comment.