-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistent-function-queue.js
51 lines (44 loc) · 1.42 KB
/
persistent-function-queue.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
const PersistentQueue = require('./persistent-queue')
function evaluateFunctionSource (source, ...args) {
args = args.map(arg => JSON.stringify(arg))
const invocation = `(${source})(...[${args}])`
// console.log('Invoking')
// console.log(` ${invocation}`)
return eval(invocation)
}
// function evaluateFunction (func, ...args) {
// const source = func.toString()
// return evaluateFunctionSource(source, ...args)
// }
class PersistentFunctionQueue {
constructor (db) {
this.queue = new PersistentQueue(db)
}
enqueue (func) {
if (typeof func !== 'function') throw new Error(`Expected a function but got ${func}`)
this.queue.push(func.toString())
}
dequeue (input) {
const nextFunctionSource = this.queue.shift()
if (!nextFunctionSource) return
return evaluateFunctionSource(nextFunctionSource, input)
}
isEmpty () {
return this.queue.isEmpty()
}
}
module.exports = PersistentFunctionQueue
/*
Queue
enqueue: (func) =>
load the queue from storage
add a function at the back of the queue
store the updated queue
dequeue: () =>
load the queue from storage
retrieve and parse user input (derived from one of the items the last dequeued function returned)
execute the function at the front of the queue; pass it the parsed user input
send the return value of the function to alfred
remove the function from the front of the queue
store the updated queue
*/