vivth 0.11.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/.vivth/dist/init.mjs +24 -0
  2. package/README.md +2157 -69
  3. package/README.src.md +35 -0
  4. package/bun.lock +57 -3
  5. package/dev/index.mjs +24 -25
  6. package/index.mjs +51 -29
  7. package/package.json +11 -7
  8. package/src/bundler/CompileMJS.mjs +110 -0
  9. package/src/bundler/EsBundler.mjs +79 -0
  10. package/src/class/Console.mjs +62 -0
  11. package/src/class/Derived.mjs +36 -25
  12. package/src/class/Effect.mjs +106 -0
  13. package/src/class/EnvSignal.mjs +88 -0
  14. package/src/class/EventSignal.mjs +200 -0
  15. package/src/class/ListDerived.mjs +39 -0
  16. package/src/class/ListSignal.mjs +256 -0
  17. package/src/class/Paths.mjs +70 -0
  18. package/src/class/QChannel.mjs +184 -0
  19. package/src/class/SafeExit.mjs +131 -0
  20. package/src/class/Setup.mjs +73 -0
  21. package/src/class/Signal.mjs +152 -54
  22. package/src/class/WorkerMainThread.mjs +328 -0
  23. package/src/class/WorkerResult.mjs +30 -0
  24. package/src/class/WorkerThread.mjs +151 -0
  25. package/src/common/Base64URL.mjs +26 -0
  26. package/src/common/EventNameSpace.mjs +8 -0
  27. package/src/common/eventObjects.mjs +5 -0
  28. package/src/common/lazie.mjs +3 -0
  29. package/src/doc/JSautoDOC.mjs +386 -0
  30. package/src/doc/parsedFile.mjs +537 -0
  31. package/src/function/CreateImmutable.mjs +64 -0
  32. package/src/function/EventCheck.mjs +27 -0
  33. package/src/function/EventObject.mjs +21 -0
  34. package/src/function/IsAsync.mjs +23 -0
  35. package/src/function/LazyFactory.mjs +71 -0
  36. package/src/function/Timeout.mjs +23 -0
  37. package/src/function/Try.mjs +64 -0
  38. package/src/function/TryAsync.mjs +15 -4
  39. package/src/function/TrySync.mjs +9 -4
  40. package/src/function/TsToMjs.mjs +67 -0
  41. package/src/function/WriteFileSafe.mjs +37 -0
  42. package/src/types/{AnyButUndefined.type.mjs → AnyButUndefined.mjs} +1 -0
  43. package/src/types/ExtnameType.mjs +6 -0
  44. package/src/types/IsListSignal.mjs +6 -0
  45. package/src/types/ListArg.mjs +6 -0
  46. package/src/types/MutationType.mjs +8 -0
  47. package/src/types/QCBFIFOReturn.mjs +6 -0
  48. package/src/types/QCBReturn.mjs +6 -0
  49. package/tsconfig.json +3 -3
  50. package/types/dev/index.d.mts +1 -0
  51. package/types/index.d.mts +34 -8
  52. package/types/src/bundler/A.d.mts +1 -0
  53. package/types/src/bundler/CompileMJS.d.mts +8 -0
  54. package/types/src/bundler/EsBundler.d.mts +7 -0
  55. package/types/src/class/Console.d.mts +40 -0
  56. package/types/src/class/Derived.d.mts +21 -9
  57. package/types/src/class/Effect.d.mts +77 -0
  58. package/types/src/class/EnvSignal.d.mts +47 -0
  59. package/types/src/class/EventSignal.d.mts +145 -0
  60. package/types/src/class/ListDerived.d.mts +35 -0
  61. package/types/src/class/ListSignal.d.mts +150 -0
  62. package/types/src/class/Paths.d.mts +50 -0
  63. package/types/src/class/QChannel.d.mts +115 -0
  64. package/types/src/class/SafeExit.d.mts +76 -0
  65. package/types/src/class/Setup.d.mts +76 -0
  66. package/types/src/class/Signal.d.mts +105 -26
  67. package/types/src/class/WorkerMainThread.d.mts +149 -0
  68. package/types/src/class/WorkerResult.d.mts +25 -0
  69. package/types/src/class/WorkerThread.d.mts +70 -0
  70. package/types/src/common/Base64URL.d.mts +1 -0
  71. package/types/src/common/EventNameSpace.d.mts +6 -0
  72. package/types/src/common/eventObjects.d.mts +3 -0
  73. package/types/src/common/lazie.d.mts +1 -0
  74. package/types/src/doc/JSautoDOC.d.mts +76 -0
  75. package/types/src/doc/parsedFile.d.mts +154 -0
  76. package/types/src/function/CreateImmutable.d.mts +3 -0
  77. package/types/src/function/EventCheck.d.mts +2 -0
  78. package/types/src/function/EventObject.d.mts +4 -0
  79. package/types/src/function/IsAsync.d.mts +1 -0
  80. package/types/src/function/LazyFactory.d.mts +4 -0
  81. package/types/src/function/Timeout.d.mts +1 -0
  82. package/types/src/function/Try.d.mts +1 -0
  83. package/types/src/function/TsToMjs.d.mts +4 -0
  84. package/types/src/function/WriteFileSafe.d.mts +2 -0
  85. package/types/src/types/{AnyButUndefined.type.d.mts → AnyButUndefined.d.mts} +3 -0
  86. package/types/src/types/ExtnameType.d.mts +4 -0
  87. package/types/src/types/IsListSignal.d.mts +4 -0
  88. package/types/src/types/ListArg.d.mts +4 -0
  89. package/types/src/types/MutationType.d.mts +5 -0
  90. package/types/src/types/QCBFIFOReturn.d.mts +4 -0
  91. package/types/src/types/QCBReturn.d.mts +7 -0
  92. package/src/class/$.mjs +0 -68
  93. package/src/class/PingFIFO.mjs +0 -78
  94. package/src/class/PingUnique.mjs +0 -84
  95. package/src/class/Q.mjs +0 -98
  96. package/src/class/QFIFO.mjs +0 -66
  97. package/src/class/QUnique.mjs +0 -75
  98. package/src/common.mjs +0 -16
  99. package/src/function/NewQBlock.mjs +0 -39
  100. package/types/src/class/$.d.mts +0 -40
  101. package/types/src/class/PingFIFO.d.mts +0 -57
  102. package/types/src/class/PingUnique.d.mts +0 -48
  103. package/types/src/class/Q.d.mts +0 -63
  104. package/types/src/class/QFIFO.d.mts +0 -47
  105. package/types/src/class/QUnique.d.mts +0 -46
  106. package/types/src/common.d.mts +0 -2
  107. package/types/src/function/NewQBlock.d.mts +0 -1
