1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _utils = require("@wdio/utils");
|
9 |
|
10 | const TIMEOUT_ERROR = 'timeout';
|
11 |
|
12 | class Timer {
|
13 | constructor(delay, timeout, fn, leading) {
|
14 | this._delay = delay;
|
15 | this._timeout = timeout;
|
16 | this._fn = fn;
|
17 | this._leading = leading;
|
18 | this._conditionExecutedCnt = 0;
|
19 |
|
20 | if (_utils.hasWdioSyncSupport && !fn.name.includes('async') && Boolean(global.browser)) {
|
21 | this._fn = () => (0, _utils.runFnInFiberContext)(fn)();
|
22 | }
|
23 |
|
24 | const retPromise = new Promise((resolve, reject) => {
|
25 | this._resolve = resolve;
|
26 | this._reject = reject;
|
27 | });
|
28 | this.start();
|
29 | return retPromise;
|
30 | }
|
31 |
|
32 | start() {
|
33 | this._start = Date.now();
|
34 | this._ticks = 0;
|
35 | emitTimerEvent({
|
36 | id: this._start,
|
37 | start: true
|
38 | });
|
39 |
|
40 | if (this._leading) {
|
41 | this.tick();
|
42 | } else {
|
43 | this._timeoutId = setTimeout(this.tick.bind(this), this._delay);
|
44 | }
|
45 |
|
46 | this._mainTimeoutId = setTimeout(() => {
|
47 | if (!this.wasConditionExecuted()) {
|
48 | return;
|
49 | }
|
50 |
|
51 | emitTimerEvent({
|
52 | id: this._start,
|
53 | timeout: true
|
54 | });
|
55 | const reason = this.lastError || new Error(TIMEOUT_ERROR);
|
56 |
|
57 | this._reject(reason);
|
58 |
|
59 | this.stop();
|
60 | }, this._timeout);
|
61 | }
|
62 |
|
63 | stop() {
|
64 | if (this._timeoutId) {
|
65 | clearTimeout(this._timeoutId);
|
66 | }
|
67 |
|
68 | this._timeoutId = null;
|
69 | }
|
70 |
|
71 | stopMain() {
|
72 | emitTimerEvent({
|
73 | id: this._start
|
74 | });
|
75 | clearTimeout(this._mainTimeoutId);
|
76 | }
|
77 |
|
78 | tick() {
|
79 | const result = this._fn();
|
80 |
|
81 | if (typeof result.then !== 'function') {
|
82 | if (!result) {
|
83 | return this.checkCondition(new Error('return value was never truthy'));
|
84 | }
|
85 |
|
86 | return this.checkCondition(null, result);
|
87 | }
|
88 |
|
89 | result.then(res => this.checkCondition(null, res), err => this.checkCondition(err));
|
90 | }
|
91 |
|
92 | checkCondition(err, res) {
|
93 | ++this._conditionExecutedCnt;
|
94 | this.lastError = err;
|
95 |
|
96 | if (res) {
|
97 | this._resolve(res);
|
98 |
|
99 | this.stop();
|
100 | this.stopMain();
|
101 | return;
|
102 | }
|
103 |
|
104 | let diff = Date.now() - this._start - this._ticks++ * this._delay;
|
105 |
|
106 | let delay = Math.max(0, this._delay - diff);
|
107 | this.stop();
|
108 |
|
109 | if (this.hasTime(delay)) {
|
110 | this._timeoutId = setTimeout(this.tick.bind(this), delay);
|
111 | } else {
|
112 | this.stopMain();
|
113 | const reason = this.lastError || new Error(TIMEOUT_ERROR);
|
114 |
|
115 | this._reject(reason);
|
116 | }
|
117 | }
|
118 |
|
119 | hasTime(delay) {
|
120 | return Date.now() - this._start + delay <= this._timeout;
|
121 | }
|
122 |
|
123 | wasConditionExecuted() {
|
124 | return this._conditionExecutedCnt > 0;
|
125 | }
|
126 |
|
127 | }
|
128 |
|
129 | function emitTimerEvent(payload) {
|
130 | if (_utils.hasWdioSyncSupport) {
|
131 | process.emit('WDIO_TIMER', payload);
|
132 | }
|
133 | }
|
134 |
|
135 | var _default = Timer;
|
136 | exports.default = _default; |
\ | No newline at end of file |