Skip to content

Commit

Permalink
Do not skip _Query_ execution with .refresh with different params
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Mar 15, 2023
1 parent 23ca062 commit 0822244
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-spoons-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Do not skip _Query_ execution with `.refresh` with different params
2 changes: 1 addition & 1 deletion packages/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"size": {
"executor": "./tools/executors/size-limit:size-limit",
"options": {
"limit": "18.2 kB",
"limit": "18.3 kB",
"outputPath": "dist/packages/core"
},
"dependsOn": [
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/query/__tests__/create_headless_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,36 @@ describe('createHeadlessQuery#refresh', () => {

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

test('skip query in case of same params', async () => {
const query = createHeadlessQuery({
contract: unknownContract,
mapData: ({ result }) => result,
});

const listener = vi.fn(() => 'data from query');

const scope = fork({ handlers: [[query.__.executeFx, listener]] });

await allSettled(query.refresh, { scope, params: 1 });
await allSettled(query.refresh, { scope, params: 1 });

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

test('start query in case of changed params', async () => {
const query = createHeadlessQuery({
contract: unknownContract,
mapData: ({ result }) => result,
});

const listener = vi.fn(() => 'data from query');

const scope = fork({ handlers: [[query.__.executeFx, listener]] });

await allSettled(query.refresh, { scope, params: 1 });
await allSettled(query.refresh, { scope, params: 2 });

expect(listener).toBeCalledTimes(2);
});
});
6 changes: 5 additions & 1 deletion packages/core/src/query/create_headless_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { Validator } from '../validation/type';
import { Query, QueryMeta, QuerySymbol } from './type';
import { Event } from 'effector';
import { isEqual } from '../libs/lohyphen';

export interface SharedQueryFactoryConfig<Data, Initial = Data> {
name?: string;
Expand Down Expand Up @@ -134,7 +135,10 @@ export function createHeadlessQuery<

sample({
clock: refresh,
filter: $stale,
source: { stale: $stale, latestParams: operation.__.$latestParams },
filter: ({ stale, latestParams }, params) =>
stale || !isEqual(params, latestParams),
fn: (_, params) => params,
target: operation.start,
});

Expand Down

0 comments on commit 0822244

Please sign in to comment.