Skip to content

Commit

Permalink
Rename minimumWait option to wait and require Node.js 18 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobo authored Aug 31, 2023
1 parent 34078d9 commit 46baeec
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion fixtures/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ asyncExitHook(
console.log('quux');
},
{
minimumWait: 200,
wait: 200,
},
);

Expand Down
2 changes: 1 addition & 1 deletion fixtures/signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exitHook(signal => {
asyncExitHook(async signal => {
console.log(signal);
}, {
minimumWait: 200,
wait: 200,
});

setInterval(() => {}, 1 << 30);
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ import {asyncExitHook} from 'exit-hook';
asyncExitHook(() => {
console.log('Exiting');
}, {
minimumWait: 500
wait: 500
});
throw new Error('🦄');
//=> 'Exiting'
// Removing an exit hook:
const unsubscribe = asyncExitHook(() => {}, {minimumWait: 500});
const unsubscribe = asyncExitHook(() => {}, {wait: 500});
unsubscribe();
```
Expand All @@ -83,7 +83,7 @@ import {asyncExitHook, gracefulExit} from 'exit-hook';
asyncExitHook(() => {
console.log('Exiting');
}, {
minimumWait: 500
wait: 500
});
// Instead of `process.exit()`
Expand All @@ -94,7 +94,7 @@ export function gracefulExit(signal?: number): void;

export interface Options {
/**
The amount of time in milliseconds that the `onExit` function is expected to take.
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.
*/
minimumWait: number;
wait: number;
}
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ async function exit(shouldManuallyExit, isSynchronous, signal) {
}

function addHook(options) {
const {onExit, minimumWait, isSynchronous} = options;
const asyncCallbackConfig = [onExit, minimumWait];
const {onExit, wait, isSynchronous} = options;
const asyncCallbackConfig = [onExit, wait];

if (isSynchronous) {
callbacks.add(onExit);
Expand Down Expand Up @@ -116,13 +116,13 @@ export function asyncExitHook(onExit, options = {}) {
throw new TypeError('onExit must be a function');
}

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

return addHook({
onExit,
minimumWait: options.minimumWait,
wait: options.wait,
isSynchronous: false,
});
}
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import exitHook, {asyncExitHook} from './index.js';
const unsubscribe = exitHook(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function

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

expectType<() => void>(unsubscribe);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ The callback function to execute when the process exits via `gracefulExit`, and

Type: `object`

##### minimumWait
##### wait

Type: `number`

The amount of time in milliseconds that the `onExit` function is expected to take.
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.

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

asyncExitHook(async () => {
console.log('Exiting');
}, {
minimumWait: 300
wait: 300
});

throw new Error('🦄');
Expand All @@ -102,7 +102,7 @@ import {asyncExitHook} from 'exit-hook';
const unsubscribe = asyncExitHook(async () => {
console.log('Exiting');
}, {
minimumWait: 300
wait: 300
});

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

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.

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.
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.
11 changes: 8 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('listener count', t => {
const unsubscribe4 = asyncExitHook(
async () => {},
{
minimumWait: 100,
wait: 100,
},
);
t.is(process.listenerCount('exit'), 1);
Expand All @@ -77,13 +77,18 @@ test('type enforcing', t => {
// Non-function passed to `asyncExitHook`.
t.throws(() => {
asyncExitHook(null, {
minimumWait: 100,
wait: 100,
});
}, {
instanceOf: TypeError,
});

// Non-numeric passed to `minimumWait` option.
// Non-numeric passed to `wait` option.
t.throws(() => {
asyncExitHook(async () => true, {wait: 'abc'});
});

// Empty value passed to `wait` option.
t.throws(() => {
asyncExitHook(async () => true, {});
});
Expand Down

0 comments on commit 46baeec

Please sign in to comment.