1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = treeProcessor;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const noop = () => {};
|
16 | function treeProcessor(options) {
|
17 | const {nodeComplete, nodeStart, queueRunnerFactory, runnableIds, tree} =
|
18 | options;
|
19 | function isEnabled(node, parentEnabled) {
|
20 | return parentEnabled || runnableIds.indexOf(node.id) !== -1;
|
21 | }
|
22 | function getNodeHandler(node, parentEnabled) {
|
23 | const enabled = isEnabled(node, parentEnabled);
|
24 | return node.children
|
25 | ? getNodeWithChildrenHandler(node, enabled)
|
26 | : getNodeWithoutChildrenHandler(node, enabled);
|
27 | }
|
28 | function getNodeWithoutChildrenHandler(node, enabled) {
|
29 | return function fn(done = noop) {
|
30 | node.execute(done, enabled);
|
31 | };
|
32 | }
|
33 | function getNodeWithChildrenHandler(node, enabled) {
|
34 | return async function fn(done = noop) {
|
35 | nodeStart(node);
|
36 | await queueRunnerFactory({
|
37 | onException: error => node.onException(error),
|
38 | queueableFns: wrapChildren(node, enabled),
|
39 | userContext: node.sharedUserContext()
|
40 | });
|
41 | nodeComplete(node);
|
42 | done();
|
43 | };
|
44 | }
|
45 | function hasNoEnabledTest(node) {
|
46 | return (
|
47 | node.disabled ||
|
48 | node.markedPending ||
|
49 | (node.children?.every(hasNoEnabledTest) ?? false)
|
50 | );
|
51 | }
|
52 | function wrapChildren(node, enabled) {
|
53 | if (!node.children) {
|
54 | throw new Error('`node.children` is not defined.');
|
55 | }
|
56 | const children = node.children.map(child => ({
|
57 | fn: getNodeHandler(child, enabled)
|
58 | }));
|
59 | if (hasNoEnabledTest(node)) {
|
60 | return children;
|
61 | }
|
62 | return node.beforeAllFns.concat(children).concat(node.afterAllFns);
|
63 | }
|
64 | const treeHandler = getNodeHandler(tree, false);
|
65 | return treeHandler();
|
66 | }
|