vivth 1.0.6 → 1.1.0
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 +11 -0
- package/dev/index.mjs +1 -0
- package/package.json +1 -1
- package/src/class/SafeExit.mjs +28 -9
- package/types/src/class/SafeExit.d.mts +13 -1
package/README.md
CHANGED
|
@@ -1051,6 +1051,15 @@ npm i vivth
|
|
|
1051
1051
|
/**
|
|
1052
1052
|
* @param {Object} options
|
|
1053
1053
|
* @param {ExitEventNames} options.exitEventNames
|
|
1054
|
+
* @param {()=>void} options.exitCallback
|
|
1055
|
+
* - standard node/bun:
|
|
1056
|
+
* ```js
|
|
1057
|
+
* () => process.exit(0),
|
|
1058
|
+
* ```
|
|
1059
|
+
* - Deno:
|
|
1060
|
+
* ```js
|
|
1061
|
+
* () => Deno.exit(0),
|
|
1062
|
+
* ```
|
|
1054
1063
|
* @param {(eventName:string)=>void} [options.exitCallbackListeners]
|
|
1055
1064
|
* - default value
|
|
1056
1065
|
* ```js
|
|
@@ -1070,7 +1079,9 @@ npm i vivth
|
|
|
1070
1079
|
|
|
1071
1080
|
new SafeExit({
|
|
1072
1081
|
// exitEventNames are blank by default, you need to manually name them all;
|
|
1082
|
+
// 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
1073
1083
|
exitEventNames: ['SIGINT', 'SIGTERM', ...otherExitEventNames],
|
|
1084
|
+
exitCallback = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
1074
1085
|
// optional deno example
|
|
1075
1086
|
exitCallbackListeners = (eventName) => {
|
|
1076
1087
|
const sig = Deno.signal(eventName);
|
package/dev/index.mjs
CHANGED
package/package.json
CHANGED
package/src/class/SafeExit.mjs
CHANGED
|
@@ -28,6 +28,15 @@ export class SafeExit {
|
|
|
28
28
|
* @description
|
|
29
29
|
* @param {Object} options
|
|
30
30
|
* @param {ExitEventNames} options.exitEventNames
|
|
31
|
+
* @param {()=>void} options.exitCallback
|
|
32
|
+
* - standard node/bun:
|
|
33
|
+
* ```js
|
|
34
|
+
* () => process.exit(0),
|
|
35
|
+
* ```
|
|
36
|
+
* - Deno:
|
|
37
|
+
* ```js
|
|
38
|
+
* () => Deno.exit(0),
|
|
39
|
+
* ```
|
|
31
40
|
* @param {(eventName:string)=>void} [options.exitCallbackListeners]
|
|
32
41
|
* - default value
|
|
33
42
|
* ```js
|
|
@@ -44,7 +53,9 @@ export class SafeExit {
|
|
|
44
53
|
*
|
|
45
54
|
* new SafeExit({
|
|
46
55
|
* // exitEventNames are blank by default, you need to manually name them all;
|
|
56
|
+
* // 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
47
57
|
* exitEventNames: ['SIGINT', 'SIGTERM', ...otherExitEventNames],
|
|
58
|
+
* exitCallback = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
48
59
|
* // optional deno example
|
|
49
60
|
* exitCallbackListeners = (eventName) => {
|
|
50
61
|
* const sig = Deno.signal(eventName);
|
|
@@ -56,11 +67,12 @@ export class SafeExit {
|
|
|
56
67
|
* }
|
|
57
68
|
* });
|
|
58
69
|
*/
|
|
59
|
-
constructor({ exitEventNames, exitCallbackListeners = undefined }) {
|
|
70
|
+
constructor({ exitEventNames, exitCallback, exitCallbackListeners = undefined }) {
|
|
60
71
|
if (SafeExit.instance) {
|
|
61
72
|
return SafeExit.instance;
|
|
62
73
|
}
|
|
63
74
|
SafeExit.instance = this;
|
|
75
|
+
this.#exit = exitCallback;
|
|
64
76
|
if (exitCallbackListeners) {
|
|
65
77
|
this.#exitCallbackListeners = exitCallbackListeners;
|
|
66
78
|
}
|
|
@@ -79,6 +91,9 @@ export class SafeExit {
|
|
|
79
91
|
*/
|
|
80
92
|
#register = (exitEventNames) => {
|
|
81
93
|
exitEventNames.forEach((eventName) => {
|
|
94
|
+
if (eventName == 'exit') {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
82
97
|
this.#exitCallbackListeners(eventName);
|
|
83
98
|
});
|
|
84
99
|
};
|
|
@@ -107,6 +122,10 @@ export class SafeExit {
|
|
|
107
122
|
addCallback = (cb) => {
|
|
108
123
|
safeCleanUpCBs.add(cb);
|
|
109
124
|
};
|
|
125
|
+
/**
|
|
126
|
+
* @type {()=>void}
|
|
127
|
+
*/
|
|
128
|
+
#exit;
|
|
110
129
|
#autoCleanUp = new Effect(async ({ subscribe }) => {
|
|
111
130
|
if (!subscribe(this.exiting.env).value) {
|
|
112
131
|
return;
|
|
@@ -117,15 +136,15 @@ export class SafeExit {
|
|
|
117
136
|
setOfEffects.forEach((effect) => {
|
|
118
137
|
effect.options.removeEffect();
|
|
119
138
|
});
|
|
120
|
-
|
|
121
|
-
TryAsync(async () => {
|
|
139
|
+
for await (const cleanup of safeCleanUpCBs) {
|
|
140
|
+
const [_, error] = await TryAsync(async () => {
|
|
122
141
|
await cleanup();
|
|
123
|
-
}).then(([_, error]) => {
|
|
124
|
-
if (!error) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
Console.error(error);
|
|
128
142
|
});
|
|
129
|
-
|
|
143
|
+
if (!error) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
Console.error(error);
|
|
147
|
+
}
|
|
148
|
+
this.#exit();
|
|
130
149
|
});
|
|
131
150
|
}
|
|
@@ -19,6 +19,15 @@ export class SafeExit<ExitEventNames extends [string, ...string[]]> {
|
|
|
19
19
|
* @description
|
|
20
20
|
* @param {Object} options
|
|
21
21
|
* @param {ExitEventNames} options.exitEventNames
|
|
22
|
+
* @param {()=>void} options.exitCallback
|
|
23
|
+
* - standard node/bun:
|
|
24
|
+
* ```js
|
|
25
|
+
* () => process.exit(0),
|
|
26
|
+
* ```
|
|
27
|
+
* - Deno:
|
|
28
|
+
* ```js
|
|
29
|
+
* () => Deno.exit(0),
|
|
30
|
+
* ```
|
|
22
31
|
* @param {(eventName:string)=>void} [options.exitCallbackListeners]
|
|
23
32
|
* - default value
|
|
24
33
|
* ```js
|
|
@@ -35,7 +44,9 @@ export class SafeExit<ExitEventNames extends [string, ...string[]]> {
|
|
|
35
44
|
*
|
|
36
45
|
* new SafeExit({
|
|
37
46
|
* // exitEventNames are blank by default, you need to manually name them all;
|
|
47
|
+
* // 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
38
48
|
* exitEventNames: ['SIGINT', 'SIGTERM', ...otherExitEventNames],
|
|
49
|
+
* exitCallback = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
39
50
|
* // optional deno example
|
|
40
51
|
* exitCallbackListeners = (eventName) => {
|
|
41
52
|
* const sig = Deno.signal(eventName);
|
|
@@ -47,8 +58,9 @@ export class SafeExit<ExitEventNames extends [string, ...string[]]> {
|
|
|
47
58
|
* }
|
|
48
59
|
* });
|
|
49
60
|
*/
|
|
50
|
-
constructor({ exitEventNames, exitCallbackListeners }: {
|
|
61
|
+
constructor({ exitEventNames, exitCallback, exitCallbackListeners }: {
|
|
51
62
|
exitEventNames: ExitEventNames;
|
|
63
|
+
exitCallback: () => void;
|
|
52
64
|
exitCallbackListeners?: (eventName: string) => void;
|
|
53
65
|
});
|
|
54
66
|
/**
|