@esportsplus/reactivity 0.1.9 → 0.1.10
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/build/constants.d.ts +3 -2
- package/build/constants.js +3 -2
- package/build/dispose.d.ts +4 -0
- package/build/dispose.js +14 -0
- package/build/index.d.ts +1 -2
- package/build/index.js +1 -2
- package/build/macro.d.ts +3 -2
- package/build/macro.js +3 -3
- package/build/reactive/array.d.ts +1 -1
- package/build/reactive/array.js +5 -4
- package/build/reactive/index.js +1 -1
- package/build/reactive/object.js +6 -6
- package/build/reset.d.ts +4 -0
- package/build/reset.js +14 -0
- package/build/resource.js +13 -13
- package/build/signal.d.ts +46 -24
- package/build/signal.js +156 -105
- package/build/types.d.ts +4 -18
- package/build/types.js +2 -2
- package/package.json +2 -2
- package/src/constants.ts +4 -2
- package/src/index.ts +1 -2
- package/src/macro.ts +4 -4
- package/src/reactive/array.ts +4 -4
- package/src/reactive/index.ts +1 -1
- package/src/reactive/object.ts +5 -6
- package/src/resource.ts +14 -14
- package/src/signal.ts +211 -134
- package/src/types.ts +4 -22
package/build/constants.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ declare const DIRTY = 2;
|
|
|
4
4
|
declare const DISPOSED = 3;
|
|
5
5
|
declare const COMPUTED = 0;
|
|
6
6
|
declare const EFFECT = 1;
|
|
7
|
-
declare const
|
|
8
|
-
|
|
7
|
+
declare const ROOT = 2;
|
|
8
|
+
declare const SIGNAL = 3;
|
|
9
|
+
export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL };
|
package/build/constants.js
CHANGED
|
@@ -4,5 +4,6 @@ const DIRTY = 2;
|
|
|
4
4
|
const DISPOSED = 3;
|
|
5
5
|
const COMPUTED = 0;
|
|
6
6
|
const EFFECT = 1;
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const ROOT = 2;
|
|
8
|
+
const SIGNAL = 3;
|
|
9
|
+
export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL };
|
package/build/dispose.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isArray } from './utilities';
|
|
2
|
+
export default (dispose) => {
|
|
3
|
+
if (dispose == null) {
|
|
4
|
+
}
|
|
5
|
+
else if (isArray(dispose)) {
|
|
6
|
+
for (let i = 0, n = dispose.length; i < n; i++) {
|
|
7
|
+
dispose[i].dispose();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
dispose.dispose();
|
|
12
|
+
}
|
|
13
|
+
return dispose;
|
|
14
|
+
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { default as macro } from './macro';
|
|
2
2
|
export { default as resource } from './resource';
|
|
3
|
-
export * as core from './signal';
|
|
4
|
-
export { effect, root } from './signal';
|
|
5
3
|
export { default as reactive } from './reactive';
|
|
4
|
+
export { computed, dispose, effect, reset, root, signal } from './signal';
|
|
6
5
|
export * from './types';
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { default as macro } from './macro';
|
|
2
2
|
export { default as resource } from './resource';
|
|
3
|
-
export * as core from './signal';
|
|
4
|
-
export { effect, root } from './signal';
|
|
5
3
|
export { default as reactive } from './reactive';
|
|
4
|
+
export { computed, dispose, effect, reset, root, signal } from './signal';
|
|
6
5
|
export * from './types';
|
package/build/macro.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import { Computed
|
|
2
|
+
import { Computed } from './signal';
|
|
3
|
+
import { Options } from './types';
|
|
3
4
|
type Function<A extends unknown[], R> = Computed<(...args: A) => R>['fn'];
|
|
4
5
|
declare class Macro<A extends unknown[], R> extends CustomFunction {
|
|
5
6
|
#private;
|
|
@@ -7,5 +8,5 @@ declare class Macro<A extends unknown[], R> extends CustomFunction {
|
|
|
7
8
|
dispose(): void;
|
|
8
9
|
reset(): void;
|
|
9
10
|
}
|
|
10
|
-
declare const _default: <A extends unknown[], R>(fn: (
|
|
11
|
+
declare const _default: <A extends unknown[], R>(fn: () => (...args: A) => R, options?: Options) => Macro<A, R>;
|
|
11
12
|
export default _default;
|
package/build/macro.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import {
|
|
2
|
+
import { Computed } from './signal';
|
|
3
3
|
class Macro extends CustomFunction {
|
|
4
4
|
#factory;
|
|
5
5
|
constructor(fn, options = {}) {
|
|
6
6
|
super((...args) => {
|
|
7
|
-
return
|
|
7
|
+
return this.#factory.get()(...args);
|
|
8
8
|
});
|
|
9
|
-
this.#factory =
|
|
9
|
+
this.#factory = new Computed(fn, options);
|
|
10
10
|
}
|
|
11
11
|
dispose() {
|
|
12
12
|
this.#factory.dispose();
|
|
@@ -54,7 +54,7 @@ declare class ReactiveObjectArray<T extends Object> extends ReactiveArray<Node<T
|
|
|
54
54
|
push(...values: T[]): number;
|
|
55
55
|
shift(): Node<T> | undefined;
|
|
56
56
|
sort(): never;
|
|
57
|
-
splice(start: number, deleteCount?: number, ...values: T[]): Node<T> | Node<T>[] | undefined;
|
|
57
|
+
splice(start: number, deleteCount?: number, ...values: T[]): Node<T> | Node<T>[] | null | undefined;
|
|
58
58
|
unshift(...values: T[]): number;
|
|
59
59
|
}
|
|
60
60
|
export { ReactiveArray, ReactiveObjectArray };
|
package/build/reactive/array.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { dispose
|
|
1
|
+
import { dispose } from '../signal';
|
|
2
|
+
import { Signal } from '../types';
|
|
2
3
|
import { ReactiveObject } from './object';
|
|
3
4
|
function factory(data, options = {}) {
|
|
4
5
|
let signals = [];
|
|
@@ -14,7 +15,7 @@ class ReactiveArray extends Array {
|
|
|
14
15
|
#signal;
|
|
15
16
|
constructor(data) {
|
|
16
17
|
super(...data);
|
|
17
|
-
this.#signal =
|
|
18
|
+
this.#signal = new Signal(false);
|
|
18
19
|
}
|
|
19
20
|
set length(n) {
|
|
20
21
|
if (n > this.length) {
|
|
@@ -23,7 +24,7 @@ class ReactiveArray extends Array {
|
|
|
23
24
|
this.splice(n);
|
|
24
25
|
}
|
|
25
26
|
trigger() {
|
|
26
|
-
|
|
27
|
+
this.#signal.set(!this.#signal.value);
|
|
27
28
|
}
|
|
28
29
|
dispatch(event, data) {
|
|
29
30
|
this.#signal.dispatch(event, data);
|
|
@@ -93,7 +94,7 @@ class ReactiveArray extends Array {
|
|
|
93
94
|
return removed;
|
|
94
95
|
}
|
|
95
96
|
track() {
|
|
96
|
-
|
|
97
|
+
this.#signal.get();
|
|
97
98
|
}
|
|
98
99
|
unshift(...items) {
|
|
99
100
|
let length = super.unshift(...items);
|
package/build/reactive/index.js
CHANGED
package/build/reactive/object.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Computed, Signal } from '../types';
|
|
2
2
|
import { defineProperty, isArray } from '../utilities';
|
|
3
3
|
import { ReactiveArray, ReactiveObjectArray } from './array';
|
|
4
4
|
class ReactiveObject {
|
|
@@ -8,11 +8,11 @@ class ReactiveObject {
|
|
|
8
8
|
for (let key in data) {
|
|
9
9
|
let input = data[key];
|
|
10
10
|
if (typeof input === 'function') {
|
|
11
|
-
let node = nodes[key] =
|
|
11
|
+
let node = nodes[key] = new Computed(input, options);
|
|
12
12
|
defineProperty(this, key, {
|
|
13
13
|
enumerable: true,
|
|
14
14
|
get() {
|
|
15
|
-
return
|
|
15
|
+
return node.get();
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
}
|
|
@@ -33,14 +33,14 @@ class ReactiveObject {
|
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
else {
|
|
36
|
-
let node = nodes[key] =
|
|
36
|
+
let node = nodes[key] = new Signal(input, options);
|
|
37
37
|
defineProperty(this, key, {
|
|
38
38
|
enumerable: true,
|
|
39
39
|
get() {
|
|
40
|
-
return
|
|
40
|
+
return node.get();
|
|
41
41
|
},
|
|
42
42
|
set(value) {
|
|
43
|
-
|
|
43
|
+
node.set(value);
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
46
|
}
|
package/build/reset.d.ts
ADDED
package/build/reset.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isArray } from './utilities';
|
|
2
|
+
export default (reset) => {
|
|
3
|
+
if (reset == null) {
|
|
4
|
+
}
|
|
5
|
+
else if (isArray(reset)) {
|
|
6
|
+
for (let i = 0, n = reset.length; i < n; i++) {
|
|
7
|
+
reset[i].reset();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
reset.reset();
|
|
12
|
+
}
|
|
13
|
+
return reset;
|
|
14
|
+
};
|
package/build/resource.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import {
|
|
2
|
+
import { Signal } from './signal';
|
|
3
3
|
class Resource extends CustomFunction {
|
|
4
4
|
#data;
|
|
5
5
|
#input;
|
|
@@ -8,36 +8,36 @@ class Resource extends CustomFunction {
|
|
|
8
8
|
constructor(fn, options = {}) {
|
|
9
9
|
super((...args) => {
|
|
10
10
|
this.stop = null;
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
this.#input.set(args);
|
|
12
|
+
this.#ok.set(null);
|
|
13
13
|
fn(...args)
|
|
14
14
|
.then((value) => {
|
|
15
15
|
if (this.stop === true) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
this.#data.set(value);
|
|
19
|
+
this.#ok.set(true);
|
|
20
20
|
})
|
|
21
21
|
.catch(() => {
|
|
22
22
|
if (this.stop === true) {
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
this.#data.set(undefined);
|
|
26
|
+
this.#ok.set(false);
|
|
27
27
|
});
|
|
28
28
|
});
|
|
29
|
-
this.#data =
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
this.#data = new Signal(undefined, options);
|
|
30
|
+
this.#input = new Signal(null, options);
|
|
31
|
+
this.#ok = new Signal(null, options);
|
|
32
32
|
}
|
|
33
33
|
get data() {
|
|
34
|
-
return
|
|
34
|
+
return this.#data.get();
|
|
35
35
|
}
|
|
36
36
|
get input() {
|
|
37
|
-
return
|
|
37
|
+
return this.#input.get();
|
|
38
38
|
}
|
|
39
39
|
get ok() {
|
|
40
|
-
return
|
|
40
|
+
return this.#ok.get();
|
|
41
41
|
}
|
|
42
42
|
dispose() {
|
|
43
43
|
this.#data.dispose();
|
package/build/signal.d.ts
CHANGED
|
@@ -1,34 +1,56 @@
|
|
|
1
|
-
import { Changed,
|
|
2
|
-
declare class
|
|
3
|
-
changed: Changed | null;
|
|
4
|
-
fn: Computed<T>['fn'] | Effect['fn'] | null;
|
|
1
|
+
import { Changed, Event, Function, Listener, NeverAsync, Options, State, Type } from './types';
|
|
2
|
+
declare class Core<T> {
|
|
5
3
|
listeners: Record<Event, (Listener<any> | null)[]> | null;
|
|
6
|
-
observers:
|
|
4
|
+
observers: Core<any>[] | null;
|
|
7
5
|
root: Root | null;
|
|
8
|
-
sources:
|
|
6
|
+
sources: Core<any>[] | null;
|
|
9
7
|
state: State;
|
|
10
|
-
|
|
11
|
-
type: Type;
|
|
12
|
-
updating: boolean | null;
|
|
8
|
+
updating: boolean;
|
|
13
9
|
value: T;
|
|
14
|
-
constructor(
|
|
10
|
+
constructor(state: State, value: T);
|
|
11
|
+
get type(): Type | never;
|
|
15
12
|
dispatch<D>(event: Event, data?: D): void;
|
|
16
13
|
dispose(): void;
|
|
17
|
-
on(event: Event, listener: Listener<
|
|
18
|
-
once(event: Event, listener: Listener<
|
|
14
|
+
on<T>(event: Event, listener: Listener<T>): void;
|
|
15
|
+
once<T>(event: Event, listener: Listener<T>): void;
|
|
16
|
+
}
|
|
17
|
+
declare class Computed<T> extends Core<T> {
|
|
18
|
+
changed: Changed;
|
|
19
|
+
fn: NeverAsync<() => T>;
|
|
20
|
+
constructor(fn: Computed<T>['fn'], options?: Options);
|
|
21
|
+
get type(): Type;
|
|
22
|
+
get(): T;
|
|
23
|
+
reset(): void;
|
|
24
|
+
}
|
|
25
|
+
declare class Effect extends Core<null> {
|
|
26
|
+
fn: NeverAsync<(node: Effect) => void>;
|
|
27
|
+
task: Function;
|
|
28
|
+
constructor(fn: Effect['fn']);
|
|
29
|
+
get type(): Type;
|
|
30
|
+
reset(): void;
|
|
31
|
+
}
|
|
32
|
+
declare class Root extends Core<null> {
|
|
33
|
+
scheduler: (fn: Function) => unknown;
|
|
34
|
+
tracking: boolean;
|
|
35
|
+
constructor(scheduler: Root['scheduler'], tracking: boolean);
|
|
36
|
+
get type(): Type;
|
|
37
|
+
}
|
|
38
|
+
declare class Signal<T> extends Core<T> {
|
|
39
|
+
changed: Changed;
|
|
40
|
+
constructor(data: T, options?: Options);
|
|
41
|
+
get type(): Type;
|
|
42
|
+
get(): T;
|
|
19
43
|
reset(): void;
|
|
44
|
+
set(value: T): T;
|
|
20
45
|
}
|
|
21
|
-
declare const computed: <T>(fn: (
|
|
46
|
+
declare const computed: <T>(fn: () => T extends unknown ? T : NeverAsync<T>, options?: Options) => Computed<T>;
|
|
22
47
|
declare const dispose: <T extends {
|
|
23
|
-
dispose:
|
|
24
|
-
}>(dispose?: T | T[] | undefined) => T | T[] | undefined;
|
|
25
|
-
declare const effect: (fn: Effect['fn']
|
|
26
|
-
declare const read: <T>(node: Signal<T>) => T;
|
|
48
|
+
dispose: VoidFunction;
|
|
49
|
+
}>(dispose?: T | T[] | null | undefined) => T | T[] | null | undefined;
|
|
50
|
+
declare const effect: (fn: Effect['fn']) => Effect;
|
|
27
51
|
declare const reset: <T extends {
|
|
28
|
-
reset:
|
|
29
|
-
}>(reset?: T | T[] | undefined) => T | T[] | undefined;
|
|
30
|
-
declare
|
|
31
|
-
declare const signal: <T>(
|
|
32
|
-
|
|
33
|
-
export default Signal;
|
|
34
|
-
export { computed, dispose, effect, read, reset, root, signal, write };
|
|
52
|
+
reset: VoidFunction;
|
|
53
|
+
}>(reset?: T | T[] | null | undefined) => T | T[] | null | undefined;
|
|
54
|
+
declare const root: <T>(fn: (root: Root) => T, scheduler?: Root['scheduler']) => T;
|
|
55
|
+
declare const signal: <T>(value: T, options?: Options) => Signal<T>;
|
|
56
|
+
export { computed, dispose, effect, reset, root, signal, Computed, Effect, Signal };
|