@hydranium/protocol 1.0.0-next.281 → 1.0.0-next.283

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.
@@ -31,8 +31,13 @@ export interface WaitForOptions {
31
31
  readonly timeoutMs?: number;
32
32
  /** Poll the predicate every this-many milliseconds. Default 5. */
33
33
  readonly intervalMs?: number;
34
- /** Error message on timeout, and when the predicate throws. Default a generic phrasing. */
35
- readonly message?: string;
34
+ /**
35
+ * Error message on timeout, and when the predicate throws. A function is
36
+ * called only then, so it can report the state the wait ended on; when it
37
+ * throws, the default text is used, with its throw as the error's `cause`
38
+ * unless the predicate threw. Default a generic phrasing.
39
+ */
40
+ readonly message?: string | (() => string);
36
41
  }
37
42
  /**
38
43
  * Resolve once `predicate()` returns true, polling at `intervalMs`; reject with
@@ -1 +1 @@
1
- {"version":3,"file":"wait-for.d.ts","sourceRoot":"","sources":["../../src/testing/wait-for.ts"],"names":[],"mappings":"AAAA;;;;;;;kFAOkF;AAElF;;;;;;;;;;;;;;;;;;GAkBG;AAEH,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC5B,yFAAyF;IACzF,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,kEAAkE;IAClE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,2FAA2F;IAC3F,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,OAAO,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8C7F;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,EAAE,SAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3C"}
1
+ {"version":3,"file":"wait-for.d.ts","sourceRoot":"","sources":["../../src/testing/wait-for.ts"],"names":[],"mappings":"AAAA;;;;;;;kFAOkF;AAElF;;;;;;;;;;;;;;;;;;GAkBG;AAEH,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC5B,yFAAyF;IACzF,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,kEAAkE;IAClE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B;;;;;OAKG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC;CAC7C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,OAAO,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8D7F;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,EAAE,SAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3C"}
@@ -30,11 +30,13 @@ exports.tick = tick;
30
30
  function waitFor(predicate, options = {}) {
31
31
  const timeoutMs = options.timeoutMs ?? 2000;
32
32
  const intervalMs = options.intervalMs ?? 5;
33
- const message = options.message ?? `Timed out after ${timeoutMs}ms waiting for a condition`;
33
+ const defaultMessage = `Timed out after ${timeoutMs}ms waiting for a condition`;
34
34
  return new Promise((resolve, reject) => {
35
- // A throw would leave the promise unsettled from a timer, or escape
36
- // `waitFor` itself on the first check; it rejects as the timeout instead,
37
- // with the throw as cause. True once the promise is settled.
35
+ // A throw from the predicate or the message function would leave the
36
+ // promise unsettled from the poll's or the deadline's timer, or escape
37
+ // `waitFor` itself on the first check; it rejects instead, with the
38
+ // predicate's throw, else the message function's, as cause. True once
39
+ // the promise is settled.
38
40
  const settle = (timedOut) => {
39
41
  let met = false;
40
42
  let threw = false;
@@ -48,13 +50,27 @@ function waitFor(predicate, options = {}) {
48
50
  }
49
51
  if (met) {
50
52
  resolve();
53
+ return true;
51
54
  }
52
- else if (timedOut || threw) {
53
- reject(new Error(message, threw ? { cause } : undefined));
54
- }
55
- else {
55
+ if (!timedOut && !threw) {
56
56
  return false;
57
57
  }
58
+ let message = defaultMessage;
59
+ try {
60
+ if (typeof options.message === 'function') {
61
+ message = options.message();
62
+ }
63
+ else if (options.message !== undefined) {
64
+ message = options.message;
65
+ }
66
+ }
67
+ catch (error) {
68
+ if (!threw) {
69
+ threw = true;
70
+ cause = error;
71
+ }
72
+ }
73
+ reject(new Error(message, threw ? { cause } : undefined));
58
74
  return true;
59
75
  };
60
76
  if (settle(false)) {
@@ -1 +1 @@
1
- {"version":3,"file":"wait-for.js","sourceRoot":"","sources":["../../src/testing/wait-for.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF;;;;AAgClF;;;;;;;;;;;;;;;;GAgBG;AACH,iBAAwB,SAAwB,EAAE,OAAO,GAAmB,EAAE;IAC3E,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC;IAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC;IAC3C,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,mBAAmB,SAAS,4BAA4B,CAAC;IAC5F,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,oEAAoE;QACpE,0EAA0E;QAC1E,6DAA6D;QAC7D,MAAM,MAAM,GAAG,CAAC,QAAiB,EAAW,EAAE;YAC3C,IAAI,GAAG,GAAG,KAAK,CAAC;YAChB,IAAI,KAAK,GAAG,KAAK,CAAC;YAClB,IAAI,KAAc,CAAC;YACnB,IAAI,CAAC;gBACF,GAAG,GAAG,SAAS,EAAE,CAAC;YACrB,CAAC;YAAC,OAAO,KAAc,EAAE,CAAC;gBACvB,KAAK,GAAG,IAAI,CAAC;gBACb,KAAK,GAAG,KAAK,CAAC;YACjB,CAAC;YACD,IAAI,GAAG,EAAE,CAAC;gBACP,OAAO,EAAE,CAAC;YACb,CAAC;iBAAM,IAAI,QAAQ,IAAI,KAAK,EA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K,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;YAC1D,OAAO,IAAI,CAAC;QACf,CAAC,CAAC;QACF,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO;QACV,CAAC;QACD,MAAM,IAAI,GAAG,GAAS,EAAE;YACrB,aAAa,CAAC,IAAI,CAAC,CAAC;YACpB,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC1B,CAAC,CAAC;QACF,wEAAwE;QACxE,iEAAiE;QACjE,MAAM,QAAQ,GAAG,UAAU,CAAC,GAAG,EAAE;YAC9B,MAAM,CAAC,IAAI,CAAC,CAAC;YACb,IAAI,EAAE,CAAC;QACV,CAAC,EAAE,SAAS,CAAC,CAAC;QACd,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,EAAE;YAC3B,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;gBACjB,IAAI,EAAE,CAAC;YACV,CAAC;QACJ,CAAC,EAAE,UAAU,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACN,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,cAAqB,EAAE,GAAG,EAAE;IACzB,OAAO,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAChE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hydranium/protocol",
3
- "version": "1.0.0-next.281",
3
+ "version": "1.0.0-next.283",
4
4
  "description": "Generic, language-agnostic types, constants, and pure utilities for the hydranium framework.",
5
5
  "keywords": [
6
6
  "hydranium",
@@ -33,8 +33,13 @@ export interface WaitForOptions {
33
33
  readonly timeoutMs?: number;
34
34
  /** Poll the predicate every this-many milliseconds. Default 5. */
