vivth 1.0.6 → 1.1.1
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 +42 -31
- package/dev/index.mjs +3 -2
- package/index.mjs +6 -6
- package/package.json +1 -1
- package/src/class/SafeExit.mjs +42 -23
- package/types/index.d.mts +5 -5
- package/types/src/class/SafeExit.d.mts +22 -10
package/README.md
CHANGED
|
@@ -68,12 +68,12 @@ npm i vivth
|
|
|
68
68
|
- [TsToMjs](#tstomjs)
|
|
69
69
|
- [WriteFileSafe](#writefilesafe)
|
|
70
70
|
- [AnyButUndefined](#anybutundefined)
|
|
71
|
-
- [ExtnameType](#extnametype)
|
|
72
|
-
- [IsListSignal](#islistsignal)
|
|
73
|
-
- [ListArg](#listarg)
|
|
74
|
-
- [MutationType](#mutationtype)
|
|
75
|
-
- [QCBFIFOReturn](#qcbfiforeturn)
|
|
76
71
|
- [QCBReturn](#qcbreturn)
|
|
72
|
+
- [QCBFIFOReturn](#qcbfiforeturn)
|
|
73
|
+
- [MutationType](#mutationtype)
|
|
74
|
+
- [ListArg](#listarg)
|
|
75
|
+
- [IsListSignal](#islistsignal)
|
|
76
|
+
- [ExtnameType](#extnametype)
|
|
77
77
|
|
|
78
78
|
<h2 id="compilemjs">CompileMJS</h2>
|
|
79
79
|
|
|
@@ -1031,7 +1031,7 @@ npm i vivth
|
|
|
1031
1031
|
|
|
1032
1032
|
```js
|
|
1033
1033
|
/**
|
|
1034
|
-
* @template {[string, ...string[]]}
|
|
1034
|
+
* @template {[string, ...string[]]} eventNames
|
|
1035
1035
|
*/
|
|
1036
1036
|
```
|
|
1037
1037
|
|
|
@@ -1050,8 +1050,17 @@ npm i vivth
|
|
|
1050
1050
|
```js
|
|
1051
1051
|
/**
|
|
1052
1052
|
* @param {Object} options
|
|
1053
|
-
* @param {
|
|
1054
|
-
* @param {(
|
|
1053
|
+
* @param {eventNames} options.eventNames
|
|
1054
|
+
* @param {()=>void} options.terminator
|
|
1055
|
+
* - standard node/bun:
|
|
1056
|
+
* ```js
|
|
1057
|
+
* () => process.exit(0),
|
|
1058
|
+
* ```
|
|
1059
|
+
* - Deno:
|
|
1060
|
+
* ```js
|
|
1061
|
+
* () => Deno.exit(0),
|
|
1062
|
+
* ```
|
|
1063
|
+
* @param {(eventName:string)=>void} [options.listener]
|
|
1055
1064
|
* - default value
|
|
1056
1065
|
* ```js
|
|
1057
1066
|
* (eventName) => {
|
|
@@ -1069,10 +1078,12 @@ npm i vivth
|
|
|
1069
1078
|
import { SafeExit, Console } from 'vivth';
|
|
1070
1079
|
|
|
1071
1080
|
new SafeExit({
|
|
1072
|
-
//
|
|
1073
|
-
|
|
1081
|
+
// eventNames 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;
|
|
1083
|
+
eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
1084
|
+
terminator = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
1074
1085
|
// optional deno example
|
|
1075
|
-
|
|
1086
|
+
listener = (eventName) => {
|
|
1076
1087
|
const sig = Deno.signal(eventName);
|
|
1077
1088
|
for await (const _ of sig) {
|
|
1078
1089
|
exiting.correction(true);
|
|
@@ -2123,76 +2134,76 @@ npm i vivth
|
|
|
2123
2134
|
```
|
|
2124
2135
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2125
2136
|
|
|
2126
|
-
<h2 id="
|
|
2137
|
+
<h2 id="qcbreturn">QCBReturn</h2>
|
|
2127
2138
|
|
|
2128
2139
|
- jsdoc types:
|
|
2129
2140
|
|
|
2130
2141
|
```js
|
|
2131
2142
|
/**
|
|
2132
|
-
* -
|
|
2133
|
-
* @typedef {
|
|
2143
|
+
* - return type of Q callback;
|
|
2144
|
+
* @typedef {{resume:()=>void, isLastOnQ:boolean}} QCBReturn
|
|
2134
2145
|
*/
|
|
2135
2146
|
```
|
|
2136
2147
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2137
2148
|
|
|
2138
|
-
<h2 id="
|
|
2149
|
+
<h2 id="qcbfiforeturn">QCBFIFOReturn</h2>
|
|
2139
2150
|
|
|
2140
2151
|
- jsdoc types:
|
|
2141
2152
|
|
|
2142
2153
|
```js
|
|
2143
2154
|
/**
|
|
2144
|
-
* -
|
|
2145
|
-
* @typedef {
|
|
2155
|
+
* - return type of Q callback fifo;
|
|
2156
|
+
* @typedef {Omit<import("./src/types/QCBReturn.mjs").QCBReturn, "isLastOnQ">} QCBFIFOReturn
|
|
2146
2157
|
*/
|
|
2147
2158
|
```
|
|
2148
2159
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2149
2160
|
|
|
2150
|
-
<h2 id="
|
|
2161
|
+
<h2 id="mutationtype">MutationType</h2>
|
|
2151
2162
|
|
|
2152
2163
|
- jsdoc types:
|
|
2153
2164
|
|
|
2154
2165
|
```js
|
|
2155
2166
|
/**
|
|
2156
|
-
* - ListSignal
|
|
2157
|
-
* @typedef {
|
|
2167
|
+
* - `ListSignal` mutation type;
|
|
2168
|
+
* @typedef {'push'|'unshift'|'splice'|'swap'|'modify'|'shift'|'remove'} MutationType
|
|
2169
|
+
* - instance method: serves as helper to mutate, and notify for `effects`;
|
|
2170
|
+
* > - `slice` uses `splice` in the background, you don't need to manually reindex when using it;
|
|
2158
2171
|
*/
|
|
2159
2172
|
```
|
|
2160
2173
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2161
2174
|
|
|
2162
|
-
<h2 id="
|
|
2175
|
+
<h2 id="listarg">ListArg</h2>
|
|
2163
2176
|
|
|
2164
2177
|
- jsdoc types:
|
|
2165
2178
|
|
|
2166
2179
|
```js
|
|
2167
2180
|
/**
|
|
2168
|
-
* -
|
|
2169
|
-
* @typedef {
|
|
2170
|
-
* - instance method: serves as helper to mutate, and notify for `effects`;
|
|
2171
|
-
* > - `slice` uses `splice` in the background, you don't need to manually reindex when using it;
|
|
2181
|
+
* - ListSignal argument type;
|
|
2182
|
+
* @typedef {Record<string, string>} ListArg
|
|
2172
2183
|
*/
|
|
2173
2184
|
```
|
|
2174
2185
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2175
2186
|
|
|
2176
|
-
<h2 id="
|
|
2187
|
+
<h2 id="islistsignal">IsListSignal</h2>
|
|
2177
2188
|
|
|
2178
2189
|
- jsdoc types:
|
|
2179
2190
|
|
|
2180
2191
|
```js
|
|
2181
2192
|
/**
|
|
2182
|
-
* -
|
|
2183
|
-
* @typedef {
|
|
2193
|
+
* - `EnvSignal.get` argument whether signal need to be a list or not;
|
|
2194
|
+
* @typedef {boolean} IsListSignal
|
|
2184
2195
|
*/
|
|
2185
2196
|
```
|
|
2186
2197
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2187
2198
|
|
|
2188
|
-
<h2 id="
|
|
2199
|
+
<h2 id="extnametype">ExtnameType</h2>
|
|
2189
2200
|
|
|
2190
2201
|
- jsdoc types:
|
|
2191
2202
|
|
|
2192
2203
|
```js
|
|
2193
2204
|
/**
|
|
2194
|
-
* -
|
|
2195
|
-
* @typedef {{
|
|
2205
|
+
* - jsRuntime extention naming convention;
|
|
2206
|
+
* @typedef {`.${string}`} ExtnameType
|
|
2196
2207
|
*/
|
|
2197
2208
|
```
|
|
2198
2209
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
package/dev/index.mjs
CHANGED
|
@@ -11,8 +11,9 @@ new paths({
|
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
new safeExit({
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
eventNames: ['SIGINT', 'SIGTERM', 'exit'],
|
|
15
|
+
terminator: () => process.exit(0),
|
|
16
|
+
listener: (eventName) => {
|
|
16
17
|
process.once(eventName, function () {
|
|
17
18
|
safeExit.instance.exiting.correction(true);
|
|
18
19
|
Console.log(`safe exit via "${eventName}"`);
|
package/index.mjs
CHANGED
|
@@ -41,20 +41,20 @@ export { WriteFileSafe } from './src/function/WriteFileSafe.mjs';
|
|
|
41
41
|
* @typedef {import('./src/types/AnyButUndefined.mjs').AnyButUndefined} AnyButUndefined
|
|
42
42
|
*/
|
|
43
43
|
/**
|
|
44
|
-
* @typedef {import('./src/types/
|
|
44
|
+
* @typedef {import('./src/types/QCBReturn.mjs').QCBReturn} QCBReturn
|
|
45
45
|
*/
|
|
46
46
|
/**
|
|
47
|
-
* @typedef {import('./src/types/
|
|
47
|
+
* @typedef {import('./src/types/QCBFIFOReturn.mjs').QCBFIFOReturn} QCBFIFOReturn
|
|
48
48
|
*/
|
|
49
49
|
/**
|
|
50
|
-
* @typedef {import('./src/types/
|
|
50
|
+
* @typedef {import('./src/types/MutationType.mjs').MutationType} MutationType
|
|
51
51
|
*/
|
|
52
52
|
/**
|
|
53
|
-
* @typedef {import('./src/types/
|
|
53
|
+
* @typedef {import('./src/types/ListArg.mjs').ListArg} ListArg
|
|
54
54
|
*/
|
|
55
55
|
/**
|
|
56
|
-
* @typedef {import('./src/types/
|
|
56
|
+
* @typedef {import('./src/types/IsListSignal.mjs').IsListSignal} IsListSignal
|
|
57
57
|
*/
|
|
58
58
|
/**
|
|
59
|
-
* @typedef {import('./src/types/
|
|
59
|
+
* @typedef {import('./src/types/ExtnameType.mjs').ExtnameType} ExtnameType
|
|
60
60
|
*/
|
package/package.json
CHANGED
package/src/class/SafeExit.mjs
CHANGED
|
@@ -15,7 +15,7 @@ export const safeCleanUpCBs = new Set();
|
|
|
15
15
|
* @description
|
|
16
16
|
* - class helper for describing how to Safely Response on exit events
|
|
17
17
|
* - singleton;
|
|
18
|
-
* @template {[string, ...string[]]}
|
|
18
|
+
* @template {[string, ...string[]]} eventNames
|
|
19
19
|
*/
|
|
20
20
|
export class SafeExit {
|
|
21
21
|
/**
|
|
@@ -27,8 +27,17 @@ export class SafeExit {
|
|
|
27
27
|
/**
|
|
28
28
|
* @description
|
|
29
29
|
* @param {Object} options
|
|
30
|
-
* @param {
|
|
31
|
-
* @param {(
|
|
30
|
+
* @param {eventNames} options.eventNames
|
|
31
|
+
* @param {()=>void} options.terminator
|
|
32
|
+
* - standard node/bun:
|
|
33
|
+
* ```js
|
|
34
|
+
* () => process.exit(0),
|
|
35
|
+
* ```
|
|
36
|
+
* - Deno:
|
|
37
|
+
* ```js
|
|
38
|
+
* () => Deno.exit(0),
|
|
39
|
+
* ```
|
|
40
|
+
* @param {(eventName:string)=>void} [options.listener]
|
|
32
41
|
* - default value
|
|
33
42
|
* ```js
|
|
34
43
|
* (eventName) => {
|
|
@@ -43,10 +52,12 @@ export class SafeExit {
|
|
|
43
52
|
* import { SafeExit, Console } from 'vivth';
|
|
44
53
|
*
|
|
45
54
|
* new SafeExit({
|
|
46
|
-
* //
|
|
47
|
-
*
|
|
55
|
+
* // eventNames 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;
|
|
57
|
+
* eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
58
|
+
* terminator = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
48
59
|
* // optional deno example
|
|
49
|
-
*
|
|
60
|
+
* listener = (eventName) => {
|
|
50
61
|
* const sig = Deno.signal(eventName);
|
|
51
62
|
* for await (const _ of sig) {
|
|
52
63
|
* exiting.correction(true);
|
|
@@ -56,15 +67,16 @@ export class SafeExit {
|
|
|
56
67
|
* }
|
|
57
68
|
* });
|
|
58
69
|
*/
|
|
59
|
-
constructor({
|
|
70
|
+
constructor({ eventNames, terminator, listener = undefined }) {
|
|
60
71
|
if (SafeExit.instance) {
|
|
61
72
|
return SafeExit.instance;
|
|
62
73
|
}
|
|
63
74
|
SafeExit.instance = this;
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
this.#exit = terminator;
|
|
76
|
+
if (listener) {
|
|
77
|
+
this.#listener = listener;
|
|
66
78
|
}
|
|
67
|
-
this.#register(
|
|
79
|
+
this.#register(eventNames);
|
|
68
80
|
}
|
|
69
81
|
/**
|
|
70
82
|
* @description
|
|
@@ -74,18 +86,21 @@ export class SafeExit {
|
|
|
74
86
|
*/
|
|
75
87
|
exiting = new EnvSignal(false);
|
|
76
88
|
/**
|
|
77
|
-
* @param {
|
|
89
|
+
* @param {eventNames} eventNames
|
|
78
90
|
* @returns {void}
|
|
79
91
|
*/
|
|
80
|
-
#register = (
|
|
81
|
-
|
|
82
|
-
|
|
92
|
+
#register = (eventNames) => {
|
|
93
|
+
eventNames.forEach((eventName) => {
|
|
94
|
+
if (eventName == 'exit') {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.#listener(eventName);
|
|
83
98
|
});
|
|
84
99
|
};
|
|
85
100
|
/**
|
|
86
101
|
* @type {(eventName:string)=>void}
|
|
87
102
|
*/
|
|
88
|
-
#
|
|
103
|
+
#listener = (eventName) => {
|
|
89
104
|
SafeExit.instance.exiting.env.value;
|
|
90
105
|
process.once(eventName, function () {
|
|
91
106
|
Console.log(`safe exit via "${eventName}"`);
|
|
@@ -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
|
}
|
package/types/index.d.mts
CHANGED
|
@@ -30,9 +30,9 @@ export { TrySync } from "./src/function/TrySync.mjs";
|
|
|
30
30
|
export { TsToMjs } from "./src/function/TsToMjs.mjs";
|
|
31
31
|
export { WriteFileSafe } from "./src/function/WriteFileSafe.mjs";
|
|
32
32
|
export type AnyButUndefined = import("./src/types/AnyButUndefined.mjs").AnyButUndefined;
|
|
33
|
-
export type ExtnameType = import("./src/types/ExtnameType.mjs").ExtnameType;
|
|
34
|
-
export type IsListSignal = import("./src/types/IsListSignal.mjs").IsListSignal;
|
|
35
|
-
export type ListArg = import("./src/types/ListArg.mjs").ListArg;
|
|
36
|
-
export type MutationType = import("./src/types/MutationType.mjs").MutationType;
|
|
37
|
-
export type QCBFIFOReturn = import("./src/types/QCBFIFOReturn.mjs").QCBFIFOReturn;
|
|
38
33
|
export type QCBReturn = import("./src/types/QCBReturn.mjs").QCBReturn;
|
|
34
|
+
export type QCBFIFOReturn = import("./src/types/QCBFIFOReturn.mjs").QCBFIFOReturn;
|
|
35
|
+
export type MutationType = import("./src/types/MutationType.mjs").MutationType;
|
|
36
|
+
export type ListArg = import("./src/types/ListArg.mjs").ListArg;
|
|
37
|
+
export type IsListSignal = import("./src/types/IsListSignal.mjs").IsListSignal;
|
|
38
|
+
export type ExtnameType = import("./src/types/ExtnameType.mjs").ExtnameType;
|
|
@@ -6,9 +6,9 @@ export const safeCleanUpCBs: Set<() => Promise<void>>;
|
|
|
6
6
|
* @description
|
|
7
7
|
* - class helper for describing how to Safely Response on exit events
|
|
8
8
|
* - singleton;
|
|
9
|
-
* @template {[string, ...string[]]}
|
|
9
|
+
* @template {[string, ...string[]]} eventNames
|
|
10
10
|
*/
|
|
11
|
-
export class SafeExit<
|
|
11
|
+
export class SafeExit<eventNames extends [string, ...string[]]> {
|
|
12
12
|
/**
|
|
13
13
|
* @description
|
|
14
14
|
* - only accessible after instantiation;
|
|
@@ -18,8 +18,17 @@ export class SafeExit<ExitEventNames extends [string, ...string[]]> {
|
|
|
18
18
|
/**
|
|
19
19
|
* @description
|
|
20
20
|
* @param {Object} options
|
|
21
|
-
* @param {
|
|
22
|
-
* @param {(
|
|
21
|
+
* @param {eventNames} options.eventNames
|
|
22
|
+
* @param {()=>void} options.terminator
|
|
23
|
+
* - standard node/bun:
|
|
24
|
+
* ```js
|
|
25
|
+
* () => process.exit(0),
|
|
26
|
+
* ```
|
|
27
|
+
* - Deno:
|
|
28
|
+
* ```js
|
|
29
|
+
* () => Deno.exit(0),
|
|
30
|
+
* ```
|
|
31
|
+
* @param {(eventName:string)=>void} [options.listener]
|
|
23
32
|
* - default value
|
|
24
33
|
* ```js
|
|
25
34
|
* (eventName) => {
|
|
@@ -34,10 +43,12 @@ export class SafeExit<ExitEventNames extends [string, ...string[]]> {
|
|
|
34
43
|
* import { SafeExit, Console } from 'vivth';
|
|
35
44
|
*
|
|
36
45
|
* new SafeExit({
|
|
37
|
-
* //
|
|
38
|
-
*
|
|
46
|
+
* // eventNames 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;
|
|
48
|
+
* eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
49
|
+
* terminator = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
39
50
|
* // optional deno example
|
|
40
|
-
*
|
|
51
|
+
* listener = (eventName) => {
|
|
41
52
|
* const sig = Deno.signal(eventName);
|
|
42
53
|
* for await (const _ of sig) {
|
|
43
54
|
* exiting.correction(true);
|
|
@@ -47,9 +58,10 @@ export class SafeExit<ExitEventNames extends [string, ...string[]]> {
|
|
|
47
58
|
* }
|
|
48
59
|
* });
|
|
49
60
|
*/
|
|
50
|
-
constructor({
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
constructor({ eventNames, terminator, listener }: {
|
|
62
|
+
eventNames: eventNames;
|
|
63
|
+
terminator: () => void;
|
|
64
|
+
listener?: (eventName: string) => void;
|
|
53
65
|
});
|
|
54
66
|
/**
|
|
55
67
|
* @description
|