-
Notifications
You must be signed in to change notification settings - Fork 791
/
check-helper.js
54 lines (52 loc) · 1.47 KB
/
check-helper.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
import toArray from './to-array';
import DqElement from './dq-element';
import AbstractVirtualNode from '../base/virtual-node/abstract-virtual-node';
/**
* Helper to denote which checks are asyncronous and provide callbacks and pass data back to the CheckResult
* @param {CheckResult} checkResult The target object
* @param {Function} callback The callback to expose when `this.async()` is called
* @return {Object} Bound to `this` for a check's fn
*/
function checkHelper(checkResult, options, resolve, reject) {
return {
isAsync: false,
async() {
this.isAsync = true;
return result => {
if (result instanceof Error === false) {
checkResult.result = result;
resolve(checkResult);
} else {
reject(result);
}
};
},
data(data) {
checkResult.data = data;
},
relatedNodes(nodes) {
if (!window.Node) {
return;
}
if (
nodes instanceof window.Node ||
nodes instanceof AbstractVirtualNode
) {
nodes = [nodes];
} else {
nodes = toArray(nodes);
}
checkResult.relatedNodes = [];
nodes.forEach(node => {
if (node instanceof AbstractVirtualNode) {
node = node.actualNode;
}
if (node instanceof window.Node) {
const dqElm = new DqElement(node);
checkResult.relatedNodes.push(dqElm);
}
});
}
};
}
export default checkHelper;