@@ -0,0 +1,71 @@
1
+ // @ts-check
2
+
3
+ import { unwrapLazy } from '../common/lazie.mjs';
4
+
5
+ /**
6
+ * @description
7
+ * - function helper for creating lazyObject:
8
+ * >- usefull for lazily instantiating an object, since instance naturally have props/methods;
9
+ * @template T
10
+ * @param {() => T} factory
11
+ * @returns {T & {[unwrapLazy]: string}}
12
+ * - the unwrapLazy prop can be accessed to force instatiation/call;
13
+ * >- `unwrapLazy` prop name can be checked by checking the list of possible prop, from your ide;
14
+ * >- as of version 1.0.0, value is `vivth:unwrapLazy;`;
15
+ * @example
16
+ * import { LazyFactory } from 'vivth';
17
+ * class MyClass{
18
+ * constructor() {
19
+ * this.myProp = 1; // will only available when accessed;
20
+ * }
21
+ * }
22
+ *
23
+ * export const myInstance = LazyFactory(() => {
24
+ * // the instance of MyClass will only be available when,
25
+ * // it's prop, or method is accessed/reassign;
26
+ * return new MyClass();
27
+ * });
28
+ *
29
+ * // on other file
30
+ * import { myInstance } from './myInstance.mjs';
31
+ *
32
+ * const a = myInstance; // not yet initiated;
33
+ * const b = a.myProp // imediately initiated;
34
+ */
35
+ export const LazyFactory = (factory) => {
36
+ let instance;
37
+ // @ts-expect-error
38
+ return new Proxy(
39
+ {},
40
+ {
41
+ get(_, prop) {
42
+ if (!instance) {
43
+ instance = factory();
44
+ }
45
+ if (prop === unwrapLazy) {
46
+ if (!instance) {
47
+ instance = factory();
48
+ }
49
+ return instance;
50
+ }
51
+ return instance[prop];
52
+ },
53
+ set(_, prop, newValue) {
54
+ if (prop === unwrapLazy) {
55
+ instance = factory();
56
+ }
57
+ if (!instance) {
58
+ instance = factory();
59
+ }
60
+ instance[prop] = newValue;
61
+ return true;
62
+ },
63
+ apply(_, thisArg, args) {
64
+ if (!instance) {
65
+ instance = factory();
66
+ }
67
+ return Reflect.apply(instance, thisArg, args);
68
+ },
69
+ }
70
+ );
71
+ };
@@ -0,0 +1,23 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @description
5
+ * - function helper to temporarily block the code in async context;
6
+ * @param {number} timeMS
7
+ * - in miliseconds;
8
+ * @returns {Promise<void>}
9
+ * @example
10
+ * import { Timeout } from 'vivth';
11
+ *
12
+ * const test = async () => {
13
+ * // code0
14
+ * await Timeout(1000);
15
+ * // code1
16
+ * }
17
+ * test();
18
+ */
19
+ export const Timeout = (timeMS) => {
20
+ return new Promise((resolve) => {
21
+ setTimeout(resolve, timeMS);
22
+ });
23
+ };
@@ -0,0 +1,64 @@
1
+ // @ts-check
2
+
3
+ import { TryAsync } from './TryAsync.mjs';
4
+
5
+ /**
6
+ * @description
7
+ * - function for error as value for chained operations;
8
+ * - utility function to brute force which key is able to run;
9
+ * - usefull to flatten indentation for error handlings;
10
+ * - caveat:
11
+ * >- run in sequence, awaiting each key bofore running next key;
12
+ * @template {string} Key
13
+ * @template ReturnType_
14
+ * @template {Record<
15
+ * Key,
16
+ * () => Promise<ReturnType_>
17
+ * >} RecordTryType
18
+ * @param {RecordTryType} tryRecord
19
+ * @returns {Promise<
20
+ * [[keyof RecordTryType, ReturnType_], undefined]
21
+ * | [[undefined, undefined], Error]
22
+ * >}
23
+ * @example
24
+ * import { Try } from 'vivth';
25
+ *
26
+ * const [[key, result], error] = await Try({
27
+ * someRuntime: async () => {
28
+ * // asuming on this one doesn't naturally throw error,
29
+ * // yet you need to continue to next key,
30
+ * // instead of returning,
31
+ * // you should throw new Error(something);
32
+ * },
33
+ * browser: async () => {
34
+ * return location?.origin;
35
+ * // if no error, stop other key function from running;
36
+ * // key = 'browser'
37
+ * // result = location?.origin;
38
+ * // error = undefined;
39
+ * // if error;
40
+ * // run nodeOrBun;
41
+ * },
42
+ * nodeOrBun: async () => {
43
+ * return process?.env?.INIT_CWD ?? process?.cwd();
44
+ * // if no error;
45
+ * // key = 'nodeOrBun'
46
+ * // result = process?.env?.INIT_CWD ?? process?.cwd();
47
+ * // if error;
48
+ * // key = undefined;
49
+ * // result = undefined;
50
+ * // error = new Error('unable to run any key');
51
+ * },
52
+ * });
53
+ */
54
+ export const Try = async (tryRecord) => {
55
+ for (const key in tryRecord) {
56
+ const callback = tryRecord[key];
57
+ const [result, error] = await TryAsync(callback);
58
+ if (error) {
59
+ continue;
60
+ }
61
+ return [[key, result], undefined];
62
+ }
63
+ return [[undefined, undefined], new Error('unable to run any key')];
64
+ };
@@ -2,13 +2,24 @@
2
2
 
