Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d922c2

Browse files
committed
feat!: reworked options
1 parent 21d2855 commit 8d922c2

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

‎README.md‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
<!-- toc -->
88

9-
- [About The Project](#about-the-project)
10-
- [Installation](#installation)
11-
- [Usage](#usage)
12-
* [Arguments](#arguments)
13-
- [Contributing](#contributing)
14-
- [License](#license)
15-
- [Contact](#contact)
9+
- [fine](#fine)
10+
- [About The Project](#about-the-project)
11+
- [Installation](#installation)
12+
- [Usage](#usage)
13+
- [Arguments](#arguments)
14+
- [Contributing](#contributing)
15+
- [License](#license)
16+
- [Contact](#contact)
1617

1718
<!-- tocstop -->
1819

@@ -41,7 +42,7 @@ const fine = require("@scdev/fine");
4142
fine(
4243
{
4344
timeout: 2000,
44-
catchPromisesReject: true,
45+
events: ["SIGINT", "SIGTERM", "uncaughtException", "unhandledRejection"],
4546
},
4647
[
4748
redis.disconnect,
@@ -55,11 +56,11 @@ fine(
5556

5657
### Arguments
5758

58-
| parameter | type | description | default |
59-
| -------------- | ---------- | --------------------------------------------------------------------- | ------- |
60-
| opts | Object | Options object | {} |
61-
| opts.timeout | Number | The time before exiting the process | 2000 |
62-
| closeFunctions | function[] | Collection of callback for custom closing events, eg: db.disconnect() | |
59+
| parameter | type | description | default |
60+
| ----------- | ---------- | --------------------------------------------------------------------- | ------------------------------------------------------------------ |
61+
| `timeout` | Number | The time before exiting the process | `2000` |
62+
| `events` | string[] | The events the process will listen on | `["SIGINT", "SIGTERM", "uncaughtException", "unhandledRejection"]` |
63+
| `callbacks` | function[] | Collection of callback for custom closing events, eg: db.disconnect() | `[]` |
6364

6465
<!-- CONTRIBUTING -->
6566

‎index.js‎

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,55 @@ const {
66

77
const NOOP = () => undefined;
88

9-
module.exports = (opts = {}, closeFunctions = []) => {
10-
const options = Object.assign(
11-
{
12-
timeout: 2000,
13-
catchPromisesReject: true,
14-
},
15-
opts
16-
);
9+
const DEFAULT_EVENTS = [
10+
"SIGINT",
11+
"SIGTERM",
12+
"uncaughtException",
13+
"unhandledRejection",
14+
];
1715

18-
let events = ["SIGINT", "SIGTERM", "uncaughtException"];
16+
function validateParameters(timeout, events, callbacks) {
17+
if (typeof timeout !== "number") {
18+
throw new TypeError("timeout parameter must be number");
19+
}
20+
if (!Array.isArray(events)) {
21+
throw new TypeError("events parameter must be an array of strings");
22+
}
1923

20-
if (options.catchPromisesReject) {
21-
events.push("unhandledRejection");
24+
if (!Array.isArray(callbacks)) {
25+
throw new TypeError("callbacks parameter must be an array of functions");
26+
} else {
27+
callbacks.map((cb) => {
28+
if (typeof cb !== "function") {
29+
throw new TypeError("callback must be a function");
30+
}
31+
});
2232
}
33+
}
34+
35+
module.exports = function fine(
36+
timeout = 2000,
37+
events = DEFAULT_EVENTS,
38+
callbacks = []
39+
) {
40+
validateParameters(timeout, events, callbacks);
41+
2342
for (const event of events) {
2443
process.once(event, () => {
2544
const code = event.match("^SIG") ? 0 : 1;
2645
process.stdout.write(`[${event}] exiting with code ${code}\n`);
27-
if (Array.isArray(closeFunctions)) {
28-
for (const cb of closeFunctions) {
29-
try {
30-
if (typeof cb === "function") {
31-
const p = cb();
32-
if (isPromise(p)) p.catch(NOOP);
33-
}
34-
// eslint-disable-next-line no-empty
35-
} catch (_) {}
36-
}
46+
for (const cb of callbacks) {
47+
try {
48+
if (typeof cb === "function") {
49+
const p = cb();
50+
if (isPromise(p)) p.catch(NOOP);
51+
}
52+
// eslint-disable-next-line no-empty
53+
} catch (_) {}
3754
}
3855
setTimeout(() => {
3956
process.exit(code);
40-
}, options.timeout).unref();
57+
}, timeout);
4158
});
4259
}
4360
};

0 commit comments

Comments
 (0)