Skip to content

Commit

Permalink
Use UnitTargetable
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Oct 23, 2023
1 parent 265bd07 commit 001203a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 60 deletions.
17 changes: 12 additions & 5 deletions src/combine-events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sample,
Store,
Unit,
UnitTargetable,
withRegion,
} from 'effector';

Expand Down Expand Up @@ -40,7 +41,7 @@ export function combineEvents<P extends Shape>(config: {

export function combineEvents<
P extends Shape,
T extends Unit<P extends Tuple ? P : Partial<P>>,
T extends UnitTargetable<P extends Tuple ? P : Partial<P>>,
>(config: { events: Events<P>; target: T; reset?: Unit<any> }): ReturnTarget<P, T>;

export function combineEvents<P>({
Expand All @@ -50,9 +51,10 @@ export function combineEvents<P>({
}: {
events: Events<P>;
reset?: Unit<any>;
target?: Unit<any>;
target?: UnitTargetable<any> | Unit<any>;
}) {
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, () => {
Expand Down Expand Up @@ -87,10 +89,15 @@ export function combineEvents<P>({
});
}

const eventsTrriggered = sample({
source: $results,
clock: [...(Object.values(events) as Unit<any>[])],
});

sample({
source: sample({ source: $results, clock: merge(Object.values(events)) }),
source: eventsTrriggered,
filter: $counter.map((value) => value === 0),
target,
target: target as UnitTargetable<any>,
});
});

Expand Down
26 changes: 13 additions & 13 deletions src/condition/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
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> = T & { [K in keyof T]: T[K] };
type EventAsReturnType<Payload> = any extends Payload ? Event<Payload> : never;

export function condition<State>(options: {
source: Event<State>;
if: ((payload: State) => boolean) | Store<boolean> | State;
then: Unit<NoInfer<State> | void>;
else: Unit<NoInfer<State> | void>;
then: UnitTargetable<NoInfer<State> | void>;
else: UnitTargetable<NoInfer<State> | void>;
}): EventAsReturnType<State>;
export function condition<State>(options: {
source: Store<State>;
if: ((payload: State) => boolean) | Store<boolean> | State;
then: Unit<State | void>;
else: Unit<State | void>;
then: UnitTargetable<State | void>;
else: UnitTargetable<State | void>;
}): Store<State>;
export function condition<Params, Done, Fail>(options: {
source: Effect<Params, Done, Fail>;
if: ((payload: Params) => boolean) | Store<boolean> | Params;
then: Unit<NoInfer<Params> | void>;
else: Unit<NoInfer<Params> | void>;
then: UnitTargetable<NoInfer<Params> | void>;
else: UnitTargetable<NoInfer<Params> | void>;
}): Effect<Params, Done, Fail>;

export function condition<State>(options: {
source: Event<State>;
if: ((payload: State) => boolean) | Store<boolean> | State;
then: Unit<NoInfer<State> | void>;
then: UnitTargetable<NoInfer<State> | void>;
}): EventAsReturnType<State>;
export function condition<State>(options: {
source: Store<State>;
if: ((payload: State) => boolean) | Store<boolean> | State;
then: Unit<NoInfer<State> | void>;
then: UnitTargetable<NoInfer<State> | void>;
}): Store<State>;
export function condition<Params, Done, Fail>(options: {
source: Effect<Params, Done, Fail>;
if: ((payload: Params) => boolean) | Store<boolean> | Params;
then: Unit<NoInfer<Params> | void>;
then: UnitTargetable<NoInfer<Params> | void>;
}): Effect<Params, Done, Fail>;

export function condition<State>(options: {
source: Event<State>;
if: ((payload: State) => boolean) | Store<boolean> | State;
else: Unit<NoInfer<State> | void>;
else: UnitTargetable<NoInfer<State> | void>;
}): EventAsReturnType<State>;
export function condition<State>(options: {
source: Store<State>;
if: ((payload: State) => boolean) | Store<boolean> | State;
else: Unit<NoInfer<State> | void>;
else: UnitTargetable<NoInfer<State> | void>;
}): Store<State>;
export function condition<Params, Done, Fail>(options: {
source: Effect<Params, Done, Fail>;
if: ((payload: Params) => boolean) | Store<boolean> | Params;
else: Unit<NoInfer<Params> | void>;
else: UnitTargetable<NoInfer<Params> | void>;
}): Effect<Params, Done, Fail>;

// Without `source`
Expand Down
34 changes: 10 additions & 24 deletions src/debounce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,38 @@ import {
createEffect,
createEvent,
createStore,
Event,
is,
sample,
Store,
Unit,
attach,
merge,
Effect,
UnitTargetable,
EventAsReturnType
} from 'effector';

type EventAsReturnType<Payload> = any extends Payload ? Event<Payload> : never;

export function debounce<T>(_: {
source: Event<T> | Effect<T, any, any> | Store<T>;
source: Unit<T>;
timeout: number | Store<number>;
name?: string;
}): EventAsReturnType<T>;
export function debounce<
T,
Target extends
| Event<T>
| Event<void>
| Effect<T, any, any>
| Effect<void, any, any>
| Store<T>,
| UnitTargetable<T>
| UnitTargetable<void>,
>(_: {
source: Event<T> | Effect<T, any, any> | Store<T>;
source: Unit<T>;
timeout: number | Store<number>;
target: Target;
name?: string;
}): Target;
export function debounce<T>({
source,
timeout,
target,
}: {
source: Event<T> | Effect<T, any, any> | Store<T>;
source: Unit<T>;
timeout?: number | Store<number>;
/** @deprecated */
name?: string;
target?:
| Event<T>
| Event<void>
| Effect<T, any, any>
| Effect<void, any, any>
| Store<T>;
target?: UnitTargetable<T> | Unit<T>
}): typeof target extends undefined ? EventAsReturnType<T> : typeof target {
if (!is.unit(source)) throw new TypeError('source must be unit from effector');

Expand All @@ -65,7 +51,7 @@ export function debounce<T>({
serialize: 'ignore',
}).on(saveReject, (_, rj) => rj);

const tick: Unit<T | void> = target ?? createEvent();
const tick = (target as UnitTargetable<T>) ?? createEvent();

const timerBaseFx = createEffect<
{
Expand All @@ -83,7 +69,7 @@ export function debounce<T>({
});
});
const timerFx = attach({
name: `debounce(${source.shortName || source.kind}) effect`,
name: `debounce(${(source as any)?.shortName || source.kind}) effect`,
source: {
timeoutId: $timeoutId,
rejectPromise: $rejecter,
Expand Down
3 changes: 2 additions & 1 deletion src/delay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Target as TargetType,
MultiTarget,
UnitValue,
UnitTargetable,
} from 'effector';

type TimeoutType<Payload> = ((payload: Payload) => number) | Store<number> | number;
Expand Down Expand Up @@ -68,7 +69,7 @@ export function delay<
target: timerFx,
});

sample({ clock: timerFx.doneData, target: targets as Unit<any>[] });
sample({ clock: timerFx.doneData, target: targets as UnitTargetable<any>[] });

return target as any;
}
Expand Down
12 changes: 4 additions & 8 deletions src/once/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import {
Unit,
Store,
Event,
Effect,
EventAsReturnType,
is,
sample,
createStore,
} from 'effector';

type SourceType<T> = Event<T> | Effect<T, any, any> | Store<T>;

export function once<T>(config: {
source: SourceType<T>;
reset?: SourceType<any>;
source: Unit<T>;
reset?: Unit<any>;
}): EventAsReturnType<T>;

export function once<T>(unit: SourceType<T>): EventAsReturnType<T>;
export function once<T>(unit: Unit<T>): EventAsReturnType<T>;

export function once<T>(
unitOrConfig: { source: SourceType<T>; reset?: SourceType<any> } | SourceType<T>,
unitOrConfig: { source: Unit<T>; reset?: Unit<any> } | Unit<T>,
): EventAsReturnType<T> {
let source: Unit<T>;
let reset: Unit<any> | undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/reset/index.ts
Original file line number Diff line number Diff line change
@@ -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<any> | Array<Unit<any>>;
target: Store<any> | Array<Store<any>>;
target: StoreWritable<any> | Array<StoreWritable<any>>;
};

export function reset(config: Required<Params>): void;
Expand Down
10 changes: 5 additions & 5 deletions src/spread/index.ts
Original file line number Diff line number Diff line change
@@ -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 = <O extends { [k: string]: unknown }>(object: O, key: string) =>
Expand All @@ -9,7 +9,7 @@ type EventAsReturnType<Payload> = any extends Payload ? Event<Payload> : never;

export function spread<Payload>(config: {
targets: {
[Key in keyof Payload]?: Unit<Payload[Key]>;
[Key in keyof Payload]?: UnitTargetable<Payload[Key]>;
};
}): EventAsReturnType<Partial<Payload>>;

Expand All @@ -20,8 +20,8 @@ export function spread<
source: Source;
targets: {
[Key in keyof Payload]?:
| EventAsReturnType<Partial<Payload[Key]>>
| Unit<NoInfer<Payload[Key]>>;
| EventCallable<Partial<Payload[Key]>>
| UnitTargetable<NoInfer<Payload[Key]>>;
};
}): Source;

Expand Down Expand Up @@ -56,7 +56,7 @@ export function spread<P>({
batch: false,
clock: hasTargetKey,
fn: (object: P) => object[targetKey],
target: currentTarget as Unit<any>,
target: currentTarget as UnitTargetable<any>,
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/throttle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
sample,
Store,
Unit,
UnitTargetable,
} from 'effector';

type EventAsReturnType<Payload> = any extends Payload ? Event<Payload> : never;
Expand All @@ -16,7 +17,7 @@ export function throttle<T>(_: {
timeout: number | Store<number>;
name?: string;
}): EventAsReturnType<T>;
export function throttle<T, Target extends Unit<T>>(_: {
export function throttle<T, Target extends UnitTargetable<T>>(_: {
source: Unit<T>;
timeout: number | Store<number>;
target: Target;
Expand All @@ -30,7 +31,7 @@ export function throttle<T>({
source: Unit<T>;
timeout: number | Store<number>;
name?: string;
target?: Unit<any>;
target?: UnitTargetable<any>;
}): EventAsReturnType<T> {
if (!is.unit(source)) throw new TypeError('source must be unit from effector');

Expand Down

0 comments on commit 001203a

Please sign in to comment.