@esportsplus/reactivity 0.17.0 → 0.17.1
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/reactive/object.js +5 -6
- package/package.json +1 -1
- package/src/reactive/object.ts +12 -13
package/build/reactive/object.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineProperty, isArray,
|
|
1
|
+
import { defineProperty, isArray, isPromise } from '@esportsplus/utilities';
|
|
2
2
|
import { computed, dispose, effect, read, root, set, signal } from '../system.js';
|
|
3
3
|
import { REACTIVE_OBJECT } from '../constants.js';
|
|
4
4
|
import { ReactiveArray } from './array.js';
|
|
@@ -6,10 +6,9 @@ class ReactiveObject {
|
|
|
6
6
|
[REACTIVE_OBJECT] = true;
|
|
7
7
|
disposers = null;
|
|
8
8
|
constructor(data) {
|
|
9
|
-
let keys = Object.keys(data);
|
|
10
|
-
|
|
11
|
-
let
|
|
12
|
-
let type = typeof value;
|
|
9
|
+
let keys = Object.keys(data), key;
|
|
10
|
+
while (key = keys.pop()) {
|
|
11
|
+
let value = data[key], type = typeof value;
|
|
13
12
|
if (type === 'function') {
|
|
14
13
|
let node;
|
|
15
14
|
defineProperty(this, key, {
|
|
@@ -75,6 +74,6 @@ class ReactiveObject {
|
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
76
|
const isReactiveObject = (value) => {
|
|
78
|
-
return
|
|
77
|
+
return typeof value === 'object' && value !== null && REACTIVE_OBJECT in value;
|
|
79
78
|
};
|
|
80
79
|
export { isReactiveObject, ReactiveObject };
|
package/package.json
CHANGED
package/src/reactive/object.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineProperty, isArray,
|
|
1
|
+
import { defineProperty, isArray, isPromise } from '@esportsplus/utilities';
|
|
2
2
|
import { computed, dispose, effect, read, root, set, signal } from '~/system';
|
|
3
3
|
import { Computed, Signal } from '~/types';
|
|
4
4
|
import { REACTIVE_OBJECT } from '~/constants';
|
|
@@ -13,13 +13,12 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
constructor(data: T) {
|
|
16
|
-
let keys = Object.keys(data)
|
|
16
|
+
let keys = Object.keys(data),
|
|
17
|
+
key: keyof T | undefined;
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
let type = typeof value;
|
|
19
|
+
while (key = keys.pop()) {
|
|
20
|
+
let value = data[key],
|
|
21
|
+
type = typeof value;
|
|
23
22
|
|
|
24
23
|
if (type === 'function') {
|
|
25
24
|
let node: Computed<T[typeof key]> | Signal<T[typeof key] | undefined> | undefined;
|
|
@@ -29,19 +28,19 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
29
28
|
get: () => {
|
|
30
29
|
if (node === undefined) {
|
|
31
30
|
root(() => {
|
|
32
|
-
node = computed(value as Computed<T[
|
|
31
|
+
node = computed(value as Computed<T[keyof T]>['fn']);
|
|
33
32
|
|
|
34
33
|
if (isPromise(node.value)) {
|
|
35
34
|
let factory = node,
|
|
36
35
|
version = 0;
|
|
37
36
|
|
|
38
|
-
node = signal<T[
|
|
37
|
+
node = signal<T[keyof T] | undefined>(undefined);
|
|
39
38
|
|
|
40
39
|
(this.disposers ??= []).push(
|
|
41
40
|
effect(() => {
|
|
42
41
|
let id = ++version;
|
|
43
42
|
|
|
44
|
-
(read(factory) as Promise<T[
|
|
43
|
+
(read(factory) as Promise<T[keyof T]>).then((v) => {
|
|
45
44
|
if (id !== version) {
|
|
46
45
|
return;
|
|
47
46
|
}
|
|
@@ -52,7 +51,7 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
52
51
|
)
|
|
53
52
|
}
|
|
54
53
|
else {
|
|
55
|
-
(this.disposers ??= []).push(() => dispose(node as Computed<T[
|
|
54
|
+
(this.disposers ??= []).push(() => dispose(node as Computed<T[keyof T]>));
|
|
56
55
|
}
|
|
57
56
|
});
|
|
58
57
|
}
|
|
@@ -65,7 +64,7 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
if (value == null || type !== 'object') {
|
|
68
|
-
//
|
|
67
|
+
// Skip isArray when possible
|
|
69
68
|
}
|
|
70
69
|
else if (isArray(value)) {
|
|
71
70
|
let node = new ReactiveArray(value);
|
|
@@ -111,7 +110,7 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
111
110
|
|
|
112
111
|
|
|
113
112
|
const isReactiveObject = (value: any): value is ReactiveObject<any> => {
|
|
114
|
-
return
|
|
113
|
+
return typeof value === 'object' && value !== null && REACTIVE_OBJECT in value;
|
|
115
114
|
};
|
|
116
115
|
|
|
117
116
|
|