vivth 0.9.4 → 0.11.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 (38) hide show
  1. package/README.md +21 -46
  2. package/dev/index.mjs +2 -0
  3. package/index.mjs +8 -8
  4. package/package.json +1 -1
  5. package/src/class/$.mjs +39 -29
  6. package/src/class/Derived.mjs +27 -11
  7. package/src/class/PingFIFO.mjs +78 -0
  8. package/src/class/PingUnique.mjs +84 -0
  9. package/src/class/Q.mjs +22 -12
  10. package/src/class/Signal.mjs +31 -23
  11. package/src/function/NewQBlock.mjs +39 -0
  12. package/src/function/{tryAsync.export.mjs → TryAsync.mjs} +1 -1
  13. package/src/function/{trySync.export.mjs → TrySync.mjs} +1 -1
  14. package/src/types/{anyButUndefined.type.mjs → AnyButUndefined.type.mjs} +1 -1
  15. package/tsconfig.json +1 -1
  16. package/types/index.d.mts +6 -8
  17. package/types/src/class/$.d.mts +26 -13
  18. package/types/src/class/Derived.d.mts +18 -4
  19. package/types/src/class/PingFIFO.d.mts +57 -0
  20. package/types/src/class/PingUnique.d.mts +48 -0
  21. package/types/src/class/Q.d.mts +12 -5
  22. package/types/src/class/Signal.d.mts +15 -14
  23. package/types/src/function/NewQBlock.d.mts +1 -0
  24. package/types/src/function/{tryAsync.export.d.mts → TryAsync.d.mts} +1 -1
  25. package/types/src/function/TrySync.d.mts +1 -0
  26. package/types/src/types/AnyButUndefined.type.d.mts +1 -0
  27. package/src/function/New$.mjs +0 -28
  28. package/src/function/NewDerived.mjs +0 -28
  29. package/src/function/NewPingFIFO.mjs +0 -91
  30. package/src/function/NewPingUnique.mjs +0 -92
  31. package/src/function/NewSignal.mjs +0 -28
  32. package/types/src/function/New$.d.mts +0 -2
  33. package/types/src/function/NewDerived.d.mts +0 -2
  34. package/types/src/function/NewPingFIFO.d.mts +0 -1
  35. package/types/src/function/NewPingUnique.d.mts +0 -1
  36. package/types/src/function/NewSignal.d.mts +0 -2
  37. package/types/src/function/trySync.export.d.mts +0 -1
  38. package/types/src/types/anyButUndefined.type.d.mts +0 -1
