@esportsplus/reactivity 0.1.19 → 0.1.21
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/.editorconfig +9 -9
- package/.gitattributes +2 -2
- package/.github/dependabot.yml +23 -0
- package/.github/workflows/bump.yml +7 -0
- package/.github/workflows/publish.yml +14 -0
- package/build/constants.js +1 -11
- package/build/index.js +6 -34
- package/build/macro.d.ts +1 -1
- package/build/macro.js +5 -10
- package/build/reactive/array.js +8 -12
- package/build/reactive/index.js +3 -5
- package/build/reactive/object.js +12 -15
- package/build/resource.js +7 -12
- package/build/signal.d.ts +3 -3
- package/build/signal.js +27 -34
- package/build/types.js +1 -2
- package/build/utilities.js +1 -5
- package/package.json +20 -20
- package/readme.md +1 -1
- package/src/constants.ts +18 -18
- package/src/index.ts +5 -5
- package/src/macro.ts +28 -28
- package/src/reactive/array.ts +212 -214
- package/src/reactive/index.ts +37 -37
- package/src/reactive/object.ts +76 -76
- package/src/resource.ts +70 -70
- package/src/signal.ts +375 -375
- package/src/types.ts +54 -54
- package/src/utilities.ts +5 -5
- package/tsconfig.json +10 -10
package/src/reactive/object.ts
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
import { computed, signal } from '~/signal';
|
|
2
|
-
import { Computed, Object, Options, Signal } from '~/types';
|
|
3
|
-
import { defineProperty, isArray } from '~/utilities';
|
|
4
|
-
import { ReactiveArray, ReactiveObjectArray } from './array';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type Node = Computed<any> | ReactiveArray<any> | ReactiveObjectArray<Object> | Signal<any>;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class ReactiveObject<T extends Object> {
|
|
11
|
-
nodes: Record<PropertyKey, Node> = {};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
constructor(data: T, options: Options = {}) {
|
|
15
|
-
let nodes = this.nodes;
|
|
16
|
-
|
|
17
|
-
for (let key in data) {
|
|
18
|
-
let input = data[key];
|
|
19
|
-
|
|
20
|
-
if (typeof input === 'function') {
|
|
21
|
-
let node = nodes[key] = computed(input as Computed<T>['fn'], options);
|
|
22
|
-
|
|
23
|
-
defineProperty(this, key, {
|
|
24
|
-
enumerable: true,
|
|
25
|
-
get() {
|
|
26
|
-
return node.get();
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
else if (isArray(input)) {
|
|
31
|
-
let node: ReactiveArray<unknown> | ReactiveObjectArray<Object>,
|
|
32
|
-
test = input[0];
|
|
33
|
-
|
|
34
|
-
if (typeof test === 'object' && test !== null && test?.constructor?.name === 'Object') {
|
|
35
|
-
node = nodes[key] = new ReactiveObjectArray(input, options);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
node = nodes[key] = new ReactiveArray(input);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
defineProperty(this, key, {
|
|
42
|
-
enumerable: true,
|
|
43
|
-
get() {
|
|
44
|
-
node.track();
|
|
45
|
-
|
|
46
|
-
return node;
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
let node = nodes[key] = signal(input, options);
|
|
52
|
-
|
|
53
|
-
defineProperty(this, key, {
|
|
54
|
-
enumerable: true,
|
|
55
|
-
get() {
|
|
56
|
-
return node.get();
|
|
57
|
-
},
|
|
58
|
-
set(value) {
|
|
59
|
-
node.set(value);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
dispose() {
|
|
68
|
-
let nodes = this.nodes;
|
|
69
|
-
|
|
70
|
-
for (let key in nodes) {
|
|
71
|
-
nodes[key].dispose();
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
1
|
+
import { computed, signal } from '~/signal';
|
|
2
|
+
import { Computed, Object, Options, Signal } from '~/types';
|
|
3
|
+
import { defineProperty, isArray } from '~/utilities';
|
|
4
|
+
import { ReactiveArray, ReactiveObjectArray } from './array';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
type Node = Computed<any> | ReactiveArray<any> | ReactiveObjectArray<Object> | Signal<any>;
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ReactiveObject<T extends Object> {
|
|
11
|
+
nodes: Record<PropertyKey, Node> = {};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
constructor(data: T, options: Options = {}) {
|
|
15
|
+
let nodes = this.nodes;
|
|
16
|
+
|
|
17
|
+
for (let key in data) {
|
|
18
|
+
let input = data[key];
|
|
19
|
+
|
|
20
|
+
if (typeof input === 'function') {
|
|
21
|
+
let node = nodes[key] = computed(input as Computed<T>['fn'], options);
|
|
22
|
+
|
|
23
|
+
defineProperty(this, key, {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get() {
|
|
26
|
+
return node.get();
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
else if (isArray(input)) {
|
|
31
|
+
let node: ReactiveArray<unknown> | ReactiveObjectArray<Object>,
|
|
32
|
+
test = input[0];
|
|
33
|
+
|
|
34
|
+
if (typeof test === 'object' && test !== null && test?.constructor?.name === 'Object') {
|
|
35
|
+
node = nodes[key] = new ReactiveObjectArray(input, options);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
node = nodes[key] = new ReactiveArray(input);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
defineProperty(this, key, {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
get() {
|
|
44
|
+
node.track();
|
|
45
|
+
|
|
46
|
+
return node;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
let node = nodes[key] = signal(input, options);
|
|
52
|
+
|
|
53
|
+
defineProperty(this, key, {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
get() {
|
|
56
|
+
return node.get();
|
|
57
|
+
},
|
|
58
|
+
set(value) {
|
|
59
|
+
node.set(value);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
dispose() {
|
|
68
|
+
let nodes = this.nodes;
|
|
69
|
+
|
|
70
|
+
for (let key in nodes) {
|
|
71
|
+
nodes[key].dispose();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
77
|
export { ReactiveObject };
|
package/src/resource.ts
CHANGED
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
-
import { signal } from './signal';
|
|
3
|
-
import { Options, Signal } from './types';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type Function<A extends unknown[], R extends Promise<unknown>> = (...args: A) => R;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
|
|
10
|
-
#data: Signal<Awaited<R>>;
|
|
11
|
-
#input: Signal<A | null>;
|
|
12
|
-
#ok: Signal<boolean | null>;
|
|
13
|
-
|
|
14
|
-
stop: boolean | null = null;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
constructor(fn: Function<A,R>, options: Options = {}) {
|
|
18
|
-
super((...args: A) => {
|
|
19
|
-
this.stop = null;
|
|
20
|
-
|
|
21
|
-
this.#input.set(args);
|
|
22
|
-
this.#ok.set(null);
|
|
23
|
-
|
|
24
|
-
fn(...args)
|
|
25
|
-
.then((value) => {
|
|
26
|
-
if (this.stop === true) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
this.#data.set(value as Awaited<R>);
|
|
31
|
-
this.#ok.set(true);
|
|
32
|
-
})
|
|
33
|
-
.catch(() => {
|
|
34
|
-
if (this.stop === true) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
this.#data.set(undefined as Awaited<R>);
|
|
39
|
-
this.#ok.set(false);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
this.#data = signal(undefined as Awaited<R>, options);
|
|
43
|
-
this.#input = signal<A | null>(null, options);
|
|
44
|
-
this.#ok = signal<boolean | null>(null, options);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
get data() {
|
|
49
|
-
return this.#data.get();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get input() {
|
|
53
|
-
return this.#input.get();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
get ok() {
|
|
57
|
-
return this.#ok.get();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
dispose() {
|
|
62
|
-
this.#data.dispose();
|
|
63
|
-
this.#input.dispose();
|
|
64
|
-
this.#ok.dispose();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
export default <A extends unknown[], R extends Promise<unknown>>(fn: Function<A,R>, options: Options = {}) => {
|
|
70
|
-
return new Resource(fn, options);
|
|
1
|
+
import CustomFunction from '@esportsplus/custom-function';
|
|
2
|
+
import { signal } from './signal';
|
|
3
|
+
import { Options, Signal } from './types';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
type Function<A extends unknown[], R extends Promise<unknown>> = (...args: A) => R;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
|
|
10
|
+
#data: Signal<Awaited<R>>;
|
|
11
|
+
#input: Signal<A | null>;
|
|
12
|
+
#ok: Signal<boolean | null>;
|
|
13
|
+
|
|
14
|
+
stop: boolean | null = null;
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
constructor(fn: Function<A,R>, options: Options = {}) {
|
|
18
|
+
super((...args: A) => {
|
|
19
|
+
this.stop = null;
|
|
20
|
+
|
|
21
|
+
this.#input.set(args);
|
|
22
|
+
this.#ok.set(null);
|
|
23
|
+
|
|
24
|
+
fn(...args)
|
|
25
|
+
.then((value) => {
|
|
26
|
+
if (this.stop === true) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.#data.set(value as Awaited<R>);
|
|
31
|
+
this.#ok.set(true);
|
|
32
|
+
})
|
|
33
|
+
.catch(() => {
|
|
34
|
+
if (this.stop === true) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.#data.set(undefined as Awaited<R>);
|
|
39
|
+
this.#ok.set(false);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
this.#data = signal(undefined as Awaited<R>, options);
|
|
43
|
+
this.#input = signal<A | null>(null, options);
|
|
44
|
+
this.#ok = signal<boolean | null>(null, options);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
get data() {
|
|
49
|
+
return this.#data.get();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get input() {
|
|
53
|
+
return this.#input.get();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get ok() {
|
|
57
|
+
return this.#ok.get();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
dispose() {
|
|
62
|
+
this.#data.dispose();
|
|
63
|
+
this.#input.dispose();
|
|
64
|
+
this.#ok.dispose();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export default <A extends unknown[], R extends Promise<unknown>>(fn: Function<A,R>, options: Options = {}) => {
|
|
70
|
+
return new Resource(fn, options);
|
|
71
71
|
};
|