File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -6,17 +6,21 @@ class Logger {
6
6
static color ( level ) {
7
7
return Logger . COLORS [ level ] || Logger . COLORS . info ;
8
8
}
9
+
9
10
log ( level , s ) {
10
11
const date = new Date ( ) . toISOString ( ) ;
11
12
const color = Logger . color ( level ) ;
12
13
console . log ( color + date + '\t' + s + '\x1b[0m' ) ;
13
14
}
15
+
14
16
warn ( s ) {
15
17
this . log ( 'warn' , s ) ;
16
18
}
19
+
17
20
error ( s ) {
18
21
this . log ( 'error' , s ) ;
19
22
}
23
+
20
24
info ( s ) {
21
25
this . log ( 'info' , s ) ;
22
26
}
@@ -46,9 +50,11 @@ class Task extends EventEmitter {
46
50
this . count = 0 ;
47
51
this . timer = null ;
48
52
}
53
+
49
54
get active ( ) {
50
55
return ! ! this . timer ;
51
56
}
57
+
52
58
start ( ) {
53
59
this . stop ( ) ;
54
60
if ( this . running ) return false ;
@@ -59,12 +65,14 @@ class Task extends EventEmitter {
59
65
} , time ) ;
60
66
return true ;
61
67
}
68
+
62
69
stop ( ) {
63
70
if ( ! this . active || this . running ) return false ;
64
71
this . clear ( this . timer ) ;
65
72
this . timer = null ;
66
73
return true ;
67
74
}
75
+
68
76
run ( ) {
69
77
if ( ! this . active || this . running ) return false ;
70
78
this . running = true ;
@@ -85,6 +93,7 @@ class Scheduler extends EventEmitter {
85
93
this . tasks = new Map ( ) ;
86
94
this . logger = new Logger ( ) ;
87
95
}
96
+
88
97
task ( name , time , exec ) {
89
98
this . stop ( name ) ;
90
99
const task = new Task ( name , time , exec ) ;
@@ -102,13 +111,15 @@ class Scheduler extends EventEmitter {
102
111
task . start ( ) ;
103
112
return task ;
104
113
}
114
+
105
115
stop ( name ) {
106
116
const task = this . tasks . get ( name ) ;
107
117
if ( task ) {
108
118
task . stop ( ) ;
109
119
this . tasks . delete ( name ) ;
110
120
}
111
121
}
122
+
112
123
stopAll ( ) {
113
124
for ( const name of this . tasks . keys ( ) ) {
114
125
this . stop ( name ) ;
You can’t perform that action at this time.
0 commit comments