@zukhruf/async 0.6.12 → 0.6.14

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
@@ -1,11 +1,9 @@
1
1
  # @zukhruf/async
2
2
 
3
- Helpers for code that waits for promises: `untilAborted` cancels a wait, `Latch` holds callers until something happened, `poll` calls an attempt again until it gives a value, and `pollChanges` reads values again and again and tells which values changed. `@zukhruf/mutex`, `@zukhruf/single-flight` and `@zukhruf/election` use them.
3
+ Helpers for code that waits for promises: `untilAborted` cancels a wait, `Latch` holds callers until something happened, `delay` waits a fixed time on a clock, `poll` calls an attempt again until it gives a value, and `pollChanges` reads values again and again and tells which values changed. `@zukhruf/mutex`, `@zukhruf/single-flight` and `@zukhruf/election` use them.
4
4
 
5
5
  The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
6
6
 
7
- > **Design for review.** The `clock` option of `poll` and `pollChanges` is not in the code yet ([ADR 0005](./docs/adr/0005-a-wait-runs-on-the-clock-that-the-caller-gives.md), backlog #3570). The released version does not take it.
8
-
9
7
  ## Cancel a wait
10
8
 
11
9
  A caller waits for a promise. The caller wants to stop the wait when a signal aborts, for example after a time limit, or when the user stops the program. `untilAborted` gives a promise that settles in the same way as the promise of the caller. When the signal aborts first, it rejects with the reason of the signal.
@@ -112,7 +110,7 @@ poll(() => queue.take(), {
112
110
  }).then(run);
113
111
  ```
114
112
 
115
- - With `ref: false`, `poll` calls `unref()` on the timer that `clock.setTimeout` gives.
113
+ - With `ref: false`, `poll` calls `unref()` on the timer that `clock.setTimeout` gives, when the timer has one. The timers of Node.js and of `createClock()` have it.
116
114
  - Without `ref`, Node.js keeps its default: the wait keeps the process alive.
117
115
  - `ref` changes only the wait between two attempts. An attempt that runs can keep the process alive.
118
116
 
@@ -169,6 +167,21 @@ To reject when the signal aborts, call `signal.throwIfAborted()` after the loop.
169
167
 
170
168
  See [ADR 0004](./docs/adr/0004-a-change-watcher-yields-each-check-with-the-keys-that-changed.md).
171
169
 
170
+ ## Wait a fixed time
171
+
172
+ `delay` waits a number of milliseconds on the clock that you give, as `setTimeout` of `node:timers/promises` does on the real clock.
173
+
174
+ ```ts
175
+ import { delay } from '@zukhruf/async';
176
+
177
+ // Wait 100 ms between two tries to reach the server.
178
+ await delay(100, { clock: globalThis, signal });
179
+ ```
180
+
181
+ - When `signal` aborts, also before the call, the timer stops and `delay` rejects with the reason of the signal.
182
+ - No listener stays on the signal after `delay` settles, so a signal that lives long can serve many waits.
183
+ - With `ref: false`, the timer does not keep the process alive.
184
+
172
185
  ## Run a wait on a clock
173
186
 
174
187
  Each wait of `poll` and `pollChanges` runs on the clock that you give. In production, give `globalThis`: the wait then uses the timers of Node.js. In a test, give a fake clock. The test then moves the time, and the wait takes no real time.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The source of the timers and of the time of a wait. Production gives
3
+ * `globalThis`; a test gives a fake clock, such as `createClock()` of
4
+ * `@sinonjs/fake-timers`, and moves it.
5
+ *
6
+ * The type has only the members that a wait calls. A timer is opaque: the wait
7
+ * keeps it to give it back to `clearTimeout`.
8
+ */
9
+ export interface Clock {
10
+ setTimeout(callback: () => void, ms: number): unknown;
11
+ clearTimeout(timer: unknown): void;
12
+ performance: {
13
+ now(): number;
14
+ };
15
+ }
16
+ //# sourceMappingURL=clock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clock.d.ts","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,WAAW,KAAK;IACpB,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC;IACtD,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IACnC,WAAW,EAAE;QAAE,GAAG,IAAI,MAAM,CAAA;KAAE,CAAC;CAChC"}
package/dist/clock.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=clock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ import type { Clock } from './clock.ts';
2
+ export interface DelayOptions {
3
+ /** The clock that the wait runs on: `globalThis` in production, a fake clock in a test. */
4
+ clock: Clock;
5
+ /** Stops the wait: the timer stops, and the delay rejects with `signal.reason`. */
6
+ signal: AbortSignal | undefined;
7
+ /**
8
+ * With `false`, the wait does not keep the process alive: `delay` calls
9
+ * `unref()` on the timer of the clock, when the timer has one. Without it,
10
+ * the timer keeps its default.
11
+ */
12
+ ref?: boolean | undefined;
13
+ }
14
+ /**
15
+ * Waits `ms` milliseconds on the clock. When the signal aborts, also before
16
+ * the call, the timer stops and the delay rejects with the reason of the
17
+ * signal. No listener stays on the signal after the delay settles.
18
+ */
19
+ export declare function delay(ms: number, { clock, signal, ref }: DelayOptions): Promise<void>;
20
+ //# sourceMappingURL=delay.d.ts.map
@@ -0,0 +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;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CACnB,EAAE,EAAE,MAAM,EACV,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,YAAY,GACnC,OAAO,CAAC,IAAI,CAAC,CAUf"}
package/dist/delay.js ADDED
@@ -0,0 +1,27 @@
1
+ import { addAbortListener } from 'node:events';
2
+ import { untilAborted } from "./until-aborted.js";
3
+ /**
4
+ * Waits `ms` milliseconds on the clock. When the signal aborts, also before
5
+ * the call, the timer stops and the delay rejects with the reason of the
6
+ * signal. No listener stays on the signal after the delay settles.
7
+ */
8
+ export function delay(ms, { clock, signal, ref }) {
9
+ const { promise, resolve } = Promise.withResolvers();
10
+ const timer = clock.setTimeout(resolve, ms);
11
+ if (ref === false)
12
+ unref(timer);
13
+ if (!signal)
14
+ return promise;
15
+ // Also runs when the signal aborted before the call.
16
+ const listening = addAbortListener(signal, () => clock.clearTimeout(timer));
17
+ return untilAborted(promise, signal).finally(() => listening[Symbol.dispose]());
18
+ }
19
+ function unref(timer) {
20
+ if (typeof timer === 'object' &&
21
+ timer !== null &&
22
+ 'unref' in timer &&
23
+ typeof timer.unref === 'function') {
24
+ timer.unref();
25
+ }
26
+ }
27
+ //# sourceMappingURL=delay.js.map
@@ -0,0 +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;;;;GAIG;AACH,MAAM,UAAU,KAAK,CACnB,EAAU,EACV,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAgB;IAEpC,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/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export { type Clock } from './clock.ts';
2
+ export { delay, type DelayOptions } from './delay.ts';
1
3
  export { Latch } from './latch.ts';
2
4
  export { poll, type PollOptions } from './poll.ts';
3
5
  export { pollChanges, type PollChangesOptions } from './poll-changes.ts';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ export {} from "./clock.js";
2
+ export { delay } from "./delay.js";
1
3
  export { Latch } from "./latch.js";
2
4
  export { poll } from "./poll.js";
3
5
  export { pollChanges } from "./poll-changes.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAoB,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,WAAW,EAA2B,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,KAAK,EAAqB,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAoB,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,WAAW,EAA2B,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
@@ -1,8 +1,11 @@
1
+ import type { Clock } from './clock.ts';
1
2
  export interface PollChangesOptions {
2
3
  /** Milliseconds to wait after a check, from when the consumer asks for the next check. */
3
4
  interval: number;
4
5
  /** Stops the wait between two checks. The iteration then ends with no error. */
5
6
  signal: AbortSignal;
7
+ /** The clock that the wait runs on: `globalThis` in production, a fake clock in a test. */
8
+ clock: Clock;
6
9
  }
7
10
  /**
8
11
  * Reads the value of each key at once, and yields all keys. Then, after each
@@ -10,5 +13,5 @@ export interface PollChangesOptions {
10
13
  * `===` to the value of the previous check: an empty set when nothing changed.
11
14
  * An error from a read rejects the iteration unchanged.
12
15
  */
13
- export declare function pollChanges<K>(reads: ReadonlyMap<K, () => Promise<unknown>>, { interval, signal }: PollChangesOptions): AsyncGenerator<ReadonlySet<K>, void, undefined>;
16
+ export declare function pollChanges<K>(reads: ReadonlyMap<K, () => Promise<unknown>>, { interval, signal, clock }: PollChangesOptions): AsyncGenerator<ReadonlySet<K>, void, undefined>;
14
17
  //# sourceMappingURL=poll-changes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"poll-changes.d.ts","sourceRoot":"","sources":["../src/poll-changes.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,kBAAkB;IACjC,0FAA0F;IAC1F,QAAQ,EAAE,MAAM,CAAC;IACjB,gFAAgF;IAChF,MAAM,EAAE,WAAW,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAuB,WAAW,CAAC,CAAC,EAClC,KAAK,EAAE,WAAW,CAAC,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,EAC7C,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,kBAAkB,GACvC,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAsBjD"}
1
+ {"version":3,"file":"poll-changes.d.ts","sourceRoot":"","sources":["../src/poll-changes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,WAAW,kBAAkB;IACjC,0FAA0F;IAC1F,QAAQ,EAAE,MAAM,CAAC;IACjB,gFAAgF;IAChF,MAAM,EAAE,WAAW,CAAC;IACpB,2FAA2F;IAC3F,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;;GAKG;AACH,wBAAuB,WAAW,CAAC,CAAC,EAClC,KAAK,EAAE,WAAW,CAAC,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,EAC7C,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,kBAAkB,GAC9C,cAAc,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAqBjD"}
@@ -1,12 +1,11 @@
1
- import { setTimeout as delay } from 'node:timers/promises';
2
- import { untilAborted } from "./until-aborted.js";
1
+ import { delay } from "./delay.js";
3
2
  /**
4
3
  * Reads the value of each key at once, and yields all keys. Then, after each
5
4
  * `interval`, reads each value again and yields the keys whose value is not
6
5
  * `===` to the value of the previous check: an empty set when nothing changed.
7
6
  * An error from a read rejects the iteration unchanged.
8
7
  */
9
- export async function* pollChanges(reads, { interval, signal }) {
8
+ export async function* pollChanges(reads, { interval, signal, clock }) {
10
9
  if (signal.aborted)
11
10
  return;
12
11
  const seen = new Map();
@@ -15,8 +14,7 @@ export async function* pollChanges(reads, { interval, signal }) {
15
14
  yield new Set(reads.keys());
16
15
  for (;;) {
17
16
  try {
18
- // delay's signal frees the timer; untilAborted ends a stopped event too.
19
- await untilAborted(delay(interval, undefined, { signal }), signal);
17
+ await delay(interval, { clock, signal });
20
18
  }
21
19
  catch (error) {
22
20
  if (signal.aborted)
@@ -1 +1 @@
1
- {"version":3,"file":"poll-changes.js","sourceRoot":"","sources":["../src/poll-changes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AASlD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,WAAW,CAChC,KAA6C,EAC7C,EAAE,QAAQ,EAAE,MAAM,EAAsB;IAExC,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO;IAC3B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAc,CAAC;IACnC,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,KAAK;QAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IAC7D,MAAM,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5B,SAAS,CAAC;QACR,IAAI,CAAC;YACH,yEAAyE;YACzE,MAAM,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;QACrE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,MAAM,CAAC,OAAO;gBAAE,OAAO;YAC3B,MAAM,KAAK,CAAC;QACd,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAK,CAAC;QAC7B,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;YAChC,MAAM,KAAK,GAAG,MAAM,IAAI,EAAE,CAAC;YAC3B,IAAI,KAAK,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;gBAAE,SAAS;YACtC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACrB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;QACD,MAAM,OAAO,CAAC;IAChB,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"poll-changes.js","sourceRoot":"","sources":["../src/poll-changes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAWnC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,WAAW,CAChC,KAA6C,EAC7C,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAsB;IAE/C,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO;IAC3B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAc,CAAC;IACnC,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,KAAK;QAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IAC7D,MAAM,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5B,SAAS,CAAC;QACR,IAAI,CAAC;YACH,MAAM,KAAK,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;QAC3C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,MAAM,CAAC,OAAO;gBAAE,OAAO;YAC3B,MAAM,KAAK,CAAC;QACd,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAK,CAAC;QAC7B,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,KAAK,EAAE,CAAC;YAChC,MAAM,KAAK,GAAG,MAAM,IAAI,EAAE,CAAC;YAC3B,IAAI,KAAK,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;gBAAE,SAAS;YACtC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACrB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;QACD,MAAM,OAAO,CAAC;IAChB,CAAC;AACH,CAAC"}
package/dist/poll.d.ts CHANGED
@@ -1,17 +1,21 @@
1
+ import type { Clock } from './clock.ts';
1
2
  export interface PollOptions {
2
3
  /** Milliseconds to wait after an attempt that gave nothing. */
3
4
  interval: number;
4
5
  /** Stops the wait between two attempts. The poll then rejects with `signal.reason`. */
5
6
  signal: AbortSignal | undefined;
7
+ /** The clock that the wait runs on: `globalThis` in production, a fake clock in a test. */
8
+ clock: Clock;
6
9
  /**
7
- * A `performance.now()` time. After it, an attempt that gives nothing
10
+ * A `clock.performance.now()` time. After it, an attempt that gives nothing
8
11
  * resolves the poll with `undefined`. The first attempt always occurs.
9
12
  */
10
13
  deadline?: number | undefined;
11
14
  /**
12
- * The `ref` of the Node.js timer of the wait between two attempts. With
13
- * `false`, the wait does not keep the process alive: the process can end
14
- * while the poll is pending. Without it, Node.js keeps its default.
15
+ * With `false`, the wait between two attempts does not keep the process
16
+ * alive: `poll` calls `unref()` on the timer of the clock, when the timer has
17
+ * one. The process can then end while the poll is pending. Without it, the
18
+ * timer keeps its default.
15
19
  */
16
20
  ref?: boolean;
17
21
  }
@@ -1 +1 @@
1
- {"version":3,"file":"poll.d.ts","sourceRoot":"","sources":["../src/poll.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,WAAW;IAC1B,+DAA+D;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,uFAAuF;IACvF,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B;;;;OAIG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACpB,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,EACrC,OAAO,EAAE,WAAW,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC1C,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AAC1B,wBAAgB,IAAI,CAAC,CAAC,EACpB,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,EACrC,OAAO,EAAE,WAAW,GAAG;IAAE,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAE,GAC9C,OAAO,CAAC,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"poll.d.ts","sourceRoot":"","sources":["../src/poll.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC,MAAM,WAAW,WAAW;IAC1B,+DAA+D;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,uFAAuF;IACvF,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IAChC,2FAA2F;IAC3F,KAAK,EAAE,KAAK,CAAC;IACb;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B;;;;;OAKG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACpB,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,EACrC,OAAO,EAAE,WAAW,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC1C,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AAC1B,wBAAgB,IAAI,CAAC,CAAC,EACpB,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,EACrC,OAAO,EAAE,WAAW,GAAG;IAAE,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAE,GAC9C,OAAO,CAAC,CAAC,CAAC,CAAC"}
package/dist/poll.js CHANGED
@@ -1,15 +1,13 @@
1
- import { setTimeout as delay } from 'node:timers/promises';
2
- import { untilAborted } from "./until-aborted.js";
3
- export async function poll(attempt, { interval, signal, deadline, ref }) {
1
+ import { delay } from "./delay.js";
2
+ export async function poll(attempt, { interval, signal, clock, deadline, ref }) {
4
3
  for (;;) {
5
4
  const value = await attempt();
6
5
  if (value !== undefined)
7
6
  return value;
8
- if (deadline !== undefined && performance.now() >= deadline) {
7
+ if (deadline !== undefined && clock.performance.now() >= deadline) {
9
8
  return undefined;
10
9
  }
11
- // delay's signal frees the timer; untilAborted rejects with the reason.
12
- await untilAborted(delay(interval, undefined, { signal, ref }), signal);
10
+ await delay(interval, { clock, signal, ref });
13
11
  }
14
12
  }
15
13
  //# sourceMappingURL=poll.js.map
package/dist/poll.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"poll.js","sourceRoot":"","sources":["../src/poll.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAiClD,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,OAAqC,EACrC,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAe;IAEhD,SAAS,CAAC;QACR,MAAM,KAAK,GAAG,MAAM,OAAO,EAAE,CAAC;QAC9B,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,KAAK,CAAC;QACtC,IAAI,QAAQ,KAAK,SAAS,IAAI,WAAW,CAAC,GAAG,EAAE,IAAI,QAAQ,EAAE,CAAC;YAC5D,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,wEAAwE;QACxE,MAAM,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;IAC1E,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"poll.js","sourceRoot":"","sources":["../src/poll.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAoCnC,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,OAAqC,EACrC,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAe;IAEvD,SAAS,CAAC;QACR,MAAM,KAAK,GAAG,MAAM,OAAO,EAAE,CAAC;QAC9B,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,KAAK,CAAC;QACtC,IAAI,QAAQ,KAAK,SAAS,IAAI,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,IAAI,QAAQ,EAAE,CAAC;YAClE,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,MAAM,KAAK,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;IAChD,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/async",
3
- "version": "0.6.12",
3
+ "version": "0.6.14",
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": {
@@ -40,5 +40,8 @@
40
40
  ],
41
41
  "publishConfig": {
42
42
  "access": "public"
43
+ },
44
+ "devDependencies": {
45
+ "@sinonjs/fake-timers": "15.4.0"
43
46
  }
44
47
  }