@kiwiproject/kiwi-test-js 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/waitForIt/waitForIt.d.ts +43 -3
- package/dist/waitForIt/waitForIt.js +74 -11
- package/package.json +1 -1
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
import { Time } from "convert";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new wait condition with defaults.
|
|
4
|
+
*
|
|
5
|
+
* Impl Note: This will NOT do anything unless `.until()` is called.
|
|
6
|
+
*/
|
|
2
7
|
export declare function wait(): WaitFor;
|
|
3
8
|
declare class WaitFor {
|
|
4
9
|
private timeoutMs;
|
|
5
|
-
private
|
|
10
|
+
private pollIntervalMs;
|
|
11
|
+
private pollDelayMs;
|
|
12
|
+
private alias;
|
|
13
|
+
private verbose;
|
|
6
14
|
constructor();
|
|
7
|
-
|
|
8
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Sets the amount of time between condition checks to wait.
|
|
17
|
+
* @param time The amount of time till the next poll
|
|
18
|
+
* @param units The units that correspond to the time. Default is ms.
|
|
19
|
+
*/
|
|
20
|
+
withPollInterval(time: number, units?: Time): this;
|
|
21
|
+
/**
|
|
22
|
+
* Sets the amount of time to wait until the condition is first checked.
|
|
23
|
+
* @param time The amount of time to wait
|
|
24
|
+
* @param units The units corresponding to the time. Default is ms.
|
|
25
|
+
*/
|
|
26
|
+
withPollDelay(time: number, units?: Time): this;
|
|
27
|
+
/**
|
|
28
|
+
* A unique name to give this wait check. Useful if more than one wait condition is used.
|
|
29
|
+
*
|
|
30
|
+
* @param alias The name to give this wait check.
|
|
31
|
+
*/
|
|
32
|
+
withAlias(alias: string): this;
|
|
33
|
+
/**
|
|
34
|
+
* Enables debug logging during the wait check.
|
|
35
|
+
*/
|
|
36
|
+
withVerbose(): this;
|
|
37
|
+
/**
|
|
38
|
+
* Sets the max time to wait for the condition to return true.
|
|
39
|
+
* @param time The amount of total time to wait for the condition to return true.
|
|
40
|
+
* @param units The units that correspond to the time. Default is ms.
|
|
41
|
+
*/
|
|
42
|
+
atMost(time: number, units?: Time): this;
|
|
43
|
+
/**
|
|
44
|
+
* Kicks off the wait check. The condition will be checked every pollInterval for a truthy response. If the max time
|
|
45
|
+
* to wait is reached, then a rejected Promise will be returned. If the condition becomes true, then a resolved Promise
|
|
46
|
+
* will be returned.
|
|
47
|
+
* @param cb The function to check if the condition is true or false.
|
|
48
|
+
*/
|
|
9
49
|
until(cb: () => boolean): Promise<string>;
|
|
10
50
|
}
|
|
11
51
|
export {};
|
|
@@ -5,40 +5,103 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.wait = void 0;
|
|
7
7
|
const convert_1 = __importDefault(require("convert"));
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new wait condition with defaults.
|
|
10
|
+
*
|
|
11
|
+
* Impl Note: This will NOT do anything unless `.until()` is called.
|
|
12
|
+
*/
|
|
8
13
|
function wait() {
|
|
9
14
|
return new WaitFor();
|
|
10
15
|
}
|
|
11
16
|
exports.wait = wait;
|
|
12
17
|
class WaitFor {
|
|
13
18
|
constructor() {
|
|
14
|
-
this.timeoutMs =
|
|
15
|
-
this.
|
|
19
|
+
this.timeoutMs = 4000;
|
|
20
|
+
this.pollIntervalMs = 100;
|
|
21
|
+
this.pollDelayMs = 0;
|
|
22
|
+
this.verbose = false;
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Sets the amount of time between condition checks to wait.
|
|
26
|
+
* @param time The amount of time till the next poll
|
|
27
|
+
* @param units The units that correspond to the time. Default is ms.
|
|
28
|
+
*/
|
|
29
|
+
withPollInterval(time, units = "ms") {
|
|
30
|
+
this.pollIntervalMs = (0, convert_1.default)(time, units).to("ms");
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Sets the amount of time to wait until the condition is first checked.
|
|
35
|
+
* @param time The amount of time to wait
|
|
36
|
+
* @param units The units corresponding to the time. Default is ms.
|
|
37
|
+
*/
|
|
38
|
+
withPollDelay(time, units = "ms") {
|
|
39
|
+
this.pollDelayMs = (0, convert_1.default)(time, units).to("ms");
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A unique name to give this wait check. Useful if more than one wait condition is used.
|
|
44
|
+
*
|
|
45
|
+
* @param alias The name to give this wait check.
|
|
46
|
+
*/
|
|
47
|
+
withAlias(alias) {
|
|
48
|
+
this.alias = alias;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Enables debug logging during the wait check.
|
|
53
|
+
*/
|
|
54
|
+
withVerbose() {
|
|
55
|
+
this.verbose = true;
|
|
19
56
|
return this;
|
|
20
57
|
}
|
|
21
|
-
|
|
22
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Sets the max time to wait for the condition to return true.
|
|
60
|
+
* @param time The amount of total time to wait for the condition to return true.
|
|
61
|
+
* @param units The units that correspond to the time. Default is ms.
|
|
62
|
+
*/
|
|
63
|
+
atMost(time, units = "ms") {
|
|
64
|
+
this.timeoutMs = (0, convert_1.default)(time, units).to("ms");
|
|
23
65
|
return this;
|
|
24
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Kicks off the wait check. The condition will be checked every pollInterval for a truthy response. If the max time
|
|
69
|
+
* to wait is reached, then a rejected Promise will be returned. If the condition becomes true, then a resolved Promise
|
|
70
|
+
* will be returned.
|
|
71
|
+
* @param cb The function to check if the condition is true or false.
|
|
72
|
+
*/
|
|
25
73
|
until(cb) {
|
|
26
|
-
const totalTries = this.timeoutMs / this.
|
|
74
|
+
const totalTries = this.timeoutMs / this.pollIntervalMs;
|
|
75
|
+
const aliasText = this.alias ? `with alias ${this.alias} ` : "";
|
|
76
|
+
if (this.verbose) {
|
|
77
|
+
console.log(`Waiting for condition ${aliasText}for up to ${totalTries} attempts checking every ${this.pollIntervalMs} ms`);
|
|
78
|
+
}
|
|
27
79
|
return new Promise((resolve, reject) => {
|
|
28
80
|
let tries = 1;
|
|
29
81
|
const loop = () => {
|
|
30
82
|
if (cb.apply(this)) {
|
|
31
|
-
|
|
83
|
+
if (this.verbose) {
|
|
84
|
+
console.log(`Condition ${aliasText}met after ${tries} of ${totalTries} tries`);
|
|
85
|
+
}
|
|
86
|
+
resolve(`Condition ${aliasText}met after ${tries} of ${totalTries} tries`);
|
|
32
87
|
}
|
|
33
88
|
else if (tries > totalTries) {
|
|
34
|
-
|
|
89
|
+
if (this.verbose) {
|
|
90
|
+
console.log(`Condition ${aliasText}was not met after ${totalTries} tries`);
|
|
91
|
+
}
|
|
92
|
+
reject(new Error(`Condition ${aliasText}was not met after ${totalTries} tries`));
|
|
35
93
|
}
|
|
36
94
|
else {
|
|
37
95
|
tries += 1;
|
|
38
|
-
setTimeout(loop, this.
|
|
96
|
+
setTimeout(loop, this.pollIntervalMs);
|
|
39
97
|
}
|
|
40
98
|
};
|
|
41
|
-
|
|
99
|
+
if (this.pollDelayMs > 0) {
|
|
100
|
+
setTimeout(loop, this.pollDelayMs);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
loop();
|
|
104
|
+
}
|
|
42
105
|
});
|
|
43
106
|
}
|
|
44
107
|
}
|
package/package.json
CHANGED