Skip to content

Commit

Permalink
Merge pull request #322 from effector/fix-trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova authored Jan 26, 2024
2 parents 0118292 + 7e8cf4e commit d28b802
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/interval/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Event, Store, createEvent, createStore, sample, attach, is } from 'effector';
import {
Event,
EventCallable,
Store,
createEvent,
createStore,
sample,
attach,
is,
} from 'effector';

export function interval<S extends unknown, F extends unknown>(config: {
timeout: number | Store<number>;
Expand Down Expand Up @@ -27,8 +36,21 @@ export function interval<S extends unknown, F extends unknown>({
leading?: boolean;
trailing?: boolean;
}): { tick: Event<void>; isRunning: Store<boolean> } & TriggerProtocol {
const setup = (start ?? createEvent()) as Event<void>;
const teardown = (stop ?? createEvent()) as Event<void>;
const setup = createEvent();
if (start) {
sample({
clock: start,
target: setup,
});
}

const teardown = createEvent();
if (stop) {
sample({
clock: stop,
target: teardown,
});
}

const tick = createEvent();
const $isRunning = createStore(false);
Expand Down Expand Up @@ -146,8 +168,8 @@ function toStoreNumber(value: number | Store<number> | unknown): Store<number> {
*/
export type TriggerProtocol = {
'@@trigger': () => {
setup: Event<void>;
teardown: Event<void>;
setup: EventCallable<void>;
teardown: EventCallable<void>;
fired: Event<unknown> | Event<void>;
};
};

0 comments on commit d28b802

Please sign in to comment.