vivth 1.0.5 → 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 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
@@ -12,6 +12,7 @@ new paths({
12
12
 
13
13
  new safeExit({
14
14
  exitEventNames: ['SIGINT', 'SIGTERM', 'exit'],
15
+ exitCallback: () => process.exit(0),
15
16
  exitCallbackListeners: (eventName) => {
16
17
  process.once(eventName, function () {
17
18
  safeExit.instance.exiting.correction(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vivth",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "extremely simple signal as library primitives",
5
5
  "main": "index.mjs",
6
6
  "types": "./types/index.d.mts",
@@ -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
- safeCleanUpCBs.forEach((cleanup) => {
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
  }
@@ -257,6 +257,7 @@ export class JSautoDOC {
257
257
  path: { relative: relativePath },
258
258
  baseName: { noExt },
259
259
  } = (await this.#parsedFilesRef.get(path_)).value;
260
+ const hasNoAutoDoc = /\/\*\*[\s\*]*?@noautodoc[\s\*]*?\*\//.test(await content.string());
260
261
  if (hasValidExportObject) {
261
262
  mjsMain.push(
262
263
  `export { ${noExt} } from '${
@@ -275,7 +276,7 @@ export class JSautoDOC {
275
276
  });
276
277
  if (!error && typedefString) {
277
278
  mjsTypes.push(typedefString.module);
278
- if (!/\/\*\*[\s\*]*?@noautodoc[\s\*]*?\*\//.test(await content.string())) {
279
+ if (!hasNoAutoDoc) {
279
280
  const nameVarID = noExt.toLowerCase();
280
281
  tableOfContent.push(`[${noExt}](#${nameVarID})`);
281
282
  apiDocuments.push(
@@ -285,19 +286,19 @@ export class JSautoDOC {
285
286
  );
286
287
  }
287
288
  }
288
- readme.forEach((ref) => {
289
- const {
290
- // fullDescription,
291
- // instanceOrStatic,
292
- // namedVar,
293
- // typeOfVar,
294
- parsedFullDescription,
295
- reference,
296
- } = ref;
297
- const { description, jsPreview } = parsedFullDescription;
298
- currentDescription.push(`\n#### reference:${reference}\n${description}\n${jsPreview}`);
299
- });
300
- if (hasValidExportObject) {
289
+ if (!hasNoAutoDoc && hasValidExportObject) {
290
+ readme.forEach((ref) => {
291
+ const {
292
+ // fullDescription,
293
+ // instanceOrStatic,
294
+ // namedVar,
295
+ // typeOfVar,
296
+ parsedFullDescription,
297
+ reference,
298
+ } = ref;
299
+ const { description, jsPreview } = parsedFullDescription;
300
+ currentDescription.push(`\n#### reference:${reference}\n${description}\n${jsPreview}`);
301
+ });
301
302
  const nameVarID = noExt.toLowerCase();
302
303
  tableOfContent.push(`[${noExt}](#${noExt.toLowerCase()})`);
303
304
  apiDocuments.push(
@@ -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
  /**