-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (59 loc) · 2.07 KB
/
index.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
'use strict';
module.exports = function createDeployer() {
const runtimeStrategies = {
};
let _payload;
let _devices;
function checkPrepareForTypeErrors(devices, payload) {
if (typeof devices !== 'object') {
throw new Error('prepare() requires a devices object');
} else if (typeof payload !== 'object') {
throw new Error('prepare() requires a payload object');
}
}
function checkUseForTypeErrors(runtime, strategyFunction) {
if (typeof runtime !== 'string') {
throw new Error('use() requires a runtime string');
} else if (typeof strategyFunction !== 'function') {
throw new Error('use() requires a strategy function');
}
}
function generateNext(next, func) {
return (err) => {
if (err) throw err;
func(_devices, _payload, next);
};
}
function addStrategyFunctionToRuntimeQueue(runtime, strategyFunction) {
if (runtimeStrategies[runtime] === undefined) {
runtimeStrategies[runtime] = [];
}
runtimeStrategies[runtime].push(strategyFunction);
}
function executeQueue(queue) {
const queueReversed = queue.slice().reverse();
const go = queueReversed.reduce(generateNext, (()=>{}));
go();
}
function use(runtime, strategyFunction) {
checkUseForTypeErrors(runtime, strategyFunction);
addStrategyFunctionToRuntimeQueue(runtime, strategyFunction);
}
function deploy(onEndHandler = () => {}) {
const runtimes = Object.keys(runtimeStrategies);
runtimes.forEach(runtime => {
runtimeStrategies[runtime].push(() => onEndHandler(runtime));
executeQueue(runtimeStrategies[runtime]);
});
}
function prepare(devices, payload) {
checkPrepareForTypeErrors(devices, payload);
_devices = devices;
_payload = payload;
}
return {
use,
deploy,
prepare
};
};