@augment-vir/common 4.0.0 → 4.1.0

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.
@@ -14,3 +14,10 @@ export declare type DeferredPromiseWrapper<T> = {
14
14
  reject: (reason?: any) => void;
15
15
  };
16
16
  export declare function createDeferredPromiseWrapper<T = void>(): DeferredPromiseWrapper<T>;
17
+ export declare type WaitForConditionInputs = {
18
+ conditionCallback: () => boolean | Promise<boolean>;
19
+ timeoutMs?: number;
20
+ intervalMs?: number;
21
+ timeoutMessage?: string;
22
+ };
23
+ export declare function waitForCondition({ conditionCallback, timeoutMs, intervalMs, timeoutMessage, }: WaitForConditionInputs): Promise<void>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createDeferredPromiseWrapper = exports.wrapPromiseInTimeout = exports.PromiseTimeoutError = exports.isPromiseLike = exports.wait = void 0;
3
+ exports.waitForCondition = exports.createDeferredPromiseWrapper = exports.wrapPromiseInTimeout = exports.PromiseTimeoutError = exports.isPromiseLike = exports.wait = void 0;
4
+ const error_1 = require("./error");
4
5
  const object_1 = require("./object");
5
6
  function wait(delayMs) {
6
7
  const deferredPromiseWrapper = createDeferredPromiseWrapper();
@@ -66,3 +67,27 @@ function createDeferredPromiseWrapper() {
66
67
  };
67
68
  }
68
69
  exports.createDeferredPromiseWrapper = createDeferredPromiseWrapper;
70
+ async function waitForCondition({ conditionCallback, timeoutMs = 10000, intervalMs = 100, timeoutMessage = '', }) {
71
+ let condition = false;
72
+ let lastError;
73
+ async function checkCondition() {
74
+ try {
75
+ condition = !!(await conditionCallback());
76
+ }
77
+ catch (error) {
78
+ condition = false;
79
+ lastError = error;
80
+ }
81
+ }
82
+ const startTime = Date.now();
83
+ await checkCondition();
84
+ while (!condition) {
85
+ await wait(intervalMs);
86
+ if (Date.now() - startTime >= timeoutMs) {
87
+ const message = timeoutMessage ? `${timeoutMessage}: ` : '';
88
+ throw new Error(`${message}Timeout of "${timeoutMs}" exceeded waiting for condition to be true${(0, error_1.extractErrorMessage)(lastError)}`);
89
+ }
90
+ await checkCondition();
91
+ }
92
+ }
93
+ exports.waitForCondition = waitForCondition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"