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.
- package/.vivth-temp/README.src.md +35 -0
- package/README.md +1243 -441
- package/README.src.md +5 -2
- package/bun.lock +228 -0
- package/index.mjs +22 -9
- package/package.json +6 -3
- package/src/bundler/CompileJS.mjs +258 -0
- package/src/bundler/CreateESPlugin.mjs +24 -0
- package/src/bundler/EsBundler.mjs +27 -13
- package/src/bundler/FSInline.mjs +57 -0
- package/src/bundler/FSInlineAnalyzer.mjs +197 -0
- package/src/bundler/FSInlineBundled.mjs +34 -0
- package/src/bundler/adds/ToBundledJSPlugin.mjs +77 -0
- package/src/bundler/adds/externals.mjs +8 -0
- package/src/bundler/adds/pluginVivthBundle.mjs +5 -0
- package/src/class/Console.mjs +48 -27
- package/src/class/Derived.mjs +55 -7
- package/src/class/Effect.mjs +100 -39
- package/src/class/EnvSignal.mjs +5 -5
- package/src/class/EventSignal.mjs +55 -5
- package/src/class/FileSafe.mjs +124 -0
- package/src/class/ListDerived.mjs +6 -3
- package/src/class/ListSignal.mjs +11 -11
- package/src/class/LitExp.mjs +405 -0
- package/src/class/Paths.mjs +39 -4
- package/src/class/QChannel.mjs +79 -28
- package/src/class/SafeExit.mjs +31 -11
- package/src/class/Setup.mjs +12 -7
- package/src/class/Signal.mjs +26 -24
- package/src/class/WorkerMainThread.mjs +108 -135
- package/src/class/WorkerMainThreadBundled.mjs +216 -0
- package/src/class/WorkerThread.mjs +40 -31
- package/src/common/Base64URL.mjs +10 -5
- package/src/common/Base64URLFromFile.mjs +24 -0
- package/src/common/keys.mjs +3 -0
- package/src/doc/JSautoDOC.mjs +32 -56
- package/src/doc/parsedFile.mjs +37 -36
- package/src/function/CreateImmutable.mjs +9 -9
- package/src/function/EventCheck.mjs +2 -2
- package/src/function/EventObject.mjs +5 -5
- package/src/function/GetRuntime.mjs +38 -0
- package/src/function/IsAsync.mjs +2 -2
- package/src/function/LazyFactory.mjs +13 -13
- package/src/function/Timeout.mjs +2 -2
- package/src/function/Try.mjs +17 -12
- package/src/function/TryAsync.mjs +5 -5
- package/src/function/TrySync.mjs +5 -5
- package/src/function/TsToMjs.mjs +5 -4
- package/src/types/LitExpKeyType.mjs +5 -0
- package/src/types/QCBReturn.mjs +1 -1
- package/src/types/Runtime.mjs +7 -0
- package/types/dev/fsInline.d.mts +1 -0
- package/types/dev/test.d.mts +1 -0
- package/types/dev/testWorker.d.mts +7 -0
- package/types/dev/testbundle.d.mts +1 -0
- package/types/dev/workerThreadClass.d.mts +13 -0
- package/types/index.d.mts +18 -9
- package/types/src/bundler/CompileJS.d.mts +78 -0
- package/types/src/bundler/CreateESPlugin.d.mts +17 -0
- package/types/src/bundler/EsBundler.d.mts +33 -4
- package/types/src/bundler/FSInline.d.mts +43 -0
- package/types/src/bundler/FSInlineAnalyzer.d.mts +36 -0
- package/types/src/bundler/FSInlineBundled.d.mts +22 -0
- package/types/src/bundler/adds/ToBundledJSPlugin.d.mts +16 -0
- package/types/src/bundler/adds/externals.d.mts +1 -0
- package/types/src/bundler/adds/pluginVivthBundle.d.mts +1 -0
- package/types/src/class/Console.d.mts +36 -5
- package/types/src/class/Derived.d.mts +132 -5
- package/types/src/class/Effect.d.mts +106 -7
- package/types/src/class/EnvSignal.d.mts +8 -8
- package/types/src/class/EventSignal.d.mts +151 -7
- package/types/src/class/FileSafe.d.mts +90 -0
- package/types/src/class/ListDerived.d.mts +8 -5
- package/types/src/class/ListSignal.d.mts +123 -18
- package/types/src/class/LitExp.d.mts +361 -0
- package/types/src/class/Paths.d.mts +30 -4
- package/types/src/class/QChannel.d.mts +69 -22
- package/types/src/class/SafeExit.d.mts +24 -9
- package/types/src/class/Setup.d.mts +13 -10
- package/types/src/class/Signal.d.mts +73 -17
- package/types/src/class/WorkerMainThread.d.mts +47 -39
- package/types/src/class/WorkerMainThreadBundled.d.mts +85 -0
- package/types/src/class/WorkerThread.d.mts +34 -26
- package/types/src/common/Base64URL.d.mts +22 -1
- package/types/src/common/Base64URLFromFile.d.mts +16 -0
- package/types/src/common/keys.d.mts +1 -0
- package/types/src/doc/JSautoDOC.d.mts +3 -19
- package/types/src/doc/parsedFile.d.mts +72 -13
- package/types/src/function/CreateImmutable.d.mts +27 -2
- package/types/src/function/EventCheck.d.mts +15 -0
- package/types/src/function/EventObject.d.mts +17 -2
- package/types/src/function/GetRuntime.d.mts +2 -0
- package/types/src/function/IsAsync.d.mts +18 -0
- package/types/src/function/LazyFactory.d.mts +35 -2
- package/types/src/function/Timeout.d.mts +16 -0
- package/types/src/function/Try.d.mts +52 -1
- package/types/src/function/TryAsync.d.mts +22 -1
- package/types/src/function/TrySync.d.mts +16 -1
- package/types/src/function/TsToMjs.d.mts +19 -0
- package/types/src/types/LitExpKeyType.d.mts +1 -0
- package/types/src/types/QCBReturn.d.mts +1 -1
- package/types/src/types/Runtime.d.mts +1 -0
- package/dev/index.mjs +0 -28
- package/src/bundler/CompileMJS.mjs +0 -110
- package/src/function/WriteFileSafe.mjs +0 -37
- package/types/src/bundler/A.d.mts +0 -1
- package/types/src/bundler/CompileMJS.d.mts +0 -8
- package/types/src/function/WriteFileSafe.d.mts +0 -2
package/README.md
CHANGED
|
@@ -24,11 +24,14 @@ npm i vivth
|
|
|
24
24
|
> - fully typed `worker-threads` abstraction;
|
|
25
25
|
> - ` error as value` function helper;
|
|
26
26
|
> - opionated `autoDocumentation`;
|
|
27
|
-
> - opionated `bundler
|
|
27
|
+
> - opionated `bundler`:
|
|
28
|
+
> > - abstracted via `esbuild`;
|
|
29
|
+
> - opionated `compiler`;
|
|
30
|
+
> > - abstracted via `pkg`, `deno`, and `bun`;
|
|
28
31
|
|
|
29
32
|
## versions:
|
|
30
33
|
|
|
31
|
-
- `1.0.0
|
|
34
|
+
- `1.0.0+:b`:
|
|
32
35
|
> - beta release;
|
|
33
36
|
> - checking edge cases;
|
|
34
37
|
> - stable API, the exposed API access are highly unlikely to changes, only the underlying code
|
|
@@ -36,29 +39,34 @@ npm i vivth
|
|
|
36
39
|
|
|
37
40
|
<h2 id="list-of-exported-api-and-typehelpers">list of exported API and typehelpers</h2>
|
|
38
41
|
|
|
39
|
-
- [
|
|
42
|
+
- [CompileJS](#compilejs)
|
|
43
|
+
- [CreateESPlugin](#createesplugin)
|
|
40
44
|
- [EsBundler](#esbundler)
|
|
45
|
+
- [FSInline](#fsinline)
|
|
46
|
+
- [FSInlineAnalyzer](#fsinlineanalyzer)
|
|
41
47
|
- [Console](#console)
|
|
42
48
|
- [Derived](#derived)
|
|
43
49
|
- [Effect](#effect)
|
|
44
50
|
- [EnvSignal](#envsignal)
|
|
45
51
|
- [EventSignal](#eventsignal)
|
|
52
|
+
- [FileSafe](#filesafe)
|
|
46
53
|
- [ListDerived](#listderived)
|
|
47
54
|
- [ListSignal](#listsignal)
|
|
55
|
+
- [LitExp](#litexp)
|
|
48
56
|
- [Paths](#paths)
|
|
49
57
|
- [QChannel](#qchannel)
|
|
50
58
|
- [SafeExit](#safeexit)
|
|
51
59
|
- [Setup](#setup)
|
|
52
60
|
- [Signal](#signal)
|
|
53
61
|
- [WorkerMainThread](#workermainthread)
|
|
54
|
-
- [WorkerResult](#workerresult)
|
|
55
|
-
- [WorkerThread](#workerthread)
|
|
56
62
|
- [Base64URL](#base64url)
|
|
63
|
+
- [Base64URLFromFile](#base64urlfromfile)
|
|
57
64
|
- [EventNameSpace](#eventnamespace)
|
|
58
65
|
- [JSautoDOC](#jsautodoc)
|
|
59
66
|
- [CreateImmutable](#createimmutable)
|
|
60
67
|
- [EventCheck](#eventcheck)
|
|
61
68
|
- [EventObject](#eventobject)
|
|
69
|
+
- [GetRuntime](#getruntime)
|
|
62
70
|
- [IsAsync](#isasync)
|
|
63
71
|
- [LazyFactory](#lazyfactory)
|
|
64
72
|
- [Timeout](#timeout)
|
|
@@ -66,50 +74,121 @@ npm i vivth
|
|
|
66
74
|
- [TryAsync](#tryasync)
|
|
67
75
|
- [TrySync](#trysync)
|
|
68
76
|
- [TsToMjs](#tstomjs)
|
|
69
|
-
- [WriteFileSafe](#writefilesafe)
|
|
70
77
|
- [AnyButUndefined](#anybutundefined)
|
|
71
|
-
- [QCBReturn](#qcbreturn)
|
|
72
|
-
- [QCBFIFOReturn](#qcbfiforeturn)
|
|
73
|
-
- [MutationType](#mutationtype)
|
|
74
|
-
- [ListArg](#listarg)
|
|
75
|
-
- [IsListSignal](#islistsignal)
|
|
76
78
|
- [ExtnameType](#extnametype)
|
|
79
|
+
- [IsListSignal](#islistsignal)
|
|
80
|
+
- [ListArg](#listarg)
|
|
81
|
+
- [LitExpKeyType](#litexpkeytype)
|
|
82
|
+
- [MutationType](#mutationtype)
|
|
83
|
+
- [QCBFIFOReturn](#qcbfiforeturn)
|
|
84
|
+
- [QCBReturn](#qcbreturn)
|
|
85
|
+
- [Runtime](#runtime)
|
|
86
|
+
- [WorkerResult](#workerresult)
|
|
87
|
+
- [WorkerThread](#workerthread)
|
|
88
|
+
- [ToBundledJSPlugin](#tobundledjsplugin)
|
|
77
89
|
|
|
78
|
-
<h2 id="
|
|
90
|
+
<h2 id="compilejs">CompileJS</h2>
|
|
79
91
|
|
|
80
92
|
|
|
81
|
-
#### reference:`
|
|
82
|
-
- function to
|
|
93
|
+
#### reference:`CompileJS`
|
|
94
|
+
- function to compile `.ts`|`.mts`|`.mjs` file, into a single executable;
|
|
95
|
+
- also generate js representation;
|
|
96
|
+
- uses [pkg](https://www.npmjs.com/package/pkg), [bun](https://bun.com/docs/bundler/executables), and [deno](https://docs.deno.com/runtime/reference/cli/compile/) compiler under the hood;
|
|
97
|
+
>- they are used only as packaging agent, and doesn't necessarily supports their advanced feature, such as, assets bundling(use [FSInline](#fsinline) instead);
|
|
98
|
+
>- `WorkerThread` will be converted to inline using `FSInline` too;
|
|
99
|
+
|
|
100
|
+
!!!WARNING!!!
|
|
101
|
+
!!!WARNING!!!
|
|
102
|
+
!!!WARNING!!!
|
|
103
|
+
|
|
104
|
+
- This function does not obfuscate and will not prevent decompilation. Do not embed environment variables or sensitive information inside `options.entryPoint`;
|
|
105
|
+
- It is designed for quick binarization, allowing execution on machines without `Node.js`, `Bun`, or `Deno` installed;
|
|
106
|
+
- The resulting binary will contain `FSInline` and `WorkerMainThread` target paths Buffers, which are loaded into memory at runtime. If your logic depends on the file system, use `node:fs` or `node:fs/promises` APIs and ship external files alongside the binary (not compiled);
|
|
107
|
+
|
|
108
|
+
!!!WARNING!!!
|
|
109
|
+
!!!WARNING!!!
|
|
110
|
+
!!!WARNING!!!
|
|
83
111
|
|
|
84
112
|
```js
|
|
85
113
|
/**
|
|
86
114
|
* @param {Object} options
|
|
87
115
|
* @param {string} options.entryPoint
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
116
|
+
* - need to be manually prefixed;
|
|
117
|
+
* @param {BufferEncoding} [options.encoding]
|
|
118
|
+
* - write and read encoding for the sources;
|
|
119
|
+
* - default: `utf-8`;
|
|
120
|
+
* @param {boolean} options.minifyFirst
|
|
121
|
+
* - minify the bundle before compilation;
|
|
122
|
+
* @param {string} options.outDir
|
|
123
|
+
* - need manual prefix;
|
|
124
|
+
* @param {'pkg'|'bun'|'deno'} [options.compiler]
|
|
125
|
+
* - default: no comilation, just bundling;
|
|
126
|
+
* - `bun` and `pkg` is checked, if there's bug on `deno`, please report on github for issues;
|
|
127
|
+
* @param {Record<string, string>} [options.compilerArguments]
|
|
128
|
+
* - `key` are to used as `--keyName`;
|
|
129
|
+
* - value are the following value of the key;
|
|
130
|
+
* - no need to add the output/outdir, as it use the `options.outDir`;
|
|
131
|
+
* @return {ReturnType<TryAsync<{compileResult:Promise<any>,
|
|
132
|
+
* commandCalled: string;
|
|
133
|
+
* compiledBinFile: string;
|
|
134
|
+
* bundledJSFile:string
|
|
135
|
+
* }>>}
|
|
96
136
|
*/
|
|
97
137
|
```
|
|
98
138
|
- <i>example</i>:
|
|
99
139
|
```js
|
|
100
|
-
import {
|
|
140
|
+
import { join } from 'node:path';
|
|
101
141
|
|
|
102
|
-
|
|
103
|
-
root: process?.env?.INIT_CWD ?? process?.cwd(),
|
|
104
|
-
});
|
|
142
|
+
import { CompileJS, Paths } from 'vivth';
|
|
105
143
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
144
|
+
const [[resultPkg, errorPkg], [resultBun, errorBun]] = await Promise.all([
|
|
145
|
+
CompileJS({
|
|
146
|
+
entryPoint: join(Paths.root, '/dev'),
|
|
147
|
+
minifyFirst: true,
|
|
148
|
+
outDir: join(Paths.root, '/dev-pkg'),
|
|
149
|
+
compiler: 'pkg',
|
|
150
|
+
compilerArguments: {
|
|
151
|
+
target: ['node18-win-x64'],
|
|
152
|
+
}
|
|
153
|
+
}),
|
|
154
|
+
CompileJS({
|
|
155
|
+
entryPoint: join(Paths.root, '/dev'),
|
|
156
|
+
minifyFirst: true,
|
|
157
|
+
outDir: join(Paths.root, '/dev-pkg'),
|
|
158
|
+
compiler: 'bun',
|
|
159
|
+
compilerArguments: {
|
|
160
|
+
target: ['bun-win-x64'],
|
|
161
|
+
}
|
|
162
|
+
}),
|
|
163
|
+
])
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
168
|
+
|
|
169
|
+
<h2 id="createesplugin">CreateESPlugin</h2>
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
#### reference:`CreateESPlugin`
|
|
173
|
+
- typed esbuild Plugin generator;
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
/**
|
|
177
|
+
* @param {string} name
|
|
178
|
+
* @param {import('esbuild').Plugin["setup"]} setup
|
|
179
|
+
* @returns {import('esbuild').Plugin}
|
|
180
|
+
*/
|
|
181
|
+
```
|
|
182
|
+
- <i>example</i>:
|
|
183
|
+
```js
|
|
184
|
+
import { CreateESPlugin } from 'vivth';
|
|
185
|
+
|
|
186
|
+
export const pluginAddCopyRight = CreateESPlugin(
|
|
187
|
+
'MyCopyrightDeclaration',
|
|
188
|
+
async (build) => {
|
|
189
|
+
// build script;
|
|
190
|
+
}
|
|
191
|
+
);
|
|
113
192
|
|
|
114
193
|
```
|
|
115
194
|
|
|
@@ -127,18 +206,21 @@ npm i vivth
|
|
|
127
206
|
* @param {Object} options
|
|
128
207
|
* @param {string} options.content
|
|
129
208
|
* - the code can also uses composites from the result from multiple readFiles;
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* @param {
|
|
133
|
-
* @param {
|
|
134
|
-
* @
|
|
209
|
+
* @param {string} options.root
|
|
210
|
+
* - use dirname of said fileString path;
|
|
211
|
+
* @param {'.mts'|'.ts'|'.mjs'} options.extension
|
|
212
|
+
* @param {boolean} [options.withBinHeader]
|
|
213
|
+
* @param {Omit<Parameters<build>[0],
|
|
214
|
+
* 'entryPoints'|'bundle'|'write'|'sourcemap'>
|
|
215
|
+
* } [esbuildOptions]
|
|
216
|
+
* @returns {ReturnType<typeof TryAsync<string>>}
|
|
135
217
|
*/
|
|
136
218
|
```
|
|
137
219
|
- <i>example</i>:
|
|
138
220
|
```js
|
|
139
221
|
import { EsBundler } from 'vivth';
|
|
140
222
|
|
|
141
|
-
const bundledString = EsBundler(
|
|
223
|
+
const bundledString = EsBundler(
|
|
142
224
|
{
|
|
143
225
|
content: ``,
|
|
144
226
|
extension: '.mts',
|
|
@@ -152,11 +234,104 @@ npm i vivth
|
|
|
152
234
|
|
|
153
235
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
154
236
|
|
|
237
|
+
<h2 id="fsinline">FSInline</h2>
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
#### reference:`FSInline`
|
|
241
|
+
- class helper to inline file;
|
|
242
|
+
- use only if you are planning to use [CompileJS](#compilejs);
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
#### reference:`FSInline.vivthFSInlineFile`
|
|
246
|
+
- declare entrypoint of file inlining;
|
|
247
|
+
>- on the dev time, it's just regullar `readFile` from `node:fs/promises`;
|
|
248
|
+
>- on the compiled, it will read file from `FSInline.vivthFSInlinelists`
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
/**
|
|
252
|
+
* @param {string} filePathFromProject
|
|
253
|
+
* - doesn't require prefix;
|
|
254
|
+
* @returns {Promise<Buffer<ArrayBuffer>>}
|
|
255
|
+
*/
|
|
256
|
+
```
|
|
257
|
+
- <i>example</i>:
|
|
258
|
+
```js
|
|
259
|
+
import { FSInline } from 'vivth';
|
|
260
|
+
|
|
261
|
+
(await FSInline.vivthFSInlineFile('/assets/text.txt')).toString('utf-8');
|
|
262
|
+
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
#### reference:`FSInline.vivthFSInlineDir`
|
|
266
|
+
- declare entrypoint of file inlining, include all files on `dir` and `subdir` that match the `fileRule`;
|
|
267
|
+
|
|
268
|
+
```js
|
|
269
|
+
/**
|
|
270
|
+
* @param {string} dirPathFromProject
|
|
271
|
+
* - doesn't require prefix;
|
|
272
|
+
* @param {RegExp} fileRule
|
|
273
|
+
* @returns {Promise<typeof FSInline["vivthFSInlineFile"]>}
|
|
274
|
+
*/
|
|
275
|
+
```
|
|
276
|
+
- <i>example</i>:
|
|
277
|
+
```js
|
|
278
|
+
import { FSInline } from 'vivth';
|
|
279
|
+
|
|
280
|
+
export const pngAssets = await FSInline.vivthFSInlineDir('/assets', /.png$/g);
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
#### reference:`FSInline.vivthFSInlinelists`
|
|
285
|
+
- placeholder for FSInline;
|
|
286
|
+
- it's remain publicly accessible so it doesn't mess with regex analyze on bundle;
|
|
287
|
+
- shouldn't be manually accessed;
|
|
288
|
+
>- access via `FSInline.vivthFSInlineFile` or `FSInline.vivthFSInlineDir`;
|
|
289
|
+
|
|
290
|
+
```js
|
|
291
|
+
/**
|
|
292
|
+
* @type {Record<string, Buffer<ArrayBuffer>>}
|
|
293
|
+
*/
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
297
|
+
|
|
298
|
+
<h2 id="fsinlineanalyzer">FSInlineAnalyzer</h2>
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
#### reference:`FSInlineAnalyzer`
|
|
302
|
+
- collections of static method to analyze content for `FSInline`;
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
#### reference:`FSInlineAnalyzer.finalContent`
|
|
306
|
+
- to be used on bundled content;
|
|
307
|
+
|
|
308
|
+
```js
|
|
309
|
+
/**
|
|
310
|
+
* @param {string} content
|
|
311
|
+
* @param {'cjs'|'esm'} format
|
|
312
|
+
* @returns {ReturnType<typeof TryAsync<string>>}
|
|
313
|
+
*/
|
|
314
|
+
```
|
|
315
|
+
- <i>example</i>:
|
|
316
|
+
```js
|
|
317
|
+
import { readFile } from 'node:fs/promises';
|
|
318
|
+
|
|
319
|
+
import { FSInlineAnalyzer } from 'vivth';
|
|
320
|
+
|
|
321
|
+
const [resultFinalContent, errorFinalContent] = await FSInlineAnalyzer.finalContent(
|
|
322
|
+
await readFile('./resultESBunlded.mjs', {encoding: 'utf-8'}),
|
|
323
|
+
'esm'
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
329
|
+
|
|
155
330
|
<h2 id="console">Console</h2>
|
|
156
331
|
|
|
157
332
|
|
|
158
333
|
#### reference:`Console`
|
|
159
|
-
- class with static methods to print to standard console with
|
|
334
|
+
- class with static methods to print to standard console with bare minimum ANSI styles;
|
|
160
335
|
|
|
161
336
|
|
|
162
337
|
#### reference:`Console.log`
|
|
@@ -165,8 +340,17 @@ npm i vivth
|
|
|
165
340
|
```js
|
|
166
341
|
/**
|
|
167
342
|
* @param {any} data
|
|
168
|
-
* @returns {void}
|
|
343
|
+
* @returns {void}
|
|
169
344
|
*/
|
|
345
|
+
```
|
|
346
|
+
- <i>example</i>:
|
|
347
|
+
```js
|
|
348
|
+
import { Console } from 'vivth';
|
|
349
|
+
|
|
350
|
+
Console.log({
|
|
351
|
+
hello: 'world!!',
|
|
352
|
+
});
|
|
353
|
+
|
|
170
354
|
```
|
|
171
355
|
|
|
172
356
|
#### reference:`Console.info`
|
|
@@ -175,8 +359,17 @@ npm i vivth
|
|
|
175
359
|
```js
|
|
176
360
|
/**
|
|
177
361
|
* @param {any} data
|
|
178
|
-
* @returns {void}
|
|
362
|
+
* @returns {void}
|
|
179
363
|
*/
|
|
364
|
+
```
|
|
365
|
+
- <i>example</i>:
|
|
366
|
+
```js
|
|
367
|
+
import { Console } from 'vivth';
|
|
368
|
+
|
|
369
|
+
Console.info({
|
|
370
|
+
hello: 'world!!',
|
|
371
|
+
});
|
|
372
|
+
|
|
180
373
|
```
|
|
181
374
|
|
|
182
375
|
#### reference:`Console.warn`
|
|
@@ -185,8 +378,17 @@ npm i vivth
|
|
|
185
378
|
```js
|
|
186
379
|
/**
|
|
187
380
|
* @param {any} data
|
|
188
|
-
* @returns {void}
|
|
381
|
+
* @returns {void}
|
|
189
382
|
*/
|
|
383
|
+
```
|
|
384
|
+
- <i>example</i>:
|
|
385
|
+
```js
|
|
386
|
+
import { Console } from 'vivth';
|
|
387
|
+
|
|
388
|
+
Console.warn({
|
|
389
|
+
hello: 'world!!',
|
|
390
|
+
});
|
|
391
|
+
|
|
190
392
|
```
|
|
191
393
|
|
|
192
394
|
#### reference:`Console.error`
|
|
@@ -195,8 +397,17 @@ npm i vivth
|
|
|
195
397
|
```js
|
|
196
398
|
/**
|
|
197
399
|
* @param {any} data
|
|
198
|
-
* @returns {void}
|
|
400
|
+
* @returns {void}
|
|
199
401
|
*/
|
|
402
|
+
```
|
|
403
|
+
- <i>example</i>:
|
|
404
|
+
```js
|
|
405
|
+
import { Console } from 'vivth';
|
|
406
|
+
|
|
407
|
+
Console.error({
|
|
408
|
+
hello: 'world!!',
|
|
409
|
+
});
|
|
410
|
+
|
|
200
411
|
```
|
|
201
412
|
|
|
202
413
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
@@ -209,8 +420,8 @@ npm i vivth
|
|
|
209
420
|
|
|
210
421
|
```js
|
|
211
422
|
/**
|
|
212
|
-
* @template
|
|
213
|
-
* @extends Signal<
|
|
423
|
+
* @template VALUE
|
|
424
|
+
* @extends Signal<VALUE>
|
|
214
425
|
*/
|
|
215
426
|
```
|
|
216
427
|
|
|
@@ -219,7 +430,9 @@ npm i vivth
|
|
|
219
430
|
|
|
220
431
|
```js
|
|
221
432
|
/**
|
|
222
|
-
* @param {(effectInstanceOptions:Effect["options"]
|
|
433
|
+
* @param {(effectInstanceOptions:Omit<Effect["options"] &
|
|
434
|
+
* Derived["options"], unwrapLazy>) =>
|
|
435
|
+
* Promise<VALUE>} derivedFunction
|
|
223
436
|
*/
|
|
224
437
|
```
|
|
225
438
|
- <i>example</i>:
|
|
@@ -241,13 +454,50 @@ npm i vivth
|
|
|
241
454
|
|
|
242
455
|
```
|
|
243
456
|
|
|
457
|
+
#### reference:`Derived_instance.options`
|
|
458
|
+
- additional helper to be accessed on effect;
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
#### reference:`Derived_instance.options.dontUpdate`
|
|
462
|
+
- return this value tandem with `isLastCalled`, to not to update the value of this instance, even when returning early;
|
|
463
|
+
|
|
464
|
+
```js
|
|
465
|
+
/**
|
|
466
|
+
* @type {Object}
|
|
467
|
+
*/
|
|
468
|
+
```
|
|
469
|
+
- <i>example</i>:
|
|
470
|
+
```js
|
|
471
|
+
import { Signal, Derived } from 'vivth';
|
|
472
|
+
|
|
473
|
+
const count = new Signal(0);
|
|
474
|
+
const double = new Derived(async({
|
|
475
|
+
subscribe,
|
|
476
|
+
dontUpdate,
|
|
477
|
+
isLastCalled,
|
|
478
|
+
}) => {
|
|
479
|
+
const currentValue = subscribe(count).value;
|
|
480
|
+
if (!(await isLastCalled(10))) {
|
|
481
|
+
return dontUpdate;
|
|
482
|
+
}
|
|
483
|
+
const res = await fetch(`some/path/${curentValue.toString()}`);
|
|
484
|
+
if (!(await isLastCalled())) {
|
|
485
|
+
return dontUpdate; // this will prevent race condition, even if the earlier fetch return late;
|
|
486
|
+
}
|
|
487
|
+
return res;
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
count.value++;
|
|
491
|
+
|
|
492
|
+
```
|
|
493
|
+
|
|
244
494
|
#### reference:`Derived_instance.value:getter`
|
|
245
495
|
- the most recent value of the instance
|
|
246
496
|
- can be turn into reactive with Effect or Derived instantiation;
|
|
247
497
|
|
|
248
498
|
```js
|
|
249
499
|
/**
|
|
250
|
-
* @
|
|
500
|
+
* @returns {VALUE}
|
|
251
501
|
*/
|
|
252
502
|
```
|
|
253
503
|
|
|
@@ -258,7 +508,7 @@ npm i vivth
|
|
|
258
508
|
```js
|
|
259
509
|
/**
|
|
260
510
|
* @private
|
|
261
|
-
* @type {
|
|
511
|
+
* @type {VALUE}
|
|
262
512
|
*/
|
|
263
513
|
```
|
|
264
514
|
|
|
@@ -276,29 +526,37 @@ npm i vivth
|
|
|
276
526
|
>- now can dynamically subscribes to signal, even on conditionals, that are not run during first run;
|
|
277
527
|
|
|
278
528
|
|
|
279
|
-
#### reference:`
|
|
529
|
+
#### reference:`Effect_instance.options`
|
|
530
|
+
- collections of lazy methods to handle effect calls of this instance;
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
#### reference:`Effect_instance.options.isLastCalled:getter`
|
|
280
534
|
|
|
281
535
|
|
|
282
536
|
```js
|
|
283
537
|
/**
|
|
284
|
-
* @
|
|
538
|
+
* @returns {(timeoutMS?:number)=>Promise<boolean>}
|
|
539
|
+
* - timeoutMS only necessary if the operation doesn't naturally await;
|
|
540
|
+
* - if it's operation such as `fetch`, you can just leave it blank;
|
|
285
541
|
*/
|
|
286
542
|
```
|
|
287
543
|
- <i>example</i>:
|
|
288
|
-
```js
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
544
|
+
```js
|
|
545
|
+
|
|
546
|
+
import { Effect } from 'vivth';
|
|
547
|
+
|
|
548
|
+
const effect = new Effect(async ({ isLastCalled }) => {
|
|
549
|
+
if (!(await isLastCalled(100))) {
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
// OR
|
|
553
|
+
const res = await fetch('some/path');
|
|
554
|
+
if (!(await isLastCalled(
|
|
555
|
+
// no need to add timeoutMS argument, as fetch are naturally add delay;
|
|
556
|
+
))) {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
})
|
|
302
560
|
|
|
303
561
|
```
|
|
304
562
|
|
|
@@ -307,16 +565,18 @@ npm i vivth
|
|
|
307
565
|
|
|
308
566
|
```js
|
|
309
567
|
/**
|
|
310
|
-
* @template {Signal}
|
|
311
|
-
* @param {
|
|
312
|
-
* @returns {
|
|
568
|
+
* @template {Signal} SIGNAL
|
|
569
|
+
* @param {SIGNAL} signal
|
|
570
|
+
* @returns {SIGNAL}
|
|
313
571
|
*/
|
|
314
572
|
```
|
|
315
573
|
- <i>example</i>:
|
|
316
|
-
```js
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
574
|
+
```js
|
|
575
|
+
import { Effect } from 'vivth';
|
|
576
|
+
|
|
577
|
+
const effect = new Effect(async () => {
|
|
578
|
+
// code
|
|
579
|
+
})
|
|
320
580
|
effect.options.subscribe(signalInstance);
|
|
321
581
|
|
|
322
582
|
```
|
|
@@ -326,18 +586,50 @@ npm i vivth
|
|
|
326
586
|
|
|
327
587
|
```js
|
|
328
588
|
/**
|
|
329
|
-
* @type {()=>void}
|
|
589
|
+
* @type {()=>void}
|
|
330
590
|
*/
|
|
331
591
|
```
|
|
332
592
|
- <i>example</i>:
|
|
333
|
-
```js
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
593
|
+
```js
|
|
594
|
+
import { Effect } from 'vivth';
|
|
595
|
+
|
|
596
|
+
const effect = new Effect(async () => {
|
|
597
|
+
// code
|
|
598
|
+
})
|
|
337
599
|
effect.options.removeEffect();
|
|
338
600
|
|
|
339
601
|
```
|
|
340
602
|
|
|
603
|
+
#### reference:`new Effect`
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
```js
|
|
607
|
+
/**
|
|
608
|
+
* @param {( arg0:
|
|
609
|
+
* Omit<Effect["options"], typeof unwrapLazy>
|
|
610
|
+
* ) =>
|
|
611
|
+
* Promise<void>} effect
|
|
612
|
+
*/
|
|
613
|
+
```
|
|
614
|
+
- <i>example</i>:
|
|
615
|
+
```js
|
|
616
|
+
import { Signal, Derived, Effect, Console } from 'vivth';
|
|
617
|
+
|
|
618
|
+
const count = new Signal(0);
|
|
619
|
+
const double = new Derived( async({$}) => $(count).value * 2); // double listen to count changes
|
|
620
|
+
new Effect(async ({
|
|
621
|
+
subscribe, // : registrar callback for this effect instance, immediately return the signal instance
|
|
622
|
+
removeEffect, // : disable this effect instance from reacting to dependency changes;
|
|
623
|
+
isLastCalled, // : check whether this callback run is this instant last called effect;
|
|
624
|
+
}) => {
|
|
625
|
+
Console.log(subscribe(double).value); // effect listen to double changes
|
|
626
|
+
const a = double.value; // no need to wrap double twice with $
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
count.value++;
|
|
630
|
+
|
|
631
|
+
```
|
|
632
|
+
|
|
341
633
|
#### reference:`Effect_instance.run`
|
|
342
634
|
- normally is to let to be automatically run when dependency signals changes, however it's also accessible as instance method;
|
|
343
635
|
|
|
@@ -348,6 +640,8 @@ npm i vivth
|
|
|
348
640
|
```
|
|
349
641
|
- <i>example</i>:
|
|
350
642
|
```js
|
|
643
|
+
import { Effect } from 'vivth';
|
|
644
|
+
|
|
351
645
|
const effect = new Effect(async ()=>{
|
|
352
646
|
// code
|
|
353
647
|
})
|
|
@@ -366,7 +660,7 @@ npm i vivth
|
|
|
366
660
|
|
|
367
661
|
```js
|
|
368
662
|
/**
|
|
369
|
-
* @template
|
|
663
|
+
* @template VALUE
|
|
370
664
|
*/
|
|
371
665
|
```
|
|
372
666
|
|
|
@@ -375,7 +669,7 @@ npm i vivth
|
|
|
375
669
|
|
|
376
670
|
```js
|
|
377
671
|
/**
|
|
378
|
-
* @param {
|
|
672
|
+
* @param {VALUE} initialValue
|
|
379
673
|
*/
|
|
380
674
|
```
|
|
381
675
|
|
|
@@ -384,7 +678,7 @@ npm i vivth
|
|
|
384
678
|
|
|
385
679
|
```js
|
|
386
680
|
/**
|
|
387
|
-
* @type {Derived<
|
|
681
|
+
* @type {Derived<VALUE>}
|
|
388
682
|
*/
|
|
389
683
|
```
|
|
390
684
|
- <i>example</i>:
|
|
@@ -405,7 +699,7 @@ npm i vivth
|
|
|
405
699
|
|
|
406
700
|
```js
|
|
407
701
|
/**
|
|
408
|
-
* @param {
|
|
702
|
+
* @param {VALUE} correctedValue
|
|
409
703
|
* @returns {void}
|
|
410
704
|
*/
|
|
411
705
|
```
|
|
@@ -428,11 +722,12 @@ npm i vivth
|
|
|
428
722
|
|
|
429
723
|
|
|
430
724
|
#### reference:`EventSignal`
|
|
431
|
-
- Signal implementation for `CustomEvent`, to dispatch and listen;
|
|
725
|
+
- Signal implementation for `CustomEvent`, to dispatch and listen;
|
|
726
|
+
- it's based on string as key, so it can be listened/dispatched even without direct instance reference;
|
|
432
727
|
|
|
433
728
|
```js
|
|
434
729
|
/**
|
|
435
|
-
* @template {IsListSignal}
|
|
730
|
+
* @template {IsListSignal} ISLIST
|
|
436
731
|
* - boolean;
|
|
437
732
|
*/
|
|
438
733
|
```
|
|
@@ -473,8 +768,22 @@ npm i vivth
|
|
|
473
768
|
|
|
474
769
|
```js
|
|
475
770
|
/**
|
|
476
|
-
* @type {Signal|ListSignal}
|
|
771
|
+
* @type {Signal|ListSignal}
|
|
477
772
|
*/
|
|
773
|
+
```
|
|
774
|
+
- <i>example</i>:
|
|
775
|
+
```js
|
|
776
|
+
import { EventSignal, Effect, Console } from 'vivth';
|
|
777
|
+
|
|
778
|
+
const myEventSignal = await EventSignal.get('dataEvent', false);
|
|
779
|
+
|
|
780
|
+
new Effect(({ subscribe })=>{
|
|
781
|
+
const listenValue = subscribe(myEventSignal.dispatch).value;
|
|
782
|
+
// dispatch can be used as two way communication;
|
|
783
|
+
Console.log({ listenValue });
|
|
784
|
+
})
|
|
785
|
+
myEventSignal.dispatch.value = 'hey';
|
|
786
|
+
|
|
478
787
|
```
|
|
479
788
|
|
|
480
789
|
#### reference:`EventSignal_instance.listen`
|
|
@@ -484,8 +793,22 @@ npm i vivth
|
|
|
484
793
|
|
|
485
794
|
```js
|
|
486
795
|
/**
|
|
487
|
-
* @type {Derived|ListDerived}
|
|
796
|
+
* @type {Derived|ListDerived}
|
|
488
797
|
*/
|
|
798
|
+
```
|
|
799
|
+
- <i>example</i>:
|
|
800
|
+
```js
|
|
801
|
+
import { EventSignal, Effect, Console } from 'vivth';
|
|
802
|
+
|
|
803
|
+
const myEventSignal = await EventSignal.get('dataEvent', false);
|
|
804
|
+
|
|
805
|
+
new Effect(({ subscribe })=>{
|
|
806
|
+
const listenValue = subscribe(myEventSignal.listen).value;
|
|
807
|
+
// listen can be used only as listener for one way communication;
|
|
808
|
+
Console.log({ listenValue });
|
|
809
|
+
})
|
|
810
|
+
myEventSignal.dispatch.value = 'hey';
|
|
811
|
+
|
|
489
812
|
```
|
|
490
813
|
|
|
491
814
|
#### reference:`EventSignal.remove`
|
|
@@ -507,7 +830,7 @@ npm i vivth
|
|
|
507
830
|
```js
|
|
508
831
|
import { EventSignal } from 'vivth';
|
|
509
832
|
|
|
510
|
-
EventSignal.remove.subscriber('yourEventSignalName',
|
|
833
|
+
EventSignal.remove.subscriber('yourEventSignalName', myEffectInstance);
|
|
511
834
|
|
|
512
835
|
```
|
|
513
836
|
|
|
@@ -556,7 +879,16 @@ npm i vivth
|
|
|
556
879
|
```
|
|
557
880
|
- <i>example</i>:
|
|
558
881
|
```js
|
|
559
|
-
|
|
882
|
+
import { EventSignal, Effect, Console } from 'vivth';
|
|
883
|
+
|
|
884
|
+
const myEventSignal = await EventSignal.get('dataEvent', false);
|
|
885
|
+
|
|
886
|
+
const myEffectInstance = new Effect(({ subscribe })=>{
|
|
887
|
+
const listenValue = subscribe(myEventSignal.dispatch).value;
|
|
888
|
+
Console.log({ listenValue });
|
|
889
|
+
})
|
|
890
|
+
myEventSignal.dispatch.value = 'hey';
|
|
891
|
+
eventSignal_instance.remove.subscriber(myEffectInstance);
|
|
560
892
|
|
|
561
893
|
```
|
|
562
894
|
|
|
@@ -570,6 +902,15 @@ npm i vivth
|
|
|
570
902
|
```
|
|
571
903
|
- <i>example</i>:
|
|
572
904
|
```js
|
|
905
|
+
import { EventSignal, Effect, Console } from 'vivth';
|
|
906
|
+
|
|
907
|
+
const myEventSignal = await EventSignal.get('dataEvent', false);
|
|
908
|
+
|
|
909
|
+
const myEffectInstance = new Effect(({ subscribe })=>{
|
|
910
|
+
const listenValue = subscribe(myEventSignal.dispatch).value;
|
|
911
|
+
Console.log({ listenValue });
|
|
912
|
+
})
|
|
913
|
+
myEventSignal.dispatch.value = 'hey';
|
|
573
914
|
eventSignal_instance.remove.allSubscribers();
|
|
574
915
|
|
|
575
916
|
```
|
|
@@ -584,12 +925,135 @@ npm i vivth
|
|
|
584
925
|
```
|
|
585
926
|
- <i>example</i>:
|
|
586
927
|
```js
|
|
928
|
+
import { EventSignal, Effect, Console } from 'vivth';
|
|
929
|
+
|
|
930
|
+
const myEventSignal = await EventSignal.get('dataEvent', false);
|
|
931
|
+
|
|
932
|
+
const myEffectInstance = new Effect(({ subscribe })=>{
|
|
933
|
+
const listenValue = subscribe(myEventSignal.dispatch).value;
|
|
934
|
+
Console.log({ listenValue });
|
|
935
|
+
});
|
|
936
|
+
|
|
587
937
|
eventSignal_instance.remove.ref();
|
|
588
938
|
|
|
589
939
|
```
|
|
590
940
|
|
|
591
941
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
592
942
|
|
|
943
|
+
<h2 id="filesafe">FileSafe</h2>
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
#### reference:`FileSafe`
|
|
947
|
+
- collection of static methods of file access with added safety to mkdir before proceeding;
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
#### reference:`FileSafe.write`
|
|
951
|
+
- method to create file safely by recursively mkdir the dirname of the outFile;
|
|
952
|
+
- also returning promise of result & error as value;
|
|
953
|
+
|
|
954
|
+
```js
|
|
955
|
+
/**
|
|
956
|
+
* @param {Parameters<writeFile>[0]} outFile
|
|
957
|
+
* @param {Parameters<writeFile>[1]} content
|
|
958
|
+
* @param {Parameters<writeFile>[2]} [options]
|
|
959
|
+
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
960
|
+
*/
|
|
961
|
+
```
|
|
962
|
+
- <i>example</i>:
|
|
963
|
+
```js
|
|
964
|
+
import { join } from 'node:path';
|
|
965
|
+
import { FileSafe, Paths } from 'vivth';
|
|
966
|
+
|
|
967
|
+
const [, errorWrite] = await FileSafe.write(
|
|
968
|
+
join(Paths.root, '/some/path.mjs'),
|
|
969
|
+
`console.log("hello-world!!");`,
|
|
970
|
+
{ encoding: 'utf-8' }
|
|
971
|
+
);
|
|
972
|
+
|
|
973
|
+
```
|
|
974
|
+
|
|
975
|
+
#### reference:`FileSafe.copy`
|
|
976
|
+
- method to copy file/dir safely by recursively mkdir the dirname of the dest;
|
|
977
|
+
- also returning promise of result & error as value;
|
|
978
|
+
|
|
979
|
+
```js
|
|
980
|
+
/**
|
|
981
|
+
* @param {Parameters<typeof copyFile>[0]} sourceFile
|
|
982
|
+
* @param {Parameters<typeof copyFile>[1]} destinationFile
|
|
983
|
+
* @param {Parameters<typeof copyFile>[2]} mode
|
|
984
|
+
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
985
|
+
*/
|
|
986
|
+
```
|
|
987
|
+
- <i>example</i>:
|
|
988
|
+
```js
|
|
989
|
+
import { join } from 'node:path';
|
|
990
|
+
import { FileSafe, Paths } from 'vivth';
|
|
991
|
+
|
|
992
|
+
const [, errorWrite] = await FileSafe.copy(
|
|
993
|
+
join(Paths.root, '/some/path.mjs'),
|
|
994
|
+
join(Paths.root, '/other/path.copy.mjs'),
|
|
995
|
+
{ encoding: 'utf-8' }
|
|
996
|
+
);
|
|
997
|
+
|
|
998
|
+
```
|
|
999
|
+
|
|
1000
|
+
#### reference:`FileSafe.rename`
|
|
1001
|
+
- method to rename file/dir safely by recursively mkdir the dirname of the dest;
|
|
1002
|
+
- also returning promise of result & error as value;
|
|
1003
|
+
|
|
1004
|
+
```js
|
|
1005
|
+
/**
|
|
1006
|
+
* @param {Parameters<typeof rename>[0]} oldPath
|
|
1007
|
+
* @param {Parameters<typeof rename>[0]} newPath
|
|
1008
|
+
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
1009
|
+
*/
|
|
1010
|
+
```
|
|
1011
|
+
- <i>example</i>:
|
|
1012
|
+
```js
|
|
1013
|
+
import { join } from 'node:path';
|
|
1014
|
+
import { FileSafe, Paths } from 'vivth';
|
|
1015
|
+
|
|
1016
|
+
const [, errorRename] = await FileSafe.rename(
|
|
1017
|
+
join(Paths.root, 'some/path'),
|
|
1018
|
+
join(Paths.root, 'other/path'),
|
|
1019
|
+
);
|
|
1020
|
+
|
|
1021
|
+
```
|
|
1022
|
+
|
|
1023
|
+
#### reference:`FileSafe.rm`
|
|
1024
|
+
- function to remove dir and file;
|
|
1025
|
+
- also returning promise of result & error as value;
|
|
1026
|
+
|
|
1027
|
+
```js
|
|
1028
|
+
/**
|
|
1029
|
+
* @param {Parameters<rm>[0]} path
|
|
1030
|
+
* @param {Parameters<rm>[1]} [rmOptions]
|
|
1031
|
+
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
1032
|
+
*/
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
#### reference:`FileSafe.mkdir`
|
|
1036
|
+
- create directory recursively;
|
|
1037
|
+
- also returning promise of result & error as value;
|
|
1038
|
+
|
|
1039
|
+
```js
|
|
1040
|
+
/**
|
|
1041
|
+
* @param {Parameters<mkdir>[0]} outDir
|
|
1042
|
+
* - absolute path
|
|
1043
|
+
* @returns {ReturnType<typeof TryAsync<string>>}
|
|
1044
|
+
*/
|
|
1045
|
+
```
|
|
1046
|
+
- <i>example</i>:
|
|
1047
|
+
```js
|
|
1048
|
+
import { join } from 'node:path';
|
|
1049
|
+
import { FileSafe, Paths } from 'vivth';
|
|
1050
|
+
|
|
1051
|
+
const [str, errorMkDir] = await FileSafe.mkdir(join(Paths.root, '/some/path/example'));
|
|
1052
|
+
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
1056
|
+
|
|
593
1057
|
<h2 id="listderived">ListDerived</h2>
|
|
594
1058
|
|
|
595
1059
|
|
|
@@ -600,8 +1064,8 @@ npm i vivth
|
|
|
600
1064
|
|
|
601
1065
|
```js
|
|
602
1066
|
/**
|
|
603
|
-
* @template {ListArg}
|
|
604
|
-
* @extends {Derived<
|
|
1067
|
+
* @template {ListArg} LISTARG
|
|
1068
|
+
* @extends {Derived<LISTARG[]>}
|
|
605
1069
|
*/
|
|
606
1070
|
```
|
|
607
1071
|
|
|
@@ -610,7 +1074,9 @@ npm i vivth
|
|
|
610
1074
|
|
|
611
1075
|
```js
|
|
612
1076
|
/**
|
|
613
|
-
* @param {(effectInstanceOptions:Effect["options"]
|
|
1077
|
+
* @param {(effectInstanceOptions:Omit<Effect["options"],
|
|
1078
|
+
* unwrapLazy>)=>
|
|
1079
|
+
* Promise<LISTARG[]>} derivedFunction
|
|
614
1080
|
*/
|
|
615
1081
|
```
|
|
616
1082
|
- <i>example</i>:
|
|
@@ -642,8 +1108,8 @@ npm i vivth
|
|
|
642
1108
|
|
|
643
1109
|
```js
|
|
644
1110
|
/**
|
|
645
|
-
* @template {import('../types/ListArg.mjs').ListArg}
|
|
646
|
-
* @extends {Signal<
|
|
1111
|
+
* @template {import('../types/ListArg.mjs').ListArg} LISTARG
|
|
1112
|
+
* @extends {Signal<LISTARG[]>}
|
|
647
1113
|
*/
|
|
648
1114
|
```
|
|
649
1115
|
|
|
@@ -663,7 +1129,7 @@ npm i vivth
|
|
|
663
1129
|
|
|
664
1130
|
```js
|
|
665
1131
|
/**
|
|
666
|
-
* @param {
|
|
1132
|
+
* @param {LISTARG[]} [value]
|
|
667
1133
|
*/
|
|
668
1134
|
```
|
|
669
1135
|
- <i>example</i>:
|
|
@@ -682,7 +1148,7 @@ npm i vivth
|
|
|
682
1148
|
|
|
683
1149
|
```js
|
|
684
1150
|
/**
|
|
685
|
-
* @
|
|
1151
|
+
* @returns {LISTARG[]}
|
|
686
1152
|
*/
|
|
687
1153
|
```
|
|
688
1154
|
|
|
@@ -692,7 +1158,7 @@ npm i vivth
|
|
|
692
1158
|
```js
|
|
693
1159
|
/**
|
|
694
1160
|
* @private
|
|
695
|
-
* @type {
|
|
1161
|
+
* @type {LISTARG[]}
|
|
696
1162
|
*/
|
|
697
1163
|
```
|
|
698
1164
|
|
|
@@ -707,7 +1173,7 @@ npm i vivth
|
|
|
707
1173
|
|
|
708
1174
|
```js
|
|
709
1175
|
/**
|
|
710
|
-
* @
|
|
1176
|
+
* @returns {Array<LISTARG>}
|
|
711
1177
|
*/
|
|
712
1178
|
```
|
|
713
1179
|
|
|
@@ -716,7 +1182,7 @@ npm i vivth
|
|
|
716
1182
|
|
|
717
1183
|
```js
|
|
718
1184
|
/**
|
|
719
|
-
* @param {...
|
|
1185
|
+
* @param {...LISTARG} listArg
|
|
720
1186
|
* @returns {void}
|
|
721
1187
|
*/
|
|
722
1188
|
```
|
|
@@ -735,7 +1201,7 @@ npm i vivth
|
|
|
735
1201
|
|
|
736
1202
|
```js
|
|
737
1203
|
/**
|
|
738
|
-
* @param {...
|
|
1204
|
+
* @param {...LISTARG} listArg
|
|
739
1205
|
* @returns {void}
|
|
740
1206
|
*/
|
|
741
1207
|
```
|
|
@@ -758,7 +1224,7 @@ npm i vivth
|
|
|
758
1224
|
|
|
759
1225
|
```js
|
|
760
1226
|
/**
|
|
761
|
-
* @param {
|
|
1227
|
+
* @param {LISTARG[]} listArgs
|
|
762
1228
|
* - new array in place of the deleted array.
|
|
763
1229
|
* @returns {void}
|
|
764
1230
|
*/
|
|
@@ -773,7 +1239,7 @@ npm i vivth
|
|
|
773
1239
|
* - The zero-based location in the data from which to start removing elements.
|
|
774
1240
|
* @param {number} deleteCount
|
|
775
1241
|
* -The number of elements to remove.
|
|
776
|
-
* @param {...
|
|
1242
|
+
* @param {...LISTARG} listArg
|
|
777
1243
|
* - new data in place of the deleted data.
|
|
778
1244
|
* @returns {void}
|
|
779
1245
|
*/
|
|
@@ -796,37 +1262,244 @@ npm i vivth
|
|
|
796
1262
|
```js
|
|
797
1263
|
/**
|
|
798
1264
|
* @param {number} index
|
|
799
|
-
* @param {Partial<
|
|
1265
|
+
* @param {Partial<LISTARG>} listArg
|
|
1266
|
+
* @returns {void}
|
|
1267
|
+
*/
|
|
1268
|
+
```
|
|
1269
|
+
|
|
1270
|
+
#### reference:`ListSignal_instance.arrayMethods.remove`
|
|
1271
|
+
- remove `List` element at specific index;
|
|
1272
|
+
|
|
1273
|
+
```js
|
|
1274
|
+
/**
|
|
1275
|
+
* @param {number} index
|
|
1276
|
+
* @returns {void}
|
|
1277
|
+
*/
|
|
1278
|
+
```
|
|
1279
|
+
|
|
1280
|
+
#### reference:`ListSignal_instance.arrayMethods.reverse`
|
|
1281
|
+
- reverses the elements in an `List` in place.
|
|
1282
|
+
|
|
1283
|
+
```js
|
|
1284
|
+
/**
|
|
1285
|
+
* @returns {void}
|
|
1286
|
+
*/
|
|
1287
|
+
```
|
|
1288
|
+
|
|
1289
|
+
#### reference:`ListSignal_instance.arrayMethods.pop`
|
|
1290
|
+
- removes the last element;
|
|
1291
|
+
|
|
1292
|
+
```js
|
|
1293
|
+
/**
|
|
800
1294
|
* @returns {void}
|
|
801
1295
|
*/
|
|
802
1296
|
```
|
|
803
1297
|
|
|
804
|
-
|
|
805
|
-
|
|
1298
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
1299
|
+
|
|
1300
|
+
<h2 id="litexp">LitExp</h2>
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
#### reference:`LitExp`
|
|
1304
|
+
- class helper to created opionated regex helper;
|
|
1305
|
+
- named capture uses `es6+` feature, you might need to add polyfill to target extremely old browser;
|
|
1306
|
+
- class name refer to `Literal Expression`;
|
|
1307
|
+
- please be patient when using this class;
|
|
1308
|
+
>- destructuring is meant for extensive typehelper;
|
|
1309
|
+
>- and destructuring can lead to unhandled error here and there;
|
|
1310
|
+
>- therefore error as value is introduced to help to prevent error on runtime;
|
|
1311
|
+
|
|
1312
|
+
```js
|
|
1313
|
+
/**
|
|
1314
|
+
* @template {LitExpKeyType} KEYS
|
|
1315
|
+
*/
|
|
1316
|
+
```
|
|
1317
|
+
|
|
1318
|
+
#### reference:`LitExp.prepare`
|
|
1319
|
+
- constructor helper;
|
|
1320
|
+
- under the hood it is an abstraction of `RegExp`, with more template literal touch;
|
|
1321
|
+
>- you can apply inline `RegExp` features on the string template literal(as constructor RegExp arg0);
|
|
1322
|
+
>>- by doing so you are opting in to make:
|
|
1323
|
+
>>>- your regex detection more robust; but
|
|
1324
|
+
>>>- `litExp_instance.make.string` to be `unusable`;
|
|
1325
|
+
>>- also mind the needs of escape for special characters;
|
|
1326
|
+
|
|
1327
|
+
```js
|
|
1328
|
+
/**
|
|
1329
|
+
* @template {LitExpKeyType} KEYS
|
|
1330
|
+
* @param {KEYS} keysAndDefaultValuePair
|
|
1331
|
+
* - keys and whether to override regex detection;
|
|
1332
|
+
* >- example:
|
|
1333
|
+
* ```js
|
|
1334
|
+
* myKey: /myCustomCapture/ // all flags will be stripped;
|
|
1335
|
+
* ```
|
|
1336
|
+
* - default value === `false` is "[\\s\\S]
|
|
1337
|
+
*?", as in whiteSpace and nonWhiteSpace 0 to more occurence;
|
|
1338
|
+
* @returns {ReturnType<typeof TrySync<(templateStringArray:TemplateStringsArray,
|
|
1339
|
+
* ...values:(keyof KEYS)[]
|
|
1340
|
+
* )=>LitExp<KEYS>>>}
|
|
1341
|
+
* - placement of `key` will determine the named capture group will be placed in the template literal;
|
|
1342
|
+
* - it is recomended to not end template literal with any of the `key`s as the regex detection might failed to detects the boundary of the end of matched string of that capture group;
|
|
1343
|
+
*/
|
|
1344
|
+
```
|
|
1345
|
+
- <i>example</i>:
|
|
1346
|
+
```js
|
|
1347
|
+
import { LitExp } from 'vivth';
|
|
1348
|
+
|
|
1349
|
+
(()=>{
|
|
1350
|
+
const [liteal, errorPrep] = LitExp.prepare({
|
|
1351
|
+
myKey: /myCustomCapture/, // is placed on (?<myKey>myCustomCapture)
|
|
1352
|
+
// use false to place "[\\s\\S]*?" instead;
|
|
1353
|
+
...keyCaptureLogicPair
|
|
1354
|
+
})
|
|
1355
|
+
if (errorPrep) {
|
|
1356
|
+
console.error(error);
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
const litExp_instance = liteal`templateLiteral:${'myKey'};`
|
|
1360
|
+
// recommended to end the template literal with any string but `key`;
|
|
1361
|
+
})()
|
|
1362
|
+
|
|
1363
|
+
```
|
|
1364
|
+
|
|
1365
|
+
#### reference:`LitExp_instance.make`
|
|
1366
|
+
- instance methods for generating things;
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
#### reference:`LitExp_instance.make.string`
|
|
1370
|
+
- to make string based on the template literal;
|
|
806
1371
|
|
|
807
1372
|
```js
|
|
808
1373
|
/**
|
|
809
|
-
* @param {
|
|
810
|
-
* @returns {
|
|
1374
|
+
* @param {Partial<ReturnType<LitExp<KEYS>["evaluate"]["execGroups"]>>[0]["result"]} overrides
|
|
1375
|
+
* @returns {string}
|
|
811
1376
|
*/
|
|
1377
|
+
```
|
|
1378
|
+
- <i>example</i>:
|
|
1379
|
+
```js
|
|
1380
|
+
import { LitExp } from 'vivth';
|
|
1381
|
+
|
|
1382
|
+
const [literal, errorPreparing] = LitExp.prepare({
|
|
1383
|
+
myKey: false,
|
|
1384
|
+
...keyCaptureLogicPair
|
|
1385
|
+
})
|
|
1386
|
+
|
|
1387
|
+
// asuming no error
|
|
1388
|
+
litExp_instance = `templateLiteral:${'myKey'};`;
|
|
1389
|
+
const [result, error] = litExp_instance.make.string({
|
|
1390
|
+
myKey: 'actualvalue',
|
|
1391
|
+
});
|
|
1392
|
+
|
|
1393
|
+
console.log(result); // "templateLiteral:actualvalue;"
|
|
1394
|
+
|
|
812
1395
|
```
|
|
813
1396
|
|
|
814
|
-
#### reference:`
|
|
815
|
-
-
|
|
1397
|
+
#### reference:`LitExp_instance.evaluate`
|
|
1398
|
+
- methods collections to evaluate string with `Literal Expression`;
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
#### reference:`LitExp_instance.evaluate.execGroups`
|
|
1402
|
+
- to exec and grouped based on `key`;
|
|
816
1403
|
|
|
817
1404
|
```js
|
|
818
1405
|
/**
|
|
819
|
-
* @
|
|
1406
|
+
* @param {string} string
|
|
1407
|
+
* @param {Object} options
|
|
1408
|
+
* @param {ConstructorParameters<typeof RegExp>[1]} options.flags
|
|
1409
|
+
* @param {boolean} options.whiteSpaceSensitive
|
|
1410
|
+
* - true: leave any whitespace as is to be used as regex detection;
|
|
1411
|
+
* - false: convert all whitespace to `\s+`;
|
|
1412
|
+
* @param {boolean} options.absoluteLeadAndFollowing
|
|
1413
|
+
* - false: standard capture;
|
|
1414
|
+
* - true: add `^` and `<h2 id="litexp">LitExp</h2>
|
|
1415
|
+
|
|
1416
|
+
to capture definition:
|
|
1417
|
+
* >- meaning string will have to match starting and end of line from capture definition;
|
|
1418
|
+
* @returns {ReturnType<typeof TrySync<{
|
|
1419
|
+
* result:{ whole:string, named: Record<keyof KEYS, string>},
|
|
1420
|
+
* regexp:RegExp}>>
|
|
1421
|
+
* }
|
|
820
1422
|
*/
|
|
1423
|
+
```
|
|
1424
|
+
- <i>example</i>:
|
|
1425
|
+
```js
|
|
1426
|
+
import { LitExp } from 'vivth';
|
|
1427
|
+
|
|
1428
|
+
const [literal, errorPreparing] = LitExp.prepare({
|
|
1429
|
+
myKey: false,
|
|
1430
|
+
...keyCaptureLogicPair
|
|
1431
|
+
})
|
|
1432
|
+
|
|
1433
|
+
// asuming no eror
|
|
1434
|
+
const litExp_instance = literal`templateLiteral:${'myKey'};`
|
|
1435
|
+
|
|
1436
|
+
const [{
|
|
1437
|
+
result:{ // asuming there's no error
|
|
1438
|
+
named: { myKey },
|
|
1439
|
+
whole,
|
|
1440
|
+
},
|
|
1441
|
+
regex, // for reference
|
|
1442
|
+
}, error] = litExp_instance.evaluate.execGroups(
|
|
1443
|
+
`templateLiteral:Something;`,
|
|
1444
|
+
{ ...options }
|
|
1445
|
+
)
|
|
1446
|
+
|
|
1447
|
+
console.log(whole); // "templateLiteral:Something;"
|
|
1448
|
+
console.log(myKey); // "Something"
|
|
1449
|
+
|
|
821
1450
|
```
|
|
822
1451
|
|
|
823
|
-
#### reference:`
|
|
824
|
-
-
|
|
1452
|
+
#### reference:`LitExp_instance.evaluate.matchedAllAndGrouped`
|
|
1453
|
+
- to match all and grouped based on `key`;
|
|
825
1454
|
|
|
826
1455
|
```js
|
|
827
1456
|
/**
|
|
828
|
-
* @
|
|
1457
|
+
* @param {Parameters<LitExp<KEYS>["evaluate"]["execGroups"]>[0]} string
|
|
1458
|
+
* @param {Omit<Parameters<LitExp<KEYS>["evaluate"]["execGroups"]>[1], 'absoluteLeadAndFollowing'>} options
|
|
1459
|
+
* @returns {ReturnType<typeof TrySync<{result:{whole:string[], named:Array<Record<keyof KEYS, string>>},
|
|
1460
|
+
* regexp: RegExp}>>
|
|
1461
|
+
* }
|
|
829
1462
|
*/
|
|
1463
|
+
```
|
|
1464
|
+
- <i>example</i>:
|
|
1465
|
+
```js
|
|
1466
|
+
import { LitExp, Console } from 'vivth';
|
|
1467
|
+
|
|
1468
|
+
const [literal, errorPreparing] = LitExp.prepare({
|
|
1469
|
+
myKey: false,
|
|
1470
|
+
...keyCaptureLogicPair
|
|
1471
|
+
})
|
|
1472
|
+
|
|
1473
|
+
// asuming no error;
|
|
1474
|
+
litExp_instance = literal`templateLiteral:${'myKey'};`
|
|
1475
|
+
|
|
1476
|
+
const [resultOfMatchedAllAndGrouped, error] = litExp_instance.evaluate.matchedAllAndGrouped(
|
|
1477
|
+
`templateLiteral:Something;
|
|
1478
|
+
templateLiteral:SomethingElse;`,
|
|
1479
|
+
{ ...options }
|
|
1480
|
+
)
|
|
1481
|
+
(()=>{
|
|
1482
|
+
if (error) {
|
|
1483
|
+
Console.error(error);
|
|
1484
|
+
return;
|
|
1485
|
+
}
|
|
1486
|
+
const {
|
|
1487
|
+
result: {
|
|
1488
|
+
whole: [whole0, whole1],
|
|
1489
|
+
named: [
|
|
1490
|
+
{ myKey: myKeyExec0, },
|
|
1491
|
+
{ myKey: myKeyExec1, },
|
|
1492
|
+
],
|
|
1493
|
+
},
|
|
1494
|
+
regexp
|
|
1495
|
+
} = resultOfMatchedAllAndGrouped;
|
|
1496
|
+
|
|
1497
|
+
console.log(whole0); // "templateLiteral:Something;"
|
|
1498
|
+
console.log(whole1); // "templateLiteral:SomethingElse;"
|
|
1499
|
+
console.log(myKeyExec0); // "Something"
|
|
1500
|
+
console.log(myKeyExec1); // "SomethingElse"
|
|
1501
|
+
})()
|
|
1502
|
+
|
|
830
1503
|
```
|
|
831
1504
|
|
|
832
1505
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
@@ -836,7 +1509,8 @@ npm i vivth
|
|
|
836
1509
|
|
|
837
1510
|
#### reference:`Paths`
|
|
838
1511
|
- class helpers to define pathReference;
|
|
839
|
-
- is a singleton;
|
|
1512
|
+
- is a singleton;
|
|
1513
|
+
- most of functionality need to access `Paths.root`, if you get warning, you can instantiate `Paths` before running anything;
|
|
840
1514
|
|
|
841
1515
|
|
|
842
1516
|
#### reference:`new Paths`
|
|
@@ -846,9 +1520,22 @@ npm i vivth
|
|
|
846
1520
|
/**
|
|
847
1521
|
* @param {Object} options
|
|
848
1522
|
* @param {string} options.root
|
|
849
|
-
* - browser:
|
|
850
|
-
*
|
|
851
|
-
*
|
|
1523
|
+
* - browser:
|
|
1524
|
+
* ```js
|
|
1525
|
+
* location.origin
|
|
1526
|
+
* ```
|
|
1527
|
+
* - node/bun compatible:
|
|
1528
|
+
* ```js
|
|
1529
|
+
* process?.env?.INIT_CWD ?? process?.cwd()
|
|
1530
|
+
* ```
|
|
1531
|
+
* - deno: need for `deno run --allow-env --allow-read your_script.ts`:
|
|
1532
|
+
* ```js
|
|
1533
|
+
* Deno.env.get("INIT_CWD") ?? Deno.cwd()
|
|
1534
|
+
* ```
|
|
1535
|
+
* - pkg:
|
|
1536
|
+
* ```js
|
|
1537
|
+
* __dirname
|
|
1538
|
+
* ```
|
|
852
1539
|
* - other: you need to check your JSRuntime for the rootPath reference;
|
|
853
1540
|
*/
|
|
854
1541
|
```
|
|
@@ -887,7 +1574,25 @@ npm i vivth
|
|
|
887
1574
|
```js
|
|
888
1575
|
import { Paths } from 'vivth';
|
|
889
1576
|
|
|
890
|
-
Paths.normalize('file:\\D:\\myFile.mjs'); //
|
|
1577
|
+
Paths.normalize('file:\\D:\\myFile.mjs'); // "file://D://myFile.mjs"
|
|
1578
|
+
|
|
1579
|
+
```
|
|
1580
|
+
|
|
1581
|
+
#### reference:`Paths.normalizesForRoot`
|
|
1582
|
+
- normalize path separator to forward slash `/`;
|
|
1583
|
+
- then starts with forward slash `/`;
|
|
1584
|
+
|
|
1585
|
+
```js
|
|
1586
|
+
/**
|
|
1587
|
+
* @param {string} path_
|
|
1588
|
+
* @returns {`/${string}`}
|
|
1589
|
+
*/
|
|
1590
|
+
```
|
|
1591
|
+
- <i>example</i>:
|
|
1592
|
+
```js
|
|
1593
|
+
import { Paths } from 'vivth';
|
|
1594
|
+
|
|
1595
|
+
Paths.normalizesForRoot('path\\myFile.mjs'); // "/path/myFile.mjs"
|
|
891
1596
|
|
|
892
1597
|
```
|
|
893
1598
|
|
|
@@ -901,7 +1606,7 @@ npm i vivth
|
|
|
901
1606
|
|
|
902
1607
|
```js
|
|
903
1608
|
/**
|
|
904
|
-
* @template {AnyButUndefined}
|
|
1609
|
+
* @template {AnyButUndefined} DEFINEDANY
|
|
905
1610
|
*/
|
|
906
1611
|
```
|
|
907
1612
|
|
|
@@ -912,8 +1617,16 @@ npm i vivth
|
|
|
912
1617
|
/**
|
|
913
1618
|
* @param {Map<AnyButUndefined, [Promise<any>, {}]>} uniqueMap
|
|
914
1619
|
* @returns {typeof QChannel}
|
|
915
|
-
* - usefull for Queue primitive on multiple library but single reference, like the Web by making the `Map` on `window` object;
|
|
1620
|
+
* - usefull for Queue primitive on multiple library but single reference, like the Web by making the `Map` on `window` object;
|
|
916
1621
|
*/
|
|
1622
|
+
```
|
|
1623
|
+
- <i>example</i>:
|
|
1624
|
+
```js
|
|
1625
|
+
import { QChannel } from 'vivth';
|
|
1626
|
+
|
|
1627
|
+
const myMappedQref = (window['myMappedQref'] = new Map());
|
|
1628
|
+
export const MyQClass = QChannel.makeQClass(myMappedQref);
|
|
1629
|
+
|
|
917
1630
|
```
|
|
918
1631
|
|
|
919
1632
|
#### reference:`QChannel.fifo`
|
|
@@ -941,9 +1654,9 @@ npm i vivth
|
|
|
941
1654
|
|
|
942
1655
|
```js
|
|
943
1656
|
/**
|
|
944
|
-
* @template
|
|
945
|
-
* @param {()=>Promise<
|
|
946
|
-
* @returns {Promise<[
|
|
1657
|
+
* @template RESULT
|
|
1658
|
+
* @param {()=>Promise<RESULT>} asyncCallback
|
|
1659
|
+
* @returns {Promise<[RESULT|undefined, Error|undefined]>}
|
|
947
1660
|
*/
|
|
948
1661
|
```
|
|
949
1662
|
- <i>example</i>:
|
|
@@ -954,9 +1667,19 @@ npm i vivth
|
|
|
954
1667
|
|
|
955
1668
|
```
|
|
956
1669
|
|
|
957
|
-
#### reference:`QChannel_instance.
|
|
958
|
-
-
|
|
959
|
-
-
|
|
1670
|
+
#### reference:`QChannel_instance.close`
|
|
1671
|
+
- disable queue;
|
|
1672
|
+
- when `closed`, `isLastOnQ` will allways return `false`;
|
|
1673
|
+
|
|
1674
|
+
```js
|
|
1675
|
+
/**
|
|
1676
|
+
* @returns {void}
|
|
1677
|
+
*/
|
|
1678
|
+
```
|
|
1679
|
+
|
|
1680
|
+
#### reference:`QChannel_instance.open`
|
|
1681
|
+
- enable queue;
|
|
1682
|
+
- when `opened`, `isLastOnQ` will evaluate whether calls are actually the last of queue;
|
|
960
1683
|
|
|
961
1684
|
```js
|
|
962
1685
|
/**
|
|
@@ -972,16 +1695,16 @@ npm i vivth
|
|
|
972
1695
|
|
|
973
1696
|
```js
|
|
974
1697
|
/**
|
|
975
|
-
* @param {
|
|
1698
|
+
* @param {DEFINEDANY} keyID
|
|
976
1699
|
* @returns {Promise<QCBReturn>}
|
|
977
1700
|
*/
|
|
978
1701
|
```
|
|
979
1702
|
- <i>example</i>:
|
|
980
1703
|
```js
|
|
981
|
-
const q = new QChannel();
|
|
1704
|
+
const q = new QChannel('channel name');
|
|
982
1705
|
const handler = async () => {
|
|
983
1706
|
const { resume, isLastOnQ } = await q.key(keyID);
|
|
984
|
-
// if (!isLastOnQ) { // imperative debounce if needed
|
|
1707
|
+
// if (!isLastOnQ()) { // imperative debounce if needed
|
|
985
1708
|
// resume();
|
|
986
1709
|
// return;
|
|
987
1710
|
// }
|
|
@@ -1002,17 +1725,19 @@ npm i vivth
|
|
|
1002
1725
|
|
|
1003
1726
|
```js
|
|
1004
1727
|
/**
|
|
1005
|
-
* @template
|
|
1006
|
-
* @param {
|
|
1007
|
-
* @param {(options:Omit<QCBReturn,
|
|
1008
|
-
*
|
|
1728
|
+
* @template RESULT
|
|
1729
|
+
* @param {DEFINEDANY} keyID
|
|
1730
|
+
* @param {(options:Omit<QCBReturn,
|
|
1731
|
+
* "resume">) =>
|
|
1732
|
+
* Promise<RESULT>} asyncCallback
|
|
1733
|
+
* @returns {ReturnType<typeof TryAsync<RESULT>>}
|
|
1009
1734
|
*/
|
|
1010
1735
|
```
|
|
1011
1736
|
- <i>example</i>:
|
|
1012
1737
|
```js
|
|
1013
|
-
const q = new QChannel();
|
|
1738
|
+
const q = new QChannel('channel name');
|
|
1014
1739
|
const [result, error] = await q.callback(keyID, async ({ isLastOnQ }) = > {
|
|
1015
|
-
// if (!isLastOnQ) { // imperative debounce if needed
|
|
1740
|
+
// if (!isLastOnQ()) { // imperative debounce if needed
|
|
1016
1741
|
// return;
|
|
1017
1742
|
// }
|
|
1018
1743
|
// code
|
|
@@ -1027,13 +1752,9 @@ npm i vivth
|
|
|
1027
1752
|
|
|
1028
1753
|
#### reference:`SafeExit`
|
|
1029
1754
|
- class helper for describing how to Safely Response on exit events
|
|
1030
|
-
- singleton;
|
|
1755
|
+
- singleton;
|
|
1756
|
+
- most of functionality might need to access `SafeExit.instance.exiting`, if you get warning, you can instantiate `SafeExit` before running anything;
|
|
1031
1757
|
|
|
1032
|
-
```js
|
|
1033
|
-
/**
|
|
1034
|
-
* @template {[string, ...string[]]} eventNames
|
|
1035
|
-
*/
|
|
1036
|
-
```
|
|
1037
1758
|
|
|
1038
1759
|
#### reference:`SafeExit.instance`
|
|
1039
1760
|
- only accessible after instantiation;
|
|
@@ -1050,7 +1771,13 @@ npm i vivth
|
|
|
1050
1771
|
```js
|
|
1051
1772
|
/**
|
|
1052
1773
|
* @param {Object} options
|
|
1053
|
-
* @param {
|
|
1774
|
+
* @param {[string, ...string[]]} options.eventNames
|
|
1775
|
+
* - eventNames are blank by default, you need to manually name them all;
|
|
1776
|
+
* - 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
1777
|
+
* - example:
|
|
1778
|
+
* ```js
|
|
1779
|
+
* ['SIGINT', 'SIGTERM']
|
|
1780
|
+
* ```
|
|
1054
1781
|
* @param {()=>void} options.terminator
|
|
1055
1782
|
* - standard node/bun:
|
|
1056
1783
|
* ```js
|
|
@@ -1070,6 +1797,17 @@ npm i vivth
|
|
|
1070
1797
|
* });
|
|
1071
1798
|
* };
|
|
1072
1799
|
* ```
|
|
1800
|
+
* - example Deno:
|
|
1801
|
+
* ```js
|
|
1802
|
+
* (eventName) => {
|
|
1803
|
+
* const sig = Deno.signal(eventName);
|
|
1804
|
+
* for await (const _ of sig) {
|
|
1805
|
+
* exiting.correction(true);
|
|
1806
|
+
* sig.dispose();
|
|
1807
|
+
* Console.log(`safe exit via "${eventName}"`);
|
|
1808
|
+
* }
|
|
1809
|
+
* }
|
|
1810
|
+
* ```
|
|
1073
1811
|
* - if your exit callback doesn't uses `process` global object you need to input on the SafeExit instantiation
|
|
1074
1812
|
*/
|
|
1075
1813
|
```
|
|
@@ -1078,12 +1816,10 @@ npm i vivth
|
|
|
1078
1816
|
import { SafeExit, Console } from 'vivth';
|
|
1079
1817
|
|
|
1080
1818
|
new SafeExit({
|
|
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
1819
|
eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
1084
|
-
terminator
|
|
1820
|
+
terminator : () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
1085
1821
|
// optional deno example
|
|
1086
|
-
listener
|
|
1822
|
+
listener : (eventName) => {
|
|
1087
1823
|
const sig = Deno.signal(eventName);
|
|
1088
1824
|
for await (const _ of sig) {
|
|
1089
1825
|
exiting.correction(true);
|
|
@@ -1142,15 +1878,17 @@ npm i vivth
|
|
|
1142
1878
|
import { Setup, Console } from 'vivth';
|
|
1143
1879
|
|
|
1144
1880
|
new Setup.safeExit({
|
|
1145
|
-
//
|
|
1146
|
-
|
|
1881
|
+
// eventNames are blank by default, you need to manually name them all;
|
|
1882
|
+
// 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
1883
|
+
eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
1884
|
+
terminator = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
1147
1885
|
// optional deno example
|
|
1148
|
-
|
|
1886
|
+
listener = (eventName) => {
|
|
1149
1887
|
const sig = Deno.signal(eventName);
|
|
1150
1888
|
for await (const _ of sig) {
|
|
1151
|
-
|
|
1889
|
+
exiting.correction(true);
|
|
1152
1890
|
sig.dispose();
|
|
1153
|
-
Console.
|
|
1891
|
+
Console.log(`safe exit via "${eventName}"`);
|
|
1154
1892
|
}
|
|
1155
1893
|
}
|
|
1156
1894
|
});
|
|
@@ -1178,9 +1916,10 @@ npm i vivth
|
|
|
1178
1916
|
- <i>example</i>:
|
|
1179
1917
|
```js
|
|
1180
1918
|
import { Setup } from 'vivth';
|
|
1919
|
+
import { Worker } from 'node:worker_threads';
|
|
1181
1920
|
|
|
1182
1921
|
Setup.workerMain({
|
|
1183
|
-
workerClass:
|
|
1922
|
+
workerClass: Worker,
|
|
1184
1923
|
basePath: 'public/assets/js/workers',
|
|
1185
1924
|
pathValidator: async (workerPath, root, base) => {
|
|
1186
1925
|
const res = await fetch(`${root}/${base}/${workerPath}`);
|
|
@@ -1192,14 +1931,16 @@ npm i vivth
|
|
|
1192
1931
|
```
|
|
1193
1932
|
|
|
1194
1933
|
#### reference:`Setup.workerThread`
|
|
1195
|
-
- correct `parentPort` reference when needed;
|
|
1934
|
+
- correct `parentPort` reference when needed;
|
|
1935
|
+
- export to create new reference to be use to create new WorkerThread instance;
|
|
1196
1936
|
|
|
1197
1937
|
|
|
1198
1938
|
- <i>example</i>:
|
|
1199
1939
|
```js
|
|
1200
1940
|
import { Setup } from 'vivth';
|
|
1941
|
+
import { parentPort } from 'node:worker_threads';
|
|
1201
1942
|
|
|
1202
|
-
Setup.workerThread({
|
|
1943
|
+
export const MyWorkerThreadRef = Setup.workerThread({parentPort});
|
|
1203
1944
|
// that is the default value, if your parentPort/equivalent API is not that;
|
|
1204
1945
|
// you need to call this method;
|
|
1205
1946
|
|
|
@@ -1215,8 +1956,24 @@ npm i vivth
|
|
|
1215
1956
|
|
|
1216
1957
|
```js
|
|
1217
1958
|
/**
|
|
1218
|
-
* @template
|
|
1959
|
+
* @template VALUE
|
|
1960
|
+
*/
|
|
1961
|
+
```
|
|
1962
|
+
|
|
1963
|
+
#### reference:`new Signal`
|
|
1964
|
+
- create a `Signal`;
|
|
1965
|
+
|
|
1966
|
+
```js
|
|
1967
|
+
/**
|
|
1968
|
+
* @param {VALUE} value
|
|
1219
1969
|
*/
|
|
1970
|
+
```
|
|
1971
|
+
- <i>example</i>:
|
|
1972
|
+
```js
|
|
1973
|
+
import { Signal, Effect } from 'vivth';
|
|
1974
|
+
|
|
1975
|
+
const count = new Signal(0);
|
|
1976
|
+
|
|
1220
1977
|
```
|
|
1221
1978
|
|
|
1222
1979
|
#### reference:`Signal_instance.subscribers`
|
|
@@ -1237,20 +1994,20 @@ npm i vivth
|
|
|
1237
1994
|
|
|
1238
1995
|
```js
|
|
1239
1996
|
/**
|
|
1240
|
-
* @param {(options:{signalInstance:Signal<
|
|
1997
|
+
* @param {(options:{signalInstance:Signal<VALUE>})=>Promise<void>} [callback]
|
|
1241
1998
|
* @returns {void}
|
|
1242
1999
|
*/
|
|
1243
2000
|
```
|
|
1244
2001
|
- <i>example</i>:
|
|
1245
2002
|
```js
|
|
1246
|
-
|
|
2003
|
+
import { Signal } from 'vivth';
|
|
1247
2004
|
|
|
2005
|
+
// for deep signal like array or object you can:
|
|
1248
2006
|
const arraySignal = new Signal([1,2]);
|
|
1249
2007
|
arraySignal.value.push(3);
|
|
1250
2008
|
arraySignal.subscribers.notify();
|
|
1251
2009
|
|
|
1252
2010
|
// OR for more complex mutation:
|
|
1253
|
-
|
|
1254
2011
|
const objectSignal = new Signal({a:'test', b:'test'});
|
|
1255
2012
|
objectSignal.subscribers.notify(async ({ signalInstance }) => {
|
|
1256
2013
|
signalInstance.value['c'] = 'testc';
|
|
@@ -1291,28 +2048,12 @@ npm i vivth
|
|
|
1291
2048
|
*/
|
|
1292
2049
|
```
|
|
1293
2050
|
|
|
1294
|
-
#### reference:`new Signal`
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
```js
|
|
1298
|
-
/**
|
|
1299
|
-
* @param {Value} value
|
|
1300
|
-
*/
|
|
1301
|
-
```
|
|
1302
|
-
- <i>example</i>:
|
|
1303
|
-
```js
|
|
1304
|
-
import { Signal, Effect } from 'vivth';
|
|
1305
|
-
|
|
1306
|
-
const count = new Signal(0);
|
|
1307
|
-
|
|
1308
|
-
```
|
|
1309
|
-
|
|
1310
2051
|
#### reference:`Signal_instance.prev:getter`
|
|
1311
2052
|
- value before change;
|
|
1312
2053
|
|
|
1313
2054
|
```js
|
|
1314
2055
|
/**
|
|
1315
|
-
* @
|
|
2056
|
+
* @returns {VALUE}
|
|
1316
2057
|
*/
|
|
1317
2058
|
```
|
|
1318
2059
|
|
|
@@ -1321,7 +2062,7 @@ npm i vivth
|
|
|
1321
2062
|
|
|
1322
2063
|
```js
|
|
1323
2064
|
/**
|
|
1324
|
-
* @
|
|
2065
|
+
* @returns {VALUE}
|
|
1325
2066
|
*/
|
|
1326
2067
|
```
|
|
1327
2068
|
- <i>example</i>:
|
|
@@ -1334,7 +2075,7 @@ npm i vivth
|
|
|
1334
2075
|
new Effect(async ({ subscribe }) =>{
|
|
1335
2076
|
const countValue = subscribe(count).value; // reactive
|
|
1336
2077
|
})
|
|
1337
|
-
const oneMoreThanCount = new
|
|
2078
|
+
const oneMoreThanCount = new Derived(async ({ subscribe }) =>{
|
|
1338
2079
|
return subscribe(count).value + 1; // reactive
|
|
1339
2080
|
})
|
|
1340
2081
|
|
|
@@ -1345,7 +2086,7 @@ npm i vivth
|
|
|
1345
2086
|
|
|
1346
2087
|
```js
|
|
1347
2088
|
/**
|
|
1348
|
-
* @type {
|
|
2089
|
+
* @type {VALUE}
|
|
1349
2090
|
*/
|
|
1350
2091
|
```
|
|
1351
2092
|
- <i>example</i>:
|
|
@@ -1365,7 +2106,8 @@ npm i vivth
|
|
|
1365
2106
|
|
|
1366
2107
|
|
|
1367
2108
|
#### reference:`WorkerMainThread`
|
|
1368
|
-
- class helper to create `Worker` instance;
|
|
2109
|
+
- class helper to create `Worker` instance;
|
|
2110
|
+
- before any `Worker` functionaily to be used, you need to setup it with `WorkerThread.setup` and `WorkerMainThread.setup` before runing anytyhing;
|
|
1369
2111
|
|
|
1370
2112
|
```js
|
|
1371
2113
|
/**
|
|
@@ -1380,12 +2122,19 @@ npm i vivth
|
|
|
1380
2122
|
/**
|
|
1381
2123
|
* @param {Object} param0
|
|
1382
2124
|
* @param {typeof WorkerMainThread["workerClass"]} param0.workerClass
|
|
2125
|
+
* - example:
|
|
2126
|
+
* ```js
|
|
2127
|
+
* import { Worker } from 'node:worker_threads';
|
|
2128
|
+
* ```
|
|
1383
2129
|
* @param {typeof WorkerMainThread["pathValidator"]} param0.pathValidator
|
|
2130
|
+
* - example:
|
|
1384
2131
|
* ```js
|
|
1385
|
-
*
|
|
1386
|
-
*
|
|
1387
|
-
*
|
|
1388
|
-
*
|
|
2132
|
+
* async (workerPath, root, base) => {
|
|
2133
|
+
* const truePathCheck = `${root}/${base}/${workerPath}`;
|
|
2134
|
+
* const res = await fetch(truePathCheck);
|
|
2135
|
+
* // might also check wheter it need base or not
|
|
2136
|
+
* return await res.ok;
|
|
2137
|
+
* }
|
|
1389
2138
|
* ```
|
|
1390
2139
|
* @param {typeof WorkerMainThread["basePath"]} [param0.basePath]
|
|
1391
2140
|
* - additonal realtivePath from rootPath;
|
|
@@ -1394,10 +2143,11 @@ npm i vivth
|
|
|
1394
2143
|
```
|
|
1395
2144
|
- <i>example</i>:
|
|
1396
2145
|
```js
|
|
2146
|
+
import { Worker } from 'node:worker_threads';
|
|
1397
2147
|
import { WorkerMainThread } from 'vivth';
|
|
1398
2148
|
|
|
1399
2149
|
WorkerMainThread.setup({
|
|
1400
|
-
workerClass:
|
|
2150
|
+
workerClass: Worker,
|
|
1401
2151
|
basePath: 'public/assets/js/workers',
|
|
1402
2152
|
pathValidator: async (workerPath, root, base) => {
|
|
1403
2153
|
const res = await fetch(`${root}/${base}/${workerPath}`);
|
|
@@ -1414,7 +2164,7 @@ npm i vivth
|
|
|
1414
2164
|
|
|
1415
2165
|
```js
|
|
1416
2166
|
/**
|
|
1417
|
-
* @type {
|
|
2167
|
+
* @type {typeof Worker|typeof import('worker_threads').Worker}
|
|
1418
2168
|
*/
|
|
1419
2169
|
```
|
|
1420
2170
|
|
|
@@ -1438,201 +2188,68 @@ npm i vivth
|
|
|
1438
2188
|
*/
|
|
1439
2189
|
```
|
|
1440
2190
|
|
|
1441
|
-
#### reference:`
|
|
1442
|
-
- create Worker_instance;
|
|
1443
|
-
|
|
1444
|
-
```js
|
|
1445
|
-
/**
|
|
1446
|
-
* @param {string} handler
|
|
1447
|
-
* - if `isInline` === `false`, `handler` should be:
|
|
1448
|
-
* >- pointing to worker thread file; WHICH
|
|
1449
|
-
* >- the path must be relative to `projectRoot`;
|
|
1450
|
-
* - if `isInline` === `true`, `handler` should be
|
|
1451
|
-
* >- string literal of prebundled worker thread script; OR
|
|
1452
|
-
* >- manually made string literal of worker thread script;
|
|
1453
|
-
* @param {Omit<WorkerOptions|import('worker_threads').WorkerOptions, 'eval'|'type'>} [options]
|
|
1454
|
-
* @param {boolean} [isInline]
|
|
1455
|
-
*/
|
|
1456
|
-
```
|
|
1457
|
-
- <i>example</i>:
|
|
1458
|
-
```js
|
|
1459
|
-
import { WorkerMainThread } from 'vivth';
|
|
1460
|
-
|
|
1461
|
-
export const myDoubleWorker = new WorkerMainThread('./doubleWorkerThread.mjs');
|
|
1462
|
-
|
|
1463
|
-
```
|
|
1464
|
-
|
|
1465
|
-
#### reference:`WorkerMainThread.isBrowser:getter`
|
|
1466
|
-
- check whether js run in browser
|
|
1467
|
-
|
|
1468
|
-
```js
|
|
1469
|
-
/**
|
|
1470
|
-
* @type {boolean}
|
|
1471
|
-
*/
|
|
1472
|
-
```
|
|
1473
|
-
|
|
1474
|
-
#### reference:`WorkerMainThread_instance.terminate`
|
|
1475
|
-
- terminate all signals that are used on this instance;
|
|
1476
|
-
|
|
1477
|
-
```js
|
|
1478
|
-
/**
|
|
1479
|
-
* @type {()=>void}
|
|
1480
|
-
*/
|
|
1481
|
-
```
|
|
1482
|
-
|
|
1483
|
-
#### reference:`WorkerMainThread_instance.receiverSignal`
|
|
1484
|
-
- result signal of the processed message;
|
|
1485
|
-
|
|
1486
|
-
```js
|
|
1487
|
-
/**
|
|
1488
|
-
* @type {Derived<WorkerResult<WT["Post"]>>}
|
|
1489
|
-
*/
|
|
1490
|
-
```
|
|
1491
|
-
- <i>example</i>:
|
|
1492
|
-
```js
|
|
1493
|
-
import { Effect } from 'vivth';
|
|
1494
|
-
import { myDoubleWorker } from './myDoubleWorker.mjs';
|
|
1495
|
-
|
|
1496
|
-
const doubleReceiverSignal = myDoubleWorker.receiverSignal;
|
|
1497
|
-
new Effect(async({ subscribe }) => {
|
|
1498
|
-
const value = subscribe(doubleReceiverSignal).value;
|
|
1499
|
-
// code
|
|
1500
|
-
})
|
|
1501
|
-
|
|
1502
|
-
```
|
|
1503
|
-
|
|
1504
|
-
#### reference:`WorkerMainThread_instance.postMessage`
|
|
1505
|
-
- callback to send message to the worker thread;
|
|
1506
|
-
|
|
1507
|
-
```js
|
|
1508
|
-
/**
|
|
1509
|
-
* @type {(event: WT["Receive"])=>void}
|
|
1510
|
-
*/
|
|
1511
|
-
```
|
|
1512
|
-
- <i>example</i>:
|
|
1513
|
-
```js
|
|
1514
|
-
import { myDoubleWorker } from './myDoubleWorker.mjs';
|
|
1515
|
-
|
|
1516
|
-
myDoubleWorker.postMessage(90);
|
|
1517
|
-
|
|
1518
|
-
```
|
|
1519
|
-
|
|
1520
|
-
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
1521
|
-
|
|
1522
|
-
<h2 id="workerresult">WorkerResult</h2>
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
#### reference:`WorkerResult`
|
|
1526
|
-
- typeHelper for `Worker` message passing;
|
|
1527
|
-
- uses error as value instead;
|
|
1528
|
-
|
|
1529
|
-
```js
|
|
1530
|
-
/**
|
|
1531
|
-
* @template POST
|
|
1532
|
-
*/
|
|
1533
|
-
```
|
|
1534
|
-
|
|
1535
|
-
#### reference:`WorkerResult_instance.data`
|
|
1536
|
-
- result value;
|
|
1537
|
-
|
|
1538
|
-
```js
|
|
1539
|
-
/**
|
|
1540
|
-
* @type {POST}
|
|
1541
|
-
*/
|
|
1542
|
-
```
|
|
1543
|
-
|
|
1544
|
-
#### reference:`WorkerResult_instance.error`
|
|
1545
|
-
- error value;
|
|
1546
|
-
|
|
1547
|
-
```js
|
|
1548
|
-
/**
|
|
1549
|
-
* @type {Error|string|undefined}
|
|
1550
|
-
*/
|
|
1551
|
-
```
|
|
1552
|
-
|
|
1553
|
-
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
1554
|
-
|
|
1555
|
-
<h2 id="workerthread">WorkerThread</h2>
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
#### reference:`WorkerThread`
|
|
1559
|
-
- class helper for `WorkerThread` creation;
|
|
1560
|
-
|
|
1561
|
-
```js
|
|
1562
|
-
/**
|
|
1563
|
-
* @template Receive
|
|
1564
|
-
* @template Post
|
|
1565
|
-
*/
|
|
1566
|
-
```
|
|
1567
|
-
|
|
1568
|
-
#### reference:`WorkerThread.setup`
|
|
1569
|
-
- need to be called and exported as new `WorkerThread` class reference;
|
|
1570
|
-
|
|
1571
|
-
```js
|
|
1572
|
-
/**
|
|
1573
|
-
* @template Receive_
|
|
1574
|
-
* @template Post_
|
|
1575
|
-
* @param {{parentPort:()=>Promise<any>}} parentPortRef
|
|
1576
|
-
* - correct parentPort reference;
|
|
1577
|
-
* @returns {typeof WorkerThread<Receive_, Post_>}
|
|
1578
|
-
*/
|
|
1579
|
-
```
|
|
1580
|
-
- <i>example</i>:
|
|
1581
|
-
```js
|
|
1582
|
-
import { WorkerThread } from 'vivth';
|
|
1583
|
-
|
|
1584
|
-
WorkerThread.setup({ parentPort: async () => await import('node:worker_threads') });
|
|
1585
|
-
// that is the default value, if your parentPort/equivalent API is not that;
|
|
1586
|
-
// you need to call this method;
|
|
1587
|
-
|
|
1588
|
-
```
|
|
1589
|
-
|
|
1590
|
-
#### reference:`new WorkerThread`
|
|
1591
|
-
- instantiate via created class from `setup` static method;
|
|
1592
|
-
|
|
2191
|
+
#### reference:`WorkerMainThread.newVivthWorker`
|
|
2192
|
+
- create Worker_instance;
|
|
2193
|
+
|
|
1593
2194
|
```js
|
|
1594
2195
|
/**
|
|
1595
|
-
* @param {
|
|
2196
|
+
* @param {string} handler
|
|
2197
|
+
* @param {Omit<WorkerOptions|import('worker_threads').WorkerOptions, 'eval'|'type'>} [options]
|
|
2198
|
+
* @returns {WorkerMainThread<WT>}
|
|
1596
2199
|
*/
|
|
1597
2200
|
```
|
|
1598
2201
|
- <i>example</i>:
|
|
1599
2202
|
```js
|
|
1600
|
-
import {
|
|
2203
|
+
import { WorkerMainThread } from 'vivth';
|
|
1601
2204
|
|
|
1602
|
-
const
|
|
1603
|
-
// if(!isLastOnQ) {
|
|
1604
|
-
// return null; // can be used for imperative debouncing;
|
|
1605
|
-
// }
|
|
1606
|
-
return ev = ev * 2;
|
|
1607
|
-
});
|
|
2205
|
+
export const myDoubleWorker = WorkerMainThread.newVivthWorker('./doubleWorkerThread.mjs');
|
|
1608
2206
|
|
|
1609
2207
|
```
|
|
1610
2208
|
|
|
1611
|
-
#### reference:`
|
|
1612
|
-
-
|
|
2209
|
+
#### reference:`WorkerMainThread_instance.terminate`
|
|
2210
|
+
- terminate all signals that are used on this instance;
|
|
1613
2211
|
|
|
1614
2212
|
```js
|
|
1615
2213
|
/**
|
|
1616
|
-
* @type {(
|
|
2214
|
+
* @type {()=>void}
|
|
1617
2215
|
*/
|
|
1618
2216
|
```
|
|
1619
2217
|
|
|
1620
|
-
#### reference:`
|
|
1621
|
-
-
|
|
2218
|
+
#### reference:`WorkerMainThread_instance.receiverSignal`
|
|
2219
|
+
- result signal of the processed message;
|
|
1622
2220
|
|
|
1623
2221
|
```js
|
|
1624
2222
|
/**
|
|
1625
|
-
* @type {
|
|
2223
|
+
* @type {Derived<WorkerResult<WT["POST"]>>}
|
|
1626
2224
|
*/
|
|
2225
|
+
```
|
|
2226
|
+
- <i>example</i>:
|
|
2227
|
+
```js
|
|
2228
|
+
import { Effect } from 'vivth';
|
|
2229
|
+
import { myDoubleWorker } from './myDoubleWorker.mjs';
|
|
2230
|
+
|
|
2231
|
+
const doubleReceiverSignal = myDoubleWorker.receiverSignal;
|
|
2232
|
+
new Effect(async({ subscribe }) => {
|
|
2233
|
+
const value = subscribe(doubleReceiverSignal).value;
|
|
2234
|
+
// code
|
|
2235
|
+
})
|
|
2236
|
+
|
|
1627
2237
|
```
|
|
1628
2238
|
|
|
1629
|
-
#### reference:`
|
|
1630
|
-
-
|
|
2239
|
+
#### reference:`WorkerMainThread_instance.postMessage`
|
|
2240
|
+
- callback to send message to the worker thread;
|
|
1631
2241
|
|
|
1632
2242
|
```js
|
|
1633
2243
|
/**
|
|
1634
|
-
* @type {
|
|
2244
|
+
* @type {(event: WT["RECEIVE"])=>void}
|
|
1635
2245
|
*/
|
|
2246
|
+
```
|
|
2247
|
+
- <i>example</i>:
|
|
2248
|
+
```js
|
|
2249
|
+
import { myDoubleWorker } from './myDoubleWorker.mjs';
|
|
2250
|
+
|
|
2251
|
+
myDoubleWorker.postMessage(90);
|
|
2252
|
+
|
|
1636
2253
|
```
|
|
1637
2254
|
|
|
1638
2255
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
@@ -1651,13 +2268,18 @@ npm i vivth
|
|
|
1651
2268
|
* @param {string} mimeType
|
|
1652
2269
|
* @param {(string:string)=>string} btoaFunction
|
|
1653
2270
|
* - check your js runtime `btoa`;
|
|
1654
|
-
*
|
|
2271
|
+
* - node compatible:
|
|
2272
|
+
* ```js
|
|
2273
|
+
* (str, prevBufferEncoding) =>
|
|
2274
|
+
* Buffer.from(str, prevBufferEncoding).toString('base64')
|
|
2275
|
+
* ```
|
|
2276
|
+
* @returns {Base64URLString}
|
|
1655
2277
|
*/
|
|
1656
2278
|
```
|
|
1657
2279
|
- <i>example</i>:
|
|
1658
2280
|
```js
|
|
1659
2281
|
import { Base64URL } from 'vivth'
|
|
1660
|
-
import
|
|
2282
|
+
import fileString from './fileString.mjs';
|
|
1661
2283
|
|
|
1662
2284
|
Base64URL(fileString, 'application/javascript', btoa);
|
|
1663
2285
|
|
|
@@ -1665,6 +2287,33 @@ npm i vivth
|
|
|
1665
2287
|
|
|
1666
2288
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
1667
2289
|
|
|
2290
|
+
<h2 id="base64urlfromfile">Base64URLFromFile</h2>
|
|
2291
|
+
|
|
2292
|
+
|
|
2293
|
+
#### reference:`Base64URLFromFile`
|
|
2294
|
+
- create inline base64 url;
|
|
2295
|
+
- usage:
|
|
2296
|
+
>- can be extremely usefull to display file on desktop app webview, without exposing http server;
|
|
2297
|
+
>- when using `FSInline`, use [Base64URL](#base64url) instead;
|
|
2298
|
+
|
|
2299
|
+
```js
|
|
2300
|
+
/**
|
|
2301
|
+
* @param {string} filePath
|
|
2302
|
+
* @returns {Promise<Base64URLString>}
|
|
2303
|
+
*/
|
|
2304
|
+
```
|
|
2305
|
+
- <i>example</i>:
|
|
2306
|
+
```js
|
|
2307
|
+
import { join } from 'node:path'
|
|
2308
|
+
|
|
2309
|
+
import { Base64URLFromFile, Paths } from 'vivth'
|
|
2310
|
+
|
|
2311
|
+
await Base64URLFromFile(join(Paths.root, '/path/to/file'));
|
|
2312
|
+
|
|
2313
|
+
```
|
|
2314
|
+
|
|
2315
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2316
|
+
|
|
1668
2317
|
<h2 id="eventnamespace">EventNameSpace</h2>
|
|
1669
2318
|
|
|
1670
2319
|
|
|
@@ -1697,7 +2346,7 @@ npm i vivth
|
|
|
1697
2346
|
>>- `"at"description` are treated as plain `markdown`;
|
|
1698
2347
|
>>- first `"at"${string}` after `"at"description` until `"at"example` will be treated as `javascript` comment block on the `markdown`;
|
|
1699
2348
|
>>- `"at"example` are treated as `javascript` block on the `markdown` file, and should be placed last on the same comment block;
|
|
1700
|
-
>>- you can always look at `vivth/src` files to check how the source, and the `README.md` and `index.mjs`
|
|
2349
|
+
>>- you can always look at `vivth/src` files to check how the source, and the `README.md` and `index.mjs` documentation/generation results;
|
|
1701
2350
|
|
|
1702
2351
|
|
|
1703
2352
|
#### reference:`new JSautoDOC`
|
|
@@ -1721,25 +2370,9 @@ npm i vivth
|
|
|
1721
2370
|
```
|
|
1722
2371
|
- <i>example</i>:
|
|
1723
2372
|
```js
|
|
1724
|
-
import {
|
|
1725
|
-
|
|
1726
|
-
const { paths, safeExit } = Setup;
|
|
1727
|
-
|
|
1728
|
-
new paths({
|
|
1729
|
-
root: process?.env?.INIT_CWD ?? process?.cwd(),
|
|
1730
|
-
});
|
|
1731
|
-
|
|
1732
|
-
new safeExit({
|
|
1733
|
-
exitEventNames: ['SIGINT', 'SIGTERM', 'exit'],
|
|
1734
|
-
exitCallbackListeners: (eventName) => {
|
|
1735
|
-
process.once(eventName, function () {
|
|
1736
|
-
safeExit.instance.exiting.correction(true);
|
|
1737
|
-
Console.log(`safe exit via "${eventName}"`);
|
|
1738
|
-
});
|
|
1739
|
-
},
|
|
1740
|
-
});
|
|
2373
|
+
import { JSautoDOC } from 'vivth';
|
|
1741
2374
|
|
|
1742
|
-
|
|
2375
|
+
new JSautoDOC({
|
|
1743
2376
|
paths: { dir: 'src', file: 'index.mjs', readMe: 'README.md' },
|
|
1744
2377
|
copyright: 'this library is made and distributed under MIT license;',
|
|
1745
2378
|
tableOfContentTitle: 'list of exported API and typehelpers',
|
|
@@ -1759,14 +2392,14 @@ npm i vivth
|
|
|
1759
2392
|
|
|
1760
2393
|
```js
|
|
1761
2394
|
/**
|
|
1762
|
-
* @template {Object}
|
|
1763
|
-
* @template {Object}
|
|
2395
|
+
* @template {Object} PARENT
|
|
2396
|
+
* @template {Object} OBJECT
|
|
1764
2397
|
* @param {string} keyName
|
|
1765
|
-
* @param {
|
|
1766
|
-
* @param {(this:
|
|
2398
|
+
* @param {PARENT} parent
|
|
2399
|
+
* @param {(this:PARENT)=>OBJECT} object
|
|
1767
2400
|
* @param {Object} [options]
|
|
1768
2401
|
* @param {boolean} [options.lazy]
|
|
1769
|
-
* @return {
|
|
2402
|
+
* @return {OBJECT}
|
|
1770
2403
|
*/
|
|
1771
2404
|
```
|
|
1772
2405
|
- <i>example</i>:
|
|
@@ -1821,9 +2454,9 @@ npm i vivth
|
|
|
1821
2454
|
|
|
1822
2455
|
```js
|
|
1823
2456
|
/**
|
|
1824
|
-
* @template {string}
|
|
1825
|
-
* @param {
|
|
1826
|
-
* @returns {{[EventNameSpace]:
|
|
2457
|
+
* @template {string} NAME
|
|
2458
|
+
* @param {NAME} name
|
|
2459
|
+
* @returns {{[EventNameSpace]: NAME}}
|
|
1827
2460
|
*/
|
|
1828
2461
|
```
|
|
1829
2462
|
- <i>example</i>:
|
|
@@ -1840,6 +2473,27 @@ npm i vivth
|
|
|
1840
2473
|
|
|
1841
2474
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
1842
2475
|
|
|
2476
|
+
<h2 id="getruntime">GetRuntime</h2>
|
|
2477
|
+
|
|
2478
|
+
|
|
2479
|
+
#### reference:`GetRuntime`
|
|
2480
|
+
- detects the current JavaScript runtime;
|
|
2481
|
+
|
|
2482
|
+
```js
|
|
2483
|
+
/**
|
|
2484
|
+
* @type {()=>Runtime}
|
|
2485
|
+
*/
|
|
2486
|
+
```
|
|
2487
|
+
- <i>example</i>:
|
|
2488
|
+
```js
|
|
2489
|
+
import { GetRuntime } form 'vivth';
|
|
2490
|
+
|
|
2491
|
+
const runtime = GetRuntime();
|
|
2492
|
+
|
|
2493
|
+
```
|
|
2494
|
+
|
|
2495
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2496
|
+
|
|
1843
2497
|
<h2 id="isasync">IsAsync</h2>
|
|
1844
2498
|
|
|
1845
2499
|
|
|
@@ -1879,17 +2533,18 @@ npm i vivth
|
|
|
1879
2533
|
|
|
1880
2534
|
```js
|
|
1881
2535
|
/**
|
|
1882
|
-
* @template
|
|
1883
|
-
* @param {() =>
|
|
1884
|
-
* @returns {
|
|
2536
|
+
* @template FACTORY
|
|
2537
|
+
* @param {() => FACTORY} factory
|
|
2538
|
+
* @returns {FACTORY & {[unwrapLazy]: ()=> FACTORY}}
|
|
1885
2539
|
* - the unwrapLazy prop can be accessed to force instatiation/call;
|
|
1886
2540
|
* >- `unwrapLazy` prop name can be checked by checking the list of possible prop, from your ide;
|
|
1887
|
-
* >- as of version 1.0.0
|
|
2541
|
+
* >- as of version `1.0.0`, value is `vivth:unwrapLazy;`;
|
|
1888
2542
|
*/
|
|
1889
2543
|
```
|
|
1890
2544
|
- <i>example</i>:
|
|
1891
2545
|
```js
|
|
1892
2546
|
import { LazyFactory } from 'vivth';
|
|
2547
|
+
|
|
1893
2548
|
class MyClass{
|
|
1894
2549
|
constructor() {
|
|
1895
2550
|
this.myProp = 1; // will only available when accessed;
|
|
@@ -1906,7 +2561,9 @@ npm i vivth
|
|
|
1906
2561
|
import { myInstance } from './myInstance.mjs';
|
|
1907
2562
|
|
|
1908
2563
|
const a = myInstance; // not yet initiated;
|
|
1909
|
-
const b = a.myProp // imediately initiated;
|
|
2564
|
+
const b = a.myProp // imediately initiated;
|
|
2565
|
+
// OR
|
|
2566
|
+
myInstance["vivth:unwrapLazy;"]() // forcefully call the callback;
|
|
1910
2567
|
|
|
1911
2568
|
```
|
|
1912
2569
|
|
|
@@ -1952,15 +2609,15 @@ npm i vivth
|
|
|
1952
2609
|
|
|
1953
2610
|
```js
|
|
1954
2611
|
/**
|
|
1955
|
-
* @template {string}
|
|
1956
|
-
* @template
|
|
2612
|
+
* @template {string} KEY
|
|
2613
|
+
* @template RETURNTYPE
|
|
1957
2614
|
* @template {Record<
|
|
1958
|
-
*
|
|
1959
|
-
* () => Promise<
|
|
2615
|
+
* KEY,
|
|
2616
|
+
* (err:{prevError:undefined|Error}) => Promise<RETURNTYPE>
|
|
1960
2617
|
* >} RecordTryType
|
|
1961
2618
|
* @param {RecordTryType} tryRecord
|
|
1962
2619
|
* @returns {Promise<
|
|
1963
|
-
* [[keyof RecordTryType,
|
|
2620
|
+
* [[keyof RecordTryType, RETURNTYPE], undefined]
|
|
1964
2621
|
* | [[undefined, undefined], Error]
|
|
1965
2622
|
* >}
|
|
1966
2623
|
*/
|
|
@@ -1970,13 +2627,13 @@ npm i vivth
|
|
|
1970
2627
|
import { Try } from 'vivth';
|
|
1971
2628
|
|
|
1972
2629
|
const [[key, result], error] = await Try({
|
|
1973
|
-
someRuntime: async () => {
|
|
2630
|
+
someRuntime: async ( prevError ) => {
|
|
1974
2631
|
// asuming on this one doesn't naturally throw error,
|
|
1975
2632
|
// yet you need to continue to next key,
|
|
1976
2633
|
// instead of returning,
|
|
1977
2634
|
// you should throw new Error(something);
|
|
1978
2635
|
},
|
|
1979
|
-
browser: async () => {
|
|
2636
|
+
browser: async ( prevError ) => {
|
|
1980
2637
|
return location?.origin;
|
|
1981
2638
|
// if no error, stop other key function from running;
|
|
1982
2639
|
// key = 'browser'
|
|
@@ -1985,7 +2642,7 @@ npm i vivth
|
|
|
1985
2642
|
// if error;
|
|
1986
2643
|
// run nodeOrBun;
|
|
1987
2644
|
},
|
|
1988
|
-
nodeOrBun: async () => {
|
|
2645
|
+
nodeOrBun: async ( prevError ) => {
|
|
1989
2646
|
return process?.env?.INIT_CWD ?? process?.cwd();
|
|
1990
2647
|
// if no error;
|
|
1991
2648
|
// key = 'nodeOrBun'
|
|
@@ -2010,9 +2667,9 @@ npm i vivth
|
|
|
2010
2667
|
|
|
2011
2668
|
```js
|
|
2012
2669
|
/**
|
|
2013
|
-
* @template
|
|
2014
|
-
* @param {()=>Promise<
|
|
2015
|
-
* @returns {Promise<[
|
|
2670
|
+
* @template RESULT
|
|
2671
|
+
* @param {()=>Promise<RESULT>} asyncFunction_
|
|
2672
|
+
* @returns {Promise<[RESULT|undefined, Error|undefined]>}
|
|
2016
2673
|
*/
|
|
2017
2674
|
```
|
|
2018
2675
|
- <i>example</i>:
|
|
@@ -2043,9 +2700,9 @@ npm i vivth
|
|
|
2043
2700
|
|
|
2044
2701
|
```js
|
|
2045
2702
|
/**
|
|
2046
|
-
* @template
|
|
2047
|
-
* @param {()=>
|
|
2048
|
-
* @returns {[
|
|
2703
|
+
* @template RESULT
|
|
2704
|
+
* @param {()=>RESULT} function_
|
|
2705
|
+
* @returns {[RESULT|undefined, Error|undefined]}
|
|
2049
2706
|
*/
|
|
2050
2707
|
```
|
|
2051
2708
|
- <i>example</i>:
|
|
@@ -2072,6 +2729,7 @@ npm i vivth
|
|
|
2072
2729
|
```js
|
|
2073
2730
|
/**
|
|
2074
2731
|
* @param {string} path_
|
|
2732
|
+
* - path from `Paths.root`;
|
|
2075
2733
|
* @param {Object} [options]
|
|
2076
2734
|
* @param {string} [options.overrideDir]
|
|
2077
2735
|
* - default: write conversion to same directory;
|
|
@@ -2091,69 +2749,61 @@ npm i vivth
|
|
|
2091
2749
|
|
|
2092
2750
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2093
2751
|
|
|
2094
|
-
<h2 id="
|
|
2095
|
-
|
|
2752
|
+
<h2 id="anybutundefined">AnyButUndefined</h2>
|
|
2096
2753
|
|
|
2097
|
-
|
|
2098
|
-
- function to create file by recursively mkdir the dirname of the outFile;
|
|
2099
|
-
- also returing promise of result & error as value;
|
|
2754
|
+
- jsdoc types:
|
|
2100
2755
|
|
|
2101
2756
|
```js
|
|
2102
|
-
/**
|
|
2103
|
-
*
|
|
2104
|
-
* @
|
|
2105
|
-
* @param {import('node:fs').WriteFileOptions} options
|
|
2106
|
-
* @returns {Promise<ReturnType<typeof TryAsync<void>>>}
|
|
2757
|
+
/**
|
|
2758
|
+
* - type helper for ID or objects;
|
|
2759
|
+
* @typedef {{}|null|number|string|boolean|symbol|bigint|function} AnyButUndefined
|
|
2107
2760
|
*/
|
|
2108
2761
|
```
|
|
2109
|
-
|
|
2110
|
-
```js
|
|
2111
|
-
import { WriteFileSafe } from 'vivth';
|
|
2112
|
-
|
|
2113
|
-
const [_, writeError] = await TryAsync(async () => {
|
|
2114
|
-
return await WriteFileSafe(
|
|
2115
|
-
'/some/path.mjs',
|
|
2116
|
-
'console.log("hello-world!!");',
|
|
2117
|
-
{ encoding: 'utf-8' }
|
|
2118
|
-
);
|
|
2119
|
-
});
|
|
2120
|
-
|
|
2121
|
-
```
|
|
2762
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2122
2763
|
|
|
2764
|
+
<h2 id="extnametype">ExtnameType</h2>
|
|
2765
|
+
|
|
2766
|
+
- jsdoc types:
|
|
2767
|
+
|
|
2768
|
+
```js
|
|
2769
|
+
/**
|
|
2770
|
+
* - jsRuntime extention naming convention;
|
|
2771
|
+
* @typedef {`.${string}`} ExtnameType
|
|
2772
|
+
*/
|
|
2773
|
+
```
|
|
2123
2774
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2124
2775
|
|
|
2125
|
-
<h2 id="
|
|
2776
|
+
<h2 id="islistsignal">IsListSignal</h2>
|
|
2126
2777
|
|
|
2127
2778
|
- jsdoc types:
|
|
2128
2779
|
|
|
2129
2780
|
```js
|
|
2130
2781
|
/**
|
|
2131
|
-
* -
|
|
2132
|
-
* @typedef {
|
|
2782
|
+
* - `EnvSignal.get` argument whether signal need to be a list or not;
|
|
2783
|
+
* @typedef {boolean} IsListSignal
|
|
2133
2784
|
*/
|
|
2134
2785
|
```
|
|
2135
2786
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2136
2787
|
|
|
2137
|
-
<h2 id="
|
|
2788
|
+
<h2 id="listarg">ListArg</h2>
|
|
2138
2789
|
|
|
2139
2790
|
- jsdoc types:
|
|
2140
2791
|
|
|
2141
2792
|
```js
|
|
2142
2793
|
/**
|
|
2143
|
-
* -
|
|
2144
|
-
* @typedef {
|
|
2794
|
+
* - ListSignal argument type;
|
|
2795
|
+
* @typedef {Record<string, string>} ListArg
|
|
2145
2796
|
*/
|
|
2146
2797
|
```
|
|
2147
2798
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2148
2799
|
|
|
2149
|
-
<h2 id="
|
|
2800
|
+
<h2 id="litexpkeytype">LitExpKeyType</h2>
|
|
2150
2801
|
|
|
2151
2802
|
- jsdoc types:
|
|
2152
2803
|
|
|
2153
2804
|
```js
|
|
2154
2805
|
/**
|
|
2155
|
-
*
|
|
2156
|
-
* @typedef {Omit<import("./src/types/QCBReturn.mjs").QCBReturn, "isLastOnQ">} QCBFIFOReturn
|
|
2806
|
+
* @typedef {Record<string, RegExp|false>} LitExpKeyType
|
|
2157
2807
|
*/
|
|
2158
2808
|
```
|
|
2159
2809
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
@@ -2172,38 +2822,190 @@ npm i vivth
|
|
|
2172
2822
|
```
|
|
2173
2823
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2174
2824
|
|
|
2175
|
-
<h2 id="
|
|
2825
|
+
<h2 id="qcbfiforeturn">QCBFIFOReturn</h2>
|
|
2176
2826
|
|
|
2177
2827
|
- jsdoc types:
|
|
2178
2828
|
|
|
2179
2829
|
```js
|
|
2180
2830
|
/**
|
|
2181
|
-
* -
|
|
2182
|
-
* @typedef {
|
|
2831
|
+
* - return type of Q callback fifo;
|
|
2832
|
+
* @typedef {Omit<import("./src/types/QCBReturn.mjs").QCBReturn, "isLastOnQ">} QCBFIFOReturn
|
|
2183
2833
|
*/
|
|
2184
2834
|
```
|
|
2185
2835
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2186
2836
|
|
|
2187
|
-
<h2 id="
|
|
2837
|
+
<h2 id="qcbreturn">QCBReturn</h2>
|
|
2188
2838
|
|
|
2189
2839
|
- jsdoc types:
|
|
2190
2840
|
|
|
2191
2841
|
```js
|
|
2192
2842
|
/**
|
|
2193
|
-
* -
|
|
2194
|
-
* @typedef {boolean}
|
|
2843
|
+
* - return type of Q callback;
|
|
2844
|
+
* @typedef {{resume:()=>void, isLastOnQ:()=>boolean}} QCBReturn
|
|
2195
2845
|
*/
|
|
2196
2846
|
```
|
|
2197
2847
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2198
2848
|
|
|
2199
|
-
<h2 id="
|
|
2849
|
+
<h2 id="runtime">Runtime</h2>
|
|
2200
2850
|
|
|
2201
2851
|
- jsdoc types:
|
|
2202
2852
|
|
|
2203
2853
|
```js
|
|
2204
2854
|
/**
|
|
2205
|
-
*
|
|
2206
|
-
*
|
|
2855
|
+
* @description
|
|
2856
|
+
* - for popular runtimes check;
|
|
2857
|
+
* @typedef {'node' | 'bun' | 'deno' | 'browser' | 'unknown'} Runtime
|
|
2858
|
+
*/
|
|
2859
|
+
```
|
|
2860
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2861
|
+
|
|
2862
|
+
<h2 id="workerresult">WorkerResult</h2>
|
|
2863
|
+
|
|
2864
|
+
|
|
2865
|
+
#### reference:`WorkerResult`
|
|
2866
|
+
- typeHelper for `Worker` message passing;
|
|
2867
|
+
- uses error as value instead;
|
|
2868
|
+
|
|
2869
|
+
```js
|
|
2870
|
+
/**
|
|
2871
|
+
* @template POST
|
|
2872
|
+
*/
|
|
2873
|
+
```
|
|
2874
|
+
|
|
2875
|
+
#### reference:`WorkerResult_instance.data`
|
|
2876
|
+
- result value;
|
|
2877
|
+
|
|
2878
|
+
```js
|
|
2879
|
+
/**
|
|
2880
|
+
* @type {POST}
|
|
2881
|
+
*/
|
|
2882
|
+
```
|
|
2883
|
+
|
|
2884
|
+
#### reference:`WorkerResult_instance.error`
|
|
2885
|
+
- error value;
|
|
2886
|
+
|
|
2887
|
+
```js
|
|
2888
|
+
/**
|
|
2889
|
+
* @type {Error|string|undefined}
|
|
2890
|
+
*/
|
|
2891
|
+
```
|
|
2892
|
+
|
|
2893
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2894
|
+
|
|
2895
|
+
<h2 id="workerthread">WorkerThread</h2>
|
|
2896
|
+
|
|
2897
|
+
|
|
2898
|
+
#### reference:`WorkerThread`
|
|
2899
|
+
- class helper for `WorkerThread` creation;
|
|
2900
|
+
- before any `Worker` functionaily to be used, you need to setup it with `WorkerThread.setup` and `WorkerMainThread.setup` before runing anytyhing;
|
|
2901
|
+
|
|
2902
|
+
```js
|
|
2903
|
+
/**
|
|
2904
|
+
* @template RECEIVE
|
|
2905
|
+
* @template POST
|
|
2906
|
+
*/
|
|
2907
|
+
```
|
|
2908
|
+
|
|
2909
|
+
#### reference:`WorkerThread.setup`
|
|
2910
|
+
- need to be called and exported as new `WorkerThread` class reference;
|
|
2911
|
+
|
|
2912
|
+
```js
|
|
2913
|
+
/**
|
|
2914
|
+
* @template RECEIVE
|
|
2915
|
+
* @template POST
|
|
2916
|
+
* @param {{parentPort:import('worker_threads')["parentPort"]}} refs
|
|
2917
|
+
* -example:
|
|
2918
|
+
* ```js
|
|
2919
|
+
* import { parentPort } from 'node:worker_threads';
|
|
2920
|
+
* ```
|
|
2921
|
+
* @returns {typeof WorkerThread<RECEIVE, POST>}
|
|
2922
|
+
*/
|
|
2923
|
+
```
|
|
2924
|
+
- <i>example</i>:
|
|
2925
|
+
```js
|
|
2926
|
+
import { WorkerThread } from 'vivth';
|
|
2927
|
+
import { parentPort } from 'node:worker_threads';
|
|
2928
|
+
|
|
2929
|
+
export const MyWorkerThreadRef = WorkerThread.setup({ parentPort });
|
|
2930
|
+
|
|
2931
|
+
```
|
|
2932
|
+
|
|
2933
|
+
#### reference:`new WorkerThread`
|
|
2934
|
+
- instantiate via created class from `setup` static method;
|
|
2935
|
+
|
|
2936
|
+
```js
|
|
2937
|
+
/**
|
|
2938
|
+
* @param {WorkerThread["handler"]} handler
|
|
2939
|
+
*/
|
|
2940
|
+
```
|
|
2941
|
+
- <i>example</i>:
|
|
2942
|
+
```js
|
|
2943
|
+
import { MyWorkerThread } from './MyWorkerThread.mjs';
|
|
2944
|
+
|
|
2945
|
+
const doubleWorker = new MyWorkerThread((ev, isLastOnQ) => {
|
|
2946
|
+
// if(!isLastOnQ()) {
|
|
2947
|
+
// return null; // can be used for imperative debouncing;
|
|
2948
|
+
// }
|
|
2949
|
+
// await fetch('some/path')
|
|
2950
|
+
// if(!isLastOnQ()) {
|
|
2951
|
+
// return;
|
|
2952
|
+
// }
|
|
2953
|
+
return ev = ev * 2;
|
|
2954
|
+
});
|
|
2955
|
+
|
|
2956
|
+
```
|
|
2957
|
+
|
|
2958
|
+
#### reference:`WorkerThread_instance.handler`
|
|
2959
|
+
- type helper;
|
|
2960
|
+
|
|
2961
|
+
```js
|
|
2962
|
+
/**
|
|
2963
|
+
* @type {(ev: RECEIVE, isLastOnQ:QCBReturn["isLastOnQ"]) => POST}
|
|
2964
|
+
*/
|
|
2965
|
+
```
|
|
2966
|
+
|
|
2967
|
+
#### reference:`WorkerThread_instance.RECEIVE`
|
|
2968
|
+
- helper type, hold no actual value;
|
|
2969
|
+
|
|
2970
|
+
```js
|
|
2971
|
+
/**
|
|
2972
|
+
* @type {RECEIVE}
|
|
2973
|
+
*/
|
|
2974
|
+
```
|
|
2975
|
+
|
|
2976
|
+
#### reference:`WorkerThread_instance.POST`
|
|
2977
|
+
- helper type, hold no actual value;
|
|
2978
|
+
|
|
2979
|
+
```js
|
|
2980
|
+
/**
|
|
2981
|
+
* @type {POST}
|
|
2207
2982
|
*/
|
|
2208
2983
|
```
|
|
2984
|
+
|
|
2985
|
+
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
2986
|
+
|
|
2987
|
+
<h2 id="tobundledjsplugin">ToBundledJSPlugin</h2>
|
|
2988
|
+
|
|
2989
|
+
|
|
2990
|
+
#### reference:`ToBundledJSPlugin`
|
|
2991
|
+
- generate `esbuild.Plugin` for changing dev time file into runtime file;
|
|
2992
|
+
- on using esbuild with this plugin, it will replace any module that have similiar file name but ended with Bundled(before extname);
|
|
2993
|
+
>- works on `.mts`|`.ts`|`.mjs`|`.cjs`|`.js`;
|
|
2994
|
+
>- `anyFileName.mjs` -> seek for and use `anyFileNameBundled.mjs`, if not found use `anyFileName.mjs`;
|
|
2995
|
+
|
|
2996
|
+
```js
|
|
2997
|
+
/**
|
|
2998
|
+
* @param {string} includedInPath
|
|
2999
|
+
* - is generalized, you can freely uses forward or backward slash;
|
|
3000
|
+
* @returns {ReturnType<CreateESPlugin>}
|
|
3001
|
+
*/
|
|
3002
|
+
```
|
|
3003
|
+
- <i>example</i>:
|
|
3004
|
+
```js
|
|
3005
|
+
import { ToBundledJSPlugin } from 'vivth';
|
|
3006
|
+
|
|
3007
|
+
export const myBundledPlugin = ToBundledJSPlugin('/myProjectName/src/');
|
|
3008
|
+
|
|
3009
|
+
```
|
|
3010
|
+
|
|
2209
3011
|
*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|