-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
59 lines (52 loc) · 1.21 KB
/
runner.js
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
class Runner {
constructor(process, delay) {
this.process = process
this.delay = delay
this.processID = null
this.step = 1
}
runIt() {
let n = this.process.next()
for (let i = 1; i < this.step; i++) {
n = this.process.next()
if (n.done) {
break
}
}
if (!n.done) {
let d = this.delay
if (n.value != null) {
d *= n.value
}
this.processID = setTimeout(this.runIt.bind(this), d)
} else {
this.processID = null
this.process = null
}
}
start() {
if (!this.running()) {
this.processID = setTimeout(this.runIt.bind(this))
}
}
pause() {
if (this.running()) {
clearTimeout(this.processID)
this.processID = null
}
}
next() {
if (this.process.next().done) {
this.processID = null
this.process = null
}
}
finish() {
this.pause()
while (!this.process.next().done) {}
this.process = null
}
running() {
return this.processID != null
}
}