@zukhruf/async 0.8.16 → 0.8.17

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/README.md CHANGED
@@ -225,9 +225,10 @@ const state = await readState(container, { signal: limit.signal });
225
225
  - A disposal stops the timer, and the signal then does not abort. `using` disposes the timeout at the end of the block.
226
226
  - The timer keeps the process alive until it fires or until the disposal. The timer of `AbortSignal.timeout` does not keep the process alive, but no call can stop it.
227
227
  - To stop a wait at the timeout or at the signal of the caller, give `AbortSignal.any([signal, limit.signal])`.
228
- - The time runs on `delay`, so a timeout can be longer than one timer can hold.
228
+ - The time runs in ten parts, each on one timer of at most 2^31-1 ms, so a timeout can be longer than one timer can hold.
229
+ - A part that ends late uses up only its own length. On macOS, a timer of Node.js counts the time of a system sleep, so a timer that was running when the computer slept fires at the wake. The timeout then still gives the wait the rest of its time: a timeout of 5 seconds that slept in its first half second aborts 4.5 seconds after the wake, not at the wake. A process that the system stopped gets the same.
229
230
 
230
- See [ADR 0006](./docs/adr/0006-a-timeout-is-a-signal-on-the-clock-that-the-caller-gives.md).
231
+ See [ADR 0006](./docs/adr/0006-a-timeout-is-a-signal-on-the-clock-that-the-caller-gives.md) and [ADR 0010](./docs/adr/0010-a-timeout-counts-its-time-in-parts.md).
231
232
 
232
233
  ## Run a wait on a clock
233
234
 
