1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fibers_1 = __importDefault(require("./fibers"));
|
7 | const logger_1 = __importDefault(require("@wdio/logger"));
|
8 | const log = (0, logger_1.default)('@wdio/sync');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | async function executeHooksWithArgs(hookName, hooks = [], args) {
|
19 | |
20 |
|
21 |
|
22 | if (typeof hooks === 'function') {
|
23 | hooks = [hooks];
|
24 | }
|
25 | |
26 |
|
27 |
|
28 | if (!Array.isArray(args)) {
|
29 | args = [args];
|
30 | }
|
31 | const hookPromise = hooks.map((hook) => new Promise((resolve) => {
|
32 | let result;
|
33 | const execHook = () => {
|
34 | delete global.browser._NOT_FIBER;
|
35 | try {
|
36 | result = hook.apply(null, args);
|
37 | }
|
38 | catch (err) {
|
39 | log.error(err.stack);
|
40 | return resolve(err);
|
41 | }
|
42 | if (result && typeof result.then === 'function') {
|
43 | return result.then(resolve, (err) => {
|
44 | log.error(err.stack);
|
45 | resolve(err);
|
46 | });
|
47 | }
|
48 | resolve(result);
|
49 | };
|
50 | |
51 |
|
52 |
|
53 | return hook.constructor.name === 'AsyncFunction' ? execHook() : (0, fibers_1.default)(execHook).run();
|
54 | }));
|
55 | const start = Date.now();
|
56 | const result = await Promise.all(hookPromise);
|
57 | if (hookPromise.length) {
|
58 | log.debug(`Finished to run "${hookName}" hook in ${Date.now() - start}ms`);
|
59 | }
|
60 | return result;
|
61 | }
|
62 | exports.default = executeHooksWithArgs;
|