Skip to content

Commit

Permalink
Fix refreshing after enabling _Query_ with different sources
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 19, 2023
1 parent f303d2d commit 8c61933
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/giant-lemons-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Fix refreshing after enabling _Query_ with different sources
33 changes: 33 additions & 0 deletions packages/core/src/trigger_api/__test__/keep_fresh.auto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,37 @@ describe('keepFresh, automatically', () => {

expect(listener).toBeCalledTimes(1);
});

test('check source after enabling', async () => {
const $url = createStore('https://api.salo.com');
const $enabled = createStore(true);

const query = createJsonQuery({
enabled: $enabled,
request: {
method: 'GET',
url: $url,
},
response: { contract: unknownContract },
});

keepFresh(query, { automatically: true });

const scope = fork({
handlers: [[query.__.executeFx, vi.fn(async () => 42)]],
});

const listener = vi.fn();

createWatch({ unit: query.refresh, fn: listener, scope });

await allSettled(query.refresh, { scope });
expect(listener).toBeCalledTimes(1);

await allSettled($enabled, { scope, params: false });
await allSettled($url, { scope, params: 'https://api.v2.salo.com' });
await allSettled($enabled, { scope, params: true });

expect(listener).toBeCalledTimes(2);
});
});
5 changes: 4 additions & 1 deletion packages/core/src/trigger_api/keep_fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export function keepFresh<Params>(

triggers.push(
sample({
clock: $nextSources.updates,
clock: [
$nextSources.updates,
query.$enabled.updates.filter({ fn: Boolean }),
],
source: [$nextSources, $previousSources] as const,
filter: ([next, prev]) => !isEqual(next, prev),
})
Expand Down

0 comments on commit 8c61933

Please sign in to comment.