@@ -279,6 +280,6 @@ function stamp(result: string, clock: WallClock) {
279
280
  stamp('passed', globalThis);
280
281
  ```
281
282
 
282
- The wall time can jump, for example when the computer sleeps. `performance.now()` does not jump. Thus a deadline uses `performance.now()`, and never the wall time.
283
+ The wall time can jump, for example when a time server sets it. `performance.now()` does not jump. Thus a deadline uses `performance.now()`, and never the wall time. On macOS, `performance.now()` and the timers of Node.js count the time of a system sleep; a `timeout` does not, see [Stop a wait after a time](#stop-a-wait-after-a-time).
283
284
 
284
285
  See [ADR 0005](./docs/adr/0005-a-wait-runs-on-the-clock-that-the-caller-gives.md).
package/dist/delay.d.ts CHANGED
@@ -11,6 +11,7 @@ export interface DelayOptions {
11
11
  */
12
12
  ref?: boolean | undefined;
13
13
  }
14
+ export declare const TIMER_MAX: number;
14
15
  /**
15
16
  * Waits `ms` milliseconds on the clock. When the signal aborts, also before
16
17
  * the call, the timer stops and the delay rejects with the reason of the
@@ -1 +1 @@
1
- {"version":3,"file":"delay.d.ts","sourceRoot":"","sources":["../src/delay.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,WAAW,YAAY;IAC3B,2FAA2F;IAC3F,KAAK,EAAE,KAAK,CAAC;IACb,mFAAmF;IACnF,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC;;;;OAIG;IACH,GAAG,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC3B;AAMD;;;;;;;;;GASG;AACH,wBAAsB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ5E"}
1
+ {"version":3,"file":"delay.d.ts","sourceRoot":"","sources":["../src/delay.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,WAAW,YAAY;IAC3B,2FAA2F;IAC3F,KAAK,EAAE,KAAK,CAAC;IACb,mFAAmF;IACnF,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC;;;;OAIG;IACH,GAAG,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC3B;AAID,eAAO,MAAM,SAAS,QAAc,CAAC;AAErC;;;;;;;;;GASG;AACH,wBAAsB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ5E"}
package/dist/delay.js CHANGED
@@ -2,7 +2,7 @@ import { addAbortListener } from 'node:events';
2
2
  import { untilAborted } from "./until-aborted.js";
3
3
  // The longest delay that one timer holds, about 24.8 days. Node.js fires a
4
4
  // timer with a longer delay after 1 ms, and so does createClock().
5
- const TIMER_MAX = 2 ** 31 - 1;
5
+ export const TIMER_MAX = 2 ** 31 - 1;
6
6
  /**
7
7
  * Waits `ms` milliseconds on the clock. When the signal aborts, also before
8
8
  * the call, the timer stops and the delay rejects with the reason of the
package/dist/delay.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"delay.js","sourceRoot":"","sources":["../src/delay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAelD,2EAA2E;AAC3E,mEAAmE;AACnE,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAE9B;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,EAAU,EAAE,OAAqB;IAC3D,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;IACjD,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,OAAO,IAAI,GAAG,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC/B,IAAI,GAAG,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC;IAC/C,CAAC;IACD,MAAM,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED,aAAa;AACb,SAAS,IAAI,CAAC,EAAU,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAgB;IAC5D,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,aAAa,EAAQ,CAAC;IAC3D,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC5C,IAAI,GAAG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AAC,WAAW,CAAC,GAAG,EAAE,CAAC;IAC/C,CAAC;IACD,MAAM,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED,aAAa;AACb,SAAS,IAAI,CAAC,EAAU,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAgB;IAC5D,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,aAAa,EAAQ,CAAC;IAC3D,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC5C,IAAI,GAAG,KAAK,KAAK;QAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IAChC,IAAI,CAAC,MAAM;QAAE,OAAO,OAAO,CAAC;IAC5B,qDAAqD;IACrD,MAAM,SAAS,GAAG,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;IAC5E,OAAO,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAChD,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAC5B,CAAC;AACJ,CAAC;AAED,SAAS,KAAK,CAAC,KAAc;IAC3B,IACE,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,IAAI,KAAK;QAChB,OAAO,KAAK,CAAC,KAAK,KAAK,UAAU,EACjC,CAAC;QACD,KAAK,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;AACH,CAAC"}
package/dist/timeout.d.ts CHANGED
@@ -11,8 +11,9 @@ export interface Timeout extends Disposable {
11
11
  /**
12
12
  * Gives a signal that aborts after `ms` milliseconds on the clock, as
13
13
  * `AbortSignal.timeout` does on the real clock. Dispose the timeout when the
14
- * wait ends: `using limit = timeout(ms, { clock })`. The time runs on `delay`,
15
- * so a timeout longer than one timer can hold lasts the full time.
14
+ * wait ends: `using limit = timeout(ms, { clock })`. The time runs in parts
15
+ * of at most one timer each, so a timeout longer than one timer can hold
16
+ * lasts the full time, and the time of a system sleep does not use it up.
16
17
  */
17
18
  export declare function timeout(ms: number, { clock }: TimeoutOptions): Timeout;
18
19
  //# sourceMappingURL=timeout.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,WAAW,cAAc;IAC7B,4FAA4F;IAC5F,KAAK,EAAE,KAAK,CAAC;CACd;AAED,gFAAgF;AAChF,MAAM,WAAW,OAAQ,SAAQ,UAAU;IACzC,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;CAC9B;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,EAAE,cAAc,GAAG,OAAO,CAkBtE"}
1
+ {"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,WAAW,cAAc;IAC7B,4FAA4F;IAC5F,KAAK,EAAE,KAAK,CAAC;CACd;AAED,gFAAgF;AAChF,MAAM,WAAW,OAAQ,SAAQ,UAAU;IACzC,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;CAC9B;AAUD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,EAAE,cAAc,GAAG,OAAO,CA0BtE"}
package/dist/timeout.js CHANGED
@@ -1,19 +1,35 @@
1
- import { delay } from "./delay.js";
1
+ import { TIMER_MAX } from "./delay.js";
2
+ // The number of parts in which a timeout counts its time. A timer of Node.js
3
+ // on macOS counts the time of a system sleep, so a timer that was running
4
+ // when the computer slept fires at the wake. Each part is one timer, and a
5
+ // part that ends late uses up only its own length: a sleep, or a process that
6
+ // the system stopped, takes at most one part from the work that the timeout
7
+ // limits.
8
+ const PARTS = 10;
2
9
  /**
3
10
  * Gives a signal that aborts after `ms` milliseconds on the clock, as
4
11
  * `AbortSignal.timeout` does on the real clock. Dispose the timeout when the
5
- * wait ends: `using limit = timeout(ms, { clock })`. The time runs on `delay`,
6
- * so a timeout longer than one timer can hold lasts the full time.
12
+ * wait ends: `using limit = timeout(ms, { clock })`. The time runs in parts
13
+ * of at most one timer each, so a timeout longer than one timer can hold
14
+ * lasts the full time, and the time of a system sleep does not use it up.
7
15
  */
8
16
  export function timeout(ms, { clock }) {
9
17
  const controller = new AbortController();
10
- const disposal = new AbortController();
11
- delay(ms, { clock, signal: disposal.signal }).then(() => controller.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError')),
12
- // The disposal stopped the wait: the signal never aborts.
13
- () => { });
18
+ const part = Math.min(Math.max(Math.ceil(ms / PARTS), 1), TIMER_MAX);
19
+ let timer;
20
+ const count = (left) => {
21
+ timer = clock.setTimeout(() => {
22
+ if (left > part) {
23
+ count(left - part);
24
+ return;
25
+ }
26
+ controller.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError'));
27
+ }, Math.min(left, part));
28
+ };
29
+ count(ms);
14
30
  return {
15
31
  signal: controller.signal,
16
- [Symbol.dispose]: () => disposal.abort(),
32
+ [Symbol.dispose]: () => clock.clearTimeout(timer),
17
33
  };
18
34
  }
19
35
  //# sourceMappingURL=timeout.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"timeout.js","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAanC;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,EAAU,EAAE,EAAE,KAAK,EAAkB;IAC3D,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,MAAM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AAC,KAAK,CACd,IAAI,YAAY,CACd,0CAA0C,EAC1C,cAAc,CACf,CACF,CAAC;QACJ,CAAC,EACD,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CACrB,CAAC;IACJ,CAAC,CAAC;IACF,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,OAAO;QACL,MAAM,EAAE,UAAU,CAAC,MAAM;QACzB,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC;KAClD,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/async",
3
- "version": "0.8.16",
3
+ "version": "0.8.17",
4
4
  "description": "Helpers for code that waits for promises: cancel a wait when an AbortSignal aborts, hold callers on a one-shot latch that never rejects, and poll an attempt until it gives a value.",
5
5
  "license": "MIT",
6
6
  "repository": {