-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduleAction.js
More file actions
executable file
·62 lines (58 loc) · 2.45 KB
/
scheduleAction.js
File metadata and controls
executable file
·62 lines (58 loc) · 2.45 KB
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
const { performance } = require('perf_hooks')
var init
module.exports = blockgenerator
function blockgenerator ({ blockinterval = 6000, intrinsics }, log, emit) {
if (init) throw Error('block generator already initialized')
var currentBlock = 0
var scheduled = []
var total = 0 // TODO: make this BigInt
init = setInterval(async () => {
// if (currentBlock === 0) {
// for (var i = 0; i < scheduled.length; i++) {
// const action = scheduled[i]
// action.executeBlock += currentBlock
// }
// }
const startTime = performance.now()
currentBlock++
const temp = [...scheduled]
scheduled = []
while (temp.length) {
if ((performance.now() - startTime) > (blockinterval - 200)) {
scheduled = [...temp, ...scheduled]
log({ type: 'Error', data: 'not able to check or execute the remaining ${temp.length} intrinsics in scheduler during blockinterval' })
emit({ from: 'blockgenerator', type: 'block', data: { number: currentBlock, startTime } })
// TODO: maybe allow listeners to `emit` (= '*') events?
return
}
const { id, from, type, data, executeBlock } = temp.shift()
if (!id) continue // old item - id removed by `cancelAction(id)`
else if (executeBlock <= currentBlock) {
log({ type: 'scheduler', data: `Executing: ${type}` })
const intrinsic = intrinsics[type]
await intrinsic(from, data)
}
else scheduled.push({ id, from, type, data, executeBlock })
}
emit({ type: 'block', data: { number: currentBlock, startTime } })
}, blockinterval)
function scheduleAction ({ from, data, delay, type }) {
if (delay <= 0) return void setTimeout(() => {
const intrinsic = intrinsics[type]
intrinsic(from, data)
}, 0)
const item = { from, type, data, executeBlock: currentBlock + delay/*currentBlock? currentBlock + delay : delay*/ }
const id = item.id = total = total + 1
scheduled.push(item)
log({ type: 'scheduler', data: { from: from.path, type, data, id, executeBlock: item.executeBlock } })
return id
}
function cancelAction (id) {
log({ type: 'scheduler', data: { text: `Cancel: ${id}`, scheduled: scheduled.filter(entry => entry.id) } })
// log({ type: 'scheduler', data: { text: `Cancel: ${id}` } })
for (var i = 0, len = scheduled.length; i < len; i++) {
if (scheduled[i].id === id) scheduled[i].id = undefined
}
}
return { scheduleAction, cancelAction }
}