@@ -1,91 +0,0 @@
1
- // @ts-check
2
-
3
- import { timeout } from '../common.mjs';
4
-
5
- /**
6
- * @description
7
- * - function to auto queue callbacks that will be called `first in first out` style;
8
- * ```js
9
- * // @ts-check
10
- * import { NewPingFIFO } from 'vivth';
11
- * const debounceMS = 0; // in miliseconds, optionals, default is 0;
12
- * const handler = () =>{
13
- * NewPingFIFO(async () => {
14
- * // your code
15
- * }, debounceMS);
16
- * }
17
- * ```
18
- */
19
- class qFIFO {
20
- static {
21
- new qFIFO();
22
- }
23
- /**
24
- * @typedef {[callback:()=>(any|Promise<any>),debounce?:(number)]} queueFIFODetails
25
- */
26
- /**
27
- * @private
28
- */
29
- constructor() {}
30
- /**
31
- * queue
32
- * @private
33
- * @type {queueFIFODetails[]}
34
- */
35
- static q = [];
36
- /**
37
- * isRunning
38
- * @private
39
- * @type {boolean}
40
- */
41
- static R = false;
42
- /**
43
- * auto initiator
44
- * @private
45
- */
46
- /**
47
- * assign
48
- * @type {(...queueFIFODetails:queueFIFODetails)=>void}
49
- */
50
- static a = (..._queue) => {
51
- qFIFO.p(_queue);
52
- if (!qFIFO.R) {
53
- qFIFO.r();
54
- }
55
- };
56
- /**
57
- * push
58
- * @private
59
- * @param {queueFIFODetails} _queue
60
- */
61
- static p = (_queue) => {
62
- qFIFO.q.push(_queue);
63
- };
64
- /**
65
- * run
66
- * @private
67
- */
68
- static r = async () => {
69
- qFIFO.R = true;
70
- while (qFIFO.q.length !== 0) {
71
- const [callback, debounceMs = 0] = qFIFO.q[0];
72
- qFIFO.q.shift();
73
- await callback();
74
- /**
75
- * conditional debounce;
76
- * queue FIFO messing up when have debouncer while `debounceMS` are set to 0;
77
- */
78
- // if (debounceMs) {
79
- await timeout(debounceMs);
80
- // }
81
- }
82
- qFIFO.R = false;
83
- };
84
- }
85
-
86
- /**
87
- * @param {()=>(any|Promise<any>)} callback
88
- * @param {number} debounce
89
- * @returns
90
- */
91
- export const NewPingFIFO = (callback, debounce = 0) => qFIFO.a(callback, debounce);
@@ -1,92 +0,0 @@
1
- // @ts-check
2
-
3
- import { timeout } from '../common.mjs';
4
-
5
- /**
6
- * @description
7
- * - function to auto queue callbacks:
8
- * > - different `uniqueID`: called `first in first out` style;
9
- * > - same `uniqueID`: will be grouped, only the already running callback and the last callback will be called;
10
- * ```js
11
- * // @ts-check
12
- * import { NewPingUnique } from 'vivth';
13
- * const uniqueID = 'yourUniqueID'; // can be anything, even a reference to an object;
14
- * const debounceMS = 0; // in miliseconds, optionals, default is 0;
15
- * const handler = () =>{
16
- * NewPingUnique(uniqueID, async () => {
17
- * // your code
18
- * }, debounceMS);
19
- * }
20
- * ```
21
- */
22
- class qUnique {
23
- /**
24
- * @typedef {import('../types/anyButUndefined.type.mjs').anyButUndefined} anyButUndefined
25
- * @typedef {Object} queueUniqueObject
26
- * @property {any} i
27
- * @property {()=>(any|Promise<any>)} c
28
- * @property {number} [d]
29
- */
30
- /**
31
- * @private
32
- * @type {Map<any, [()=>Promise<any>,number]>}
33
- */
34
- static queue = new Map();
35
- /**
36
- * isRunning
37
- * @private
38
- * @type {boolean}
39
- */
40
- static r = false;
41
- /**
42
- * assign
43
- * @type {(queueUniqueObject:queueUniqueObject)=>void}
44
- */
45
- static A = (_queue) => {
46
- qUnique.p(_queue);
47
- if (!qUnique.r) {
48
- qUnique.R();
49
- }
50
- };
51
- /**
52
- * push
53
- * @private
54
- * @param {queueUniqueObject} _queue
55
- */
56
- static p = (_queue) => {
57
- const { i, c, d } = _queue;
58
- qUnique.queue.set(i, [c, d ? d : 0]);
59
- };
60
- /**
61
- * run
62
- * @private
63
- */
64
- static R = async () => {
65
- qUnique.r = true;
66
- const keysIterator = qUnique.queue.keys();
67
- let keys = keysIterator.next();
68
- while (!keys.done) {
69
- const key = keys.value;
70
- const [callback, debounce] = qUnique.queue.get(key);
71
- qUnique.queue.delete(key);
72
- /**
73
- * debounce anyway;
74
- * queue with unique id have characteristic of messing up when have no debouncer;
75
- * especially when request comes too fast;
76
- */
77
- await timeout(debounce);
78
- await callback();
79
- keys = keysIterator.next();
80
- }
81
- qUnique.r = false;
82
- };
83
- }
84
-
85
- /**
86
- * @param {anyButUndefined} uniqueID
87
- * @param {()=>(any|Promise<any>)} callback
88
- * @param {number} debounce
89
- * @returns
90
- */
91
- export const NewPingUnique = (uniqueID, callback, debounce = 0) =>
92
- qUnique.A({ i: uniqueID, c: callback, d: debounce });
@@ -1,28 +0,0 @@
1
- // @ts-check
2
-
3
- import { Signal } from '../class/Signal.mjs';
4
-
5
- /**
6
- * @description
7
- * - function to create `signal`;
8
- * - syntatic sugar for [Signal](#signal);
9
- * ```js
10
- * import { New$, NewDerived, NewSignal } from 'vivth';
11
- * const signal = NewSignal(0);
12
- * const derived = NewDerived(async () =>{
13
- * // runs everytime signal.value changes;
14
- * return signal.value * 2;
15
- * });
16
- * const autosubscriber = New$(async ()=>{
17
- * // runs everytime signal.value changes;
18
- * console.log(signal.value);
19
- * });
20
- * signal.value = 1;
21
- * ```
22
- */
23
- /**
24
- * @template Value
25
- * @param {Value} value
26
- * @returns {Signal<Value>}
27
- */
28
- export const NewSignal = (value) => new Signal(value);
@@ -1,2 +0,0 @@
1
- export function New$(effect: () => void): $;
2
- import { $ } from '../class/$.mjs';
@@ -1,2 +0,0 @@
1
- export function NewDerived<V>(derivedFunction: () => (V)): Derived<V>;
2
- import { Derived } from '../class/Derived.mjs';
@@ -1 +0,0 @@
1
- export function NewPingFIFO(callback: () => (any | Promise<any>), debounce?: number): void;
@@ -1 +0,0 @@
1
- export function NewPingUnique(uniqueID: import("../types/anyButUndefined.type.mjs").anyButUndefined, callback: () => (any | Promise<any>), debounce?: number): void;
@@ -1,2 +0,0 @@
1
- export function NewSignal<Value>(value: Value): Signal<Value>;
2
- import { Signal } from '../class/Signal.mjs';
@@ -1 +0,0 @@
1
- export function trySync<ResultType>(function_: () => ResultType): [ResultType | undefined, Error | undefined];
@@ -1 +0,0 @@
1
- export type anyButUndefined = {} | null | number | string | boolean | symbol | bigint | Function;