Skip to content

Commit 46baeec

Browse files
authored
Rename minimumWait option to wait and require Node.js 18 (#33)
1 parent 34078d9 commit 46baeec

File tree

8 files changed

+27
-22
lines changed

8 files changed

+27
-22
lines changed

fixtures/async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ asyncExitHook(
2626
console.log('quux');
2727
},
2828
{
29-
minimumWait: 200,
29+
wait: 200,
3030
},
3131
);
3232

fixtures/signal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exitHook(signal => {
77
asyncExitHook(async signal => {
88
console.log(signal);
99
}, {
10-
minimumWait: 200,
10+
wait: 200,
1111
});
1212

1313
setInterval(() => {}, 1 << 30);

index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ import {asyncExitHook} from 'exit-hook';
5353
asyncExitHook(() => {
5454
console.log('Exiting');
5555
}, {
56-
minimumWait: 500
56+
wait: 500
5757
});
5858
5959
throw new Error('🦄');
6060
6161
//=> 'Exiting'
6262
6363
// Removing an exit hook:
64-
const unsubscribe = asyncExitHook(() => {}, {minimumWait: 500});
64+
const unsubscribe = asyncExitHook(() => {}, {wait: 500});
6565
6666
unsubscribe();
6767
```
@@ -83,7 +83,7 @@ import {asyncExitHook, gracefulExit} from 'exit-hook';
8383
asyncExitHook(() => {
8484
console.log('Exiting');
8585
}, {
86-
minimumWait: 500
86+
wait: 500
8787
});
8888
8989
// Instead of `process.exit()`
@@ -94,7 +94,7 @@ export function gracefulExit(signal?: number): void;
9494

9595
export interface Options {
9696
/**
97-
The amount of time in milliseconds that the `onExit` function is expected to take.
97+
The amount of time in milliseconds that the `onExit` function is expected to take. When multiple async handlers are registered, the longest `wait` time will be used.
9898
*/
99-
minimumWait: number;
99+
wait: number;
100100
}

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ async function exit(shouldManuallyExit, isSynchronous, signal) {
5858
}
5959

6060
function addHook(options) {
61-
const {onExit, minimumWait, isSynchronous} = options;
62-
const asyncCallbackConfig = [onExit, minimumWait];
61+
const {onExit, wait, isSynchronous} = options;
62+
const asyncCallbackConfig = [onExit, wait];
6363

6464
if (isSynchronous) {
6565
callbacks.add(onExit);
@@ -116,13 +116,13 @@ export function asyncExitHook(onExit, options = {}) {
116116
throw new TypeError('onExit must be a function');
117117
}
118118

119-
if (!(typeof options.minimumWait === 'number' && options.minimumWait > 0)) {
120-
throw new TypeError('minimumWait must be set to a positive numeric value');
119+
if (!(typeof options.wait === 'number' && options.wait > 0)) {
120+
throw new TypeError('wait must be set to a positive numeric value');
121121
}
122122

123123
return addHook({
124124
onExit,
125-
minimumWait: options.minimumWait,
125+
wait: options.wait,
126126
isSynchronous: false,
127127
});
128128
}

index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import exitHook, {asyncExitHook} from './index.js';
44
const unsubscribe = exitHook(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
55

66
const asyncUnsubscribe = asyncExitHook(async () => {}, // eslint-disable-line @typescript-eslint/no-empty-function
7-
{minimumWait: 300},
7+
{wait: 300},
88
);
99

1010
expectType<() => void>(unsubscribe);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "module",
1414
"exports": "./index.js",
1515
"engines": {
16-
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
16+
"node": ">=18.0.0"
1717
},
1818
"scripts": {
1919
"test": "xo && ava && tsd"

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ The callback function to execute when the process exits via `gracefulExit`, and
7474

7575
Type: `object`
7676

77-
##### minimumWait
77+
##### wait
7878

7979
Type: `number`
8080

81-
The amount of time in milliseconds that the `onExit` function is expected to take.
81+
The amount of time in milliseconds that the `onExit` function is expected to take. When multiple async handlers are registered, the longest `wait` time will be used.
8282

8383
```js
8484
import {asyncExitHook} from 'exit-hook';
8585

8686
asyncExitHook(async () => {
8787
console.log('Exiting');
8888
}, {
89-
minimumWait: 300
89+
wait: 300
9090
});
9191

9292
throw new Error('🦄');
@@ -102,7 +102,7 @@ import {asyncExitHook} from 'exit-hook';
102102
const unsubscribe = asyncExitHook(async () => {
103103
console.log('Exiting');
104104
}, {
105-
minimumWait: 300
105+
wait: 300
106106
});
107107

108108
unsubscribe();
@@ -135,4 +135,4 @@ Node.js does not offer an asynchronous shutdown API by default [#1](https://gith
135135

136136
If you have asynchronous hooks registered and your Node.js process is terminated in a synchronous manner, a `SYNCHRONOUS TERMINATION NOTICE` error will be logged to the console. To avoid this, ensure you're only exiting via `gracefulExit` or that an upstream process manager is sending a `SIGINT` or `SIGTERM` signal to Node.js.
137137

138-
Asynchronous hooks should make a "best effort" to perform their tasks within the `minimumWait` time, but also be written to assume they may not complete their tasks before termination.
138+
Asynchronous hooks should make a "best effort" to perform their tasks within the `wait` time, but also be written to assume they may not complete their tasks before termination.

test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test('listener count', t => {
5858
const unsubscribe4 = asyncExitHook(
5959
async () => {},
6060
{
61-
minimumWait: 100,
61+
wait: 100,
6262
},
6363
);
6464
t.is(process.listenerCount('exit'), 1);
@@ -77,13 +77,18 @@ test('type enforcing', t => {
7777
// Non-function passed to `asyncExitHook`.
7878
t.throws(() => {
7979
asyncExitHook(null, {
80-
minimumWait: 100,
80+
wait: 100,
8181
});
8282
}, {
8383
instanceOf: TypeError,
8484
});
8585

86-
// Non-numeric passed to `minimumWait` option.
86+
// Non-numeric passed to `wait` option.
87+
t.throws(() => {
88+
asyncExitHook(async () => true, {wait: 'abc'});
89+
});
90+
91+
// Empty value passed to `wait` option.
8792
t.throws(() => {
8893
asyncExitHook(async () => true, {});
8994
});

0 commit comments

Comments
 (0)