Skip to content

Commit

Permalink
Add _Store_ .$finished to _Remote Operation_ (#348)
Browse files Browse the repository at this point in the history
Add _Store_ `.$fiished` to _Remote Opration_
  • Loading branch information
igorkamyshev committed Aug 23, 2023
1 parent fe2a599 commit ca20587
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-peas-grab.md
@@ -0,0 +1,5 @@
---
'@farfetched/core': minor
---

Add _Store_ `.$fiished` to _Remote Opration_
1 change: 1 addition & 0 deletions apps/website/docs/api/primitives/mutation.md
Expand Up @@ -28,6 +28,7 @@ For convenience, there are also the following [_Stores_](https://effector.dev/do
- `$pending``true` if the _Mutation_ is in the `"pending"` state, `false` otherwise.
- `$failed``true` if the _Mutation_ is in the `"fail"` state, `false` otherwise.
- `$succeeded``true` if the _Mutation_ is in the `"done"` state, `false` otherwise.
- `$finished` <Badge type="tip" text="since v0.9.0" /> — `true` if the _Mutation_ is in the `"done"` or `"fail"` state, `false` otherwise.

### `$enabled`

Expand Down
1 change: 1 addition & 0 deletions apps/website/docs/api/primitives/query.md
Expand Up @@ -44,6 +44,7 @@ For convenience, there are also the following [_Stores_](https://effector.dev/do
- `$pending``true` if the _Query_ is in the `"pending"` state, `false` otherwise.
- `$failed` <Badge type="tip" text="since v0.2.0" /> — `true` if the _Query_ is in the `"fail"` state, `false` otherwise.
- `$succeeded` <Badge type="tip" text="since v0.2.0" /> — `true` if the _Query_ is in the `"done"` state, `false` otherwise.
- `$finished` <Badge type="tip" text="since v0.9.0" /> — `true` if the _Query_ is in the `"done"` or `"fail"` state, `false` otherwise.

### `$enabled`

Expand Down
Expand Up @@ -38,7 +38,7 @@ describe('createHeadlessMutation', () => {

expect(listeners.onSuccess).toHaveBeenCalledTimes(1);
expect(listeners.onSuccess).toHaveBeenCalledWith(
expect.objectContaining({ params: 42, result: 42, status: 'done' })
expect.objectContaining({ params: 42, result: 42 })
);

expect(listeners.onSkip).not.toHaveBeenCalled();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/mutation/create_headless_mutation.ts
Expand Up @@ -112,6 +112,7 @@ export function createHeadlessMutation<
$pending: readonly(operation.$pending),
$succeeded: readonly(operation.$succeeded),
$failed: readonly(operation.$failed),
$finished: readonly(operation.$finished),
$enabled: readonly(operation.$enabled),
finished: {
success: readonly(operation.finished.success),
Expand Down
Expand Up @@ -35,7 +35,7 @@ describe('core/createHeadlessQuery without contract', () => {
expect(scope.getState(query.$error)).toBeNull();
expect(listeners.onSuccess).toHaveBeenCalledTimes(1);
expect(listeners.onSuccess).toHaveBeenCalledWith(
expect.objectContaining({ params: 42, result: 42, status: 'done' })
expect.objectContaining({ params: 42, result: 42 })
);

expect(listeners.onSkip).not.toHaveBeenCalled();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/query/create_headless_query.ts
Expand Up @@ -247,6 +247,7 @@ export function createHeadlessQuery<
$pending: readonly(operation.$pending),
$succeeded: readonly(operation.$succeeded),
$failed: readonly(operation.$failed),
$finished: readonly(operation.$finished),
$enabled: readonly(operation.$enabled),
$stale,
aborted: readonly(aborted),
Expand Down
Expand Up @@ -98,6 +98,7 @@ describe('createRemoteOperation, disable in-flight', () => {
expect(scope.getState(operation.$pending)).toBeFalsy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeFalsy();

// do not await
allSettled(operation.start, { scope, params: 42 });
Expand All @@ -107,6 +108,7 @@ describe('createRemoteOperation, disable in-flight', () => {
expect(scope.getState(operation.$pending)).toBeTruthy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeFalsy();

executorFirstDefer.resolve('result');
await allSettled(scope);
Expand All @@ -116,6 +118,7 @@ describe('createRemoteOperation, disable in-flight', () => {
expect(scope.getState(operation.$pending)).toBeFalsy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeTruthy();
expect(scope.getState(operation.$finished)).toBeTruthy();

// do not await
allSettled(operation.start, { scope, params: 42 });
Expand All @@ -125,6 +128,7 @@ describe('createRemoteOperation, disable in-flight', () => {
expect(scope.getState(operation.$pending)).toBeTruthy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeFalsy();

executorSecondDefer.reject(new Error('error'));
await allSettled(scope);
Expand All @@ -134,5 +138,6 @@ describe('createRemoteOperation, disable in-flight', () => {
expect(scope.getState(operation.$pending)).toBeFalsy();
expect(scope.getState(operation.$failed)).toBeTruthy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeTruthy();
});
});
2 changes: 2 additions & 0 deletions packages/core/src/remote_operation/create_remote_operation.ts
Expand Up @@ -167,6 +167,7 @@ export function createRemoteOperation<
const $pending = $status.map((status) => status === 'pending');
const $failed = $status.map((status) => status === 'fail');
const $succeeded = $status.map((status) => status === 'done');
const $finished = $status.map((status) => ['fail', 'done'].includes(status));

// -- Indicate status --
sample({
Expand Down Expand Up @@ -379,6 +380,7 @@ export function createRemoteOperation<
$pending,
$failed,
$succeeded,
$finished,
$enabled,
__: {
executeFx,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/remote_operation/type.ts
Expand Up @@ -24,6 +24,8 @@ export interface RemoteOperation<Params, Data, Error, Meta> {
$failed: Store<boolean>;
/** Is fetching succeeded? */
$succeeded: Store<boolean>;
/** Is fetching finished? */
$finished: Store<boolean>;
/**
* Is operation enabled?
*
Expand Down

0 comments on commit ca20587

Please sign in to comment.