vivth 1.1.1 → 1.2.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.
Files changed (108) hide show
  1. package/.vivth-temp/README.src.md +35 -0
  2. package/README.md +1243 -441
  3. package/README.src.md +5 -2
  4. package/bun.lock +228 -0
  5. package/index.mjs +22 -9
  6. package/package.json +6 -3
  7. package/src/bundler/CompileJS.mjs +258 -0
  8. package/src/bundler/CreateESPlugin.mjs +24 -0
  9. package/src/bundler/EsBundler.mjs +27 -13
  10. package/src/bundler/FSInline.mjs +57 -0
  11. package/src/bundler/FSInlineAnalyzer.mjs +197 -0
  12. package/src/bundler/FSInlineBundled.mjs +34 -0
  13. package/src/bundler/adds/ToBundledJSPlugin.mjs +77 -0
  14. package/src/bundler/adds/externals.mjs +8 -0
  15. package/src/bundler/adds/pluginVivthBundle.mjs +5 -0
  16. package/src/class/Console.mjs +48 -27
  17. package/src/class/Derived.mjs +55 -7
  18. package/src/class/Effect.mjs +100 -39
  19. package/src/class/EnvSignal.mjs +5 -5
  20. package/src/class/EventSignal.mjs +55 -5
  21. package/src/class/FileSafe.mjs +124 -0
  22. package/src/class/ListDerived.mjs +6 -3
  23. package/src/class/ListSignal.mjs +11 -11
  24. package/src/class/LitExp.mjs +405 -0
  25. package/src/class/Paths.mjs +39 -4
  26. package/src/class/QChannel.mjs +79 -28
  27. package/src/class/SafeExit.mjs +31 -11
  28. package/src/class/Setup.mjs +12 -7
  29. package/src/class/Signal.mjs +26 -24
  30. package/src/class/WorkerMainThread.mjs +108 -135
  31. package/src/class/WorkerMainThreadBundled.mjs +216 -0
  32. package/src/class/WorkerThread.mjs +40 -31
  33. package/src/common/Base64URL.mjs +10 -5
  34. package/src/common/Base64URLFromFile.mjs +24 -0
  35. package/src/common/keys.mjs +3 -0
  36. package/src/doc/JSautoDOC.mjs +32 -56
  37. package/src/doc/parsedFile.mjs +37 -36
  38. package/src/function/CreateImmutable.mjs +9 -9
  39. package/src/function/EventCheck.mjs +2 -2
  40. package/src/function/EventObject.mjs +5 -5
  41. package/src/function/GetRuntime.mjs +38 -0
  42. package/src/function/IsAsync.mjs +2 -2
  43. package/src/function/LazyFactory.mjs +13 -13
  44. package/src/function/Timeout.mjs +2 -2
  45. package/src/function/Try.mjs +17 -12
  46. package/src/function/TryAsync.mjs +5 -5
  47. package/src/function/TrySync.mjs +5 -5
  48. package/src/function/TsToMjs.mjs +5 -4
  49. package/src/types/LitExpKeyType.mjs +5 -0
  50. package/src/types/QCBReturn.mjs +1 -1
  51. package/src/types/Runtime.mjs +7 -0
  52. package/types/dev/fsInline.d.mts +1 -0
  53. package/types/dev/test.d.mts +1 -0
  54. package/types/dev/testWorker.d.mts +7 -0
  55. package/types/dev/testbundle.d.mts +1 -0
  56. package/types/dev/workerThreadClass.d.mts +13 -0
  57. package/types/index.d.mts +18 -9
  58. package/types/src/bundler/CompileJS.d.mts +78 -0
  59. package/types/src/bundler/CreateESPlugin.d.mts +17 -0
  60. package/types/src/bundler/EsBundler.d.mts +33 -4
  61. package/types/src/bundler/FSInline.d.mts +43 -0
  62. package/types/src/bundler/FSInlineAnalyzer.d.mts +36 -0
  63. package/types/src/bundler/FSInlineBundled.d.mts +22 -0
  64. package/types/src/bundler/adds/ToBundledJSPlugin.d.mts +16 -0
  65. package/types/src/bundler/adds/externals.d.mts +1 -0
  66. package/types/src/bundler/adds/pluginVivthBundle.d.mts +1 -0
  67. package/types/src/class/Console.d.mts +36 -5
  68. package/types/src/class/Derived.d.mts +132 -5
  69. package/types/src/class/Effect.d.mts +106 -7
  70. package/types/src/class/EnvSignal.d.mts +8 -8
  71. package/types/src/class/EventSignal.d.mts +151 -7
  72. package/types/src/class/FileSafe.d.mts +90 -0
  73. package/types/src/class/ListDerived.d.mts +8 -5
  74. package/types/src/class/ListSignal.d.mts +123 -18
  75. package/types/src/class/LitExp.d.mts +361 -0
  76. package/types/src/class/Paths.d.mts +30 -4
  77. package/types/src/class/QChannel.d.mts +69 -22
  78. package/types/src/class/SafeExit.d.mts +24 -9
  79. package/types/src/class/Setup.d.mts +13 -10
  80. package/types/src/class/Signal.d.mts +73 -17
  81. package/types/src/class/WorkerMainThread.d.mts +47 -39
  82. package/types/src/class/WorkerMainThreadBundled.d.mts +85 -0
  83. package/types/src/class/WorkerThread.d.mts +34 -26
  84. package/types/src/common/Base64URL.d.mts +22 -1
  85. package/types/src/common/Base64URLFromFile.d.mts +16 -0
  86. package/types/src/common/keys.d.mts +1 -0
  87. package/types/src/doc/JSautoDOC.d.mts +3 -19
  88. package/types/src/doc/parsedFile.d.mts +72 -13
  89. package/types/src/function/CreateImmutable.d.mts +27 -2
  90. package/types/src/function/EventCheck.d.mts +15 -0
  91. package/types/src/function/EventObject.d.mts +17 -2
  92. package/types/src/function/GetRuntime.d.mts +2 -0
  93. package/types/src/function/IsAsync.d.mts +18 -0
  94. package/types/src/function/LazyFactory.d.mts +35 -2
  95. package/types/src/function/Timeout.d.mts +16 -0
  96. package/types/src/function/Try.d.mts +52 -1
  97. package/types/src/function/TryAsync.d.mts +22 -1
  98. package/types/src/function/TrySync.d.mts +16 -1
  99. package/types/src/function/TsToMjs.d.mts +19 -0
  100. package/types/src/types/LitExpKeyType.d.mts +1 -0
  101. package/types/src/types/QCBReturn.d.mts +1 -1
  102. package/types/src/types/Runtime.d.mts +1 -0
  103. package/dev/index.mjs +0 -28
  104. package/src/bundler/CompileMJS.mjs +0 -110
  105. package/src/function/WriteFileSafe.mjs +0 -37
  106. package/types/src/bundler/A.d.mts +0 -1
  107. package/types/src/bundler/CompileMJS.d.mts +0 -8
  108. package/types/src/function/WriteFileSafe.d.mts +0 -2
