@esportsplus/reactivity 0.4.7 → 0.5.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/src/types.ts CHANGED
@@ -1,82 +1,61 @@
1
- import { Function, NeverAsync, Prettify } from '@esportsplus/utilities'
1
+ import { REACTIVE, STATE_CHECK, STATE_DIRTY, STATE_IN_HEAP, STATE_NONE, STATE_RECOMPUTING } from './constants';
2
+ import { oncleanup } from './signal';
2
3
  import { ReactiveArray } from './reactive/array';
3
4
  import { ReactiveObject } from './reactive/object';
4
- import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL } from './constants';
5
- import { Reactive as ReactiveBase } from './signal';
6
5
 
7
6
 
8
- type Base<T> = Omit<ReactiveBase<T>, 'changed' | 'fn' | 'get' | 'scheduler' | 'set' | 'task' | 'tracking'>;
9
-
10
- type Changed = (a: unknown, b: unknown) => boolean;
11
-
12
- type Computed<T> = {
13
- changed: Changed;
14
- fn: NeverAsync<(instance: Computed<T>) => T>;
15
- get(): T;
16
- } & Base<T>;
17
-
18
- type Effect = {
19
- fn: NeverAsync<(instance: Effect) => void>;
20
- root: Root;
21
- task: Function;
22
- } & Omit<Base<void>, 'value'>;
7
+ interface Computed<T> extends Signal<T> {
8
+ [REACTIVE]: true;
9
+ cleanup: VoidFunction | VoidFunction[] | null;
10
+ deps: Link | null;
11
+ depsTail: Link | null;
12
+ fn: (oc?: typeof oncleanup) => T;
13
+ height: number;
14
+ nextHeap: Computed<unknown> | undefined;
15
+ prevHeap: Computed<unknown>;
16
+ state:
17
+ typeof STATE_CHECK |
18
+ typeof STATE_DIRTY |
19
+ typeof STATE_IN_HEAP |
20
+ typeof STATE_NONE |
21
+ typeof STATE_RECOMPUTING;
22
+ }
23
23
 
24
24
  type Infer<T> =
25
25
  T extends (...args: unknown[]) => unknown
26
26
  ? ReturnType<T>
27
27
  : T extends (infer U)[]
28
28
  ? ReactiveArray<U>
29
- : T extends ReactiveObject<T>
30
- ? ReactiveObject<T>
29
+ : T extends ReactiveObject<any>
30
+ ? T
31
31
  : T extends Record<PropertyKey, unknown>
32
32
  ? { [K in keyof T]: T[K] }
33
33
  : T;
34
34
 
35
- type Event = 'cleanup' | 'dispose' | 'update' | string;
36
-
37
- type Listener<D> = {
38
- once?: boolean;
39
-
40
- <V>(data: D, value: V): void;
41
- };
42
-
43
- type Options = {
44
- changed?: Changed;
45
- };
35
+ interface Link {
36
+ dep: Signal<unknown> | Computed<unknown>;
37
+ sub: Computed<unknown>;
38
+ nextDep: Link | null;
39
+ nextSub: Link | null;
40
+ prevSub: Link | null;
41
+ }
46
42
 
47
43
  type Reactive<T> = T extends Record<PropertyKey, unknown>
48
44
  ? ReactiveObject<T>
49
45
  : ReactiveArray<T>;
50
46
 
51
- type Root = {
52
- scheduler: Scheduler;
53
- tracking: boolean;
54
- value: void;
55
- } & Omit<ReactiveBase<void>, 'root'>;
56
-
57
- type Scheduler = (fn: Function) => unknown;
58
-
59
47
  type Signal<T> = {
60
- changed: Changed;
61
- get(): T;
62
- set(value: T): T;
63
- } & Base<T>;
64
-
65
- type State = typeof CHECK | typeof CLEAN | typeof DIRTY | typeof DISPOSED;
66
-
67
- type Type = typeof COMPUTED | typeof EFFECT | typeof ROOT | typeof SIGNAL;
48
+ [REACTIVE]: true;
49
+ subs: Link | null;
50
+ subsTail: Link | null;
51
+ value: T;
52
+ };
68
53
 
69
54
 
70
55
  export type {
71
- Changed, Computed,
72
- Effect, Event,
73
- Function,
56
+ Computed,
74
57
  Infer,
75
- Listener,
76
- NeverAsync,
77
- Options,
78
- Prettify,
79
- Reactive, ReactiveArray, ReactiveObject, Root,
80
- Scheduler, Signal, State,
81
- Type
58
+ Link,
59
+ Signal,
60
+ Reactive, ReactiveArray, ReactiveObject
82
61
  };
package/build/macro.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { Options } from './types.js';
3
- declare class Macro<A extends unknown[], R> extends CustomFunction {
4
- private factory;
5
- constructor(fn: Macro<A, R>['factory']['fn'], options?: Options);
6
- dispose(): void;
7
- }
8
- declare const _default: <A extends unknown[], R>(fn: Macro<A, R>["factory"]["fn"], options?: Options) => Macro<A, R>;
9
- export default _default;
package/build/macro.js DELETED
@@ -1,17 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal.js';
3
- class Macro extends CustomFunction {
4
- factory;
5
- constructor(fn, options = {}) {
6
- super((...args) => {
7
- return this.factory.get()(...args);
8
- });
9
- this.factory = computed(fn, options);
10
- }
11
- dispose() {
12
- this.factory.dispose();
13
- }
14
- }
15
- export default (fn, options = {}) => {
16
- return new Macro(fn, options);
17
- };
package/src/macro.ts DELETED
@@ -1,26 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal';
3
- import { Computed, Options } from './types';
4
-
5
-
6
- class Macro<A extends unknown[], R> extends CustomFunction {
7
- private factory: Computed<(...args: A) => R>;
8
-
9
-
10
- constructor(fn: Macro<A,R>['factory']['fn'], options: Options = {}) {
11
- super((...args: A) => {
12
- return this.factory.get()(...args);
13
- });
14
- this.factory = computed(fn, options);
15
- }
16
-
17
-
18
- dispose() {
19
- this.factory.dispose();
20
- }
21
- }
22
-
23
-
24
- export default <A extends unknown[], R>(fn: Macro<A,R>['factory']['fn'], options: Options = {}) => {
25
- return new Macro(fn, options);
26
- };