-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.js
More file actions
82 lines (70 loc) · 1.98 KB
/
utils.js
File metadata and controls
82 lines (70 loc) · 1.98 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const _ = require('lodash');
const {
Collection,
} = require('mongodb');
const utils = {
wait: (timeout) => {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
},
createEndlessLoop(callback, timeout) {
return utils.createConditionalLoop(async next => {
await callback();
next();
}, timeout);
},
createConditionalLoop(callback, timeout) {
const start = () => {
return new Promise((resolve, reject) => {
const next = () => {
setTimeout(async () => {
try {
await callback(() => {
next();
}, value => {
resolve(value);
});
} catch (ex) {
reject(ex);
}
}, timeout);
};
next();
});
};
return {
start,
};
},
validateSaga(saga) {
if (!saga) {
throw new Error('saga not found.');
}
if (!saga.id || !_.isString(saga.id)) {
throw new Error('saga must have an `id` field of type `String`');
}
if (!saga.flow) {
throw new Error('saga must have a `flow` field of type `Object`');
}
let propCount = 0;
for (const propKey in saga.flow) {
const prop = saga.flow[propKey];
if (!prop.id || !_.isString(prop.id)) {
throw new Error('saga `flow item` must have an `id` field of type `String`');
}
if (!prop.transaction || !_.isFunction(prop.transaction)) {
throw new Error('saga `flow item` must have a `transaction` field of type `Function`');
}
if (!prop.compensation || !_.isFunction(prop.compensation)) {
throw new Error('saga `flow item` must have a `compensation` field of type `Function`');
}
propCount += 1;
}
if (propCount < 2) {
throw new Error('saga `flow` prop must at least have `2` transaction/compensation pairs');
}
},
isMongoCollection: (object) => object instanceof Collection,
};
module.exports = utils;