Skip to content

Commit

Permalink
Fix extra refresh in keepFresh while source is changing for disable…
Browse files Browse the repository at this point in the history
…d _Query_
  • Loading branch information
igorkamyshev committed Apr 19, 2023
1 parent 6e572cb commit d393e78
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-cups-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Fix extra refresh in `keepFresh` while source is changing for disabled _Query_
32 changes: 31 additions & 1 deletion packages/core/src/trigger_api/__test__/keep_fresh.auto.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { allSettled, createStore, fork } from 'effector';
import { allSettled, createStore, createWatch, fork } from 'effector';
import { describe, test, expect, vi } from 'vitest';
import { unknownContract } from '../../contract/unknown_contract';
import { createJsonQuery } from '../../query/create_json_query';
Expand Down Expand Up @@ -215,4 +215,34 @@ describe('keepFresh, automatically', () => {

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

test('does not use sources while disabled as source for refresh', async () => {
const $url = createStore('https://api.salo.com');

const query = createJsonQuery({
enabled: $url.map((url) => url.length > 0),
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 });

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

expect(listener).toBeCalledTimes(1);
});
});
21 changes: 15 additions & 6 deletions packages/core/src/trigger_api/keep_fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@ export function keepFresh<Params>(

if (config.automatically) {
const finalyParams = query.finished.finally.map(get('params'));
const $previousSources = combine(
query.__.lowLevelAPI.sourced.map((sourced) =>
normalizeSourced({ field: sourced, clock: finalyParams })
)
);

const $previousSources = createStore<any[]>([], { serialize: 'ignore' });

// @ts-expect-error I have no idea
sample({
clock: finalyParams,
source: combine(
query.__.lowLevelAPI.sourced.map((sourced) =>
normalizeSourced({ field: sourced, clock: finalyParams })
)
),
filter: query.$enabled,
target: $previousSources,
});

const sourcesUpdated = sample({
clock: query.__.lowLevelAPI.sourced.map(extractSource).filter(is.store),
Expand All @@ -116,7 +125,7 @@ export function keepFresh<Params>(
);
}

const forceFresh = merge(triggers);
const forceFresh = sample({ clock: triggers, filter: query.$enabled });

sample({
clock: forceFresh,
Expand Down

0 comments on commit d393e78

Please sign in to comment.