@breadc/death 0.9.4 → 0.9.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @breadc/death
2
2
 
3
- [![version](https://img.shields.io/npm/v/@breadc/death&label=@breadc/death)](https://www.npmjs.com/package/@breadc/death) [![CI](https://github.com/yjl9903/Breadc/actions/workflows/ci.yml/badge.svg)](https://github.com/yjl9903/Breadc/actions/workflows/ci.yml)
3
+ [![version](https://img.shields.io/npm/v/@breadc/death?label=@breadc/death)](https://www.npmjs.com/package/@breadc/death) [![CI](https://github.com/yjl9903/Breadc/actions/workflows/ci.yml/badge.svg)](https://github.com/yjl9903/Breadc/actions/workflows/ci.yml)
4
4
 
5
5
  Easily register termination signals callbacks.
6
6
 
package/dist/index.cjs CHANGED
@@ -4,26 +4,23 @@ const node_events = require('node:events');
4
4
 
5
5
  const emitter = new node_events.EventEmitter();
6
6
  const handlers = {
7
- SIGINT: makeHandler(),
8
- SIGTERM: makeHandler(),
9
- SIGQUIT: makeHandler()
7
+ SIGINT: makeHandler("SIGINT"),
8
+ SIGTERM: makeHandler("SIGTERM"),
9
+ SIGQUIT: makeHandler("SIGQUIT")
10
10
  };
11
11
  function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {}) {
12
12
  const cleanUps = [];
13
13
  if (SIGINT) {
14
14
  registerCallback("SIGINT", handlers.SIGINT);
15
- emitter.addListener("SIGINT", callback);
16
- cleanUps.push(() => emitter.removeListener("SIGINT", callback));
15
+ cleanUps.push(handlers.SIGINT.addCallback(callback));
17
16
  }
18
17
  if (SIGTERM) {
19
18
  registerCallback("SIGTERM", handlers.SIGTERM);
20
- emitter.addListener("SIGTERM", callback);
21
- cleanUps.push(() => emitter.removeListener("SIGTERM", callback));
19
+ cleanUps.push(handlers.SIGTERM.addCallback(callback));
22
20
  }
23
21
  if (SIGQUIT) {
24
22
  registerCallback("SIGQUIT", handlers.SIGQUIT);
25
- emitter.addListener("SIGQUIT", callback);
26
- cleanUps.push(() => emitter.removeListener("SIGQUIT", callback));
23
+ cleanUps.push(handlers.SIGQUIT.addCallback(callback));
27
24
  }
28
25
  return () => {
29
26
  for (const cleanUp of cleanUps) {
@@ -31,31 +28,56 @@ function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {
31
28
  }
32
29
  };
33
30
  }
34
- function registerCallback(signal, callback) {
35
- process.on(signal, callback);
36
- return () => {
37
- process.off(signal, callback);
38
- };
31
+ function registerCallback(signal, handler) {
32
+ if (handler.count === 0) {
33
+ handler.count += 1;
34
+ process.on(signal, handler.listener);
35
+ }
39
36
  }
40
37
  function makeHandler(signal) {
41
- return async (signal2) => {
42
- const listeners = emitter.listeners(signal2);
43
- const context = {
44
- terminate: "kill",
45
- exit: void 0,
46
- kill: signal2
47
- };
48
- for (const listener of listeners.reverse()) {
49
- await listener(signal2, context);
50
- }
51
- if (context.terminate === "kill" || context.terminate === "exit") {
52
- process.removeListener("SIGINT", handlers.SIGINT);
53
- process.removeListener("SIGTERM", handlers.SIGTERM);
54
- process.removeListener("SIGQUIT", handlers.SIGQUIT);
55
- if (context.terminate === "kill") {
56
- process.kill(process.pid, context.kill);
57
- } else {
58
- process.exit(context.exit);
38
+ const callbacks = /* @__PURE__ */ new WeakMap();
39
+ return {
40
+ count: 0,
41
+ callbacks,
42
+ addCallback(callback) {
43
+ {
44
+ const count = callbacks.get(callback);
45
+ if (count !== void 0) {
46
+ callbacks.set(callback, count + 1);
47
+ } else {
48
+ callbacks.set(callback, 1);
49
+ emitter.addListener(signal, callback);
50
+ }
51
+ }
52
+ return () => {
53
+ const count = callbacks.get(callback);
54
+ if (count === void 0 || count <= 1) {
55
+ callbacks.delete(callback);
56
+ emitter.removeListener(signal, callback);
57
+ } else {
58
+ callbacks.set(callback, count - 1);
59
+ }
60
+ };
61
+ },
62
+ async listener(signal2) {
63
+ const listeners = emitter.listeners(signal2);
64
+ const context = {
65
+ terminate: "kill",
66
+ exit: void 0,
67
+ kill: signal2
68
+ };
69
+ for (const listener of listeners.reverse()) {
70
+ await listener(signal2, context);
71
+ }
72
+ if (context.terminate === "kill" || context.terminate === "exit") {
73
+ process.removeListener("SIGINT", handlers.SIGINT.listener);
74
+ process.removeListener("SIGTERM", handlers.SIGTERM.listener);
75
+ process.removeListener("SIGQUIT", handlers.SIGQUIT.listener);
76
+ if (context.terminate === "kill") {
77
+ process.kill(process.pid, context.kill);
78
+ } else {
79
+ process.exit(context.exit);
80
+ }
59
81
  }
60
82
  }
61
83
  };
package/dist/index.mjs CHANGED
@@ -2,26 +2,23 @@ import { EventEmitter } from 'node:events';
2
2
 
3
3
  const emitter = new EventEmitter();
4
4
  const handlers = {
5
- SIGINT: makeHandler(),
6
- SIGTERM: makeHandler(),
7
- SIGQUIT: makeHandler()
5
+ SIGINT: makeHandler("SIGINT"),
6
+ SIGTERM: makeHandler("SIGTERM"),
7
+ SIGQUIT: makeHandler("SIGQUIT")
8
8
  };
9
9
  function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {}) {
10
10
  const cleanUps = [];
11
11
  if (SIGINT) {
12
12
  registerCallback("SIGINT", handlers.SIGINT);
13
- emitter.addListener("SIGINT", callback);
14
- cleanUps.push(() => emitter.removeListener("SIGINT", callback));
13
+ cleanUps.push(handlers.SIGINT.addCallback(callback));
15
14
  }
16
15
  if (SIGTERM) {
17
16
  registerCallback("SIGTERM", handlers.SIGTERM);
18
- emitter.addListener("SIGTERM", callback);
19
- cleanUps.push(() => emitter.removeListener("SIGTERM", callback));
17
+ cleanUps.push(handlers.SIGTERM.addCallback(callback));
20
18
  }
21
19
  if (SIGQUIT) {
22
20
  registerCallback("SIGQUIT", handlers.SIGQUIT);
23
- emitter.addListener("SIGQUIT", callback);
24
- cleanUps.push(() => emitter.removeListener("SIGQUIT", callback));
21
+ cleanUps.push(handlers.SIGQUIT.addCallback(callback));
25
22
  }
26
23
  return () => {
27
24
  for (const cleanUp of cleanUps) {
@@ -29,31 +26,56 @@ function onDeath(callback, { SIGINT = true, SIGTERM = true, SIGQUIT = true } = {
29
26
  }
30
27
  };
31
28
  }
32
- function registerCallback(signal, callback) {
33
- process.on(signal, callback);
34
- return () => {
35
- process.off(signal, callback);
36
- };
29
+ function registerCallback(signal, handler) {
30
+ if (handler.count === 0) {
31
+ handler.count += 1;
32
+ process.on(signal, handler.listener);
33
+ }
37
34
  }
38
35
  function makeHandler(signal) {
39
- return async (signal2) => {
40
- const listeners = emitter.listeners(signal2);
41
- const context = {
42
- terminate: "kill",
43
- exit: void 0,
44
- kill: signal2
45
- };
46
- for (const listener of listeners.reverse()) {
47
- await listener(signal2, context);
48
- }
49
- if (context.terminate === "kill" || context.terminate === "exit") {
50
- process.removeListener("SIGINT", handlers.SIGINT);
51
- process.removeListener("SIGTERM", handlers.SIGTERM);
52
- process.removeListener("SIGQUIT", handlers.SIGQUIT);
53
- if (context.terminate === "kill") {
54
- process.kill(process.pid, context.kill);
55
- } else {
56
- process.exit(context.exit);
36
+ const callbacks = /* @__PURE__ */ new WeakMap();
37
+ return {
38
+ count: 0,
39
+ callbacks,
40
+ addCallback(callback) {
41
+ {
42
+ const count = callbacks.get(callback);
43
+ if (count !== void 0) {
44
+ callbacks.set(callback, count + 1);
45
+ } else {
46
+ callbacks.set(callback, 1);
47
+ emitter.addListener(signal, callback);
48
+ }
49
+ }
50
+ return () => {
51
+ const count = callbacks.get(callback);
52
+ if (count === void 0 || count <= 1) {
53
+ callbacks.delete(callback);
54
+ emitter.removeListener(signal, callback);
55
+ } else {
56
+ callbacks.set(callback, count - 1);
57
+ }
58
+ };
59
+ },
60
+ async listener(signal2) {
61
+ const listeners = emitter.listeners(signal2);
62
+ const context = {
63
+ terminate: "kill",
64
+ exit: void 0,
65
+ kill: signal2
66
+ };
67
+ for (const listener of listeners.reverse()) {
68
+ await listener(signal2, context);
69
+ }
70
+ if (context.terminate === "kill" || context.terminate === "exit") {
71
+ process.removeListener("SIGINT", handlers.SIGINT.listener);
72
+ process.removeListener("SIGTERM", handlers.SIGTERM.listener);
73
+ process.removeListener("SIGQUIT", handlers.SIGQUIT.listener);
74
+ if (context.terminate === "kill") {
75
+ process.kill(process.pid, context.kill);
76
+ } else {
77
+ process.exit(context.exit);
78
+ }
57
79
  }
58
80
  }
59
81
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@breadc/death",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "Easily register termination signals callbacks.",
5
5
  "keywords": [
6
6
  "breadc",
@@ -37,7 +37,7 @@
37
37
  "dist"
38
38
  ],
39
39
  "devDependencies": {
40
- "@types/node": "^18.16.7",
40
+ "@types/node": "^18.16.8",
41
41
  "vitest": "^0.31.0"
42
42
  },
43
43
  "scripts": {