@zukhruf/async 0.3.20 → 0.3.21
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 +15 -0
- package/dist/poll.d.ts +6 -0
- package/dist/poll.d.ts.map +1 -1
- package/dist/poll.js +2 -2
- package/dist/poll.js.map +1 -1
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -87,4 +87,19 @@ const job = await poll(() => queue.take(), {
|
|
|
87
87
|
- One deadline can stop many polls. A campaign of `@zukhruf/election` uses one deadline for all its attempts.
|
|
88
88
|
- Without a deadline, the type of the result has no `undefined`.
|
|
89
89
|
|
|
90
|
+
### Let the process end during a poll
|
|
91
|
+
|
|
92
|
+
A Node.js process ends when it has no more work. A timer is work. Thus the wait between two attempts keeps the process alive. Some polls run in the background, and their process must be able to end during the poll. For example, a server that does not keep its process alive must not watch a lock with a poll that does.
|
|
93
|
+
|
|
94
|
+
For such a poll, give `ref: false`. Then the wait does not keep the process alive. The process can end while the poll is pending, and the poll then never resolves.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
// The worker takes jobs, but it does not keep the process alive.
|
|
98
|
+
poll(() => queue.take(), { interval: 100, signal, ref: false }).then(run);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
- `ref` is the `ref` option of the Node.js timer (`setTimeout` of `node:timers/promises`). `poll` gives it to the timer unchanged.
|
|
102
|
+
- Without `ref`, Node.js keeps its default: the wait keeps the process alive.
|
|
103
|
+
- `ref` changes only the wait between two attempts. An attempt that runs can keep the process alive.
|
|
104
|
+
|
|
90
105
|
See [ADR 0003](./docs/adr/0003-the-election-and-the-file-lock-stores-poll-through-one-function.md).
|
package/dist/poll.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export interface PollOptions {
|
|
|
8
8
|
* resolves the poll with `undefined`. The first attempt always occurs.
|
|
9
9
|
*/
|
|
10
10
|
deadline?: number | undefined;
|
|
11
|
+
/**
|
|
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
|
+
*/
|
|
16
|
+
ref?: boolean;
|
|
11
17
|
}
|
|
12
18
|
/**
|
|
13
19
|
* Calls `attempt` until it gives a value other than `undefined`, and waits
|
package/dist/poll.d.ts.map
CHANGED
|
@@ -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;
|
|
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"}
|
package/dist/poll.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
2
2
|
import { untilAborted } from "./until-aborted.js";
|
|
3
|
-
export async function poll(attempt, { interval, signal, deadline }) {
|
|
3
|
+
export async function poll(attempt, { interval, signal, deadline, ref }) {
|
|
4
4
|
for (;;) {
|
|
5
5
|
const value = await attempt();
|
|
6
6
|
if (value !== undefined)
|
|
@@ -9,7 +9,7 @@ export async function poll(attempt, { interval, signal, deadline }) {
|
|
|
9
9
|
return undefined;
|
|
10
10
|
}
|
|
11
11
|
// delay's signal frees the timer; untilAborted rejects with the reason.
|
|
12
|
-
await untilAborted(delay(interval, undefined, { signal }), signal);
|
|
12
|
+
await untilAborted(delay(interval, undefined, { signal, ref }), signal);
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
//# 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;
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/async",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
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": {
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"polling"
|
|
24
24
|
],
|
|
25
25
|
"type": "module",
|
|
26
|
-
"types": "./dist/index.d.ts",
|
|
27
26
|
"exports": {
|
|
28
27
|
"./package.json": "./package.json",
|
|
29
28
|
".": {
|