Skip to content

Commit

Permalink
Add info about _Query_ status and corresponding data to `.finished.fi…
Browse files Browse the repository at this point in the history
…nally` _Event_
  • Loading branch information
igorkamyshev committed Aug 23, 2023
1 parent 43917a7 commit 901545b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-days-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farfetched/core': minor
---

Add info about _Query_ status and corresponding data to `.finished.finally` _Event_
3 changes: 3 additions & 0 deletions apps/website/docs/api/primitives/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ This section describes the [_Event_](https://effector.dev/docs/api/effector/even

- `params` with the parameters that were used to start the _Query_
- `meta` with the execution metadata
- `status` <Badge type="tip" text="since v0.9.0" /> with a string `"done"`, `"fail"` or `"skip"` for success, failure or skip respectively
- `result` <Badge type="tip" text="since v0.9.0" /> if the `status` is `"done"` with the result of the _Query_
- `error` <Badge type="tip" text="since v0.9.0" /> if the `status` is `"fail"` with the error of the _Query_

## `aborted` <Badge type="tip" text="since v0.9.0" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('createHeadlessMutation', () => {

expect(listeners.onFinally).toHaveBeenCalledTimes(1);
expect(listeners.onFinally).toHaveBeenCalledWith(
expect.objectContaining({ params: 42 })
expect.objectContaining({ params: 42, result: 42 })
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('core/createHeadlessQuery without contract', () => {

expect(listeners.onFinally).toHaveBeenCalledTimes(1);
expect(listeners.onFinally).toHaveBeenCalledWith(
expect.objectContaining({ params: 42 })
expect.objectContaining({ params: 42, result: 42 })
);
});

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('core/createHeadlessQuery without contract', () => {

expect(listeners.onFinally).toHaveBeenCalledTimes(1);
expect(listeners.onFinally).toHaveBeenCalledWith(
expect.objectContaining({ params: 42 })
expect.objectContaining({ params: 42, error: new Error('from mock') })
);
});

Expand Down
43 changes: 38 additions & 5 deletions packages/core/src/remote_operation/create_remote_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ export function createRemoteOperation<
meta: ExecutionMeta;
}>(),
skip: createEvent<{ params: Params; meta: ExecutionMeta }>(),
finally: createEvent<{ params: Params; meta: ExecutionMeta }>(),
finally: createEvent<
{ params: Params; meta: ExecutionMeta } & (
| {
status: 'done';
result: MappedData;
}
| { status: 'fail'; error: Error | InvalidDataError }
| { status: 'skip' }
)
>(),
};

// -- Main stores --
Expand Down Expand Up @@ -331,10 +340,34 @@ export function createRemoteOperation<

// -- Send finally --
sample({
clock: [finished.success, finished.failure, finished.skip],
fn({ params, meta }) {
return { params, meta };
},
clock: finished.success,
fn: ({ params, result, meta }) => ({
status: 'done' as const,
params,
result,
meta,
}),
target: finished.finally,
});

sample({
clock: finished.failure,
fn: ({ params, error, meta }) => ({
status: 'fail' as const,
params,
error,
meta,
}),
target: finished.finally,
});

sample({
clock: finished.skip,
fn: ({ params, meta }) => ({
status: 'skip' as const,
params,
meta,
}),
target: finished.finally,
});

Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/remote_operation/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export interface RemoteOperation<Params, Data, Error, Meta> {
/** Query execution was skipped due to `enabled` field in config */
skip: Event<{ params: Params; meta: ExecutionMeta }>;
/** Query was ended, it merges `success`, `error` and `skip` */
finally: Event<{ params: Params; meta: ExecutionMeta }>;
finally: Event<
{ params: Params; meta: ExecutionMeta } & (
| { status: 'done'; result: Data }
| { status: 'fail'; error: Error }
| { status: 'skip' }
)
>;
};
/**
* DO NOT USE THIS FIELD IN PRODUCTION
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/retry/__tests__/retry.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ describe('retry with query', () => {
"meta": {
"attempt": 1,
"maxAttempts": 3,
"stale": false,
"stopErrorPropagation": false,
},
"params": "Initial",
},
Expand All @@ -141,6 +143,8 @@ describe('retry with query', () => {
"meta": {
"attempt": 2,
"maxAttempts": 3,
"stale": false,
"stopErrorPropagation": false,
},
"params": "Initial 1",
},
Expand All @@ -151,6 +155,8 @@ describe('retry with query', () => {
"meta": {
"attempt": 3,
"maxAttempts": 3,
"stale": false,
"stopErrorPropagation": false,
},
"params": "Initial 1 2",
},
Expand Down

0 comments on commit 901545b

Please sign in to comment.