35
35
  readonly intervalMs?: number;
36
- /** Error message on timeout, and when the predicate throws. Default a generic phrasing. */
37
- readonly message?: string;
36
+ /**
37
+ * Error message on timeout, and when the predicate throws. A function is
38
+ * called only then, so it can report the state the wait ended on; when it
39
+ * throws, the default text is used, with its throw as the error's `cause`
40
+ * unless the predicate threw. Default a generic phrasing.
41
+ */
42
+ readonly message?: string | (() => string);
38
43
  }
39
44
 
40
45
  /**
@@ -57,11 +62,13 @@ export interface WaitForOptions {
57
62
  export function waitFor(predicate: () => boolean, options: WaitForOptions = {}): Promise<void> {
58
63
  const timeoutMs = options.timeoutMs ?? 2000;
59
64
  const intervalMs = options.intervalMs ?? 5;
60
- const message = options.message ?? `Timed out after ${timeoutMs}ms waiting for a condition`;
65
+ const defaultMessage = `Timed out after ${timeoutMs}ms waiting for a condition`;
61
66
  return new Promise<void>((resolve, reject) => {
62
- // A throw would leave the promise unsettled from a timer, or escape
63
- // `waitFor` itself on the first check; it rejects as the timeout instead,
64
- // with the throw as cause. True once the promise is settled.
67
+ // A throw from the predicate or the message function would leave the
68
+ // promise unsettled from the poll's or the deadline's timer, or escape
69
+ // `waitFor` itself on the first check; it rejects instead, with the
70
+ // predicate's throw, else the message function's, as cause. True once
71
+ // the promise is settled.
65
72
  const settle = (timedOut: boolean): boolean => {
66
73
  let met = false;
67
74
  let threw = false;
@@ -74,11 +81,25 @@ export function waitFor(predicate: () => boolean, options: WaitForOptions = {}):
74
81
  }
75
82
  if (met) {
76
83
  resolve();
77
- } else if (timedOut || threw) {
78
- reject(new Error(message, threw ? { cause } : undefined));
79
- } else {
84
+ return true;
85
+ }
86
+ if (!timedOut && !threw) {
80
87
  return false;
81
88
  }
89
+ let message = defaultMessage;
90
+ try {
91
+ if (typeof options.message === 'function') {
92
+ message = options.message();
93
+ } else if (options.message !== undefined) {
94
+ message = options.message;
95
+ }
96
+ } catch (error: unknown) {
97
+ if (!threw) {
98
+ threw = true;
99
+ cause = error;
100
+ }
101
+ }
102
+ reject(new Error(message, threw ? { cause } : undefined));
82
103
  return true;
83
104
  };
84
105
  if (settle(false)) {