@@ -10,15 +10,17 @@ export class Setup {
10
10
  * import { Setup, Console } from 'vivth';
11
11
  *
12
12
  * new Setup.safeExit({
13
- * // exitEventNames are blank by default, you need to manually name them all;
14
- * exitEventNames: ['SIGINT', 'SIGTERM', ...otherExitEventNames],
13
+ * // eventNames are blank by default, you need to manually name them all;
14
+ * // 'exit' will be omited, as it might cause async callbacks failed to execute;
15
+ * eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
16
+ * terminator = () => process.exit(0), // OR on deno () => Deno.exit(0),
15
17
  * // optional deno example
16
- * exitCallbackListeners = (eventName) => {
18
+ * listener = (eventName) => {
17
19
  * const sig = Deno.signal(eventName);
18
20
  * for await (const _ of sig) {
19
- * SafeExit.instance.exiting.correction(true);
21
+ * exiting.correction(true);
20
22
  * sig.dispose();
21
- * Console.info(`safe exit via "${eventName}"`);
23
+ * Console.log(`safe exit via "${eventName}"`);
22
24
  * }
23
25
  * }
24
26
  * });
@@ -40,9 +42,10 @@ export class Setup {
40
42
  * - proxy `WorkerMainThread_instance` for Setup;
41
43
  * @example
42
44
  * import { Setup } from 'vivth';
45
+ * import { Worker } from 'node:worker_threads';
43
46
  *
44
47
  * Setup.workerMain({
45
- * workerClass: async () => await (import('worker_threads')).Worker,
48
+ * workerClass: Worker,
46
49
  * basePath: 'public/assets/js/workers',
47
50
  * pathValidator: async (workerPath, root, base) => {
48
51
  * const res = await fetch(`${root}/${base}/${workerPath}`);
@@ -59,16 +62,16 @@ export class Setup {
59
62
  /**
60
63
  * @description
61
64
  * - correct `parentPort` reference when needed;
65
+ * - export to create new reference to be use to create new WorkerThread instance;
62
66
  * @example
63
67
  * import { Setup } from 'vivth';
68
+ * import { parentPort } from 'node:worker_threads';
64
69
  *
65
- * Setup.workerThread({ parentPort: async () => await import('node:worker_threads') });
70
+ * export const MyWorkerThreadRef = Setup.workerThread({parentPort});
66
71
  * // that is the default value, if your parentPort/equivalent API is not that;
67
72
  * // you need to call this method;
68
73
  */
69
- static workerThread: <Receive_, Post_>(parentPortRef: {
70
- parentPort: () => Promise<any>;
71
- }) => typeof WorkerThread<Receive_, Post_>;
74
+ static workerThread: typeof WorkerThread.setup;
72
75
  }
73
76
  import { SafeExit } from './SafeExit.mjs';
74
77
  import { Paths } from './Paths.mjs';
@@ -5,22 +5,23 @@ export const setOFSignals: Set<Signal<any>>;
5
5
  /**
6
6
  * @description
7
7
  * - a class for creating effect to signals;
8
- * @template Value
8
+ * @template VALUE
9
9
  */
10
- export class Signal<Value> {
10
+ export class Signal<VALUE> {
11
11
  /**
12
12
  * @param {Set<Effect>} setOfSubscribers
13
13
  */
14
14
  static #notify: (setOfSubscribers: Set<Effect>) => void;
15
15
  /**
16
16
  * @description
17
- * @param {Value} value
17
+ * - create a `Signal`;
18
+ * @param {VALUE} value
18
19
  * @example
19
20
  * import { Signal, Effect } from 'vivth';
20
21
  *
21
22
  * const count = new Signal(0);
22
23
  */
23
- constructor(value: Value);
24
+ constructor(value: VALUE);
24
25
  /**
25
26
  * @description
26
27
  * - subsrcibers reference of this instance;
@@ -37,17 +38,17 @@ export class Signal<Value> {
37
38
  * @instance subscribers
38
39
  * @description
39
40
  * - manually notify on non primitive value or value that have depths;
40
- * @param {(options:{signalInstance:Signal<Value>})=>Promise<void>} [callback]
41
+ * @param {(options:{signalInstance:Signal<VALUE>})=>Promise<void>} [callback]
41
42
  * @returns {void}
42
43
  * @example
43
- * // for deep signal like array or object you can:
44
+ * import { Signal } from 'vivth';
44
45
  *
46
+ * // for deep signal like array or object you can:
45
47
  * const arraySignal = new Signal([1,2]);
46
48
  * arraySignal.value.push(3);
47
49
  * arraySignal.subscribers.notify();
48
50
  *
49
51
  * // OR for more complex mutation:
50
- *
51
52
  * const objectSignal = new Signal({a:'test', b:'test'});
52
53
  * objectSignal.subscribers.notify(async ({ signalInstance }) => {
53
54
  * signalInstance.value['c'] = 'testc';
@@ -55,10 +56,42 @@ export class Signal<Value> {
55
56
  * });
56
57
  */
57
58
  notify: (callback?: (options: {
58
- signalInstance: Signal<Value>;
59
+ signalInstance: Signal<VALUE>;
59
60
  }) => Promise<void>) => void;
60
61
  } & {
61
- "vivth:unwrapLazy;": string;
62
+ "vivth:unwrapLazy;": () => {
63
+ /**
64
+ * @instance subscribers
65
+ * @description
66
+ * - subscribedEffects
67
+ * @type {Set<Effect>}
68
+ */
69
+ setOf: Set<Effect>;
70
+ /**
71
+ * @instance subscribers
72
+ * @description
73
+ * - manually notify on non primitive value or value that have depths;
74
+ * @param {(options:{signalInstance:Signal<VALUE>})=>Promise<void>} [callback]
75
+ * @returns {void}
76
+ * @example
77
+ * import { Signal } from 'vivth';
78
+ *
79
+ * // for deep signal like array or object you can:
80
+ * const arraySignal = new Signal([1,2]);
81
+ * arraySignal.value.push(3);
82
+ * arraySignal.subscribers.notify();
83
+ *
84
+ * // OR for more complex mutation:
85
+ * const objectSignal = new Signal({a:'test', b:'test'});
86
+ * objectSignal.subscribers.notify(async ({ signalInstance }) => {
87
+ * signalInstance.value['c'] = 'testc';
88
+ * signalInstance.value['d'] = 'testd';
89
+ * });
90
+ */
91
+ notify: (callback?: (options: {
92
+ signalInstance: Signal<VALUE>;
93
+ }) => Promise<void>) => void;
94
+ };
62
95
  };
63
96
  /**
64
97
  * @description
@@ -88,18 +121,41 @@ export class Signal<Value> {
88
121
  */
89
122
  ref: () => void;
90
123
  } & {
91
- "vivth:unwrapLazy;": string;
124
+ "vivth:unwrapLazy;": () => {
125
+ /**
126
+ * @instance remove
127
+ * @description
128
+ * - remove effect subscriber to react from this instance value changes;
129
+ * @param {Effect} effectInstance
130
+ * @returns {void}
131
+ */
132
+ subscriber: (effectInstance: Effect) => void;
133
+ /**
134
+ * @instance remove
135
+ * @description
136
+ * - remove all effect subscribers to react from this instance value changes;
137
+ * @type {()=>void}
138
+ */
139
+ allSubscribers: () => void;
140
+ /**
141
+ * @instance remove
142
+ * @description
143
+ * - remove this instance from `vivth` reactivity engine, and nullify it's own value;
144
+ * @type {()=>void}
145
+ */
146
+ ref: () => void;
147
+ };
92
148
  };
93
149
  /**
94
150
  * @description
95
151
  * - value before change;
96
- * @type {Value}
152
+ * @returns {VALUE}
97
153
  */
98
- get prev(): Value;
154
+ get prev(): VALUE;
99
155
  /**
100
156
  * @description
101
157
  * - assign new value then automatically notify all subscribers;
102
- * @type {Value}
158
+ * @type {VALUE}
103
159
  * @example
104
160
  * import { Signal } from 'vivth';
105
161
  *
@@ -108,11 +164,11 @@ export class Signal<Value> {
108
164
  * // OR
109
165
  * count.value = 9;
110
166
  */
111
- set value(newValue: Value);
167
+ set value(newValue: VALUE);
112
168
  /**
113
169
  * @description
114
170
  * - value after change;
115
- * @type {Value}
171
+ * @returns {VALUE}
116
172
  * @example
117
173
  * import { Signal, Effect, Derived } from 'vivth';
118
174
  *
@@ -122,11 +178,11 @@ export class Signal<Value> {
122
178
  * new Effect(async ({ subscribe }) =>{
123
179
  * const countValue = subscribe(count).value; // reactive
124
180
  * })
125
- * const oneMoreThanCount = new Effect(async ({ subscribe }) =>{
181
+ * const oneMoreThanCount = new Derived(async ({ subscribe }) =>{
126
182
  * return subscribe(count).value + 1; // reactive
127
183
  * })
128
184
  */
129
- get value(): Value;
185
+ get value(): VALUE;
130
186
  #private;
131
187
  }
132
188
  import { Effect } from './Effect.mjs';
@@ -1,37 +1,50 @@
1
1
  /**
2
- * @template A
3
- * @typedef {import('./WorkerResult.mjs').WorkerResult<A>} WorkerResult
2
+ * @template POST
3
+ * @typedef {import('./WorkerResult.mjs').WorkerResult<POST>} WorkerResult
4
4
  */
5
5
  /**
6
6
  * @typedef {import('./WorkerThread.mjs').WorkerThread} WorkerThread
7
+ * @typedef {import('../common/lazie.mjs').unwrapLazy} unwrapLazy
7
8
  */
8
9
  /**
9
10
  * @description
10
11
  * - class helper to create `Worker` instance;
12
+ * - before any `Worker` functionaily to be used, you need to setup it with `WorkerThread.setup` and `WorkerMainThread.setup` before runing anytyhing;
11
13
  * @template {WorkerThread} WT
12
14
  */
13
15
  export class WorkerMainThread<WT extends WorkerThread> {
16
+ /**
17
+ * @type {boolean}
18
+ */
14
19
  static #isRegistered: boolean;
15
20
  /**
16
21
  * @description
17
22
  * - need to be called first, before any `WorkerMainThread` instantiation:
18
23
  * @param {Object} param0
19
24
  * @param {typeof WorkerMainThread["workerClass"]} param0.workerClass
25
+ * - example:
26
+ * ```js
27
+ * import { Worker } from 'node:worker_threads';
28
+ * ```
20
29
  * @param {typeof WorkerMainThread["pathValidator"]} param0.pathValidator
30
+ * - example:
21
31
  * ```js
22
- * async(relativePath) => {
23
- * // verify whether relativePath exist, then return the full path
24
- * // use fetch | fs, chained with Paths.instance.root + WorkerMainThread.basePath;
25
- * }
32
+ * async (workerPath, root, base) => {
33
+ * const truePathCheck = `${root}/${base}/${workerPath}`;
34
+ * const res = await fetch(truePathCheck);
35
+ * // might also check wheter it need base or not
36
+ * return await res.ok;
37
+ * }
26
38
  * ```
27
39
  * @param {typeof WorkerMainThread["basePath"]} [param0.basePath]
28
40
  * - additonal realtivePath from rootPath;
29
41
  * - default: '';
30
42
  * @example
43
+ * import { Worker } from 'node:worker_threads';
31
44
  * import { WorkerMainThread } from 'vivth';
32
45
  *
33
46
  * WorkerMainThread.setup({
34
- * workerClass: async () => await (import('worker_threads')).Worker,
47
+ * workerClass: Worker,
35
48
  * basePath: 'public/assets/js/workers',
36
49
  * pathValidator: async (workerPath, root, base) => {
37
50
  * const res = await fetch(`${root}/${base}/${workerPath}`);
@@ -49,9 +62,9 @@ export class WorkerMainThread<WT extends WorkerThread> {
49
62
  * @description
50
63
  * - reference for `Worker` class;
51
64
  * - edit via `setup`;
52
- * @type {()=>Promise<typeof Worker|typeof import('worker_threads').Worker>}
65
+ * @type {typeof Worker|typeof import('worker_threads').Worker}
53
66
  */
54
- static workerClass: () => Promise<typeof Worker | typeof import("worker_threads").Worker>;
67
+ static workerClass: typeof Worker | typeof import("worker_threads").Worker;
55
68
  /**
56
69
  * @description
57
70
  * - reference for worker file `basePath`;
@@ -73,44 +86,38 @@ export class WorkerMainThread<WT extends WorkerThread> {
73
86
  static #options: import("worker_threads").WorkerOptions & {
74
87
  type?: "module";
75
88
  };
89
+ /**
90
+ * @template {WorkerThread} WT
91
+ * @description
92
+ * - create Worker_instance;
93
+ * @param {string} handler
94
+ * @param {Omit<WorkerOptions|import('worker_threads').WorkerOptions, 'eval'|'type'>} [options]
95
+ * @returns {WorkerMainThread<WT>}
96
+ * @example
97
+ * import { WorkerMainThread } from 'vivth';
98
+ *
99
+ * export const myDoubleWorker = WorkerMainThread.newVivthWorker('./doubleWorkerThread.mjs');
100
+ */
101
+ static newVivthWorker: (handler: string, options?: Omit<WorkerOptions | import("worker_threads").WorkerOptions, "eval" | "type">) => WorkerMainThread<WT_1>;
76
102
  /**
77
103
  * @param {string} handler
78
104
  * @param { WorkerOptions
79
105
  * | import('worker_threads').WorkerOptions} options
80
106
  * @param {WorkerMainThread} worker
81
107
  * @param {(any:any)=>void} listener
82
- * @param {boolean} isInline
83
108
  * @returns {Promise<void>}
84
109
  */
85
- static #workerFilehandler: (handler: string, options: WorkerOptions | import("worker_threads").WorkerOptions, worker: WorkerMainThread<any>, listener: (any: any) => void, isInline: boolean) => Promise<void>;
86
- /**
87
- * @type {boolean}
88
- */
89
- static #isBrowser: boolean;
90
- /**
91
- * @description
92
- * - check whether js run in browser
93
- * @type {boolean}
94
- */
95
- static get isBrowser(): boolean;
110
+ static #workerFilehandler: (handler: string, options: WorkerOptions | import("worker_threads").WorkerOptions, worker: WorkerMainThread<any>, listener: (any: any) => void) => Promise<void>;
96
111
  /**
97
- * @description
98
- * - create Worker_instance;
99
- * @param {string} handler
100
- * - if `isInline` === `false`, `handler` should be:
101
- * >- pointing to worker thread file; WHICH
102
- * >- the path must be relative to `projectRoot`;
103
- * - if `isInline` === `true`, `handler` should be
104
- * >- string literal of prebundled worker thread script; OR
105
- * >- manually made string literal of worker thread script;
106
- * @param {Omit<WorkerOptions|import('worker_threads').WorkerOptions, 'eval'|'type'>} [options]
107
- * @param {boolean} [isInline]
112
+ * @private
113
+ * @param {Parameters<typeof WorkerMainThread<WT>["newVivthWorker"]>[0]} handler
114
+ * @param {Parameters<typeof WorkerMainThread<WT>["newVivthWorker"]>[1]} [options]
108
115
  * @example
109
116
  * import { WorkerMainThread } from 'vivth';
110
117
  *
111
- * export const myDoubleWorker = new WorkerMainThread('./doubleWorkerThread.mjs');
118
+ * export const myDoubleWorker = WorkerMainThread.newVivthWorker('./doubleWorkerThread.mjs');
112
119
  */
113
- constructor(handler: string, options?: Omit<WorkerOptions | import("worker_threads").WorkerOptions, "eval" | "type">, isInline?: boolean);
120
+ private constructor();
114
121
  /**
115
122
  * @description
116
123
  * - terminate all signals that are used on this instance;
@@ -120,7 +127,7 @@ export class WorkerMainThread<WT extends WorkerThread> {
120
127
  /**
121
128
  * @description
122
129
  * - result signal of the processed message;
123
- * @type {Derived<WorkerResult<WT["Post"]>>}
130
+ * @type {Derived<WorkerResult<WT["POST"]>>}
124
131
  * @example
125
132
  * import { Effect } from 'vivth';
126
133
  * import { myDoubleWorker } from './myDoubleWorker.mjs';
@@ -131,19 +138,20 @@ export class WorkerMainThread<WT extends WorkerThread> {
131
138
  * // code
132
139
  * })
133
140
  */
134
- receiverSignal: Derived<WorkerResult<WT["Post"]>>;
141
+ receiverSignal: Derived<WorkerResult<WT["POST"]>>;
135
142
  /**
136
143
  * @description
137
144
  * - callback to send message to the worker thread;
138
- * @type {(event: WT["Receive"])=>void}
145
+ * @type {(event: WT["RECEIVE"])=>void}
139
146
  * @example
140
147
  * import { myDoubleWorker } from './myDoubleWorker.mjs';
141
148
  *
142
149
  * myDoubleWorker.postMessage(90);
143
150
  */
144
- postMessage: (event: WT["Receive"]) => void;
151
+ postMessage: (event: WT["RECEIVE"]) => void;
145
152
  #private;
146
153
  }
147
- export type WorkerResult<A> = import("./WorkerResult.mjs").WorkerResult<A>;
154
+ export type WorkerResult<POST> = import("./WorkerResult.mjs").WorkerResult<POST>;
148
155
  export type WorkerThread = import("./WorkerThread.mjs").WorkerThread<any, any>;
156
+ export type unwrapLazy = "vivth:unwrapLazy;";
149
157
  import { Derived } from './Derived.mjs';
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @template POST
3
+ * @typedef {import('./WorkerResult.mjs').WorkerResult<POST>} WorkerResult
4
+ */
5
+ /**
6
+ * @typedef {import('./WorkerThread.mjs').WorkerThread} WorkerThread
7
+ * @typedef {import('../common/lazie.mjs').unwrapLazy} unwrapLazy
8
+ */
9
+ /**
10
+ * @template {WorkerThread} WT
11
+ */
12
+ export class WorkerMainThread<WT extends WorkerThread> {
13
+ /**
14
+ * @type {boolean}
15
+ */
16
+ static #isRegistered: boolean;
17
+ /**
18
+ * @param {Object} param0
19
+ * @param {typeof WorkerMainThread["workerClass"]} param0.workerClass
20
+ * @param {typeof WorkerMainThread["pathValidator"]} param0.pathValidator
21
+ * @param {typeof WorkerMainThread["basePath"]} [param0.basePath]
22
+ */
23
+ static setup: ({ workerClass, pathValidator, basePath }: {
24
+ workerClass: (typeof WorkerMainThread)["workerClass"];
25
+ pathValidator: (typeof WorkerMainThread)["pathValidator"];
26
+ basePath?: (typeof WorkerMainThread)["basePath"];
27
+ }) => void;
28
+ /**
29
+ * @type {typeof Worker|typeof import('worker_threads').Worker}
30
+ */
31
+ static workerClass: typeof Worker | typeof import("worker_threads").Worker;
32
+ /**
33
+ * @type {string}
34
+ */
35
+ static basePath: string;
36
+ /**
37
+ * @type {(paths:{worker: string, root:string, base: string})=>Promise<string>}
38
+ */
39
+ static pathValidator: (paths: {
40
+ worker: string;
41
+ root: string;
42
+ base: string;
43
+ }) => Promise<string>;
44
+ static #options: import("worker_threads").WorkerOptions & {
45
+ type?: "module";
46
+ };
47
+ /**
48
+ * @template {WorkerThread} WT
49
+ * @param {string} handler
50
+ * @param {Omit<WorkerOptions|import('worker_threads').WorkerOptions, 'eval'|'type'>} [options]
51
+ * @returns {WorkerMainThread<WT>}
52
+ */
53
+ static newVivthWorker: (handler: string, options?: Omit<WorkerOptions | import("worker_threads").WorkerOptions, "eval" | "type">) => WorkerMainThread<WT_1>;
54
+ /**
55
+ * @param {string} handler
56
+ * @param { WorkerOptions
57
+ * | import('worker_threads').WorkerOptions} options
58
+ * @param {WorkerMainThread} worker
59
+ * @param {(any:any)=>void} listener
60
+ * @returns {Promise<void>}
61
+ */
62
+ static #workerFilehandler: (handler: string, options: WorkerOptions | import("worker_threads").WorkerOptions, worker: WorkerMainThread<any>, listener: (any: any) => void) => Promise<void>;
63
+ /**
64
+ * @param {Parameters<typeof WorkerMainThread<WT>["newVivthWorker"]>[0]} handler
65
+ * @param {Parameters<typeof WorkerMainThread<WT>["newVivthWorker"]>[1]} [options]
66
+ */
67
+ constructor(handler: Parameters<(typeof WorkerMainThread<WT>)["newVivthWorker"]>[0], options?: Parameters<(typeof WorkerMainThread<WT>)["newVivthWorker"]>[1]);
68
+ /**
69
+ * @type {()=>void}
70
+ */
71
+ terminate: () => void;
72
+ /**
73
+ * @type {Derived<WorkerResult<WT["POST"]>>}
74
+ */
75
+ receiverSignal: Derived<WorkerResult<WT["POST"]>>;
76
+ /**
77
+ * @type {(event: WT["RECEIVE"])=>void}
78
+ */
79
+ postMessage: (event: WT["RECEIVE"]) => void;
80
+ #private;
81
+ }
82
+ export type WorkerResult<POST> = import("./WorkerResult.mjs").WorkerResult<POST>;
83
+ export type WorkerThread = import("./WorkerThread.mjs").WorkerThread<any, any>;
84
+ export type unwrapLazy = "vivth:unwrapLazy;";
85
+ import { Derived } from './Derived.mjs';
@@ -1,34 +1,38 @@
1
1
  /**
2
2
  * @description
3
3
  * - class helper for `WorkerThread` creation;
4
- * @template Receive
5
- * @template Post
4
+ * - before any `Worker` functionaily to be used, you need to setup it with `WorkerThread.setup` and `WorkerMainThread.setup` before runing anytyhing;
5
+ * @template RECEIVE
6
+ * @template POST
6
7
  */
7
- export class WorkerThread<Receive, Post> {
8
+ export class WorkerThread<RECEIVE, POST> {
8
9
  /**
9
- * @type {{parentPort:()=>Promise<any>}}
10
+ * @typedef {import('../types/QCBReturn.mjs').QCBReturn} QCBReturn
10
11
  */
11
- static #parentPortRef: {
12
- parentPort: () => Promise<any>;
13
- };
12
+ /**
13
+ * @type {Parameters<typeof WorkerThread["setup"]>[0]}
14
+ */
15
+ static #refs: Parameters<(typeof WorkerThread)["setup"]>[0];
14
16
  /**
15
17
  * @description
16
18
  * - need to be called and exported as new `WorkerThread` class reference;
17
- * @template Receive_
18
- * @template Post_
19
- * @param {{parentPort:()=>Promise<any>}} parentPortRef
20
- * - correct parentPort reference;
21
- * @returns {typeof WorkerThread<Receive_, Post_>}
19
+ * @template RECEIVE
20
+ * @template POST
21
+ * @param {{parentPort:import('worker_threads')["parentPort"]}} refs
22
+ * -example:
23
+ * ```js
24
+ * import { parentPort } from 'node:worker_threads';
25
+ * ```
26
+ * @returns {typeof WorkerThread<RECEIVE, POST>}
22
27
  * @example
23
28
  * import { WorkerThread } from 'vivth';
29
+ * import { parentPort } from 'node:worker_threads';
24
30
  *
25
- * WorkerThread.setup({ parentPort: async () => await import('node:worker_threads') });
26
- * // that is the default value, if your parentPort/equivalent API is not that;
27
- * // you need to call this method;
31
+ * export const MyWorkerThreadRef = WorkerThread.setup({ parentPort });
28
32
  */
29
- static setup: (parentPortRef: {
30
- parentPort: () => Promise<any>;
31
- }) => typeof WorkerThread<Receive_, Post_>;
33
+ static setup<RECEIVE_1, POST_1>(refs: {
34
+ parentPort: typeof import("worker_threads")["parentPort"];
35
+ }): typeof WorkerThread<RECEIVE_1, POST_1>;
32
36
  /**
33
37
  * @param {any} ev
34
38
  */
@@ -41,30 +45,34 @@ export class WorkerThread<Receive, Post> {
41
45
  * import { MyWorkerThread } from './MyWorkerThread.mjs';
42
46
  *
43
47
  * const doubleWorker = new MyWorkerThread((ev, isLastOnQ) => {
44
- * // if(!isLastOnQ) {
48
+ * // if(!isLastOnQ()) {
45
49
  * // return null; // can be used for imperative debouncing;
46
50
  * // }
51
+ * // await fetch('some/path')
52
+ * // if(!isLastOnQ()) {
53
+ * // return;
54
+ * // }
47
55
  * return ev = ev \* 2;
48
56
  * });
49
57
  */
50
- constructor(handler: (ev: any, isLastOnQ: boolean) => any);
58
+ constructor(handler: (ev: any, isLastOnQ: () => boolean) => any);
51
59
  /**
52
60
  * @description
53
61
  * - type helper;
54
- * @type {(ev: Receive, isLastOnQ:boolean) => Post}
62
+ * @type {(ev: RECEIVE, isLastOnQ:QCBReturn["isLastOnQ"]) => POST}
55
63
  */
56
- handler: (ev: Receive, isLastOnQ: boolean) => Post;
64
+ handler: (ev: RECEIVE, isLastOnQ: () => boolean) => POST;
57
65
  /**
58
66
  * @description
59
67
  * - helper type, hold no actual value;
60
- * @type {Receive}
68
+ * @type {RECEIVE}
61
69
  */
62
- Receive: Receive;
70
+ RECEIVE: RECEIVE;
63
71
  /**
64
72
  * @description
65
73
  * - helper type, hold no actual value;
66
- * @type {Post}
74
+ * @type {POST}
67
75
  */
68
- Post: Post;
76
+ POST: POST;
69
77
  #private;
70
78
  }
@@ -1 +1,22 @@
1
- export function Base64URL(fileString: string, mimeType: string, btoaFunction: (string: string) => string): string;
1
+ /**
2
+ * @description
3
+ * - create inline base64 url;
4
+ * - usage:
5
+ * >- can be extremely usefull to display file on desktop app webview, without exposing http server;
6
+ * @param {string} fileString
7
+ * @param {string} mimeType
8
+ * @param {(string:string)=>string} btoaFunction
9
+ * - check your js runtime `btoa`;
10
+ * - node compatible:
11
+ * ```js
12
+ * (str, prevBufferEncoding) =>
13
+ * Buffer.from(str, prevBufferEncoding).toString('base64')
14
+ * ```
15
+ * @returns {Base64URLString}
16
+ * @example
17
+ * import { Base64URL } from 'vivth'
18
+ * import fileString from './fileString.mjs';
19
+ *
20
+ * Base64URL(fileString, 'application/javascript', btoa);
21
+ */
22
+ export function Base64URL(fileString: string, mimeType: string, btoaFunction: (string: string) => string): Base64URLString;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @description
3
+ * - create inline base64 url;
4
+ * - usage:
5
+ * >- can be extremely usefull to display file on desktop app webview, without exposing http server;
6
+ * >- when using `FSInline`, use [Base64URL](#base64url) instead;
7
+ * @param {string} filePath
8
+ * @returns {Promise<Base64URLString>}
9
+ * @example
10
+ * import { join } from 'node:path'
11
+ *
12
+ * import { Base64URLFromFile, Paths } from 'vivth'
13
+ *
14
+ * await Base64URLFromFile(join(Paths.root, '/path/to/file'));
15
+ */
16
+ export function Base64URLFromFile(filePath: string): Promise<Base64URLString>;
@@ -0,0 +1 @@
1
+ export const keys_vivthGenereteDir: ".vivth";
@@ -14,7 +14,7 @@
14
14
  * >>- `"at"description` are treated as plain `markdown`;
15
15
  * >>- first `"at"${string}` after `"at"description` until `"at"example` will be treated as `javascript` comment block on the `markdown`;
16
16
  * >>- `"at"example` are treated as `javascript` block on the `markdown` file, and should be placed last on the same comment block;
17
- * >>- you can always look at `vivth/src` files to check how the source, and the `README.md` and `index.mjs` is documentation/generation results;
17
+ * >>- you can always look at `vivth/src` files to check how the source, and the `README.md` and `index.mjs` documentation/generation results;
18
18
  */
19
19
  export class JSautoDOC {
20
20
  /**
@@ -36,25 +36,9 @@ export class JSautoDOC {
36
36
  * @param {import('chokidar').ChokidarOptions} [options.option]
37
37
  * - ChokidarOptions;
38
38
  * @example
39
- * import { Console, Setup, JSautoDOC } from 'vivth';
39
+ * import { JSautoDOC } from 'vivth';
40
40
  *
41
- * const { paths, safeExit } = Setup;
42
- *
43
- * new paths({
44
- * root: process?.env?.INIT_CWD ?? process?.cwd(),
45
- * });
46
- *
47
- * new safeExit({
48
- * exitEventNames: ['SIGINT', 'SIGTERM', 'exit'],
49
- * exitCallbackListeners: (eventName) => {
50
- * process.once(eventName, function () {
51
- * safeExit.instance.exiting.correction(true);
52
- * Console.log(`safe exit via "${eventName}"`);
53
- * });
54
- * },
55
- * });
56
- *
57
- * new JSautoDOC({
41
+ * new JSautoDOC({
58
42
  * paths: { dir: 'src', file: 'index.mjs', readMe: 'README.md' },
59
43
  * copyright: 'this library is made and distributed under MIT license;',
60
44
  * tableOfContentTitle: 'list of exported API and typehelpers',