Skip to content

Commit

Permalink
Allow clock in reset to be optional (#272)
Browse files Browse the repository at this point in the history
Co-authored-by: Sova <[email protected]>
Co-authored-by: Viktor <[email protected]>
  • Loading branch information
3 people authored Feb 16, 2023
1 parent f67cde3 commit 9ddcb90
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/reset/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Unit, Store } from 'effector';
import { createEvent } from 'effector';

export function reset({
clock,
target,
}: {
clock: Unit<any> | Array<Unit<any>>;
import type { Event, Unit, Store } from 'effector';

type Params = {
clock?: Unit<any> | Array<Unit<any>>;
target: Store<any> | Array<Store<any>>;
}): void {
};

export function reset(config: Required<Params>): void;
export function reset(config: Pick<Params, 'target'>): Event<void>;

export function reset({ clock, target }: Params) {
const targets = Array.isArray(target) ? target : [target];
const clocks = Array.isArray(clock) ? clock : [clock];
const clocks = Array.isArray(clock) ? clock : [clock ?? createEvent()];
targets.forEach((target) => {
target.reset(clocks);
});

return clock === undefined ? clocks[0] : undefined;
}
74 changes: 72 additions & 2 deletions src/reset/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { reset } from 'patronum';
import { reset } from 'patronum/reset';
```

````
## `reset({ clock, target })`

### Motivation

Expand All @@ -20,7 +20,7 @@ The method allow to reset many stores by a single line

```ts
reset({ clock, target });
````
```

- When `clock` is triggered, reset store/stores in `target` to the initial value.

Expand Down Expand Up @@ -48,6 +48,17 @@ reset({
});
```

```ts
import { createStore } from 'effector';
import { reset } from 'patronum/reset';

const $post = createStore(null);
const $comments = createStore([]);
const $draftComment = createStore('');

const resetEvent = reset({ target: [$post, $comments, $draftComment] });
```

[Try it](https://share.effector.dev/06hpVftG)

### Alternative
Expand All @@ -74,3 +85,62 @@ resetOnSomeCases.onCreateStore((store) => {
store.reset([pageUnmounted, userSessionFinished]);
});
```

## `reset({ target })`

### Motivation

The method allow to reset many stores by a single line with no `clock` passing

:::note since
The `clock` argument became optional since patronum 1.15.0
:::

### Formulae

```ts
const resetEvent = reset({ target });
```

- When `resetEvent` is triggered, reset store/stores in `target` to the initial value.

### Arguments

1. `target: Store<any> | Array<Store<any>>` — Each of these stores will be reset to the initial values when `resetEvent` is triggered.

### Returns

- `resetEvent` `(Event<void>)` — New event that reset store/stores in `target`.

### Example

```ts
import { createEvent, createStore } from 'effector';
import { reset } from 'patronum/reset';

const $post = createStore(null);
const $comments = createStore([]);
const $draftComment = createStore('');

const resetEvent = reset({ target: [$post, $comments, $draftComment] });
```

### Alternative

Write reset event by yourself:

```ts
import { createEvent, createStore } from 'effector';
import { reset } from 'patronum/reset';

const $post = createStore(null);
const $comments = createStore([]);
const $draftComment = createStore('');

const resetEvent = createEvent();

reset({
clock: resetEvent,
target: [$post, $comments, $draftComment],
});
```
13 changes: 13 additions & 0 deletions src/reset/reset.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ test('do not affects original store state', async () => {
expect(scope.getState($source)).toEqual(0);
expect($source.getState()).toEqual(0);
});

test('works in forked scope without passed clock', async () => {
const setSource = createEvent<number>();
const $source = restore(setSource, 0);
const resetEvent = reset({ target: $source });

const scope = fork();
await allSettled(setSource, { scope, params: 100 });
expect(scope.getState($source)).toEqual(100);

await allSettled(resetEvent, { scope });
expect(scope.getState($source)).toEqual(0);
});
14 changes: 14 additions & 0 deletions src/reset/reset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,17 @@ describe('multiple targets', () => {
expect([$sourceA.getState(), $sourceB.getState()]).toEqual([0, '0']);
});
});

describe('without clock in params', () => {
test('should return reset event', () => {
const setValue = createEvent<number>();
const $source = restore(setValue, 0);
const resetEvent = reset({ target: $source });

setValue(100);
expect($source.getState()).toEqual(100);

resetEvent();
expect($source.getState()).toEqual(0);
});
});
39 changes: 39 additions & 0 deletions test-typings/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expectType } from 'tsd';
import { createEvent, createEffect, createStore } from 'effector';
import { reset } from '../src/reset';

import type { Event } from 'effector';

// With clock in params
{
const event = createEvent();
const $store = createStore('');
const $clock = createStore('');
const fx = createEffect();

expectType<void>(reset({ clock: event, target: $store }));
expectType<void>(reset({ clock: event, target: [$store] }));
expectType<void>(reset({ clock: [event], target: [$store] }));
expectType<void>(reset({ clock: [event], target: $store }));

expectType<void>(reset({ clock: fx, target: $store }));
expectType<void>(reset({ clock: fx, target: [$store] }));
expectType<void>(reset({ clock: [fx], target: [$store] }));
expectType<void>(reset({ clock: [fx], target: $store }));

expectType<void>(reset({ clock: $clock, target: $store }));
expectType<void>(reset({ clock: $clock, target: [$store] }));
expectType<void>(reset({ clock: [$clock], target: [$store] }));
expectType<void>(reset({ clock: [$clock], target: $store }));

expectType<void>(reset({ clock: [event, $clock, fx], target: $store }));
expectType<void>(reset({ clock: [event, $clock, fx], target: [$store] }));
}

// Without clock in params
{
const $store = createStore('');

expectType<Event<void>>(reset({ target: $store }));
expectType<Event<void>>(reset({ target: [$store] }));
}

1 comment on commit 9ddcb90

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚛 size-compare report

Comparing f67cde32...9ddcb906

File +/- Base Current +/- gzip Base gzip Current gzip
dist/and/index.cjs = 397 B 397 B = 251 B 251 B
dist/and/index.js = 413 B 413 B = 245 B 245 B
dist/babel-preset.cjs = 434 B 434 B = 249 B 249 B
dist/combine-events/index.cjs = 2.46 kB 2.46 kB = 840 B 840 B
dist/combine-events/index.js = 3.95 kB 3.95 kB = 1.35 kB 1.35 kB
dist/condition/index.cjs = 1.37 kB 1.37 kB = 505 B 505 B
dist/condition/index.js = 1.29 kB 1.29 kB = 461 B 461 B
dist/debounce/index.cjs = 3.99 kB 3.99 kB = 1.31 kB 1.31 kB
dist/debounce/index.js = 3.84 kB 3.84 kB = 1.29 kB 1.29 kB
dist/debug/index.cjs = 11.3 kB 11.3 kB = 3.16 kB 3.16 kB
dist/debug/index.js = 13.3 kB 13.3 kB = 3.79 kB 3.79 kB
dist/delay/index.cjs = 1.76 kB 1.76 kB = 708 B 708 B
dist/delay/index.js = 1.81 kB 1.81 kB = 702 B 702 B
dist/either/index.cjs = 600 B 600 B = 334 B 334 B
dist/either/index.js = 477 B 477 B = 270 B 270 B
dist/empty/index.cjs = 175 B 175 B = 151 B 151 B
dist/empty/index.js = 79 B 79 B = 89 B 89 B
dist/equals/index.cjs = 336 B 336 B = 249 B 249 B
dist/equals/index.js = 221 B 221 B = 179 B 179 B
dist/every/index.cjs = 1.22 kB 1.22 kB = 513 B 513 B
dist/every/index.js = 1.08 kB 1.08 kB = 442 B 442 B
dist/format/index.cjs = 642 B 642 B = 366 B 366 B
dist/format/index.js = 687 B 687 B = 368 B 368 B
dist/in-flight/index.cjs = 641 B 641 B = 357 B 357 B
dist/in-flight/index.js = 546 B 546 B = 305 B 305 B
dist/index.cjs = 1.57 kB 1.57 kB = 359 B 359 B
dist/index.js = 1.09 kB 1.09 kB = 279 B 279 B
dist/interval/index.cjs = 3.88 kB 3.88 kB = 1.14 kB 1.14 kB
dist/interval/index.js = 3.74 kB 3.74 kB = 1.12 kB 1.12 kB
dist/macro.cjs = 1.91 kB 1.91 kB = 808 B 808 B
dist/not/index.cjs = 161 B 161 B = 148 B 148 B
dist/not/index.js = 69 B 69 B = 81 B 81 B
dist/or/index.cjs = 393 B 393 B = 249 B 249 B
dist/or/index.js = 411 B 411 B = 245 B 245 B
dist/patronum.cjs +0.46% 18.7 kB 18.8 kB +0.51% 5.93 kB 5.96 kB
dist/patronum.js +0.39% 17.8 kB 17.8 kB +0.47% 6.01 kB 6.04 kB
dist/patronum.umd.js +0.48% 19.8 kB 19.9 kB +0.46% 6.04 kB 6.07 kB
dist/pending/index.cjs = 909 B 909 B = 495 B 495 B
dist/pending/index.js = 828 B 828 B = 444 B 444 B
dist/reset/index.cjs +62.35% 324 B 526 B +50.00% 208 B 312 B
dist/reset/index.js +76.31% 249 B 439 B +63.06% 157 B 256 B
dist/reshape/index.cjs = 419 B 419 B = 242 B 242 B
dist/reshape/index.js = 379 B 379 B = 201 B 201 B
dist/snapshot/index.cjs = 756 B 756 B = 346 B 346 B
dist/snapshot/index.js = 641 B 641 B = 288 B 288 B
dist/some/index.cjs = 1.16 kB 1.16 kB = 474 B 474 B
dist/some/index.js = 1.02 kB 1.02 kB = 407 B 407 B
dist/split-map/index.cjs = 628 B 628 B = 359 B 359 B
dist/split-map/index.js = 575 B 575 B = 318 B 318 B
dist/spread/index.cjs = 1.26 kB 1.26 kB = 534 B 534 B
dist/spread/index.js = 1.28 kB 1.28 kB = 516 B 516 B
dist/status/index.cjs = 426 B 426 B = 265 B 265 B
dist/status/index.js = 339 B 339 B = 208 B 208 B
dist/throttle/index.cjs = 2.1 kB 2.1 kB = 814 B 814 B
dist/throttle/index.js = 1.99 kB 1.99 kB = 775 B 775 B
dist/time/index.cjs = 719 B 719 B = 375 B 375 B
dist/time/index.js = 621 B 621 B = 321 B 321 B

Please sign in to comment.