3
3
  /**
4
4
  * @description
5
- * - error as value for asynchronous operation
6
- * returns: Promise<[`ResultType`|`undefined`, `Error`|`undefined`]>
7
- */
8
- /**
5
+ * - function for error as value for asynchronous operation;
6
+ * - usefull to flatten indentation for error handlings;
9
7
  * @template ResultType
10
8
  * @param {()=>Promise<ResultType>} asyncFunction_
11
9
  * @returns {Promise<[ResultType|undefined, Error|undefined]>}
10
+ * @example
11
+ * import { TryAsync } from 'vivth';
12
+ *
13
+ * let [res, error] = await TryAsync(async () => {
14
+ * return await fetch('./someAPI/Path');
15
+ * });
16
+ *
17
+ * [res, error] = await TryAsync(async () => {
18
+ * if(!res.ok) {
19
+ * throw new Error(404);
20
+ * }
21
+ * return await res.json();
22
+ * })
12
23
  */
13
24
  export const TryAsync = async (asyncFunction_) => {
14
25
  try {
@@ -2,13 +2,18 @@
2
2
 
3
3
  /**
4
4
  * @description
5
- * - error as value for synchronous operation
6
- * returns: [`ResultType`|`undefined`, `Error`|`undefined`]
7
- */
8
- /**
5
+ * - function for error as value for synchronous operation;
6
+ * - usefull to flatten indentation for error handlings;
9
7
  * @template ResultType
10
8
  * @param {()=>ResultType} function_
11
9
  * @returns {[ResultType|undefined, Error|undefined]}
10
+ * @example
11
+ * import { readFileSync } from 'fs';
12
+ * import { TrySync } from './yourModule.js';
13
+ *
14
+ * const [data, error] = TrySync(() => {
15
+ * return readFileSync('./some/file.txt', 'utf-8');
16
+ * });
12
17
  */
13
18
  export const TrySync = (function_) => {
14
19
  try {
@@ -0,0 +1,67 @@
1
+ // @ts-check
2
+
3
+ import { readFile } from 'node:fs/promises';
4
+ import { basename, dirname, extname, join } from 'node:path';
5
+
6
+ import { transform } from 'esbuild';
7
+
8
+ import { Console } from '../class/Console.mjs';
9
+ import { Paths } from '../class/Paths.mjs';
10
+ import { TryAsync } from './TryAsync.mjs';
11
+ import { WriteFileSafe } from './WriteFileSafe.mjs';
12
+
13
+ /**
14
+ * @description
15
+ * - turn `.mts`||`.ts` file into `.mjs`, no bundling, just translation;
16
+ * - on certain circumstance where `.mjs` result needed to be typed, you need to manually add `jsdoc`;
17
+ * >- uses `"at"preserve` to register `jsdoc` inline;
18
+ * @param {string} path_
19
+ * @param {Object} [options]
20
+ * @param {string} [options.overrideDir]
21
+ * - default: write conversion to same directory;
22
+ * - path are relative to project root;
23
+ * @param {BufferEncoding} [options.encoding]
24
+ * - default: `utf-8`;
25
+ * @returns {Promise<void>}
26
+ * @example
27
+ * import { TsToMjs } from 'vivth';
28
+ *
29
+ * TsToMjs('./myFile.mts', { encoding: 'utf-8', overrideDir: './other/dir' });
30
+ */
31
+ export const TsToMjs = async (path_, { overrideDir = undefined, encoding = 'utf-8' } = {}) => {
32
+ const rootPath = Paths.normalize(Paths.root);
33
+ if (!Paths.normalize(path_).startsWith(rootPath)) {
34
+ path_ = Paths.normalize(join(rootPath, path_));
35
+ }
36
+ const ext = extname(path_);
37
+ if (ext !== '.ts' && ext !== '.mts') {
38
+ return;
39
+ }
40
+ const [content, error] = await TryAsync(async () => {
41
+ return await readFile(path_, { encoding });
42
+ });
43
+ if (error) {
44
+ Console.error(error);
45
+ return;
46
+ }
47
+ const [result, transformError] = await TryAsync(async () => {
48
+ return await transform(content, {
49
+ loader: 'ts',
50
+ format: 'esm',
51
+ sourcemap: false,
52
+ target: 'esnext',
53
+ legalComments: 'inline',
54
+ });
55
+ });
56
+ if (transformError) {
57
+ Console.error(transformError);
58
+ return;
59
+ }
60
+ const outputDir = overrideDir ? join(rootPath, overrideDir) : dirname(path_);
61
+ const outputPath = join(outputDir, basename(path_).replace(ext, '.mjs'));
62
+ const [_, writeError] = await WriteFileSafe(outputPath, result.code, { encoding });
63
+ if (!writeError) {
64
+ return;
65
+ }
66
+ Console.error(writeError);
67
+ };
@@ -0,0 +1,37 @@
1
+ // @ts-check
2
+
3
+ import { writeFile, mkdir } from 'node:fs/promises';
4
+ import { dirname, join } from 'node:path';
5
+
6
+ import { TryAsync } from './TryAsync.mjs';
7
+ import { Paths } from '../class/Paths.mjs';
8
+
9
+ /**
10
+ * @description
11
+ * - function to create file by recursively mkdir the dirname of the outFile;
12
+ * - also returing promise of result & error as value;
13
+ * @param {string} outFile
14
+ * @param {string} content
15
+ * @param {import('node:fs').WriteFileOptions} options
16
+ * @returns {Promise<ReturnType<typeof TryAsync<void>>>}
17
+ * @example
18
+ * import { WriteFileSafe } from 'vivth';
19
+ *
20
+ * const [_, writeError] = await TryAsync(async () => {
21
+ * return await WriteFileSafe(
22
+ * '/some/path.mjs',
23
+ * 'console.log("hello-world!!");',
24
+ * { encoding: 'utf-8' }
25
+ * );
26
+ * });
27
+ */
28
+ export const WriteFileSafe = async (outFile, content, options) => {
29
+ return await TryAsync(async () => {
30
+ const rootPath = Paths.normalize(Paths.root);
31
+ if (!Paths.normalize(outFile).startsWith(rootPath)) {
32
+ outFile = join(rootPath, outFile);
33
+ }
34
+ await mkdir(dirname(outFile), { recursive: true });
35
+ return writeFile(outFile, content, options);
36
+ });
37
+ };
@@ -1,5 +1,6 @@
1
1
  // @ts-check
2
2
 
3
3
  /**
4
+ * - type helper for ID or objects;
4
5
  * @typedef {{}|null|number|string|boolean|symbol|bigint|function} AnyButUndefined
5
6
  */
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * - jsRuntime extention naming convention;
5
+ * @typedef {`.${string}`} ExtnameType
6
+ */
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * - `EnvSignal.get` argument whether signal need to be a list or not;
5
+ * @typedef {boolean} IsListSignal
6
+ */
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * - ListSignal argument type;
5
+ * @typedef {Record<string, string>} ListArg
6
+ */
@@ -0,0 +1,8 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * - `ListSignal` mutation type;
5
+ * @typedef {'push'|'unshift'|'splice'|'swap'|'modify'|'shift'|'remove'} MutationType
6
+ * - instance method: serves as helper to mutate, and notify for `effects`;
7
+ * > - `slice` uses `splice` in the background, you don't need to manually reindex when using it;
8
+ */
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * - return type of Q callback fifo;
5
+ * @typedef {Omit<import("./QCBReturn.mjs").QCBReturn, "isLastOnQ">} QCBFIFOReturn
6
+ */
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * - return type of Q callback;
5
+ * @typedef {{resume:()=>void, isLastOnQ:boolean}} QCBReturn
6
+ */
package/tsconfig.json CHANGED
@@ -4,11 +4,11 @@
4
4
  "declaration": true,
5
5
  "emitDeclarationOnly": true,
6
6
  "outDir": "./types",
7
- "module": "ESNext",
8
- "target": "ES2020",
7
+ "module": "esnext",
8
+ "target": "esnext",
9
9
  "moduleResolution": "node",
10
10
  "esModuleInterop": true,
11
11
  "skipLibCheck": true
12
12
  },
13
- "include": ["./index.mjs", "./src"]
13
+ "include": ["./index.mjs", "./src", "./dev"]
14
14
  }
@@ -0,0 +1 @@
1
+ export {};
package/types/index.d.mts CHANGED
@@ -1,12 +1,38 @@
1
- export { $ } from "./src/class/$.mjs";
1
+ export { CompileMJS } from "./src/bundler/CompileMJS.mjs";
2
+ export { EsBundler } from "./src/bundler/EsBundler.mjs";
3
+ export { Console } from "./src/class/Console.mjs";
2
4
  export { Derived } from "./src/class/Derived.mjs";
3
- export { PingFIFO } from "./src/class/PingFIFO.mjs";
4
- export { PingUnique } from "./src/class/PingUnique.mjs";
5
- export { Q } from "./src/class/Q.mjs";
6
- export { QFIFO } from "./src/class/QFIFO.mjs";
7
- export { QUnique } from "./src/class/QUnique.mjs";
5
+ export { Effect } from "./src/class/Effect.mjs";
6
+ export { EnvSignal } from "./src/class/EnvSignal.mjs";
7
+ export { EventSignal } from "./src/class/EventSignal.mjs";
8
+ export { ListDerived } from "./src/class/ListDerived.mjs";
9
+ export { ListSignal } from "./src/class/ListSignal.mjs";
10
+ export { Paths } from "./src/class/Paths.mjs";
11
+ export { QChannel } from "./src/class/QChannel.mjs";
12
+ export { SafeExit } from "./src/class/SafeExit.mjs";
13
+ export { Setup } from "./src/class/Setup.mjs";
8
14
  export { Signal } from "./src/class/Signal.mjs";
9
- export { NewQBlock } from "./src/function/NewQBlock.mjs";
15
+ export { WorkerMainThread } from "./src/class/WorkerMainThread.mjs";
16
+ export { WorkerResult } from "./src/class/WorkerResult.mjs";
17
+ export { WorkerThread } from "./src/class/WorkerThread.mjs";
18
+ export { Base64URL } from "./src/common/Base64URL.mjs";
19
+ export { EventNameSpace } from "./src/common/EventNameSpace.mjs";
20
+ export { JSautoDOC } from "./src/doc/JSautoDOC.mjs";
21
+ export { CreateImmutable } from "./src/function/CreateImmutable.mjs";
22
+ export { EventCheck } from "./src/function/EventCheck.mjs";
23
+ export { EventObject } from "./src/function/EventObject.mjs";
24
+ export { IsAsync } from "./src/function/IsAsync.mjs";
25
+ export { LazyFactory } from "./src/function/LazyFactory.mjs";
26
+ export { Timeout } from "./src/function/Timeout.mjs";
27
+ export { Try } from "./src/function/Try.mjs";
10
28
  export { TryAsync } from "./src/function/TryAsync.mjs";
11
29
  export { TrySync } from "./src/function/TrySync.mjs";
12
- export type AnyButUndefined = {} | null | number | string | boolean | symbol | bigint | Function;
30
+ export { TsToMjs } from "./src/function/TsToMjs.mjs";
31
+ export { WriteFileSafe } from "./src/function/WriteFileSafe.mjs";
32
+ export type AnyButUndefined = import("./src/types/AnyButUndefined.mjs").AnyButUndefined;
33
+ export type ExtnameType = import("./src/types/ExtnameType.mjs").ExtnameType;
34
+ export type IsListSignal = import("./src/types/IsListSignal.mjs").IsListSignal;
35
+ export type ListArg = import("./src/types/ListArg.mjs").ListArg;
36
+ export type MutationType = import("./src/types/MutationType.mjs").MutationType;
37
+ export type QCBFIFOReturn = import("./src/types/QCBFIFOReturn.mjs").QCBFIFOReturn;
38
+ export type QCBReturn = import("./src/types/QCBReturn.mjs").QCBReturn;
@@ -0,0 +1 @@
1
+ export function A(entryPoint: string, encoding: BufferEncoding, output: string, minify?: boolean): Promise<void>;
@@ -0,0 +1,8 @@
1
+ export function CompileMJS({ entryPoint, encoding, outputNoExt, minify, asBinary, }: {
2
+ entryPoint: string;
3
+ outputNoExt: string;
4
+ encoding: BufferEncoding;
5
+ minify?: boolean;
6
+ asBinary?: boolean;
7
+ }): Promise<ReturnType<typeof WriteFileSafe>>;
8
+ import { WriteFileSafe } from '../function/WriteFileSafe.mjs';
@@ -0,0 +1,7 @@
1
+ export function EsBundler({ content, extension, asBinary }: {
2
+ content: string;
3
+ extension: string;
4
+ asBinary?: boolean;
5
+ }, esbuildOptions?: Omit<Parameters<typeof build>[0], "entryPoints" | "bundle" | "write" | "format" | "sourcemap" | "external" | "stdin">): Promise<ReturnType<typeof TryAsync<string>>>;
6
+ import { build } from 'esbuild';
7
+ import { TryAsync } from '../function/TryAsync.mjs';
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @description
3
+ * - class with static methods to print to standard console with added style;
4
+ */
5
+ export class Console {
6
+ /**
7
+ * @param {string} prefix
8
+ * @param {'log'|'info'|'error'|'warn'} mode
9
+ * - Console method to use
10
+ * @param {any} data
11
+ * @param {string} color
12
+ * @param {string} bgcolor
13
+ * @returns {void}
14
+ */
15
+ static "__#1@#call": (prefix: string, mode: "log" | "info" | "error" | "warn", data: any, color: string, bgcolor: string) => void;
16
+ /**
17
+ * @description
18
+ * @param {any} data
19
+ * @returns {void}
20
+ */
21
+ static log: (data: any) => void;
22
+ /**
23
+ * @description
24
+ * @param {any} data
25
+ * @returns {void}
26
+ */
27
+ static info: (data: any) => void;
28
+ /**
29
+ * @description
30
+ * @param {any} data
31
+ * @returns {void}
32
+ */
33
+ static warn: (data: any) => void;
34
+ /**
35
+ * @description
36
+ * @param {any} data
37
+ * @returns {void}
38
+ */
39
+ static error: (data: any) => void;
40
+ }
@@ -1,19 +1,31 @@
1
1
  /**
2
2
  * @description
3
- * - a class for creating signal which its value are derived from other signal (`Derived` and `Signal` alike);
4
- * - can be subscribed by using [New$](#new$);
5
- * - for minimal total bundle size use `function` [NewDerived](#newderived) instead;
6
- */
7
- /**
3
+ * - a class for creating derived version of [Signal](#signal);
8
4
  * @template V
9
5
  * @extends Signal<V>
10
6
  */
11
7
  export class Derived<V> extends Signal<V> {
12
8
  /**
13
- * @param {(arg0:{remove$:()=>void})=>V} derivedFunction
9
+ * @description
10
+ * - Derived used [Signal](#signal) and [Effect](#effect) under the hood;
11
+ * @param {(effectInstanceOptions:Effect["options"])=>Promise<V>} derivedFunction
12
+ * @example
13
+ * import { Signal, Derived } from 'vivth';
14
+ *
15
+ * const count = new Signal(0);
16
+ * const double = new Derived(async({
17
+ * subscribe,
18
+ * // : registrar callback for this derived instance, immediately return the signal instance
19
+ * }) => {
20
+ * return subscribe(count).value + count.value;
21
+ * // double listen to count changes, by returning the value, double.value also changes
22
+ * // notice the count.value are accessed double, but it's all safe,
23
+ * // since the wrapped one is the only one that are recorded as notifier.
24
+ * });
25
+ *
26
+ * count.value++;
14
27
  */
15
- constructor(derivedFunction: (arg0: {
16
- remove$: () => void;
17
- }) => V);
28
+ constructor(derivedFunction: (effectInstanceOptions: Effect["options"]) => Promise<V>);
18
29
  }
19
30
  import { Signal } from './Signal.mjs';
31
+ import { Effect } from './Effect.mjs';
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @type {Set<Effect>}
3
+ */
4
+ export const setOfEffects: Set<Effect>;
5
+ /**
6
+ * @description
7
+ * - a class for creating effect;
8
+ * - compared to previous class <b>$</b> reactivity model, `Effect`:
9
+ * >- doesn't autosubscribe at first run;
10
+ * >- it is using passed <b>$</b> named `arg0` options as subscriber;
11
+ * >- doesn't block other queues during first run(previously blocks other queues to safely register signal autoscubscriber);
12
+ * >- now can dynamically subscribes to signal, even on conditionals, that are not run during first run;
13
+ */
14
+ export class Effect {
15
+ /**
16
+ * @description
17
+ * @param {(arg0:Effect["options"])=>Promise<void>} effect
18
+ * @example
19
+ * import { Signal, Derived, Effect, Console } from 'vivth';
20
+ *
21
+ * const count = new Signal(0);
22
+ * const double = new Derived( async({$}) => $(count).value \* 2); // double listen to count changes
23
+ * new Effect(async ({
24
+ * subscribe, // : registrar callback for this effect instance, immediately return the signal instance
25
+ * removeEffect, // : disable this effect instance from reacting to dependency changes;
26
+ * }) => {
27
+ * Console.log(subscribe(double).value); // effect listen to double changes
28
+ * const a = double.value; // no need to wrap double twice with $
29
+ * })
30
+ *
31
+ * count.value++;
32
+ */
33
+ constructor(effect: (arg0: Effect["options"]) => Promise<void>);
34
+ options: {
35
+ /**
36
+ * @instance options
37
+ * @description
38
+ * - normally it's passed as argument to constructor, however it is also accessible from `options` property;
39
+ * @template {Signal} S
40
+ * @param {S} signal
41
+ * @returns {S}
42
+ * @example
43
+ * const effect = new Effect(async () => {
44
+ * // code
45
+ * })
46
+ * effect.options.subscribe(signalInstance);
47
+ */
48
+ subscribe: <S extends Signal<any>>(signal: S) => S;
49
+ /**
50
+ * @instance options
51
+ * @description
52
+ * - normally it's passed as argument to constructor, however it is also accessible from `options` property;
53
+ * @type {()=>void}
54
+ * @example
55
+ * const effect = new Effect(async () => {
56
+ * // code
57
+ * })
58
+ * effect.options.removeEffect();
59
+ */
60
+ removeEffect: () => void;
61
+ } & {
62
+ "vivth:unwrapLazy;": string;
63
+ };
64
+ /**
65
+ * @description
66
+ * - normally is to let to be automatically run when dependency signals changes, however it's also accessible as instance method;
67
+ * @returns {void}
68
+ * @example
69
+ * const effect = new Effect(async ()=>{
70
+ * // code
71
+ * })
72
+ * effect.run();
73
+ */
74
+ run: () => void;
75
+ #private;
76
+ }
77
+ import { Signal } from './Signal.mjs';
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @description
3
+ * - non browser API;
4
+ * - uses [Signal](#signal) and [Derived](#derived) under the hood;
5
+ * @template V
6
+ */
7
+ export class EnvSignal<V> {
8
+ /**
9
+ * @description
10
+ * - create `EnvSignal` instance;
11
+ * @param {V} initialValue
12
+ */
13
+ constructor(initialValue: V);
14
+ /**
15
+ * @description
16
+ * - exposed property to listen to;
17
+ * @type {Derived<V>}
18
+ * @example
19
+ * import { EnvSignal, Effect } from 'vivth';
20
+ *
21
+ * export const myEnv = new EnvSignal(true);
22
+ * new Effect(async ({ subscribe }) => {
23
+ * const myEnvValue = subscribe(myEnv.env).value;
24
+ * // code
25
+ * })
26
+ */
27
+ env: Derived<V>;
28
+ /**
29
+ * @description
30
+ * - call to correct the value of previously declared value;
31
+ * - can only be called once;
32
+ * @param {V} correctedValue
33
+ * @returns {void}
34
+ * @example
35
+ * import { EnvSignal } from 'vivth';
36
+ *
37
+ * export const myEnv = new EnvSignal(true);
38
+ *
39
+ * // somewhere else on the program
40
+ * import { myEnv } from '$src/myEnv'
41
+ *
42
+ * myEnv.correction(false); // this will notify all subscribers;
43
+ */
44
+ correction: (correctedValue: V) => void;
45
+ #private;
46
+ }
47
+ import { Derived } from './Derived.mjs';