Skip to content

Commit

Permalink
Add .reset _Event_ to _Mutation_
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Dec 4, 2023
1 parent b6125e1 commit 7331eb8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-bees-grow.md
@@ -0,0 +1,5 @@
---
'@farfetched/core': minor
---

Add `.reset` _Event_ to _Mutation_
4 changes: 4 additions & 0 deletions apps/website/docs/api/primitives/mutation.md
Expand Up @@ -14,6 +14,10 @@ This section describes the [_Event_](https://effector.dev/docs/api/effector/even

Unconditionally starts the _Mutation_ with the given parameters.

### `reset` <Badge type="tip" text="since v0.12.0" />

Resets the _Mutation_ to the initial state.

## Stores

This section describes the [_Stores_](https://effector.dev/docs/api/effector/store) that can be used to read the _Mutation_ state.
Expand Down
Expand Up @@ -22,6 +22,23 @@ describe('createHeadlessMutation', () => {
expect(mockFn).toHaveBeenCalledWith(42);
});

test('reset reset $status', async () => {
const mutation = createHeadlessMutation({
contract: unknownContract,
mapData: ({ result }) => result,
});

const scope = fork({ handlers: [[mutation.__.executeFx, () => null]] });

await allSettled(mutation.start, { scope, params: 42 });

expect(scope.getState(mutation.$status)).toBe('done');

await allSettled(mutation.reset, { scope });

expect(scope.getState(mutation.$status)).toBe('initial');
});

test('finished.success triggers after executeFx.done', async () => {
const mutation = createHeadlessMutation({
contract: unknownContract,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/mutation/create_headless_mutation.ts
Expand Up @@ -107,6 +107,7 @@ export function createHeadlessMutation<

return {
start: operation.start,
reset: operation.reset,
started: readonly(operation.started),
aborted: readonly(operation.aborted),
$status: readonly(operation.$status),
Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/query/create_headless_query.ts
Expand Up @@ -102,7 +102,6 @@ export function createHeadlessQuery<
});

const refresh = createEvent<Params>();
const reset = createEvent();

// -- Main stores --
const $data = createStore<MappedData | Initial>(initialData, {
Expand Down Expand Up @@ -193,13 +192,8 @@ export function createHeadlessQuery<
// -- Reset state --

sample({
clock: reset,
target: [
$data.reinit,
$error.reinit,
$stale.reinit,
operation.__.lowLevelAPI.resetStatus,
],
clock: operation.reset,
target: [$data.reinit, $error.reinit, $stale.reinit],
});

// -- Protocols --
Expand Down Expand Up @@ -254,9 +248,9 @@ export function createHeadlessQuery<
// -- Public API --

return {
reset,
refresh,
start: operation.start,
reset: operation.reset,
started: readonly(operation.started),
$data: readonly($data),
$error: readonly($error),
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/query/type.ts
Expand Up @@ -48,8 +48,6 @@ export interface Query<Params, Data, Error, InitialData = null>
* Is data stale?
*/
$stale: StoreWritable<boolean>;
/** Event to reset the whole state of the query */
reset: EventCallable<void>;
'@@unitShape': () => {
data: Store<Data | InitialData>;
error: Store<Error | null>;
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/remote_operation/create_remote_operation.ts
@@ -1,5 +1,4 @@
import {
type Event,
createEffect,
createEvent,
createStore,
Expand Down Expand Up @@ -110,6 +109,7 @@ export function createRemoteOperation<
} = createDataSourceHandlers<Params>(dataSources);

const start = createEvent<Params>();
const reset = createEvent();

const started = createEvent<{ params: Params; meta: ExecutionMeta }>();

Expand Down Expand Up @@ -174,8 +174,7 @@ export function createRemoteOperation<
serialize,
});

const resetStatus = createEvent();
sample({ clock: resetStatus, target: $status.reinit });
sample({ clock: reset, target: $status.reinit });

const $statusHistory = createStore<FetchingStatus[]>([], {
serialize: 'ignore',
Expand Down Expand Up @@ -440,6 +439,7 @@ export function createRemoteOperation<
finished,
started,
aborted,
reset,
$status,
$idle,
$pending,
Expand All @@ -462,7 +462,6 @@ export function createRemoteOperation<
pushData,
startWithMeta,
callObjectCreated,
resetStatus,
},
},
};
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/remote_operation/type.ts
Expand Up @@ -47,8 +47,10 @@ export interface RemoteOperation<
* + true — query will be executed after any `start` call
*/
$enabled: Store<boolean>;
/** Event to trigger query */
/** Event to trigger operation */
start: EventCallable<Params>;
/** Event to reset the whole state of the operation */
reset: EventCallable<void>;
/** Event that trigered after operation started */
started: Event<{ params: Params; meta: ExecutionMeta }>;
aborted: Event<{ params: Params; meta: ExecutionMeta }>;
Expand Down Expand Up @@ -119,7 +121,6 @@ export interface RemoteOperation<
pushError: EventCallable<Error>;
startWithMeta: EventCallable<{ params: Params; meta: ExecutionMeta }>;
callObjectCreated: Event<CallObject>;
resetStatus: EventCallable<void>;
} & ExtraLowLevelAPI;
experimentalAPI?: {
attach: <Source, NewParams>(config: {
Expand Down

0 comments on commit 7331eb8

Please sign in to comment.