Skip to content

Commit

Permalink
Remove guard usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Oct 12, 2023
1 parent 2a94b6b commit 0008daf
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/combine-events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Effect,
Event,
EventAsReturnType,
guard,
sample,
is,
merge,
sample,
Expand Down Expand Up @@ -88,7 +88,7 @@ export function combineEvents<P>({
});
}

guard({
sample({
source: sample({ source: $results, clock: merge(Object.values(events)) }),
filter: $counter.map((value) => value === 0),
target,
Expand Down
6 changes: 3 additions & 3 deletions src/condition/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEvent, Effect, Event, guard, is, Store, Unit, split } from 'effector';
import { createEvent, Effect, Event, sample, is, Store, Unit, split } from 'effector';

type NoInfer<T> = T & { [K in keyof T]: T[K] };
type EventAsReturnType<Payload> = any extends Payload ? Event<Payload> : never;
Expand Down Expand Up @@ -96,13 +96,13 @@ export function condition<State>({
},
} as any);
} else if (thenBranch) {
guard({
sample({
source,
filter: checker,
target: thenBranch as Unit<State>,
});
} else if (elseBranch) {
guard({
sample({
source,
filter: inverse(checker as any),
target: elseBranch as Unit<State>,
Expand Down
4 changes: 2 additions & 2 deletions src/condition/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { condition } from 'patronum/condition';

## Motivation

Condition is very similar to [`guard`], but allows you to have `else` branch along with simple `if` matcher.
Condition is very similar to [`sample`], but allows you to have `else` branch along with simple `if` matcher.

[`guard`]: https://effector.dev/docs/api/effector/guard
[`sample`]: https://effector.dev/docs/api/effector/sample

## `condition({ source: Unit, if: Store, then?: Unit, else?: Unit })`

Expand Down
5 changes: 2 additions & 3 deletions src/debounce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Store,
Unit,
attach,
guard,
merge,
Effect,
} from 'effector';
Expand Down Expand Up @@ -127,10 +126,10 @@ export function debounce<T>({
const requestTick = merge([
source,
// debounce timeout is restarted on timeout change
guard({ clock: $timeout, filter: timerFx.pending }),
sample({ clock: $timeout, filter: timerFx.pending }),
]);

guard({
sample({
clock: requestTick,
filter: $canTick,
target: triggerTick,
Expand Down
9 changes: 4 additions & 5 deletions src/interval/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Store,
createEvent,
createStore,
guard,
sample,
attach,
is,
Expand Down Expand Up @@ -81,15 +80,15 @@ export function interval<S extends unknown, F extends unknown>({
},
});

guard({
sample({
clock: setup,
source: $timeout,
filter: $notRunning,
target: timeoutFx,
});

if (leading) {
const onReady = guard({ clock: setup, filter: $notRunning });
const onReady = sample({ clock: setup, filter: $notRunning });
sample({ clock: onReady, target: tick });
}

Expand All @@ -99,14 +98,14 @@ export function interval<S extends unknown, F extends unknown>({
target: $isRunning,
});

guard({
sample({
clock: timeoutFx.done,
source: $timeout,
filter: $isRunning,
target: timeoutFx,
});

guard({
sample({
clock: timeoutFx.done,
filter: $isRunning,
target: tick.prepend(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/interval/interval.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createEvent,
fork,
createStore,
guard,
sample,
createWatch,
} from 'effector';
import { argumentHistory, wait, watch } from '../../test-library';
Expand Down Expand Up @@ -91,7 +91,7 @@ test('does not leaves unresolved timeout effect, if stopped', async () => {
const stop = createEvent();
const { tick } = interval({ timeout: 1, start, stop });
const $count = createStore(0).on(tick, (s) => s + 1);
guard({
sample({
source: $count,
clock: tick,
filter: (c) => c === 6,
Expand Down
4 changes: 2 additions & 2 deletions src/spread/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEvent, Event, guard, sample, Unit } from 'effector';
import { createEvent, Event, sample, Unit } from 'effector';

const hasPropBase = {}.hasOwnProperty;
const hasOwnProp = <O extends { [k: string]: unknown }>(object: O, key: string) =>
Expand Down Expand Up @@ -45,7 +45,7 @@ export function spread<P>({
if (hasOwnProp(targets, targetKey)) {
const currentTarget = targets[targetKey];

const hasTargetKey = guard({
const hasTargetKey = sample({
source,
greedy: true,
filter: (object): object is any =>
Expand Down
4 changes: 2 additions & 2 deletions src/spread/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ source = spread({ targets: { field: target, ... } })
#### Conditionally save value to stores

```ts
import { createStore, createEvent, guard } from 'effector';
import { createStore, createEvent, sample } from 'effector';
import { spread } from 'patronum/spread';

const $first = createStore('');
const $last = createStore('');

const formReceived = createEvent();

guard({
sample({
source: formReceived,
filter: (form) => form.first.length > 0 && form.last.length > 0,
target: spread({
Expand Down
3 changes: 1 addition & 2 deletions src/throttle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
createEvent,
createStore,
Event,
guard,
is,
sample,
Store,
Expand Down Expand Up @@ -54,7 +53,7 @@ export function throttle<T>({
.on(triggerTick, () => false)
.on(target, () => true);

guard({
sample({
clock: source,
filter: $canTick,
target: triggerTick,
Expand Down

0 comments on commit 0008daf

Please sign in to comment.