-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduler.ts
71 lines (59 loc) · 1.75 KB
/
scheduler.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { DirectedGraph } from '../directed-graph';
import {
add,
build,
createTag,
debug,
remove,
removeTag,
run,
} from '../scheduler';
import type { Runnable, Tag } from '../scheduler-types';
import type { OptionsObject } from './types';
import { createOptionsFns } from './utils/create-options-fns';
export class Scheduler<T extends Scheduler.Context = Scheduler.Context> {
dag: DirectedGraph<Runnable<T>>;
tags: Map<symbol | string, Tag<any>>;
symbols: Map<symbol | string, Runnable<T>>;
constructor() {
this.dag = new DirectedGraph<Runnable<T>>();
this.tags = new Map<symbol | string, Tag<T>>();
this.symbols = new Map<symbol | string, Runnable<T>>();
}
add(runnable: Runnable<T>, options?: OptionsObject): Scheduler<T> {
const optionsFns = createOptionsFns<T>(options);
add(this, runnable, ...optionsFns);
return this;
}
run(context: T): Scheduler<T> {
run(this, context);
return this;
}
build(): Scheduler<T> {
build(this);
return this;
}
remove(runnable: Runnable): Scheduler<T> {
remove(this, runnable);
return this;
}
debug(): Scheduler<T> {
debug(this);
return this;
}
createTag(id: symbol | string, options?: OptionsObject): Scheduler<T> {
const optionsFns = createOptionsFns<T>(options);
createTag(this, id, ...optionsFns);
return this;
}
removeTag(id: symbol | string): Scheduler<T> {
removeTag(this, id);
return this;
}
hasTag(id: symbol | string): boolean {
return this.tags.has(id);
}
getRunnable(id: symbol | string): Runnable<T> | undefined {
return this.symbols.get(id);
}
}