-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstep.ts
102 lines (88 loc) · 2.11 KB
/
step.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Server-side start/stop/step support for process-step plugin
import * as wiki from "seran/wiki.ts"
export class ProcessStep {
name: string
status: string
running: boolean
waiting: any
resume: any
run: any
constructor(name, running, run) {
this.name = name
this.status = 'beginning';
this.running = running;
this.waiting = null;
this.resume = null;
this.run = run;
}
async step(now) {
this.status = now
console.log(this.name, now)
if (!this.running) {
return this.waiting = new Promise(resolve => {
this.resume = resolve
})
} else {
return null
}
}
async button(action) {
async function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
if (action == 'start') {
console.log('start')
if (!this.running && !this.waiting) {
this.running = true
let t0 = Date.now()
console.log('run',this.run)
this.run().then(() => {
console.log('done',this.run)
this.running=false;
this.status=`complete in ${(Date.now()-t0)/1000} seconds`
});
} else if (this.waiting) {
this.waiting = null;
this.running = true
console.log('resume',this.run)
this.resume()
}
}
if (action == 'step') {
console.log('step',this.run)
if (this.running) {
this.running = false;
} else if (this.waiting) {
this.waiting = null;
await sleep(30)
this.resume()
}
}
if (action == 'stop') {
console.log('stop',this.run)
if (this.running) {
this.running = false;
}
}
}
register(handler) {
function route (verb) {
handler.route(`/${this.name}\\?action=${verb}`, req => {
this.button(verb)
wiki.serveJson(req, {
running: this.running,
waiting: !!this.waiting,
status: this.status
})
return true
})
}
route.call(this,'start')
route.call(this,'stop')
route.call(this,'step')
route.call(this,'state')
return this
}
}