-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
postpone.ts
43 lines (38 loc) · 942 Bytes
/
postpone.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {
createEvent,
createStore,
sample,
type Event,
type EventAsReturnType,
type Store,
} from 'effector';
import { and } from './and';
import { not } from './not';
/**
* Откладывает выполнение события до переданного указанного стора в состояние `true`
*
* @returns событие, которое будет вызывано после вызова clock, когда until будет true
*/
export function postpone<T>({
clock,
until,
}: {
clock: Event<T>;
until: Store<boolean>;
}): EventAsReturnType<T> {
const target = createEvent<T>();
const $fired = createStore(false, {
serialize: 'ignore',
name: 'ff.$fired',
sid: 'ff.$fired',
})
.on(target, () => true)
.on(clock, () => false);
sample({
clock: [clock, until],
source: clock,
filter: and(until, not($fired)),
target,
});
return target;
}