From 001203abd9f3057d702414efea586361d44be263 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Mon, 23 Oct 2023 19:40:30 +0700 Subject: [PATCH] Use UnitTargetable --- src/combine-events/index.ts | 17 ++++++++++++----- src/condition/index.ts | 26 +++++++++++++------------- src/debounce/index.ts | 34 ++++++++++------------------------ src/delay/index.ts | 3 ++- src/once/index.ts | 12 ++++-------- src/reset/index.ts | 4 ++-- src/spread/index.ts | 10 +++++----- src/throttle/index.ts | 5 +++-- 8 files changed, 51 insertions(+), 60 deletions(-) diff --git a/src/combine-events/index.ts b/src/combine-events/index.ts index 9bc28dc6..c41cefd7 100644 --- a/src/combine-events/index.ts +++ b/src/combine-events/index.ts @@ -9,6 +9,7 @@ import { sample, Store, Unit, + UnitTargetable, withRegion, } from 'effector'; @@ -40,7 +41,7 @@ export function combineEvents

(config: { export function combineEvents< P extends Shape, - T extends Unit

>, + T extends UnitTargetable

>, >(config: { events: Events

; target: T; reset?: Unit }): ReturnTarget; export function combineEvents

({ @@ -50,9 +51,10 @@ export function combineEvents

({ }: { events: Events

; reset?: Unit; - target?: Unit; + target?: UnitTargetable | Unit; }) { - if (!is.unit(target)) throwError('target should be a unit'); + if (!(is.unit(target) && is.targetable(target))) + throwError('target should be a targetable unit'); if (reset && !is.unit(reset)) throwError('reset should be a unit'); withRegion(target, () => { @@ -87,10 +89,15 @@ export function combineEvents

({ }); } + const eventsTrriggered = sample({ + source: $results, + clock: [...(Object.values(events) as Unit[])], + }); + sample({ - source: sample({ source: $results, clock: merge(Object.values(events)) }), + source: eventsTrriggered, filter: $counter.map((value) => value === 0), - target, + target: target as UnitTargetable, }); }); diff --git a/src/condition/index.ts b/src/condition/index.ts index aa726c41..689270a3 100644 --- a/src/condition/index.ts +++ b/src/condition/index.ts @@ -1,4 +1,4 @@ -import { createEvent, Effect, Event, sample, is, Store, Unit, split } from 'effector'; +import { createEvent, Effect, Event, sample, is, Store, Unit, UnitTargetable, split } from 'effector'; type NoInfer = T & { [K in keyof T]: T[K] }; type EventAsReturnType = any extends Payload ? Event : never; @@ -6,52 +6,52 @@ type EventAsReturnType = any extends Payload ? Event : never; export function condition(options: { source: Event; if: ((payload: State) => boolean) | Store | State; - then: Unit | void>; - else: Unit | void>; + then: UnitTargetable | void>; + else: UnitTargetable | void>; }): EventAsReturnType; export function condition(options: { source: Store; if: ((payload: State) => boolean) | Store | State; - then: Unit; - else: Unit; + then: UnitTargetable; + else: UnitTargetable; }): Store; export function condition(options: { source: Effect; if: ((payload: Params) => boolean) | Store | Params; - then: Unit | void>; - else: Unit | void>; + then: UnitTargetable | void>; + else: UnitTargetable | void>; }): Effect; export function condition(options: { source: Event; if: ((payload: State) => boolean) | Store | State; - then: Unit | void>; + then: UnitTargetable | void>; }): EventAsReturnType; export function condition(options: { source: Store; if: ((payload: State) => boolean) | Store | State; - then: Unit | void>; + then: UnitTargetable | void>; }): Store; export function condition(options: { source: Effect; if: ((payload: Params) => boolean) | Store | Params; - then: Unit | void>; + then: UnitTargetable | void>; }): Effect; export function condition(options: { source: Event; if: ((payload: State) => boolean) | Store | State; - else: Unit | void>; + else: UnitTargetable | void>; }): EventAsReturnType; export function condition(options: { source: Store; if: ((payload: State) => boolean) | Store | State; - else: Unit | void>; + else: UnitTargetable | void>; }): Store; export function condition(options: { source: Effect; if: ((payload: Params) => boolean) | Store | Params; - else: Unit | void>; + else: UnitTargetable | void>; }): Effect; // Without `source` diff --git a/src/debounce/index.ts b/src/debounce/index.ts index a89c7328..b39e84c1 100644 --- a/src/debounce/index.ts +++ b/src/debounce/index.ts @@ -2,52 +2,38 @@ import { createEffect, createEvent, createStore, - Event, is, sample, Store, Unit, attach, merge, - Effect, + UnitTargetable, + EventAsReturnType } from 'effector'; -type EventAsReturnType = any extends Payload ? Event : never; - export function debounce(_: { - source: Event | Effect | Store; + source: Unit; timeout: number | Store; - name?: string; }): EventAsReturnType; export function debounce< T, Target extends - | Event - | Event - | Effect - | Effect - | Store, + | UnitTargetable + | UnitTargetable, >(_: { - source: Event | Effect | Store; + source: Unit; timeout: number | Store; target: Target; - name?: string; }): Target; export function debounce({ source, timeout, target, }: { - source: Event | Effect | Store; + source: Unit; timeout?: number | Store; - /** @deprecated */ - name?: string; - target?: - | Event - | Event - | Effect - | Effect - | Store; + target?: UnitTargetable | Unit }): typeof target extends undefined ? EventAsReturnType : typeof target { if (!is.unit(source)) throw new TypeError('source must be unit from effector'); @@ -65,7 +51,7 @@ export function debounce({ serialize: 'ignore', }).on(saveReject, (_, rj) => rj); - const tick: Unit = target ?? createEvent(); + const tick = (target as UnitTargetable) ?? createEvent(); const timerBaseFx = createEffect< { @@ -83,7 +69,7 @@ export function debounce({ }); }); const timerFx = attach({ - name: `debounce(${source.shortName || source.kind}) effect`, + name: `debounce(${(source as any)?.shortName || source.kind}) effect`, source: { timeoutId: $timeoutId, rejectPromise: $rejecter, diff --git a/src/delay/index.ts b/src/delay/index.ts index 6d38b737..44b08001 100644 --- a/src/delay/index.ts +++ b/src/delay/index.ts @@ -10,6 +10,7 @@ import { Target as TargetType, MultiTarget, UnitValue, + UnitTargetable, } from 'effector'; type TimeoutType = ((payload: Payload) => number) | Store | number; @@ -68,7 +69,7 @@ export function delay< target: timerFx, }); - sample({ clock: timerFx.doneData, target: targets as Unit[] }); + sample({ clock: timerFx.doneData, target: targets as UnitTargetable[] }); return target as any; } diff --git a/src/once/index.ts b/src/once/index.ts index 63028fb5..4d82bf30 100644 --- a/src/once/index.ts +++ b/src/once/index.ts @@ -1,25 +1,21 @@ import { Unit, - Store, Event, - Effect, EventAsReturnType, is, sample, createStore, } from 'effector'; -type SourceType = Event | Effect | Store; - export function once(config: { - source: SourceType; - reset?: SourceType; + source: Unit; + reset?: Unit; }): EventAsReturnType; -export function once(unit: SourceType): EventAsReturnType; +export function once(unit: Unit): EventAsReturnType; export function once( - unitOrConfig: { source: SourceType; reset?: SourceType } | SourceType, + unitOrConfig: { source: Unit; reset?: Unit } | Unit, ): EventAsReturnType { let source: Unit; let reset: Unit | undefined; diff --git a/src/reset/index.ts b/src/reset/index.ts index 18892be2..a81deacd 100644 --- a/src/reset/index.ts +++ b/src/reset/index.ts @@ -1,10 +1,10 @@ import { createEvent } from 'effector'; -import type { Event, Unit, Store } from 'effector'; +import type { Event, Unit, Store, StoreWritable } from 'effector'; type Params = { clock?: Unit | Array>; - target: Store | Array>; + target: StoreWritable | Array>; }; export function reset(config: Required): void; diff --git a/src/spread/index.ts b/src/spread/index.ts index fd431db5..8dae1a68 100644 --- a/src/spread/index.ts +++ b/src/spread/index.ts @@ -1,4 +1,4 @@ -import { createEvent, Event, sample, Unit } from 'effector'; +import { createEvent, Event, EventCallable, sample, Unit, UnitTargetable } from 'effector'; const hasPropBase = {}.hasOwnProperty; const hasOwnProp = (object: O, key: string) => @@ -9,7 +9,7 @@ type EventAsReturnType = any extends Payload ? Event : never; export function spread(config: { targets: { - [Key in keyof Payload]?: Unit; + [Key in keyof Payload]?: UnitTargetable; }; }): EventAsReturnType>; @@ -20,8 +20,8 @@ export function spread< source: Source; targets: { [Key in keyof Payload]?: - | EventAsReturnType> - | Unit>; + | EventCallable> + | UnitTargetable>; }; }): Source; @@ -56,7 +56,7 @@ export function spread

({ batch: false, clock: hasTargetKey, fn: (object: P) => object[targetKey], - target: currentTarget as Unit, + target: currentTarget as UnitTargetable, }); } } diff --git a/src/throttle/index.ts b/src/throttle/index.ts index db3e1034..687a0073 100644 --- a/src/throttle/index.ts +++ b/src/throttle/index.ts @@ -7,6 +7,7 @@ import { sample, Store, Unit, + UnitTargetable, } from 'effector'; type EventAsReturnType = any extends Payload ? Event : never; @@ -16,7 +17,7 @@ export function throttle(_: { timeout: number | Store; name?: string; }): EventAsReturnType; -export function throttle>(_: { +export function throttle>(_: { source: Unit; timeout: number | Store; target: Target; @@ -30,7 +31,7 @@ export function throttle({ source: Unit; timeout: number | Store; name?: string; - target?: Unit; + target?: UnitTargetable; }): EventAsReturnType { if (!is.unit(source)) throw new TypeError('source must be unit from effector');