-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-controller.ts
More file actions
36 lines (29 loc) · 845 Bytes
/
create-controller.ts
File metadata and controls
36 lines (29 loc) · 845 Bytes
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
import { sample } from 'effector';
import { defaultDomain } from './domain';
import { Controller, ControllerConfig, Subscription } from './types';
export const createController = (config?: ControllerConfig): Controller => {
const { cancel: userCancel, domain = defaultDomain } = config ?? {};
const cancel = domain.createEvent();
const onCancel = (fn: () => void): Subscription => {
return cancel.watch(() => fn());
};
const $controller = domain
.createStore(new AbortController())
.on(cancel, controller => {
controller.abort();
return new AbortController();
});
if (userCancel) {
sample({
source: $controller,
clock: userCancel,
fn: () => {},
target: cancel,
});
}
return {
getSignal: () => $controller.getState().signal,
cancel,
onCancel,
};
};