@signaltree/core 4.0.16 → 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 +27 -0
- 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 +161 -0
- package/dist/enhancers/computed/lib/computed.js +21 -0
- package/dist/enhancers/devtools/lib/devtools.js +321 -0
- package/dist/enhancers/entities/lib/entities.js +115 -0
- package/dist/enhancers/index.js +72 -0
- package/dist/enhancers/memoization/lib/memoization.js +379 -0
- package/dist/enhancers/middleware/lib/async-helpers.js +77 -0
- package/dist/enhancers/middleware/lib/middleware.js +136 -0
- package/dist/enhancers/presets/lib/presets.js +88 -0
- package/dist/enhancers/serialization/constants.js +15 -0
- package/dist/enhancers/serialization/lib/serialization.js +660 -0
- package/dist/enhancers/time-travel/lib/time-travel.js +193 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +19 -0
- package/dist/is-built-in-object.js +23 -0
- package/dist/lib/constants.js +57 -0
- package/dist/lib/memory/memory-manager.js +164 -0
- package/dist/lib/performance/diff-engine.js +156 -0
- package/dist/lib/performance/path-index.js +156 -0
- package/dist/lib/performance/update-engine.js +188 -0
- package/dist/lib/security/security-validator.js +121 -0
- package/dist/lib/signal-tree.js +631 -0
- package/dist/lib/types.js +3 -0
- package/dist/lib/utils.js +254 -0
- package/dist/lru-cache.js +64 -0
- package/dist/parse-path.js +13 -0
- package/package.json +30 -16
- package/src/enhancers/batching/index.d.ts +1 -0
- package/src/enhancers/batching/jest.config.d.ts +15 -0
- package/src/enhancers/batching/lib/batching.d.ts +16 -0
- package/src/enhancers/batching/test-setup.d.ts +1 -0
- package/src/enhancers/computed/index.d.ts +1 -0
- package/src/enhancers/computed/jest.config.d.ts +15 -0
- package/src/enhancers/computed/lib/computed.d.ts +12 -0
- package/src/enhancers/devtools/index.d.ts +1 -0
- package/src/enhancers/devtools/jest.config.d.ts +15 -0
- package/src/enhancers/devtools/lib/devtools.d.ts +77 -0
- package/src/enhancers/devtools/test-setup.d.ts +1 -0
- package/src/enhancers/entities/index.d.ts +1 -0
- package/src/enhancers/entities/jest.config.d.ts +15 -0
- package/src/enhancers/entities/lib/entities.d.ts +22 -0
- package/src/enhancers/entities/test-setup.d.ts +1 -0
- package/src/enhancers/index.d.ts +3 -0
- package/src/enhancers/memoization/index.d.ts +1 -0
- package/src/enhancers/memoization/jest.config.d.ts +15 -0
- package/src/enhancers/memoization/lib/memoization.d.ts +65 -0
- package/src/enhancers/memoization/test-setup.d.ts +1 -0
- package/src/enhancers/middleware/index.d.ts +2 -0
- package/src/enhancers/middleware/jest.config.d.ts +15 -0
- package/src/enhancers/middleware/lib/async-helpers.d.ts +8 -0
- package/src/enhancers/middleware/lib/middleware.d.ts +11 -0
- package/src/enhancers/middleware/test-setup.d.ts +1 -0
- package/src/enhancers/presets/index.d.ts +1 -0
- package/src/enhancers/presets/jest.config.d.ts +15 -0
- package/src/enhancers/presets/lib/presets.d.ts +11 -0
- package/src/enhancers/presets/test-setup.d.ts +1 -0
- package/src/enhancers/serialization/constants.d.ts +14 -0
- package/src/enhancers/serialization/index.d.ts +2 -0
- package/src/enhancers/serialization/jest.config.d.ts +15 -0
- package/src/enhancers/serialization/lib/serialization.d.ts +59 -0
- package/src/enhancers/serialization/test-setup.d.ts +1 -0
- package/src/enhancers/time-travel/index.d.ts +1 -0
- package/src/enhancers/time-travel/jest.config.d.ts +15 -0
- package/src/enhancers/time-travel/lib/time-travel.d.ts +36 -0
- package/src/enhancers/time-travel/lib/utils.d.ts +1 -0
- package/src/enhancers/time-travel/test-setup.d.ts +1 -0
- package/src/enhancers/types.d.ts +105 -0
- package/src/index.d.ts +17 -0
- package/src/lib/constants.d.ts +42 -0
- package/src/lib/memory/memory-manager.d.ts +30 -0
- package/src/lib/performance/diff-engine.d.ts +33 -0
- package/src/lib/performance/path-index.d.ts +25 -0
- package/src/lib/performance/update-engine.d.ts +32 -0
- package/src/lib/security/security-validator.d.ts +33 -0
- package/src/lib/signal-tree.d.ts +8 -0
- package/src/lib/types.d.ts +164 -0
- package/src/lib/utils.d.ts +28 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { deepClone } from '../../../deep-clone.js';
|
|
2
|
+
import { deepEqual } from '../../../deep-equal.js';
|
|
3
|
+
|
|
4
|
+
class TimeTravelManager {
|
|
5
|
+
tree;
|
|
6
|
+
config;
|
|
7
|
+
restoreStateFn;
|
|
8
|
+
history = [];
|
|
9
|
+
currentIndex = -1;
|
|
10
|
+
maxHistorySize;
|
|
11
|
+
includePayload;
|
|
12
|
+
actionNames;
|
|
13
|
+
constructor(tree, config = {}, restoreStateFn) {
|
|
14
|
+
this.tree = tree;
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.restoreStateFn = restoreStateFn;
|
|
17
|
+
this.maxHistorySize = config.maxHistorySize ?? 50;
|
|
18
|
+
this.includePayload = config.includePayload ?? true;
|
|
19
|
+
this.actionNames = {
|
|
20
|
+
update: 'UPDATE',
|
|
21
|
+
set: 'SET',
|
|
22
|
+
batch: 'BATCH',
|
|
23
|
+
...config.actionNames
|
|
24
|
+
};
|
|
25
|
+
this.addEntry('INIT', this.tree());
|
|
26
|
+
}
|
|
27
|
+
addEntry(action, state, payload) {
|
|
28
|
+
if (this.currentIndex < this.history.length - 1) {
|
|
29
|
+
this.history = this.history.slice(0, this.currentIndex + 1);
|
|
30
|
+
}
|
|
31
|
+
const entry = {
|
|
32
|
+
state: deepClone(state),
|
|
33
|
+
timestamp: Date.now(),
|
|
34
|
+
action: this.actionNames[action] || action,
|
|
35
|
+
...(this.includePayload && payload !== undefined && {
|
|
36
|
+
payload
|
|
37
|
+
})
|
|
38
|
+
};
|
|
39
|
+
this.history.push(entry);
|
|
40
|
+
this.currentIndex = this.history.length - 1;
|
|
41
|
+
if (this.history.length > this.maxHistorySize) {
|
|
42
|
+
this.history.shift();
|
|
43
|
+
this.currentIndex--;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
undo() {
|
|
47
|
+
if (!this.canUndo()) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
this.currentIndex--;
|
|
51
|
+
const entry = this.history[this.currentIndex];
|
|
52
|
+
this.restoreState(entry.state);
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
redo() {
|
|
56
|
+
if (!this.canRedo()) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
this.currentIndex++;
|
|
60
|
+
const entry = this.history[this.currentIndex];
|
|
61
|
+
this.restoreState(entry.state);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
getHistory() {
|
|
65
|
+
return this.history.map(entry => ({
|
|
66
|
+
...entry,
|
|
67
|
+
state: deepClone(entry.state)
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
resetHistory() {
|
|
71
|
+
const currentState = this.tree();
|
|
72
|
+
this.history = [];
|
|
73
|
+
this.currentIndex = -1;
|
|
74
|
+
this.addEntry('RESET', currentState);
|
|
75
|
+
}
|
|
76
|
+
jumpTo(index) {
|
|
77
|
+
if (index < 0 || index >= this.history.length) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
this.currentIndex = index;
|
|
81
|
+
const entry = this.history[index];
|
|
82
|
+
this.restoreState(entry.state);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
getCurrentIndex() {
|
|
86
|
+
return this.currentIndex;
|
|
87
|
+
}
|
|
88
|
+
canUndo() {
|
|
89
|
+
return this.currentIndex > 0;
|
|
90
|
+
}
|
|
91
|
+
canRedo() {
|
|
92
|
+
return this.currentIndex < this.history.length - 1;
|
|
93
|
+
}
|
|
94
|
+
restoreState(state) {
|
|
95
|
+
if (this.restoreStateFn) {
|
|
96
|
+
this.restoreStateFn(state);
|
|
97
|
+
} else {
|
|
98
|
+
this.tree(state);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function withTimeTravel(config = {}) {
|
|
103
|
+
return tree => {
|
|
104
|
+
const originalTreeCall = tree.bind(tree);
|
|
105
|
+
let isRestoring = false;
|
|
106
|
+
const timeTravelManager = new TimeTravelManager(tree, config, state => {
|
|
107
|
+
isRestoring = true;
|
|
108
|
+
try {
|
|
109
|
+
originalTreeCall(state);
|
|
110
|
+
} finally {
|
|
111
|
+
isRestoring = false;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const enhancedTree = function (...args) {
|
|
115
|
+
if (args.length === 0) {
|
|
116
|
+
return originalTreeCall();
|
|
117
|
+
} else {
|
|
118
|
+
if (isRestoring) {
|
|
119
|
+
if (args.length === 1) {
|
|
120
|
+
const arg = args[0];
|
|
121
|
+
if (typeof arg === 'function') {
|
|
122
|
+
return originalTreeCall(arg);
|
|
123
|
+
} else {
|
|
124
|
+
return originalTreeCall(arg);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const beforeState = originalTreeCall();
|
|
130
|
+
let result;
|
|
131
|
+
if (args.length === 1) {
|
|
132
|
+
const arg = args[0];
|
|
133
|
+
if (typeof arg === 'function') {
|
|
134
|
+
result = originalTreeCall(arg);
|
|
135
|
+
} else {
|
|
136
|
+
result = originalTreeCall(arg);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const afterState = originalTreeCall();
|
|
140
|
+
const statesEqual = deepEqual(beforeState, afterState);
|
|
141
|
+
if (!statesEqual) {
|
|
142
|
+
timeTravelManager.addEntry('update', afterState);
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
Object.setPrototypeOf(enhancedTree, Object.getPrototypeOf(tree));
|
|
148
|
+
Object.assign(enhancedTree, tree);
|
|
149
|
+
if ('state' in tree) {
|
|
150
|
+
Object.defineProperty(enhancedTree, 'state', {
|
|
151
|
+
value: tree.state,
|
|
152
|
+
enumerable: false,
|
|
153
|
+
configurable: true
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
if ('$' in tree) {
|
|
157
|
+
Object.defineProperty(enhancedTree, '$', {
|
|
158
|
+
value: tree['$'],
|
|
159
|
+
enumerable: false,
|
|
160
|
+
configurable: true
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
enhancedTree.undo = () => {
|
|
164
|
+
timeTravelManager.undo();
|
|
165
|
+
};
|
|
166
|
+
enhancedTree.redo = () => {
|
|
167
|
+
timeTravelManager.redo();
|
|
168
|
+
};
|
|
169
|
+
enhancedTree.getHistory = () => timeTravelManager.getHistory();
|
|
170
|
+
enhancedTree.resetHistory = () => {
|
|
171
|
+
timeTravelManager.resetHistory();
|
|
172
|
+
};
|
|
173
|
+
enhancedTree.jumpTo = index => {
|
|
174
|
+
timeTravelManager.jumpTo(index);
|
|
175
|
+
};
|
|
176
|
+
enhancedTree.canUndo = () => timeTravelManager.canUndo();
|
|
177
|
+
enhancedTree.canRedo = () => timeTravelManager.canRedo();
|
|
178
|
+
enhancedTree.getCurrentIndex = () => timeTravelManager.getCurrentIndex();
|
|
179
|
+
return Object.assign(enhancedTree, {
|
|
180
|
+
__timeTravel: timeTravelManager
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function enableTimeTravel(maxHistorySize) {
|
|
185
|
+
return withTimeTravel({
|
|
186
|
+
maxHistorySize
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
function getTimeTravel(tree) {
|
|
190
|
+
return tree.__timeTravel;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { enableTimeTravel, getTimeTravel, withTimeTravel };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { signalTree } from './lib/signal-tree.js';
|
|
2
|
+
export { composeEnhancers, createLazySignalTree, isAnySignal, isNodeAccessor, toWritableSignal } from './lib/utils.js';
|
|
3
|
+
export { SecurityPresets, SecurityValidator } from './lib/security/security-validator.js';
|
|
4
|
+
export { createEnhancer, resolveEnhancerOrder } from './enhancers/index.js';
|
|
5
|
+
export { ENHANCER_META } from './lib/types.js';
|
|
6
|
+
export { flushBatchedUpdates, getBatchQueueSize, hasPendingUpdates, withBatching, withHighPerformanceBatching } from './enhancers/batching/lib/batching.js';
|
|
7
|
+
export { cleanupMemoizationCache, clearAllCaches, getGlobalCacheStats, memoize, memoizeReference, memoizeShallow, withComputedMemoization, withDeepStateMemoization, withHighFrequencyMemoization, withHighPerformanceMemoization, withLightweightMemoization, withMemoization, withSelectorMemoization, withShallowMemoization } from './enhancers/memoization/lib/memoization.js';
|
|
8
|
+
export { enableTimeTravel, getTimeTravel, withTimeTravel } from './enhancers/time-travel/lib/time-travel.js';
|
|
9
|
+
export { enableEntities, withEntities, withHighPerformanceEntities } from './enhancers/entities/lib/entities.js';
|
|
10
|
+
export { applyPersistence, applySerialization, createIndexedDBAdapter, createStorageAdapter, enableSerialization, withPersistence, withSerialization } from './enhancers/serialization/lib/serialization.js';
|
|
11
|
+
export { enableDevTools, withDevTools, withFullDevTools, withProductionDevTools } from './enhancers/devtools/lib/devtools.js';
|
|
12
|
+
export { createLoggingMiddleware, createValidationMiddleware, withMiddleware } from './enhancers/middleware/lib/middleware.js';
|
|
13
|
+
export { createAsyncOperation, trackAsync } from './enhancers/middleware/lib/async-helpers.js';
|
|
14
|
+
export { TREE_PRESETS, combinePresets, createDevTree, createPresetConfig, getAvailablePresets, validatePreset } from './enhancers/presets/lib/presets.js';
|
|
15
|
+
export { computedEnhancer, createComputed } from './enhancers/computed/lib/computed.js';
|
|
16
|
+
export { SIGNAL_TREE_CONSTANTS, SIGNAL_TREE_MESSAGES } from './lib/constants.js';
|
|
17
|
+
export { deepEqual, deepEqual as equal } from './deep-equal.js';
|
|
18
|
+
export { parsePath } from './parse-path.js';
|
|
19
|
+
export { isBuiltInObject } from './is-built-in-object.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function isBuiltInObject(value) {
|
|
2
|
+
if (value === null || value === undefined) return false;
|
|
3
|
+
if (value instanceof Date || value instanceof RegExp || typeof value === 'function' || value instanceof Map || value instanceof Set || value instanceof WeakMap || value instanceof WeakSet || value instanceof ArrayBuffer || value instanceof DataView || value instanceof Error || value instanceof Promise) {
|
|
4
|
+
return true;
|
|
5
|
+
}
|
|
6
|
+
if (value instanceof Int8Array || value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int16Array || value instanceof Uint16Array || value instanceof Int32Array || value instanceof Uint32Array || value instanceof Float32Array || value instanceof Float64Array || value instanceof BigInt64Array || value instanceof BigUint64Array) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (typeof window !== 'undefined') {
|
|
10
|
+
if (value instanceof URL || value instanceof URLSearchParams || value instanceof FormData || value instanceof Blob || typeof File !== 'undefined' && value instanceof File || typeof FileList !== 'undefined' && value instanceof FileList || typeof Headers !== 'undefined' && value instanceof Headers || typeof Request !== 'undefined' && value instanceof Request || typeof Response !== 'undefined' && value instanceof Response || typeof AbortController !== 'undefined' && value instanceof AbortController || typeof AbortSignal !== 'undefined' && value instanceof AbortSignal) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const NodeBuffer = globalThis === null || globalThis === void 0 ? void 0 : globalThis.Buffer;
|
|
16
|
+
if (NodeBuffer && value instanceof NodeBuffer) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
} catch (_a) {}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { isBuiltInObject };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { DEFAULT_PATH_CACHE_SIZE } from '../constants.js';
|
|
2
|
+
|
|
3
|
+
const SIGNAL_TREE_CONSTANTS = {
|
|
4
|
+
MAX_PATH_CACHE_SIZE: DEFAULT_PATH_CACHE_SIZE,
|
|
5
|
+
LAZY_THRESHOLD: 50,
|
|
6
|
+
ESTIMATE_MAX_DEPTH: 3,
|
|
7
|
+
ESTIMATE_SAMPLE_SIZE_ARRAY: 3,
|
|
8
|
+
ESTIMATE_SAMPLE_SIZE_OBJECT: 5,
|
|
9
|
+
DEFAULT_CACHE_SIZE: 100,
|
|
10
|
+
DEFAULT_BATCH_SIZE: 10
|
|
11
|
+
};
|
|
12
|
+
const DEV_MESSAGES = {
|
|
13
|
+
NULL_OR_UNDEFINED: 'null/undefined',
|
|
14
|
+
CIRCULAR_REF: 'circular ref',
|
|
15
|
+
UPDATER_INVALID: 'updater invalid',
|
|
16
|
+
LAZY_FALLBACK: 'lazy fallback',
|
|
17
|
+
SIGNAL_CREATION_FAILED: 'signal creation failed',
|
|
18
|
+
UPDATE_PATH_NOT_FOUND: 'update path not found',
|
|
19
|
+
UPDATE_FAILED: 'update failed',
|
|
20
|
+
ROLLBACK_FAILED: 'rollback failed',
|
|
21
|
+
CLEANUP_ERROR: 'cleanup error',
|
|
22
|
+
PRESET_UNKNOWN: 'unknown preset',
|
|
23
|
+
STRATEGY_SELECTION: 'strategy select',
|
|
24
|
+
TREE_DESTROYED: 'destroyed',
|
|
25
|
+
UPDATE_TRANSACTION: 'update tx',
|
|
26
|
+
BATCH_NOT_ENABLED: 'batching disabled',
|
|
27
|
+
MEMOIZE_NOT_ENABLED: 'memoize disabled',
|
|
28
|
+
MIDDLEWARE_NOT_AVAILABLE: 'middleware missing',
|
|
29
|
+
ENTITY_HELPERS_NOT_AVAILABLE: 'entity helpers missing',
|
|
30
|
+
ASYNC_ACTIONS_NOT_AVAILABLE: 'async actions missing',
|
|
31
|
+
TIME_TRAVEL_NOT_AVAILABLE: 'time travel missing',
|
|
32
|
+
OPTIMIZE_NOT_AVAILABLE: 'optimize missing',
|
|
33
|
+
UPDATE_OPTIMIZED_NOT_AVAILABLE: 'update optimized missing',
|
|
34
|
+
CACHE_NOT_AVAILABLE: 'cache missing',
|
|
35
|
+
PERFORMANCE_NOT_ENABLED: 'performance disabled',
|
|
36
|
+
ENHANCER_ORDER_FAILED: 'enhancer order failed',
|
|
37
|
+
ENHANCER_CYCLE_DETECTED: 'enhancer cycle',
|
|
38
|
+
ENHANCER_REQUIREMENT_MISSING: 'enhancer req missing',
|
|
39
|
+
ENHANCER_PROVIDES_MISSING: 'enhancer provides missing',
|
|
40
|
+
ENHANCER_FAILED: 'enhancer failed',
|
|
41
|
+
ENHANCER_NOT_FUNCTION: 'enhancer not function',
|
|
42
|
+
EFFECT_NO_CONTEXT: 'no angular context',
|
|
43
|
+
SUBSCRIBE_NO_CONTEXT: 'no angular context'
|
|
44
|
+
};
|
|
45
|
+
const PROD_MESSAGES = (() => {
|
|
46
|
+
const out = {};
|
|
47
|
+
let i = 0;
|
|
48
|
+
for (const k of Object.keys(DEV_MESSAGES)) {
|
|
49
|
+
out[k] = String(i++);
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
})();
|
|
53
|
+
const _isProdByEnv = Boolean(typeof globalThis === 'object' && globalThis !== null && 'process' in globalThis && typeof globalThis.process === 'object' && 'env' in globalThis.process && globalThis.process.env.NODE_ENV === 'production');
|
|
54
|
+
const _isDev = typeof ngDevMode !== 'undefined' ? Boolean(ngDevMode) : !_isProdByEnv;
|
|
55
|
+
const SIGNAL_TREE_MESSAGES = Object.freeze(_isDev ? DEV_MESSAGES : PROD_MESSAGES);
|
|
56
|
+
|
|
57
|
+
export { SIGNAL_TREE_CONSTANTS, SIGNAL_TREE_MESSAGES };
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
class SignalMemoryManager {
|
|
2
|
+
cache = new Map();
|
|
3
|
+
registry = null;
|
|
4
|
+
config;
|
|
5
|
+
stats = {
|
|
6
|
+
cleanedUpSignals: 0,
|
|
7
|
+
peakCachedSignals: 0,
|
|
8
|
+
manualDisposes: 0
|
|
9
|
+
};
|
|
10
|
+
constructor(config = {}) {
|
|
11
|
+
this.config = {
|
|
12
|
+
enableAutoCleanup: config.enableAutoCleanup ?? true,
|
|
13
|
+
debugMode: config.debugMode ?? false,
|
|
14
|
+
onCleanup: config.onCleanup ?? (() => {})
|
|
15
|
+
};
|
|
16
|
+
if (this.config.enableAutoCleanup && typeof FinalizationRegistry !== 'undefined') {
|
|
17
|
+
this.registry = new FinalizationRegistry(path => {
|
|
18
|
+
this.handleCleanup(path);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
if (this.config.debugMode) {
|
|
22
|
+
console.log('[SignalMemoryManager] Initialized', {
|
|
23
|
+
autoCleanup: this.config.enableAutoCleanup,
|
|
24
|
+
hasRegistry: !!this.registry
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
cacheSignal(path, signal) {
|
|
29
|
+
const ref = new WeakRef(signal);
|
|
30
|
+
const entry = {
|
|
31
|
+
ref,
|
|
32
|
+
path,
|
|
33
|
+
cachedAt: Date.now()
|
|
34
|
+
};
|
|
35
|
+
this.cache.set(path, entry);
|
|
36
|
+
if (this.registry) {
|
|
37
|
+
this.registry.register(signal, path, signal);
|
|
38
|
+
}
|
|
39
|
+
const currentSize = this.cache.size;
|
|
40
|
+
if (currentSize > this.stats.peakCachedSignals) {
|
|
41
|
+
this.stats.peakCachedSignals = currentSize;
|
|
42
|
+
}
|
|
43
|
+
if (this.config.debugMode) {
|
|
44
|
+
console.log(`[SignalMemoryManager] Cached signal: ${path}`, {
|
|
45
|
+
cacheSize: currentSize,
|
|
46
|
+
peak: this.stats.peakCachedSignals
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
getSignal(path) {
|
|
51
|
+
const entry = this.cache.get(path);
|
|
52
|
+
if (!entry) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const signal = entry.ref.deref();
|
|
56
|
+
if (!signal) {
|
|
57
|
+
this.cache.delete(path);
|
|
58
|
+
if (this.config.debugMode) {
|
|
59
|
+
console.log(`[SignalMemoryManager] Signal GC'd: ${path}`);
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return signal;
|
|
64
|
+
}
|
|
65
|
+
hasSignal(path) {
|
|
66
|
+
return this.cache.has(path);
|
|
67
|
+
}
|
|
68
|
+
removeSignal(path) {
|
|
69
|
+
const entry = this.cache.get(path);
|
|
70
|
+
if (!entry) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const signal = entry.ref.deref();
|
|
74
|
+
if (signal && this.registry) {
|
|
75
|
+
this.registry.unregister(signal);
|
|
76
|
+
}
|
|
77
|
+
this.cache.delete(path);
|
|
78
|
+
if (this.config.debugMode) {
|
|
79
|
+
console.log(`[SignalMemoryManager] Removed signal: ${path}`);
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
handleCleanup(path) {
|
|
84
|
+
this.cache.delete(path);
|
|
85
|
+
this.stats.cleanedUpSignals++;
|
|
86
|
+
const currentStats = this.getStats();
|
|
87
|
+
if (this.config.debugMode) {
|
|
88
|
+
console.log(`[SignalMemoryManager] Auto cleanup: ${path}`, currentStats);
|
|
89
|
+
}
|
|
90
|
+
this.config.onCleanup(path, currentStats);
|
|
91
|
+
}
|
|
92
|
+
getStats() {
|
|
93
|
+
let validSignals = 0;
|
|
94
|
+
for (const [path, entry] of this.cache.entries()) {
|
|
95
|
+
if (entry.ref.deref()) {
|
|
96
|
+
validSignals++;
|
|
97
|
+
} else {
|
|
98
|
+
this.cache.delete(path);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const estimatedMemoryBytes = validSignals * 100;
|
|
102
|
+
return {
|
|
103
|
+
cachedSignals: validSignals,
|
|
104
|
+
cleanedUpSignals: this.stats.cleanedUpSignals,
|
|
105
|
+
peakCachedSignals: this.stats.peakCachedSignals,
|
|
106
|
+
manualDisposes: this.stats.manualDisposes,
|
|
107
|
+
estimatedMemoryBytes
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
dispose() {
|
|
111
|
+
if (this.config.debugMode) {
|
|
112
|
+
console.log('[SignalMemoryManager] Disposing', {
|
|
113
|
+
cachedSignals: this.cache.size
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (this.registry) {
|
|
117
|
+
for (const entry of this.cache.values()) {
|
|
118
|
+
const signal = entry.ref.deref();
|
|
119
|
+
if (signal) {
|
|
120
|
+
this.registry.unregister(signal);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
this.cache.clear();
|
|
125
|
+
this.stats.manualDisposes++;
|
|
126
|
+
if (this.config.debugMode) {
|
|
127
|
+
console.log('[SignalMemoryManager] Disposed', this.getStats());
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
getCachedPaths() {
|
|
131
|
+
const paths = [];
|
|
132
|
+
for (const [path, entry] of this.cache.entries()) {
|
|
133
|
+
if (entry.ref.deref()) {
|
|
134
|
+
paths.push(path);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return paths;
|
|
138
|
+
}
|
|
139
|
+
clearStale() {
|
|
140
|
+
let removed = 0;
|
|
141
|
+
for (const [path, entry] of this.cache.entries()) {
|
|
142
|
+
if (!entry.ref.deref()) {
|
|
143
|
+
this.cache.delete(path);
|
|
144
|
+
removed++;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (this.config.debugMode && removed > 0) {
|
|
148
|
+
console.log(`[SignalMemoryManager] Cleared ${removed} stale entries`);
|
|
149
|
+
}
|
|
150
|
+
return removed;
|
|
151
|
+
}
|
|
152
|
+
resetStats() {
|
|
153
|
+
this.stats = {
|
|
154
|
+
cleanedUpSignals: 0,
|
|
155
|
+
peakCachedSignals: 0,
|
|
156
|
+
manualDisposes: 0
|
|
157
|
+
};
|
|
158
|
+
if (this.config.debugMode) {
|
|
159
|
+
console.log('[SignalMemoryManager] Stats reset');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export { SignalMemoryManager };
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
var ChangeType;
|
|
2
|
+
(function (ChangeType) {
|
|
3
|
+
ChangeType["ADD"] = "add";
|
|
4
|
+
ChangeType["UPDATE"] = "update";
|
|
5
|
+
ChangeType["DELETE"] = "delete";
|
|
6
|
+
ChangeType["REPLACE"] = "replace";
|
|
7
|
+
})(ChangeType || (ChangeType = {}));
|
|
8
|
+
class DiffEngine {
|
|
9
|
+
defaultOptions = {
|
|
10
|
+
maxDepth: 100,
|
|
11
|
+
detectDeletions: false,
|
|
12
|
+
ignoreArrayOrder: false,
|
|
13
|
+
equalityFn: (a, b) => a === b,
|
|
14
|
+
keyValidator: undefined
|
|
15
|
+
};
|
|
16
|
+
diff(current, updates, options = {}) {
|
|
17
|
+
const opts = {
|
|
18
|
+
...this.defaultOptions,
|
|
19
|
+
...options
|
|
20
|
+
};
|
|
21
|
+
const changes = [];
|
|
22
|
+
const visited = new WeakSet();
|
|
23
|
+
this.traverse(current, updates, [], changes, visited, opts, 0);
|
|
24
|
+
return {
|
|
25
|
+
changes,
|
|
26
|
+
hasChanges: changes.length > 0
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
traverse(curr, upd, path, changes, visited, opts, depth) {
|
|
30
|
+
if (depth > opts.maxDepth) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (typeof upd !== 'object' || upd === null) {
|
|
34
|
+
if (!opts.equalityFn(curr, upd)) {
|
|
35
|
+
changes.push({
|
|
36
|
+
type: curr === undefined ? ChangeType.ADD : ChangeType.UPDATE,
|
|
37
|
+
path: [...path],
|
|
38
|
+
value: upd,
|
|
39
|
+
oldValue: curr
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (visited.has(upd)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
visited.add(upd);
|
|
48
|
+
if (Array.isArray(upd)) {
|
|
49
|
+
this.diffArrays(curr, upd, path, changes, visited, opts, depth);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!curr || typeof curr !== 'object' || Array.isArray(curr)) {
|
|
53
|
+
changes.push({
|
|
54
|
+
type: ChangeType.REPLACE,
|
|
55
|
+
path: [...path],
|
|
56
|
+
value: upd,
|
|
57
|
+
oldValue: curr
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const currObj = curr;
|
|
62
|
+
const updObj = upd;
|
|
63
|
+
for (const key in updObj) {
|
|
64
|
+
if (Object.prototype.hasOwnProperty.call(updObj, key)) {
|
|
65
|
+
if (opts.keyValidator && !opts.keyValidator(key)) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
this.traverse(currObj[key], updObj[key], [...path, key], changes, visited, opts, depth + 1);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (opts.detectDeletions) {
|
|
72
|
+
for (const key in currObj) {
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(currObj, key) && !(key in updObj)) {
|
|
74
|
+
changes.push({
|
|
75
|
+
type: ChangeType.DELETE,
|
|
76
|
+
path: [...path, key],
|
|
77
|
+
oldValue: currObj[key]
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
diffArrays(curr, upd, path, changes, visited, opts, depth) {
|
|
84
|
+
if (!Array.isArray(curr)) {
|
|
85
|
+
changes.push({
|
|
86
|
+
type: ChangeType.REPLACE,
|
|
87
|
+
path: [...path],
|
|
88
|
+
value: upd,
|
|
89
|
+
oldValue: curr
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (opts.ignoreArrayOrder) {
|
|
94
|
+
this.diffArraysUnordered(curr, upd, path, changes, opts);
|
|
95
|
+
} else {
|
|
96
|
+
this.diffArraysOrdered(curr, upd, path, changes, visited, opts, depth);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
diffArraysOrdered(curr, upd, path, changes, visited, opts, depth) {
|
|
100
|
+
const maxLength = Math.max(curr.length, upd.length);
|
|
101
|
+
for (let i = 0; i < maxLength; i++) {
|
|
102
|
+
if (i >= upd.length) {
|
|
103
|
+
if (opts.detectDeletions) {
|
|
104
|
+
changes.push({
|
|
105
|
+
type: ChangeType.DELETE,
|
|
106
|
+
path: [...path, i],
|
|
107
|
+
oldValue: curr[i]
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} else if (i >= curr.length) {
|
|
111
|
+
changes.push({
|
|
112
|
+
type: ChangeType.ADD,
|
|
113
|
+
path: [...path, i],
|
|
114
|
+
value: upd[i]
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
this.traverse(curr[i], upd[i], [...path, i], changes, visited, opts, depth + 1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
diffArraysUnordered(curr, upd, path, changes, opts) {
|
|
122
|
+
const currSet = new Set(curr.map(v => this.stringify(v)));
|
|
123
|
+
const updSet = new Set(upd.map(v => this.stringify(v)));
|
|
124
|
+
upd.forEach((value, index) => {
|
|
125
|
+
const str = this.stringify(value);
|
|
126
|
+
if (!currSet.has(str)) {
|
|
127
|
+
changes.push({
|
|
128
|
+
type: ChangeType.ADD,
|
|
129
|
+
path: [...path, index],
|
|
130
|
+
value
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
if (opts.detectDeletions) {
|
|
135
|
+
curr.forEach((value, index) => {
|
|
136
|
+
const str = this.stringify(value);
|
|
137
|
+
if (!updSet.has(str)) {
|
|
138
|
+
changes.push({
|
|
139
|
+
type: ChangeType.DELETE,
|
|
140
|
+
path: [...path, index],
|
|
141
|
+
oldValue: value
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
stringify(value) {
|
|
148
|
+
try {
|
|
149
|
+
return JSON.stringify(value);
|
|
150
|
+
} catch {
|
|
151
|
+
return String(value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { ChangeType, DiffEngine };
|