-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule.ts
81 lines (68 loc) · 2.12 KB
/
schedule.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
72
73
74
75
76
77
78
79
80
81
import { DirectedGraph } from '../directed-graph/directed-graph';
import {
add,
build,
createTag,
debug,
has,
remove,
removeTag,
run,
} from '../scheduler/scheduler';
import type { Runnable, Tag } from '../scheduler/types';
import type { OptionsObject, SingleOptionsObject } from './types';
import { createOptionsFns } from './utils/create-options-fns';
export class Schedule<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<T>): Schedule<T>;
add(runnable: Runnable<T>, options?: SingleOptionsObject<T>): Schedule<T>;
add(
runnable: Runnable<T> | Runnable<T>[],
options?: OptionsObject<T> | SingleOptionsObject<T>
): Schedule<T> {
const optionsFns = createOptionsFns<T>(options);
add(this, runnable, ...optionsFns);
return this;
}
has(runnable: Runnable<T>): boolean {
return has(this, runnable);
}
async run(context: T): Promise<Schedule<T>> {
await run(this, context);
return this;
}
build(): Schedule<T> {
build(this);
return this;
}
remove(runnable: Runnable<T>): Schedule<T> {
remove(this, runnable);
return this;
}
debug(): Schedule<T> {
debug(this);
return this;
}
createTag(id: symbol | string, options?: OptionsObject<T>): Schedule<T> {
const optionsFns = createOptionsFns<T>(options);
createTag(this, id, ...optionsFns);
return this;
}
removeTag(id: symbol | string): Schedule<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);
}
}