@zukhruf/async 0.6.13 → 0.6.15
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 +35 -3
- package/dist/clock.d.ts +16 -0
- package/dist/clock.d.ts.map +1 -0
- package/dist/clock.js +2 -0
- package/dist/clock.js.map +1 -0
- package/dist/delay.d.ts +20 -0
- package/dist/delay.d.ts.map +1 -0
- package/dist/delay.js +27 -0
- package/dist/delay.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/poll-changes.d.ts +4 -1
- package/dist/poll-changes.d.ts.map +1 -1
- package/dist/poll-changes.js +3 -5
- package/dist/poll-changes.js.map +1 -1
- package/dist/poll.d.ts +8 -4
- package/dist/poll.d.ts.map +1 -1
- package/dist/poll.js +4 -6
- package/dist/poll.js.map +1 -1
- package/dist/timeout.d.ts +17 -0
- package/dist/timeout.d.ts.map +1 -0
- package/dist/timeout.js +16 -0
- package/dist/timeout.js.map +1 -0
- package/package.json +4 -1
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, `timeout` stops a wait after a 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.
|
|
@@ -169,6 +167,40 @@ 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
|
+
|
|
185
|
+
## Stop a wait after a time
|
|
186
|
+
|
|
187
|
+
`timeout` gives a signal that aborts after a number of milliseconds on the clock that you give, as `AbortSignal.timeout` does on the real clock. Give the signal to a wait. Dispose the timeout when the wait ends.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import { timeout } from '@zukhruf/async';
|
|
191
|
+
|
|
192
|
+
// Stop the read when the server does not answer in 5 seconds.
|
|
193
|
+
using limit = timeout(5_000, { clock: globalThis });
|
|
194
|
+
const state = await readState(container, { signal: limit.signal });
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
- The signal aborts with a `DOMException` whose name is `TimeoutError`, as the signal of `AbortSignal.timeout` does.
|
|
198
|
+
- A disposal stops the timer, and the signal then does not abort. `using` disposes the timeout at the end of the block.
|
|
199
|
+
- 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.
|
|
200
|
+
- To stop a wait at the timeout or at the signal of the caller, give `AbortSignal.any([signal, limit.signal])`.
|
|
201
|
+
|
|
202
|
+
See [ADR 0006](./docs/adr/0006-a-timeout-is-a-signal-on-the-clock-that-the-caller-gives.md).
|
|
203
|
+
|
|
172
204
|
## Run a wait on a clock
|
|
173
205
|
|
|
174
206
|
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.
|
package/dist/clock.d.ts
ADDED
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":""}
|
package/dist/delay.d.ts
ADDED
|
@@ -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,5 +1,8 @@
|
|
|
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';
|
|
6
|
+
export { timeout, type Timeout, type TimeoutOptions } from './timeout.ts';
|
|
4
7
|
export { untilAborted } from './until-aborted.ts';
|
|
5
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
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";
|
|
6
|
+
export { timeout } from "./timeout.js";
|
|
4
7
|
export { untilAborted } from "./until-aborted.js";
|
|
5
8
|
//# sourceMappingURL=index.js.map
|
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,OAAO,EAAqC,MAAM,cAAc,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/poll-changes.d.ts
CHANGED
|
@@ -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":"
|
|
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"}
|
package/dist/poll-changes.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
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)
|
package/dist/poll-changes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"poll-changes.js","sourceRoot":"","sources":["../src/poll-changes.ts"],"names":[],"mappings":"
|
|
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
|
-
*
|
|
13
|
-
* `
|
|
14
|
-
* while the poll is pending. Without it,
|
|
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
|
}
|
package/dist/poll.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"poll.d.ts","sourceRoot":"","sources":["../src/poll.ts"],"names":[],"mappings":"
|
|
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 {
|
|
2
|
-
|
|
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
|
-
|
|
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":"
|
|
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"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Clock } from './clock.ts';
|
|
2
|
+
export interface TimeoutOptions {
|
|
3
|
+
/** The clock that the timer runs on: `globalThis` in production, a fake clock in a test. */
|
|
4
|
+
clock: Clock;
|
|
5
|
+
}
|
|
6
|
+
/** A signal that aborts after a time on a clock. A disposal stops its timer. */
|
|
7
|
+
export interface Timeout extends Disposable {
|
|
8
|
+
/** Aborts after the time, with a `DOMException` whose name is `TimeoutError`. */
|
|
9
|
+
readonly signal: AbortSignal;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Gives a signal that aborts after `ms` milliseconds on the clock, as
|
|
13
|
+
* `AbortSignal.timeout` does on the real clock. Dispose the timeout when the
|
|
14
|
+
* wait ends: `using limit = timeout(ms, { clock })`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function timeout(ms: number, { clock }: TimeoutOptions): Timeout;
|
|
17
|
+
//# sourceMappingURL=timeout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,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;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,EAAE,cAAc,GAAG,OAAO,CActE"}
|
package/dist/timeout.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gives a signal that aborts after `ms` milliseconds on the clock, as
|
|
3
|
+
* `AbortSignal.timeout` does on the real clock. Dispose the timeout when the
|
|
4
|
+
* wait ends: `using limit = timeout(ms, { clock })`.
|
|
5
|
+
*/
|
|
6
|
+
export function timeout(ms, { clock }) {
|
|
7
|
+
const controller = new AbortController();
|
|
8
|
+
const timer = clock.setTimeout(() => {
|
|
9
|
+
controller.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError'));
|
|
10
|
+
}, ms);
|
|
11
|
+
return {
|
|
12
|
+
signal: controller.signal,
|
|
13
|
+
[Symbol.dispose]: () => clock.clearTimeout(timer),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=timeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.js","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,EAAU,EAAE,EAAE,KAAK,EAAkB;IAC3D,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE;QAClC,UAAU,CAAC,KAAK,CACd,IAAI,YAAY,CACd,0CAA0C,EAC1C,cAAc,CACf,CACF,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IACP,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.6.
|
|
3
|
+
"version": "0.6.15",
|
|
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
|
}
|