@signaltree/core 4.0.15 → 4.1.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/README.md +106 -38
- package/dist/constants.js +6 -0
- package/dist/deep-clone.js +80 -0
- package/dist/deep-equal.js +41 -0
- package/dist/enhancers/batching/lib/batching.js +141 -135
- package/dist/enhancers/computed/lib/computed.js +18 -16
- package/dist/enhancers/devtools/lib/devtools.js +303 -260
- package/dist/enhancers/entities/lib/entities.js +109 -104
- package/dist/enhancers/index.js +65 -77
- package/dist/enhancers/memoization/lib/memoization.js +339 -351
- package/dist/enhancers/middleware/lib/async-helpers.js +71 -79
- package/dist/enhancers/middleware/lib/middleware.js +126 -169
- package/dist/enhancers/presets/lib/presets.js +82 -71
- package/dist/enhancers/serialization/constants.js +14 -13
- package/dist/enhancers/serialization/lib/serialization.js +615 -623
- package/dist/enhancers/time-travel/lib/time-travel.js +178 -177
- package/dist/index.d.ts +1 -17
- package/dist/index.js +19 -16
- package/dist/is-built-in-object.js +23 -0
- package/dist/lib/constants.js +51 -55
- package/dist/lib/memory/memory-manager.js +152 -154
- package/dist/lib/performance/diff-engine.js +141 -141
- package/dist/lib/performance/path-index.js +139 -137
- package/dist/lib/performance/update-engine.js +171 -176
- package/dist/lib/security/security-validator.js +110 -128
- package/dist/lib/signal-tree.js +577 -611
- package/dist/lib/types.js +3 -9
- package/dist/lib/utils.js +236 -268
- package/dist/lru-cache.js +64 -0
- package/dist/parse-path.js +13 -0
- package/package.json +30 -16
- package/src/index.d.ts +17 -0
- package/{dist → src}/lib/utils.d.ts +1 -0
- package/dist/enhancers/batching/index.js +0 -1
- package/dist/enhancers/batching/jest.config.js +0 -21
- package/dist/enhancers/batching/test-setup.js +0 -5
- package/dist/enhancers/computed/index.js +0 -1
- package/dist/enhancers/computed/jest.config.js +0 -21
- package/dist/enhancers/devtools/index.js +0 -1
- package/dist/enhancers/devtools/jest.config.js +0 -21
- package/dist/enhancers/devtools/test-setup.js +0 -5
- package/dist/enhancers/entities/index.js +0 -1
- package/dist/enhancers/entities/jest.config.js +0 -21
- package/dist/enhancers/entities/test-setup.js +0 -5
- package/dist/enhancers/memoization/index.js +0 -1
- package/dist/enhancers/memoization/jest.config.js +0 -21
- package/dist/enhancers/memoization/test-setup.js +0 -5
- package/dist/enhancers/middleware/index.js +0 -2
- package/dist/enhancers/middleware/jest.config.js +0 -21
- package/dist/enhancers/middleware/test-setup.js +0 -5
- package/dist/enhancers/presets/index.js +0 -1
- package/dist/enhancers/presets/jest.config.js +0 -21
- package/dist/enhancers/presets/test-setup.js +0 -5
- package/dist/enhancers/serialization/index.js +0 -2
- package/dist/enhancers/serialization/jest.config.js +0 -21
- package/dist/enhancers/serialization/test-setup.js +0 -5
- package/dist/enhancers/time-travel/index.js +0 -1
- package/dist/enhancers/time-travel/jest.config.js +0 -21
- package/dist/enhancers/time-travel/lib/utils.js +0 -1
- package/dist/enhancers/time-travel/test-setup.js +0 -5
- package/dist/enhancers/types.js +0 -0
- /package/{dist → src}/enhancers/batching/index.d.ts +0 -0
- /package/{dist → src}/enhancers/batching/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/batching/lib/batching.d.ts +0 -0
- /package/{dist → src}/enhancers/batching/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/computed/index.d.ts +0 -0
- /package/{dist → src}/enhancers/computed/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/computed/lib/computed.d.ts +0 -0
- /package/{dist → src}/enhancers/devtools/index.d.ts +0 -0
- /package/{dist → src}/enhancers/devtools/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/devtools/lib/devtools.d.ts +0 -0
- /package/{dist → src}/enhancers/devtools/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/entities/index.d.ts +0 -0
- /package/{dist → src}/enhancers/entities/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/entities/lib/entities.d.ts +0 -0
- /package/{dist → src}/enhancers/entities/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/index.d.ts +0 -0
- /package/{dist → src}/enhancers/memoization/index.d.ts +0 -0
- /package/{dist → src}/enhancers/memoization/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/memoization/lib/memoization.d.ts +0 -0
- /package/{dist → src}/enhancers/memoization/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/middleware/index.d.ts +0 -0
- /package/{dist → src}/enhancers/middleware/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/middleware/lib/async-helpers.d.ts +0 -0
- /package/{dist → src}/enhancers/middleware/lib/middleware.d.ts +0 -0
- /package/{dist → src}/enhancers/middleware/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/presets/index.d.ts +0 -0
- /package/{dist → src}/enhancers/presets/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/presets/lib/presets.d.ts +0 -0
- /package/{dist → src}/enhancers/presets/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/serialization/constants.d.ts +0 -0
- /package/{dist → src}/enhancers/serialization/index.d.ts +0 -0
- /package/{dist → src}/enhancers/serialization/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/serialization/lib/serialization.d.ts +0 -0
- /package/{dist → src}/enhancers/serialization/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/time-travel/index.d.ts +0 -0
- /package/{dist → src}/enhancers/time-travel/jest.config.d.ts +0 -0
- /package/{dist → src}/enhancers/time-travel/lib/time-travel.d.ts +0 -0
- /package/{dist → src}/enhancers/time-travel/lib/utils.d.ts +0 -0
- /package/{dist → src}/enhancers/time-travel/test-setup.d.ts +0 -0
- /package/{dist → src}/enhancers/types.d.ts +0 -0
- /package/{dist → src}/lib/constants.d.ts +0 -0
- /package/{dist → src}/lib/memory/memory-manager.d.ts +0 -0
- /package/{dist → src}/lib/performance/diff-engine.d.ts +0 -0
- /package/{dist → src}/lib/performance/path-index.d.ts +0 -0
- /package/{dist → src}/lib/performance/update-engine.d.ts +0 -0
- /package/{dist → src}/lib/security/security-validator.d.ts +0 -0
- /package/{dist → src}/lib/signal-tree.d.ts +0 -0
- /package/{dist → src}/lib/types.d.ts +0 -0
package/dist/lib/signal-tree.js
CHANGED
|
@@ -1,665 +1,631 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { resolveEnhancerOrder } from '../enhancers';
|
|
3
|
-
import {
|
|
4
|
-
import { SignalMemoryManager } from './memory/memory-manager';
|
|
5
|
-
import { OptimizedUpdateEngine } from './performance/update-engine';
|
|
6
|
-
import { SecurityValidator } from './security/security-validator';
|
|
7
|
-
import { createLazySignalTree,
|
|
1
|
+
import { signal, isSignal, computed, effect, inject, DestroyRef } from '@angular/core';
|
|
2
|
+
import { resolveEnhancerOrder } from '../enhancers/index.js';
|
|
3
|
+
import { SIGNAL_TREE_MESSAGES, SIGNAL_TREE_CONSTANTS } from './constants.js';
|
|
4
|
+
import { SignalMemoryManager } from './memory/memory-manager.js';
|
|
5
|
+
import { OptimizedUpdateEngine } from './performance/update-engine.js';
|
|
6
|
+
import { SecurityValidator } from './security/security-validator.js';
|
|
7
|
+
import { createLazySignalTree, unwrap } from './utils.js';
|
|
8
|
+
import { deepEqual } from '../deep-equal.js';
|
|
9
|
+
import { isBuiltInObject } from '../is-built-in-object.js';
|
|
10
|
+
|
|
8
11
|
const NODE_ACCESSOR_SYMBOL = Symbol.for('NodeAccessor');
|
|
9
12
|
function makeNodeAccessor() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return accessor;
|
|
13
|
+
const accessor = function (arg) {
|
|
14
|
+
if (arguments.length === 0) {
|
|
15
|
+
return unwrap(accessor);
|
|
16
|
+
}
|
|
17
|
+
if (typeof arg === 'function') {
|
|
18
|
+
const updater = arg;
|
|
19
|
+
const currentValue = unwrap(accessor);
|
|
20
|
+
const newValue = updater(currentValue);
|
|
21
|
+
recursiveUpdate(accessor, newValue);
|
|
22
|
+
} else {
|
|
23
|
+
recursiveUpdate(accessor, arg);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
accessor[NODE_ACCESSOR_SYMBOL] = true;
|
|
27
|
+
return accessor;
|
|
26
28
|
}
|
|
27
29
|
function makeRootNodeAccessor(readSignal, writeSignal) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
if (typeof arg === 'function') {
|
|
33
|
-
const updater = arg;
|
|
34
|
-
writeSignal.set(updater(readSignal()));
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
writeSignal.set(arg);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
accessor[NODE_ACCESSOR_SYMBOL] = true;
|
|
41
|
-
return accessor;
|
|
42
|
-
}
|
|
43
|
-
function recursiveUpdate(target, updates) {
|
|
44
|
-
if (!updates || typeof updates !== 'object') {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
let targetObj;
|
|
48
|
-
if (isNodeAccessor(target)) {
|
|
49
|
-
targetObj = target;
|
|
30
|
+
const accessor = function (arg) {
|
|
31
|
+
if (arguments.length === 0) {
|
|
32
|
+
return readSignal();
|
|
50
33
|
}
|
|
51
|
-
|
|
52
|
-
|
|
34
|
+
if (typeof arg === 'function') {
|
|
35
|
+
const updater = arg;
|
|
36
|
+
writeSignal.set(updater(readSignal()));
|
|
37
|
+
} else {
|
|
38
|
+
writeSignal.set(arg);
|
|
53
39
|
}
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
};
|
|
41
|
+
accessor[NODE_ACCESSOR_SYMBOL] = true;
|
|
42
|
+
return accessor;
|
|
43
|
+
}
|
|
44
|
+
function recursiveUpdate(target, updates) {
|
|
45
|
+
if (!updates || typeof updates !== 'object') {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let targetObj;
|
|
49
|
+
if (isNodeAccessor(target)) {
|
|
50
|
+
targetObj = target;
|
|
51
|
+
} else if (target && typeof target === 'object') {
|
|
52
|
+
targetObj = target;
|
|
53
|
+
} else {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const updatesObj = updates;
|
|
57
|
+
for (const key in updatesObj) {
|
|
58
|
+
if (!(key in targetObj)) {
|
|
59
|
+
continue;
|
|
56
60
|
}
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
else if (isNodeAccessor(targetProp)) {
|
|
70
|
-
if (updateValue && typeof updateValue === 'object') {
|
|
71
|
-
recursiveUpdate(targetProp, updateValue);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
targetProp(updateValue);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
61
|
+
const targetProp = targetObj[key];
|
|
62
|
+
const updateValue = updatesObj[key];
|
|
63
|
+
if (isSignal(targetProp)) {
|
|
64
|
+
if ('set' in targetProp && typeof targetProp.set === 'function') {
|
|
65
|
+
targetProp.set(updateValue);
|
|
66
|
+
}
|
|
67
|
+
} else if (isNodeAccessor(targetProp)) {
|
|
68
|
+
if (updateValue && typeof updateValue === 'object') {
|
|
69
|
+
recursiveUpdate(targetProp, updateValue);
|
|
70
|
+
} else {
|
|
71
|
+
targetProp(updateValue);
|
|
72
|
+
}
|
|
77
73
|
}
|
|
74
|
+
}
|
|
78
75
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
value &&
|
|
82
|
-
value[NODE_ACCESSOR_SYMBOL] === true);
|
|
76
|
+
function isNodeAccessor(value) {
|
|
77
|
+
return typeof value === 'function' && value && value[NODE_ACCESSOR_SYMBOL] === true;
|
|
83
78
|
}
|
|
84
79
|
function estimateObjectSize(obj, maxDepth = SIGNAL_TREE_CONSTANTS.ESTIMATE_MAX_DEPTH, currentDepth = 0) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
for (let i = 0; i < sampleSize; i++) {
|
|
105
|
-
const value = obj[keys[i]];
|
|
106
|
-
size += estimateObjectSize(value, maxDepth, currentDepth + 1) * 0.5;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
return 1;
|
|
80
|
+
if (currentDepth >= maxDepth) return 1;
|
|
81
|
+
if (obj === null || obj === undefined) return 0;
|
|
82
|
+
if (typeof obj !== 'object') return 1;
|
|
83
|
+
let size = 0;
|
|
84
|
+
try {
|
|
85
|
+
if (Array.isArray(obj)) {
|
|
86
|
+
size = obj.length;
|
|
87
|
+
const sampleSize = Math.min(SIGNAL_TREE_CONSTANTS.ESTIMATE_SAMPLE_SIZE_ARRAY, obj.length);
|
|
88
|
+
for (let i = 0; i < sampleSize; i++) {
|
|
89
|
+
size += estimateObjectSize(obj[i], maxDepth, currentDepth + 1) * 0.1;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
const keys = Object.keys(obj);
|
|
93
|
+
size = keys.length;
|
|
94
|
+
const sampleSize = Math.min(SIGNAL_TREE_CONSTANTS.ESTIMATE_SAMPLE_SIZE_OBJECT, keys.length);
|
|
95
|
+
for (let i = 0; i < sampleSize; i++) {
|
|
96
|
+
const value = obj[keys[i]];
|
|
97
|
+
size += estimateObjectSize(value, maxDepth, currentDepth + 1) * 0.5;
|
|
98
|
+
}
|
|
112
99
|
}
|
|
113
|
-
|
|
100
|
+
} catch {
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
return Math.floor(size);
|
|
114
104
|
}
|
|
115
105
|
function shouldUseLazy(obj, config, precomputedSize) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return true;
|
|
122
|
-
const estimatedSize = precomputedSize ?? estimateObjectSize(obj);
|
|
123
|
-
return estimatedSize > SIGNAL_TREE_CONSTANTS.LAZY_THRESHOLD;
|
|
106
|
+
if (config.useLazySignals !== undefined) return config.useLazySignals;
|
|
107
|
+
if (config.debugMode || config.enableDevTools) return false;
|
|
108
|
+
if (config.batchUpdates && config.useMemoization) return true;
|
|
109
|
+
const estimatedSize = precomputedSize ?? estimateObjectSize(obj);
|
|
110
|
+
return estimatedSize > SIGNAL_TREE_CONSTANTS.LAZY_THRESHOLD;
|
|
124
111
|
}
|
|
125
112
|
function validateTree(obj, config, path = []) {
|
|
126
|
-
|
|
127
|
-
|
|
113
|
+
if (!config.security) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const validator = new SecurityValidator(config.security);
|
|
117
|
+
function validate(value, currentPath) {
|
|
118
|
+
if (value === null || value === undefined) {
|
|
119
|
+
return;
|
|
128
120
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (Array.isArray(value)) {
|
|
142
|
-
value.forEach((item, index) => {
|
|
143
|
-
validate(item, [...currentPath, String(index)]);
|
|
144
|
-
});
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const keys = [
|
|
148
|
-
...Object.keys(value),
|
|
149
|
-
...Object.getOwnPropertyNames(value),
|
|
150
|
-
];
|
|
151
|
-
const uniqueKeys = [...new Set(keys)];
|
|
152
|
-
for (const key of uniqueKeys) {
|
|
153
|
-
if (typeof key === 'symbol')
|
|
154
|
-
continue;
|
|
155
|
-
try {
|
|
156
|
-
validator.validateKey(key);
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
const err = error;
|
|
160
|
-
throw new Error(`${err.message}\nPath: ${[...currentPath, key].join('.')}`);
|
|
161
|
-
}
|
|
162
|
-
const val = value[key];
|
|
163
|
-
try {
|
|
164
|
-
validator.validateValue(val);
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
const err = error;
|
|
168
|
-
throw new Error(`${err.message}\nPath: ${[...currentPath, key].join('.')}`);
|
|
169
|
-
}
|
|
170
|
-
validate(val, [...currentPath, key]);
|
|
171
|
-
}
|
|
121
|
+
if (typeof value !== 'object') {
|
|
122
|
+
validator.validateValue(value);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (isBuiltInObject(value)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (Array.isArray(value)) {
|
|
129
|
+
value.forEach((item, index) => {
|
|
130
|
+
validate(item, [...currentPath, String(index)]);
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
172
133
|
}
|
|
173
|
-
|
|
134
|
+
const keys = [...Object.keys(value), ...Object.getOwnPropertyNames(value)];
|
|
135
|
+
const uniqueKeys = [...new Set(keys)];
|
|
136
|
+
for (const key of uniqueKeys) {
|
|
137
|
+
if (typeof key === 'symbol') continue;
|
|
138
|
+
try {
|
|
139
|
+
validator.validateKey(key);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
const err = error;
|
|
142
|
+
throw new Error(`${err.message}\nPath: ${[...currentPath, key].join('.')}`);
|
|
143
|
+
}
|
|
144
|
+
const val = value[key];
|
|
145
|
+
try {
|
|
146
|
+
validator.validateValue(val);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const err = error;
|
|
149
|
+
throw new Error(`${err.message}\nPath: ${[...currentPath, key].join('.')}`);
|
|
150
|
+
}
|
|
151
|
+
validate(val, [...currentPath, key]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
validate(obj, path);
|
|
174
155
|
}
|
|
175
156
|
function createEqualityFn(useShallowComparison) {
|
|
176
|
-
|
|
157
|
+
return useShallowComparison ? Object.is : deepEqual;
|
|
177
158
|
}
|
|
178
159
|
function createSignalStore(obj, equalityFn) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
store[key] = signal(value, {
|
|
211
|
-
equal: equalityFn,
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
else if (Array.isArray(value) || isBuiltInObject(value)) {
|
|
215
|
-
store[key] = signal(value, {
|
|
216
|
-
equal: equalityFn,
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
const branch = createSignalStore(value, equalityFn);
|
|
221
|
-
const callableBranch = makeNodeAccessor();
|
|
222
|
-
for (const branchKey in branch) {
|
|
223
|
-
if (Object.prototype.hasOwnProperty.call(branch, branchKey)) {
|
|
224
|
-
try {
|
|
225
|
-
Object.defineProperty(callableBranch, branchKey, {
|
|
226
|
-
value: branch[branchKey],
|
|
227
|
-
enumerable: true,
|
|
228
|
-
configurable: true,
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
catch {
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
store[key] = callableBranch;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
catch (error) {
|
|
239
|
-
console.warn(`${SIGNAL_TREE_MESSAGES.SIGNAL_CREATION_FAILED} "${key}":`, error);
|
|
240
|
-
store[key] = signal(value, {
|
|
241
|
-
equal: equalityFn,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
160
|
+
if (obj === null || obj === undefined || typeof obj !== 'object') {
|
|
161
|
+
return signal(obj, {
|
|
162
|
+
equal: equalityFn
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
if (Array.isArray(obj)) {
|
|
166
|
+
return signal(obj, {
|
|
167
|
+
equal: equalityFn
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (isBuiltInObject(obj)) {
|
|
171
|
+
return signal(obj, {
|
|
172
|
+
equal: equalityFn
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const store = {};
|
|
176
|
+
const processedObjects = new WeakSet();
|
|
177
|
+
if (processedObjects.has(obj)) {
|
|
178
|
+
console.warn(SIGNAL_TREE_MESSAGES.CIRCULAR_REF);
|
|
179
|
+
return signal(obj, {
|
|
180
|
+
equal: equalityFn
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
processedObjects.add(obj);
|
|
184
|
+
try {
|
|
185
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
186
|
+
try {
|
|
187
|
+
if (typeof key === 'symbol') continue;
|
|
188
|
+
if (isSignal(value)) {
|
|
189
|
+
store[key] = value;
|
|
190
|
+
continue;
|
|
244
191
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
192
|
+
if (value === null || value === undefined) {
|
|
193
|
+
store[key] = signal(value, {
|
|
194
|
+
equal: equalityFn
|
|
195
|
+
});
|
|
196
|
+
} else if (typeof value !== 'object') {
|
|
197
|
+
store[key] = signal(value, {
|
|
198
|
+
equal: equalityFn
|
|
199
|
+
});
|
|
200
|
+
} else if (Array.isArray(value) || isBuiltInObject(value)) {
|
|
201
|
+
store[key] = signal(value, {
|
|
202
|
+
equal: equalityFn
|
|
203
|
+
});
|
|
204
|
+
} else {
|
|
205
|
+
const branch = createSignalStore(value, equalityFn);
|
|
206
|
+
const callableBranch = makeNodeAccessor();
|
|
207
|
+
for (const branchKey in branch) {
|
|
208
|
+
if (Object.prototype.hasOwnProperty.call(branch, branchKey)) {
|
|
209
|
+
try {
|
|
210
|
+
Object.defineProperty(callableBranch, branchKey, {
|
|
211
|
+
value: branch[branchKey],
|
|
212
|
+
enumerable: true,
|
|
213
|
+
configurable: true
|
|
214
|
+
});
|
|
215
|
+
} catch {}
|
|
260
216
|
}
|
|
217
|
+
}
|
|
218
|
+
store[key] = callableBranch;
|
|
261
219
|
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.warn(`${SIGNAL_TREE_MESSAGES.SIGNAL_CREATION_FAILED} "${key}":`, error);
|
|
222
|
+
store[key] = signal(value, {
|
|
223
|
+
equal: equalityFn
|
|
224
|
+
});
|
|
225
|
+
}
|
|
262
226
|
}
|
|
263
|
-
|
|
264
|
-
|
|
227
|
+
const symbols = Object.getOwnPropertySymbols(obj);
|
|
228
|
+
for (const sym of symbols) {
|
|
229
|
+
const value = obj[sym];
|
|
230
|
+
try {
|
|
231
|
+
if (isSignal(value)) {
|
|
232
|
+
store[sym] = value;
|
|
233
|
+
} else {
|
|
234
|
+
store[sym] = signal(value, {
|
|
235
|
+
equal: equalityFn
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
} catch (error) {
|
|
239
|
+
console.warn(SIGNAL_TREE_MESSAGES.SIGNAL_CREATION_FAILED, error);
|
|
240
|
+
}
|
|
265
241
|
}
|
|
266
|
-
|
|
242
|
+
} catch (error) {
|
|
243
|
+
throw new Error(`Failed to create signal store: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
244
|
+
}
|
|
245
|
+
return store;
|
|
267
246
|
}
|
|
268
247
|
function enhanceTree(tree, config = {}) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
try {
|
|
320
|
-
const result = enhancer(currentTree);
|
|
321
|
-
if (result !== currentTree)
|
|
322
|
-
currentTree = result;
|
|
323
|
-
const provs = enhancer.metadata?.provides ?? [];
|
|
324
|
-
for (const p of provs)
|
|
325
|
-
provided.add(p);
|
|
326
|
-
if (config.debugMode && provs.length > 0) {
|
|
327
|
-
for (const p of provs) {
|
|
328
|
-
if (!(p in currentTree)) {
|
|
329
|
-
console.warn(SIGNAL_TREE_MESSAGES.ENHANCER_PROVIDES_MISSING.replace('%s', enhancer.metadata?.name ?? String(i)).replace('%s', p));
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
catch (error) {
|
|
335
|
-
const name = enhancer.metadata?.name || `enhancer at index ${i}`;
|
|
336
|
-
console.error(SIGNAL_TREE_MESSAGES.ENHANCER_FAILED.replace('%s', name), error);
|
|
337
|
-
if (config.debugMode) {
|
|
338
|
-
console.error('[SignalTree] Enhancer stack trace:', enhancer);
|
|
339
|
-
console.error('[SignalTree] Tree state at failure:', currentTree);
|
|
340
|
-
}
|
|
341
|
-
throw error;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
return currentTree;
|
|
345
|
-
});
|
|
346
|
-
tree.destroy = () => {
|
|
347
|
-
try {
|
|
348
|
-
if (isLazy) {
|
|
349
|
-
const state = tree.state;
|
|
350
|
-
if (state && typeof state === 'object' && '__cleanup__' in state) {
|
|
351
|
-
const cleanup = state.__cleanup__;
|
|
352
|
-
if (typeof cleanup === 'function') {
|
|
353
|
-
cleanup();
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
if (config.debugMode) {
|
|
358
|
-
console.log(SIGNAL_TREE_MESSAGES.TREE_DESTROYED);
|
|
248
|
+
const isLazy = config.useLazySignals ?? shouldUseLazy(tree.state, config);
|
|
249
|
+
tree.with = (...enhancers) => {
|
|
250
|
+
if (enhancers.length === 0) {
|
|
251
|
+
return tree;
|
|
252
|
+
}
|
|
253
|
+
const coreCapabilities = new Set();
|
|
254
|
+
if (config.batchUpdates) coreCapabilities.add('batchUpdate');
|
|
255
|
+
if (config.useMemoization) coreCapabilities.add('memoize');
|
|
256
|
+
if (config.enableTimeTravel) coreCapabilities.add('undo');
|
|
257
|
+
if (config.enableDevTools) coreCapabilities.add('connectDevTools');
|
|
258
|
+
try {
|
|
259
|
+
for (const key of Object.keys(tree)) coreCapabilities.add(String(key));
|
|
260
|
+
} catch {}
|
|
261
|
+
const hasMetadata = enhancers.some(e => Boolean(e.metadata && (e.metadata.requires || e.metadata.provides)));
|
|
262
|
+
let orderedEnhancers = enhancers;
|
|
263
|
+
if (hasMetadata) {
|
|
264
|
+
try {
|
|
265
|
+
orderedEnhancers = resolveEnhancerOrder(enhancers, coreCapabilities, config.debugMode);
|
|
266
|
+
} catch (err) {
|
|
267
|
+
console.warn(SIGNAL_TREE_MESSAGES.ENHANCER_ORDER_FAILED, err);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const provided = new Set(coreCapabilities);
|
|
271
|
+
let currentTree = tree;
|
|
272
|
+
for (let i = 0; i < orderedEnhancers.length; i++) {
|
|
273
|
+
const enhancer = orderedEnhancers[i];
|
|
274
|
+
if (typeof enhancer !== 'function') {
|
|
275
|
+
throw new Error(SIGNAL_TREE_MESSAGES.ENHANCER_NOT_FUNCTION.replace('%d', String(i)));
|
|
276
|
+
}
|
|
277
|
+
const reqs = enhancer.metadata?.requires ?? [];
|
|
278
|
+
for (const r of reqs) {
|
|
279
|
+
if (!(r in currentTree) && !provided.has(r)) {
|
|
280
|
+
const name = enhancer.metadata?.name ?? `enhancer#${i}`;
|
|
281
|
+
const msg = SIGNAL_TREE_MESSAGES.ENHANCER_REQUIREMENT_MISSING.replace('%s', name).replace('%s', r);
|
|
282
|
+
if (config.debugMode) {
|
|
283
|
+
throw new Error(msg);
|
|
284
|
+
} else {
|
|
285
|
+
console.warn(msg);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const result = enhancer(currentTree);
|
|
291
|
+
if (result !== currentTree) currentTree = result;
|
|
292
|
+
const provs = enhancer.metadata?.provides ?? [];
|
|
293
|
+
for (const p of provs) provided.add(p);
|
|
294
|
+
if (config.debugMode && provs.length > 0) {
|
|
295
|
+
for (const p of provs) {
|
|
296
|
+
if (!(p in currentTree)) {
|
|
297
|
+
console.warn(SIGNAL_TREE_MESSAGES.ENHANCER_PROVIDES_MISSING.replace('%s', enhancer.metadata?.name ?? String(i)).replace('%s', p));
|
|
359
298
|
}
|
|
299
|
+
}
|
|
360
300
|
}
|
|
361
|
-
|
|
362
|
-
|
|
301
|
+
} catch (error) {
|
|
302
|
+
const name = enhancer.metadata?.name || `enhancer at index ${i}`;
|
|
303
|
+
console.error(SIGNAL_TREE_MESSAGES.ENHANCER_FAILED.replace('%s', name), error);
|
|
304
|
+
if (config.debugMode) {
|
|
305
|
+
console.error('[SignalTree] Enhancer stack trace:', enhancer);
|
|
306
|
+
console.error('[SignalTree] Tree state at failure:', currentTree);
|
|
363
307
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return currentTree;
|
|
312
|
+
};
|
|
313
|
+
tree.destroy = () => {
|
|
314
|
+
try {
|
|
315
|
+
if (isLazy) {
|
|
316
|
+
const state = tree.state;
|
|
317
|
+
if (state && typeof state === 'object' && '__cleanup__' in state) {
|
|
318
|
+
const cleanup = state.__cleanup__;
|
|
319
|
+
if (typeof cleanup === 'function') {
|
|
320
|
+
cleanup();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (config.debugMode) {
|
|
325
|
+
console.log(SIGNAL_TREE_MESSAGES.TREE_DESTROYED);
|
|
326
|
+
}
|
|
327
|
+
} catch (error) {
|
|
328
|
+
console.error(SIGNAL_TREE_MESSAGES.CLEANUP_ERROR, error);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
addStubMethods(tree, config);
|
|
332
|
+
return tree;
|
|
367
333
|
}
|
|
368
334
|
function addStubMethods(tree, config) {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
console.warn(SIGNAL_TREE_MESSAGES.MEMOIZE_NOT_ENABLED);
|
|
378
|
-
void cacheKey;
|
|
379
|
-
return computed(() => fn(tree()));
|
|
380
|
-
};
|
|
381
|
-
tree.memoizedUpdate = (updater) => {
|
|
382
|
-
if (config.debugMode) {
|
|
383
|
-
console.warn(SIGNAL_TREE_MESSAGES.MEMOIZE_NOT_ENABLED);
|
|
384
|
-
}
|
|
385
|
-
tree((current) => ({ ...current, ...updater(current) }));
|
|
386
|
-
};
|
|
387
|
-
tree.clearMemoCache = () => {
|
|
388
|
-
if (config.debugMode) {
|
|
389
|
-
console.warn(SIGNAL_TREE_MESSAGES.MEMOIZE_NOT_ENABLED);
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
tree.getCacheStats = () => ({
|
|
393
|
-
size: 0,
|
|
394
|
-
hitRate: 0,
|
|
395
|
-
totalHits: 0,
|
|
396
|
-
totalMisses: 0,
|
|
397
|
-
keys: [],
|
|
335
|
+
tree.batchUpdate = updater => {
|
|
336
|
+
console.warn(SIGNAL_TREE_MESSAGES.BATCH_NOT_ENABLED);
|
|
337
|
+
tree(current => {
|
|
338
|
+
const partial = updater(current);
|
|
339
|
+
return {
|
|
340
|
+
...current,
|
|
341
|
+
...partial
|
|
342
|
+
};
|
|
398
343
|
});
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
}
|
|
344
|
+
};
|
|
345
|
+
tree.memoize = (fn, cacheKey) => {
|
|
346
|
+
console.warn(SIGNAL_TREE_MESSAGES.MEMOIZE_NOT_ENABLED);
|
|
347
|
+
return computed(() => fn(tree()));
|
|
348
|
+
};
|
|
349
|
+
tree.memoizedUpdate = updater => {
|
|
350
|
+
if (config.debugMode) {
|
|
351
|
+
console.warn(SIGNAL_TREE_MESSAGES.MEMOIZE_NOT_ENABLED);
|
|
352
|
+
}
|
|
353
|
+
tree(current => ({
|
|
354
|
+
...current,
|
|
355
|
+
...updater(current)
|
|
356
|
+
}));
|
|
357
|
+
};
|
|
358
|
+
tree.clearMemoCache = () => {
|
|
359
|
+
if (config.debugMode) {
|
|
360
|
+
console.warn(SIGNAL_TREE_MESSAGES.MEMOIZE_NOT_ENABLED);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
tree.getCacheStats = () => ({
|
|
364
|
+
size: 0,
|
|
365
|
+
hitRate: 0,
|
|
366
|
+
totalHits: 0,
|
|
367
|
+
totalMisses: 0,
|
|
368
|
+
keys: []
|
|
369
|
+
});
|
|
370
|
+
tree.effect = fn => {
|
|
371
|
+
try {
|
|
372
|
+
effect(() => fn(tree()));
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (config.debugMode) {
|
|
375
|
+
console.warn(SIGNAL_TREE_MESSAGES.EFFECT_NO_CONTEXT, error);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
tree.subscribe = fn => {
|
|
380
|
+
try {
|
|
381
|
+
const destroyRef = inject(DestroyRef);
|
|
382
|
+
let isDestroyed = false;
|
|
383
|
+
const effectRef = effect(() => {
|
|
384
|
+
if (!isDestroyed) {
|
|
385
|
+
fn(tree());
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
const unsubscribe = () => {
|
|
389
|
+
isDestroyed = true;
|
|
390
|
+
effectRef.destroy();
|
|
391
|
+
};
|
|
392
|
+
destroyRef.onDestroy(unsubscribe);
|
|
393
|
+
return unsubscribe;
|
|
394
|
+
} catch (error) {
|
|
395
|
+
if (config.debugMode) {
|
|
396
|
+
console.warn(SIGNAL_TREE_MESSAGES.SUBSCRIBE_NO_CONTEXT, error);
|
|
397
|
+
}
|
|
398
|
+
fn(tree());
|
|
399
|
+
return () => {};
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
tree.optimize = () => {
|
|
403
|
+
if (config.debugMode) {
|
|
404
|
+
console.warn(SIGNAL_TREE_MESSAGES.OPTIMIZE_NOT_AVAILABLE);
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
tree.clearCache = () => {
|
|
408
|
+
if (config.debugMode) {
|
|
409
|
+
console.warn(SIGNAL_TREE_MESSAGES.CACHE_NOT_AVAILABLE);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
tree.invalidatePattern = () => {
|
|
413
|
+
if (config.debugMode) {
|
|
414
|
+
console.warn(SIGNAL_TREE_MESSAGES.PERFORMANCE_NOT_ENABLED);
|
|
415
|
+
}
|
|
416
|
+
return 0;
|
|
417
|
+
};
|
|
418
|
+
const treeWithEngine = tree;
|
|
419
|
+
if (!treeWithEngine.updateEngine) {
|
|
420
|
+
treeWithEngine.updateEngine = new OptimizedUpdateEngine(tree);
|
|
421
|
+
}
|
|
422
|
+
tree.updateOptimized = (updates, options = {}) => {
|
|
450
423
|
const treeWithEngine = tree;
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
cacheMisses: 0,
|
|
477
|
-
averageUpdateTime: 0,
|
|
478
|
-
};
|
|
479
|
-
};
|
|
480
|
-
tree.addTap = (middleware) => {
|
|
481
|
-
if (config.debugMode) {
|
|
482
|
-
console.warn(SIGNAL_TREE_MESSAGES.MIDDLEWARE_NOT_AVAILABLE);
|
|
483
|
-
}
|
|
484
|
-
void middleware;
|
|
485
|
-
};
|
|
486
|
-
tree.removeTap = (id) => {
|
|
487
|
-
if (config.debugMode) {
|
|
488
|
-
console.warn(SIGNAL_TREE_MESSAGES.MIDDLEWARE_NOT_AVAILABLE);
|
|
489
|
-
}
|
|
490
|
-
void id;
|
|
491
|
-
};
|
|
492
|
-
tree.entities = () => {
|
|
493
|
-
if (config.debugMode) {
|
|
494
|
-
console.warn(SIGNAL_TREE_MESSAGES.ENTITY_HELPERS_NOT_AVAILABLE);
|
|
495
|
-
}
|
|
496
|
-
return {};
|
|
497
|
-
};
|
|
498
|
-
tree.asyncAction = (operation, asyncConfig = {}) => {
|
|
499
|
-
if (config.debugMode) {
|
|
500
|
-
console.warn(SIGNAL_TREE_MESSAGES.ASYNC_ACTIONS_NOT_AVAILABLE);
|
|
501
|
-
}
|
|
502
|
-
void operation;
|
|
503
|
-
void asyncConfig;
|
|
504
|
-
return {};
|
|
505
|
-
};
|
|
506
|
-
tree.undo = () => {
|
|
507
|
-
if (config.debugMode) {
|
|
508
|
-
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
tree.redo = () => {
|
|
512
|
-
if (config.debugMode) {
|
|
513
|
-
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
514
|
-
}
|
|
515
|
-
};
|
|
516
|
-
tree.getHistory = () => {
|
|
517
|
-
if (config.debugMode) {
|
|
518
|
-
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
519
|
-
}
|
|
520
|
-
return [];
|
|
521
|
-
};
|
|
522
|
-
tree.resetHistory = () => {
|
|
523
|
-
if (config.debugMode) {
|
|
524
|
-
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
525
|
-
}
|
|
424
|
+
const engine = treeWithEngine.updateEngine;
|
|
425
|
+
if (!engine) {
|
|
426
|
+
if (config.debugMode) {
|
|
427
|
+
console.warn(SIGNAL_TREE_MESSAGES.UPDATE_OPTIMIZED_NOT_AVAILABLE);
|
|
428
|
+
}
|
|
429
|
+
tree(current => ({
|
|
430
|
+
...current,
|
|
431
|
+
...updates
|
|
432
|
+
}));
|
|
433
|
+
return {
|
|
434
|
+
changed: true,
|
|
435
|
+
duration: 0,
|
|
436
|
+
changedPaths: Object.keys(updates),
|
|
437
|
+
stats: undefined
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
return engine.update(tree(), updates, options);
|
|
441
|
+
};
|
|
442
|
+
tree.getMetrics = () => {
|
|
443
|
+
return {
|
|
444
|
+
updates: 0,
|
|
445
|
+
computations: 0,
|
|
446
|
+
cacheHits: 0,
|
|
447
|
+
cacheMisses: 0,
|
|
448
|
+
averageUpdateTime: 0
|
|
526
449
|
};
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (
|
|
530
|
-
|
|
450
|
+
};
|
|
451
|
+
tree.addTap = middleware => {
|
|
452
|
+
if (config.debugMode) {
|
|
453
|
+
console.warn(SIGNAL_TREE_MESSAGES.MIDDLEWARE_NOT_AVAILABLE);
|
|
531
454
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
const signalState = signal(obj, {
|
|
537
|
-
equal: equalityFn,
|
|
538
|
-
});
|
|
539
|
-
const tree = makeRootNodeAccessor(signalState, signalState);
|
|
540
|
-
Object.defineProperty(tree, 'state', {
|
|
541
|
-
value: signalState,
|
|
542
|
-
enumerable: false,
|
|
543
|
-
});
|
|
544
|
-
Object.defineProperty(tree, '$', { value: signalState, enumerable: false });
|
|
545
|
-
enhanceTree(tree, config);
|
|
546
|
-
return tree;
|
|
455
|
+
};
|
|
456
|
+
tree.removeTap = id => {
|
|
457
|
+
if (config.debugMode) {
|
|
458
|
+
console.warn(SIGNAL_TREE_MESSAGES.MIDDLEWARE_NOT_AVAILABLE);
|
|
547
459
|
}
|
|
548
|
-
|
|
460
|
+
};
|
|
461
|
+
tree.entities = () => {
|
|
549
462
|
if (config.debugMode) {
|
|
550
|
-
|
|
463
|
+
console.warn(SIGNAL_TREE_MESSAGES.ENTITY_HELPERS_NOT_AVAILABLE);
|
|
551
464
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
signalState = createLazySignalTree(obj, equalityFn, '', memoryManager);
|
|
558
|
-
}
|
|
559
|
-
else {
|
|
560
|
-
signalState = createSignalStore(obj, equalityFn);
|
|
561
|
-
}
|
|
465
|
+
return {};
|
|
466
|
+
};
|
|
467
|
+
tree.asyncAction = (operation, asyncConfig = {}) => {
|
|
468
|
+
if (config.debugMode) {
|
|
469
|
+
console.warn(SIGNAL_TREE_MESSAGES.ASYNC_ACTIONS_NOT_AVAILABLE);
|
|
562
470
|
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
}
|
|
569
|
-
else {
|
|
570
|
-
throw error;
|
|
571
|
-
}
|
|
471
|
+
return {};
|
|
472
|
+
};
|
|
473
|
+
tree.undo = () => {
|
|
474
|
+
if (config.debugMode) {
|
|
475
|
+
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
572
476
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
477
|
+
};
|
|
478
|
+
tree.redo = () => {
|
|
479
|
+
if (config.debugMode) {
|
|
480
|
+
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
tree.getHistory = () => {
|
|
484
|
+
if (config.debugMode) {
|
|
485
|
+
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
486
|
+
}
|
|
487
|
+
return [];
|
|
488
|
+
};
|
|
489
|
+
tree.resetHistory = () => {
|
|
490
|
+
if (config.debugMode) {
|
|
491
|
+
console.warn(SIGNAL_TREE_MESSAGES.TIME_TRAVEL_NOT_AVAILABLE);
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
function create(obj, config = {}) {
|
|
496
|
+
if (obj === null || obj === undefined) {
|
|
497
|
+
throw new Error(SIGNAL_TREE_MESSAGES.NULL_OR_UNDEFINED);
|
|
498
|
+
}
|
|
499
|
+
validateTree(obj, config);
|
|
500
|
+
const estimatedSize = estimateObjectSize(obj);
|
|
501
|
+
const equalityFn = createEqualityFn(config.useShallowComparison ?? false);
|
|
502
|
+
if (Array.isArray(obj)) {
|
|
503
|
+
const signalState = signal(obj, {
|
|
504
|
+
equal: equalityFn
|
|
590
505
|
});
|
|
506
|
+
const tree = makeRootNodeAccessor(signalState, signalState);
|
|
591
507
|
Object.defineProperty(tree, 'state', {
|
|
592
|
-
|
|
593
|
-
|
|
508
|
+
value: signalState,
|
|
509
|
+
enumerable: false
|
|
510
|
+
});
|
|
511
|
+
Object.defineProperty(tree, '$', {
|
|
512
|
+
value: signalState,
|
|
513
|
+
enumerable: false
|
|
594
514
|
});
|
|
595
|
-
Object.defineProperty(tree, '$', { value: signalState, enumerable: false });
|
|
596
|
-
if (memoryManager) {
|
|
597
|
-
Object.defineProperty(tree, 'dispose', {
|
|
598
|
-
value: () => {
|
|
599
|
-
memoryManager?.dispose();
|
|
600
|
-
const cleanup = signalState.__cleanup__;
|
|
601
|
-
if (typeof cleanup === 'function') {
|
|
602
|
-
cleanup();
|
|
603
|
-
}
|
|
604
|
-
},
|
|
605
|
-
enumerable: false,
|
|
606
|
-
writable: false,
|
|
607
|
-
});
|
|
608
|
-
}
|
|
609
515
|
enhanceTree(tree, config);
|
|
610
|
-
for (const key in signalState) {
|
|
611
|
-
if (Object.prototype.hasOwnProperty.call(signalState, key)) {
|
|
612
|
-
if (!(key in tree)) {
|
|
613
|
-
try {
|
|
614
|
-
Object.defineProperty(tree, key, {
|
|
615
|
-
value: signalState[key],
|
|
616
|
-
enumerable: true,
|
|
617
|
-
configurable: true,
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
catch {
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
516
|
return tree;
|
|
517
|
+
}
|
|
518
|
+
const useLazy = shouldUseLazy(obj, config, estimatedSize);
|
|
519
|
+
if (config.debugMode) {
|
|
520
|
+
console.log(SIGNAL_TREE_MESSAGES.STRATEGY_SELECTION.replace('%s', useLazy ? 'lazy' : 'eager').replace('%d', String(estimatedSize)));
|
|
521
|
+
}
|
|
522
|
+
let signalState;
|
|
523
|
+
let memoryManager;
|
|
524
|
+
try {
|
|
525
|
+
if (useLazy && typeof obj === 'object') {
|
|
526
|
+
memoryManager = new SignalMemoryManager();
|
|
527
|
+
signalState = createLazySignalTree(obj, equalityFn, '', memoryManager);
|
|
528
|
+
} else {
|
|
529
|
+
signalState = createSignalStore(obj, equalityFn);
|
|
530
|
+
}
|
|
531
|
+
} catch (error) {
|
|
532
|
+
if (useLazy) {
|
|
533
|
+
console.warn(SIGNAL_TREE_MESSAGES.LAZY_FALLBACK, error);
|
|
534
|
+
signalState = createSignalStore(obj, equalityFn);
|
|
535
|
+
memoryManager = undefined;
|
|
536
|
+
} else {
|
|
537
|
+
throw error;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
const tree = function (arg) {
|
|
541
|
+
if (arguments.length === 0) {
|
|
542
|
+
return unwrap(signalState);
|
|
543
|
+
}
|
|
544
|
+
if (typeof arg === 'function') {
|
|
545
|
+
const updater = arg;
|
|
546
|
+
const currentValue = unwrap(signalState);
|
|
547
|
+
const newValue = updater(currentValue);
|
|
548
|
+
recursiveUpdate(signalState, newValue);
|
|
549
|
+
} else {
|
|
550
|
+
recursiveUpdate(signalState, arg);
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
Object.defineProperty(tree, NODE_ACCESSOR_SYMBOL, {
|
|
554
|
+
value: true,
|
|
555
|
+
enumerable: false
|
|
556
|
+
});
|
|
557
|
+
Object.defineProperty(tree, 'state', {
|
|
558
|
+
value: signalState,
|
|
559
|
+
enumerable: false
|
|
560
|
+
});
|
|
561
|
+
Object.defineProperty(tree, '$', {
|
|
562
|
+
value: signalState,
|
|
563
|
+
enumerable: false
|
|
564
|
+
});
|
|
565
|
+
if (memoryManager) {
|
|
566
|
+
Object.defineProperty(tree, 'dispose', {
|
|
567
|
+
value: () => {
|
|
568
|
+
memoryManager?.dispose();
|
|
569
|
+
const cleanup = signalState.__cleanup__;
|
|
570
|
+
if (typeof cleanup === 'function') {
|
|
571
|
+
cleanup();
|
|
572
|
+
}
|
|
573
|
+
},
|
|
574
|
+
enumerable: false,
|
|
575
|
+
writable: false
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
enhanceTree(tree, config);
|
|
579
|
+
for (const key in signalState) {
|
|
580
|
+
if (Object.prototype.hasOwnProperty.call(signalState, key)) {
|
|
581
|
+
if (!(key in tree)) {
|
|
582
|
+
try {
|
|
583
|
+
Object.defineProperty(tree, key, {
|
|
584
|
+
value: signalState[key],
|
|
585
|
+
enumerable: true,
|
|
586
|
+
configurable: true
|
|
587
|
+
});
|
|
588
|
+
} catch {}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return tree;
|
|
626
593
|
}
|
|
627
594
|
const presetConfigs = {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
595
|
+
basic: {
|
|
596
|
+
useLazySignals: false,
|
|
597
|
+
debugMode: false
|
|
598
|
+
},
|
|
599
|
+
performance: {
|
|
600
|
+
useLazySignals: true,
|
|
601
|
+
batchUpdates: true,
|
|
602
|
+
useMemoization: true,
|
|
603
|
+
useShallowComparison: true
|
|
604
|
+
},
|
|
605
|
+
development: {
|
|
606
|
+
useLazySignals: false,
|
|
607
|
+
debugMode: true,
|
|
608
|
+
enableDevTools: true,
|
|
609
|
+
trackPerformance: true
|
|
610
|
+
},
|
|
611
|
+
production: {
|
|
612
|
+
useLazySignals: true,
|
|
613
|
+
batchUpdates: true,
|
|
614
|
+
useMemoization: true,
|
|
615
|
+
debugMode: false
|
|
616
|
+
}
|
|
650
617
|
};
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
return create(obj, config);
|
|
618
|
+
function signalTree(obj, configOrPreset) {
|
|
619
|
+
if (typeof configOrPreset === 'string') {
|
|
620
|
+
const config = presetConfigs[configOrPreset];
|
|
621
|
+
if (!config) {
|
|
622
|
+
console.warn(SIGNAL_TREE_MESSAGES.PRESET_UNKNOWN.replace('%s', configOrPreset));
|
|
623
|
+
return create(obj, {});
|
|
659
624
|
}
|
|
660
|
-
const config = configOrPreset || {};
|
|
661
625
|
return create(obj, config);
|
|
626
|
+
}
|
|
627
|
+
const config = configOrPreset || {};
|
|
628
|
+
return create(obj, config);
|
|
662
629
|
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
}
|
|
630
|
+
|
|
631
|
+
export { isNodeAccessor, signalTree };
|