-
Notifications
You must be signed in to change notification settings - Fork 16
/
Timer.js
261 lines (143 loc) · 4.5 KB
/
Timer.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const assert = require ('assert')
const EventEmitter = require ('events')
const LogEvent = require ('./Log/Events/Timer.js')
const ErrorEvent = require ('./Log/Events/Error.js')
const PlannedEvent = require ('./Timer/PlannedEvent.js')
const TimeSlot = require ('./Timer/TimeSlot.js')
const Pause = require ('./Timer/Pause.js')
const Executor = require ('./Timer/Executor.js')
const TimerPromise = require ('./Timer/Promise.js')
const Throttle = require ('./Timer/Throttle.js')
const Dia = require ('./Dia.js')
const MAX_INT = 2147483647
module.exports = class extends EventEmitter {
////////// Reading params
constructor (o) {
super ()
this.uuid = Dia.new_uuid ()
assert (this.conf = o.conf, 'conf not set')
assert (this.name = o.name, 'name not set')
this.conf.add_timer (this)
if (o.on_change) this.addListener ('change', o.on_change)
this.from_to (o.from, o.to)
this.log_meta = {...(o.log_meta || {}), timer: this}
this.scheduled_event = null
this.running_event = null
new Executor (this, o)
this.executor.on ('finish', () => this.finish ())
new Throttle (this, o)
this.current_pause = null
if (o.is_paused) this.pause ()
if (o.ticker) this.set_ticker (o.ticker)
}
* adjusters () {
yield this.throttle
const {time_slot} = this; if (time_slot) yield time_slot
}
from_to (from, to) {
if (from == null && to == null) return this.time_slot = null
this.time_slot =
from == null && to == null ? null :
new TimeSlot ({from, to})
}
////////// Presentation
to_record () {
let r = {}, {scheduled_event, running_event, current_pause} = this
if (scheduled_event != null) r.ts_scheduled = scheduled_event.date.toJSON ()
if (running_event != null) r.is_busy = true
if (current_pause != null) current_pause.append_info (r)
return r
}
notify () {
let state = JSON.stringify (this.to_record ())
if (this._last_state == state) return
this._last_state = state
this.emit ('change', state, this.name)
}
////////// Logging
log (s, ms, log_event) {
let m = !ms ? '' : ': ' + new Date (ms).toJSON ()
m += ' ' + s
this.log_write (new LogEvent ({
...this.log_meta,
parent: log_event,
label: m
}))
}
log_write (e) {
this.conf.log_event (e)
return e
}
log_start (label) {
return this.log_write (new LogEvent ({
...this.log_meta,
parent: null,
phase: 'before',
label
}))
}
log_finish (e) {
return this.log_write (e.finish ())
}
////////// Setting up
clear (comment) {
let {scheduled_event} = this; if (scheduled_event == null) return null
return scheduled_event.cancel (comment)
}
in (ms, comment) {
this.at (Date.now () + ms, comment)
}
on (comment) {
this.in (0, comment)
}
at (ts, comment) {
if (!(ts instanceof Date)) ts = new Date (ts)
if (comment == null) {
comment = `at (${ts.toISOString ()}) called`
let s = (new Error ('?')).stack.split (/[\n\r]+/).slice (1).find (s => !/Timer.js:/.test (s))
if (s) {
let a = s.split (/[\(\)]/); if (a.length === 3) comment += ' from ' + a [1]
}
}
new PlannedEvent (this, ts, comment)
}
is_idle () {
if (this.scheduled_event != null) return false
if (this.running_event != null) return false
if (this.executor.is_to_reset) return false
return true
}
finish () {
if (!this.is_idle ()) return
if (this.tick ()) return
this.emit ('stop')
}
promise (comment) {
return new TimerPromise (this, comment)
}
////////// Pause
is_paused () {
return this.current_pause != null
}
pause (error) {
if (!this.is_paused ()) new Pause (this, {error})
}
resume (comment) {
const {current_pause} = this; if (current_pause != null) current_pause.cancel ()
}
////////// Ticker
set_ticker (v) {
this.ticker = v
this.tick ('ticker setup')
}
get_next_tick () {
if (this.is_paused ()) return null
let {ticker} = this; if (!ticker) return null
return ticker ()
}
tick (comment) {
let when = this.get_next_tick (); if (!when) return null
this.at (when, comment || 'tick occured')
return when
}
}