@signaltree/enterprise 4.0.6 → 4.0.9
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/LICENSE +54 -0
- package/README.md +203 -0
- package/package.json +1 -1
- package/src/lib/diff-engine.spec.ts +384 -0
- package/src/lib/diff-engine.ts +351 -0
- package/src/lib/enterprise-enhancer.ts +136 -0
- package/src/lib/enterprise.spec.ts +7 -0
- package/src/lib/enterprise.ts +3 -0
- package/src/lib/path-index.spec.ts +290 -0
- package/src/lib/path-index.ts +320 -0
- package/src/lib/scheduler.ts +16 -0
- package/src/lib/thread-pools.ts +11 -0
- package/src/lib/update-engine.spec.ts +93 -0
- package/src/lib/update-engine.ts +399 -0
- package/src/test-setup.ts +6 -0
- package/src/types/signaltree-core.d.ts +4 -0
- package/src/index.js +0 -10
- package/src/index.js.map +0 -1
- package/src/lib/diff-engine.d.ts +0 -108
- package/src/lib/diff-engine.js +0 -236
- package/src/lib/diff-engine.js.map +0 -1
- package/src/lib/enterprise-enhancer.d.ts +0 -81
- package/src/lib/enterprise-enhancer.js +0 -78
- package/src/lib/enterprise-enhancer.js.map +0 -1
- package/src/lib/enterprise.d.ts +0 -1
- package/src/lib/enterprise.js +0 -7
- package/src/lib/enterprise.js.map +0 -1
- package/src/lib/path-index.d.ts +0 -119
- package/src/lib/path-index.js +0 -265
- package/src/lib/path-index.js.map +0 -1
- package/src/lib/scheduler.d.ts +0 -2
- package/src/lib/scheduler.js +0 -25
- package/src/lib/scheduler.js.map +0 -1
- package/src/lib/thread-pools.d.ts +0 -4
- package/src/lib/thread-pools.js +0 -14
- package/src/lib/thread-pools.js.map +0 -1
- package/src/lib/update-engine.d.ts +0 -115
- package/src/lib/update-engine.js +0 -287
- package/src/lib/update-engine.js.map +0 -1
- package/src/test-setup.d.ts +0 -1
- package/src/test-setup.js +0 -8
- package/src/test-setup.js.map +0 -1
- /package/src/{index.d.ts → index.ts} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
BUSINESS SOURCE LICENSE 1.1
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jonathan D Borgia
|
|
4
|
+
|
|
5
|
+
This Business Source License 1.1 ("License") governs the use of the software and associated documentation files (the "Software"). You are granted a limited license to use the Software under the terms of this License.
|
|
6
|
+
|
|
7
|
+
1. Definitions
|
|
8
|
+
|
|
9
|
+
"Change Date" means the date on which the Change License set out in section 6 will apply to the Software. The Change Date for this release is 2028-09-05.
|
|
10
|
+
|
|
11
|
+
"Change License" means the open source license that will apply to the Software on and after the Change Date. The Change License for this release is the MIT License.
|
|
12
|
+
|
|
13
|
+
"Licensor" means the copyright owner granting rights under this License (Jonathan D Borgia).
|
|
14
|
+
|
|
15
|
+
"You" ("Licensee") means an individual or legal entity exercising rights under this License who has not violated the terms of this License or had their rights terminated.
|
|
16
|
+
|
|
17
|
+
2. License Grant
|
|
18
|
+
|
|
19
|
+
Subject to the terms and conditions of this License, Licensor hereby grants You a non-exclusive, non-transferable, worldwide license to use, reproduce, display, perform, and distribute the Software, and to make modifications and derivative works for internal use, until the Change Date.
|
|
20
|
+
|
|
21
|
+
3. Commercial Use
|
|
22
|
+
|
|
23
|
+
You may use the Software in commercial applications, including for providing services, selling products that include the Software, or otherwise exploiting the Software commercially, subject to the other terms of this License.
|
|
24
|
+
|
|
25
|
+
4. Limitations and Conditions
|
|
26
|
+
|
|
27
|
+
a. You may not remove or alter this License, the copyright notice, or notices of the Change Date.
|
|
28
|
+
|
|
29
|
+
b. You may not publicly offer a modified version of the Software that would directly compete with Licensor's public offering of the Software if doing so would circumvent the intent of this License.
|
|
30
|
+
|
|
31
|
+
c. Except as expressly provided in this License, no rights are granted to You under any patent or trademark of Licensor.
|
|
32
|
+
|
|
33
|
+
5. Disclaimer and Limitation of Liability
|
|
34
|
+
|
|
35
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. TO THE FULLEST EXTENT PERMITTED BY LAW, LICENSOR WILL NOT BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY ARISING FROM OR RELATING TO THE SOFTWARE.
|
|
36
|
+
|
|
37
|
+
6. Change License
|
|
38
|
+
|
|
39
|
+
On and after the Change Date specified above, the Software will be licensed under the Change License (MIT License) on the same terms and conditions as set forth by that Change License.
|
|
40
|
+
|
|
41
|
+
7. Governing Law
|
|
42
|
+
|
|
43
|
+
This License will be governed by and construed in accordance with the laws of the State of New York, USA, without regard to conflict of law principles.
|
|
44
|
+
|
|
45
|
+
8. Accepting this License
|
|
46
|
+
|
|
47
|
+
You accept this License by copying, modifying, or distributing the Software or any portion thereof.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
LICENSE NOTE
|
|
52
|
+
|
|
53
|
+
- Original license file replaced on 2025-09-05 to Business Source License 1.1. Change Date: 2028-09-05. Change License: MIT.
|
|
54
|
+
or standard modifications for your own applications.
|
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# @signaltree/enterprise
|
|
2
|
+
|
|
3
|
+
Enterprise-grade optimizations for SignalTree. Designed for large-scale applications with 500+ signals and high-frequency bulk updates.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Diff-based updates** - Only update signals that actually changed
|
|
8
|
+
- **Bulk operation optimization** - 2-5x faster for large state updates
|
|
9
|
+
- **Advanced change tracking** - Detailed statistics and monitoring
|
|
10
|
+
- **Path indexing** - Optimized signal lookup for large trees
|
|
11
|
+
- **Lazy initialization** - Zero overhead until first use
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @signaltree/core @signaltree/enterprise
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { signalTree } from '@signaltree/core';
|
|
23
|
+
import { withEnterprise } from '@signaltree/enterprise';
|
|
24
|
+
|
|
25
|
+
const tree = signalTree(largeState).with(withEnterprise());
|
|
26
|
+
|
|
27
|
+
// Use optimized bulk updates
|
|
28
|
+
const result = tree.updateOptimized(newData, {
|
|
29
|
+
ignoreArrayOrder: true,
|
|
30
|
+
maxDepth: 10,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(result.stats);
|
|
34
|
+
// { totalChanges: 45, adds: 10, updates: 30, deletes: 5 }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## When to Use
|
|
38
|
+
|
|
39
|
+
### ✅ Use @signaltree/enterprise when:
|
|
40
|
+
|
|
41
|
+
- You have 500+ signals in your state tree
|
|
42
|
+
- Bulk updates happen at high frequency (60Hz+)
|
|
43
|
+
- You need real-time dashboards or data feeds
|
|
44
|
+
- You're building enterprise-scale applications
|
|
45
|
+
- You need detailed update monitoring and statistics
|
|
46
|
+
|
|
47
|
+
### ❌ Skip @signaltree/enterprise when:
|
|
48
|
+
|
|
49
|
+
- Small to medium apps (<100 signals)
|
|
50
|
+
- Infrequent state updates
|
|
51
|
+
- Startup/prototype projects
|
|
52
|
+
- Bundle size is critical (adds +2.4KB gzipped)
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `withEnterprise()`
|
|
57
|
+
|
|
58
|
+
Enhancer that adds enterprise optimizations to a SignalTree.
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { signalTree } from '@signaltree/core';
|
|
62
|
+
import { withEnterprise } from '@signaltree/enterprise';
|
|
63
|
+
|
|
64
|
+
const tree = signalTree(initialState).with(withEnterprise());
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `tree.updateOptimized(updates, options?)`
|
|
68
|
+
|
|
69
|
+
Performs optimized bulk updates using diff-based change detection.
|
|
70
|
+
|
|
71
|
+
**Parameters:**
|
|
72
|
+
|
|
73
|
+
- `updates: Partial<T>` - The new state values
|
|
74
|
+
- `options?: UpdateOptions` - Configuration options
|
|
75
|
+
|
|
76
|
+
**Options:**
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
{
|
|
80
|
+
maxDepth?: number; // Maximum depth to traverse (default: 100)
|
|
81
|
+
ignoreArrayOrder?: boolean; // Ignore array element order (default: false)
|
|
82
|
+
equalityFn?: (a, b) => boolean; // Custom equality function
|
|
83
|
+
autoBatch?: boolean; // Automatically batch updates (default: true)
|
|
84
|
+
batchSize?: number; // Patches per batch (default: 10)
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Returns:**
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
{
|
|
92
|
+
changed: boolean; // Whether any changes were made
|
|
93
|
+
stats: {
|
|
94
|
+
totalChanges: number; // Total number of changes
|
|
95
|
+
adds: number; // New properties added
|
|
96
|
+
updates: number; // Properties updated
|
|
97
|
+
deletes: number; // Properties deleted
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `tree.getPathIndex()`
|
|
103
|
+
|
|
104
|
+
Get the PathIndex for debugging/monitoring. Returns `null` if `updateOptimized` hasn't been called yet (lazy initialization).
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const index = tree.getPathIndex();
|
|
108
|
+
if (index) {
|
|
109
|
+
console.log('Path index active');
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Examples
|
|
114
|
+
|
|
115
|
+
### Real-time Dashboard
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { signalTree } from '@signaltree/core';
|
|
119
|
+
import { withEnterprise } from '@signaltree/enterprise';
|
|
120
|
+
|
|
121
|
+
interface DashboardState {
|
|
122
|
+
metrics: Record<string, number>;
|
|
123
|
+
alerts: Alert[];
|
|
124
|
+
users: User[];
|
|
125
|
+
// ... hundreds more properties
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const dashboard = signalTree<DashboardState>(initialState).with(withEnterprise());
|
|
129
|
+
|
|
130
|
+
// High-frequency updates from WebSocket
|
|
131
|
+
socket.on('metrics', (newMetrics) => {
|
|
132
|
+
const result = dashboard.updateOptimized({ metrics: newMetrics }, { ignoreArrayOrder: true });
|
|
133
|
+
|
|
134
|
+
console.log(`Updated ${result.stats.updates} metrics`);
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Data Grid with Bulk Operations
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { signalTree } from '@signaltree/core';
|
|
142
|
+
import { withEnterprise } from '@signaltree/enterprise';
|
|
143
|
+
|
|
144
|
+
const grid = signalTree({
|
|
145
|
+
rows: [] as GridRow[],
|
|
146
|
+
columns: [] as GridColumn[],
|
|
147
|
+
filters: {} as FilterState,
|
|
148
|
+
selection: new Set<string>(),
|
|
149
|
+
}).with(withEnterprise());
|
|
150
|
+
|
|
151
|
+
// Bulk update from API
|
|
152
|
+
async function loadData() {
|
|
153
|
+
const data = await fetchGridData();
|
|
154
|
+
|
|
155
|
+
const result = grid.updateOptimized(data, {
|
|
156
|
+
maxDepth: 5,
|
|
157
|
+
autoBatch: true,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
console.log(`Loaded ${result.stats.adds} rows in bulk`);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Custom Equality for Complex Objects
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
const tree = signalTree(complexState).with(withEnterprise());
|
|
168
|
+
|
|
169
|
+
tree.updateOptimized(newState, {
|
|
170
|
+
equalityFn: (a, b) => {
|
|
171
|
+
// Custom deep equality for specific object types
|
|
172
|
+
if (a instanceof Date && b instanceof Date) {
|
|
173
|
+
return a.getTime() === b.getTime();
|
|
174
|
+
}
|
|
175
|
+
return a === b;
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Performance
|
|
181
|
+
|
|
182
|
+
**Bundle Size:**
|
|
183
|
+
|
|
184
|
+
- Adds +2.4KB gzipped to your bundle
|
|
185
|
+
- Zero overhead until first `updateOptimized()` call (lazy initialization)
|
|
186
|
+
|
|
187
|
+
**Performance Gains:**
|
|
188
|
+
|
|
189
|
+
- 2-5x faster for bulk updates on large state trees
|
|
190
|
+
- Scales efficiently with tree depth and complexity
|
|
191
|
+
- Minimal memory overhead with path indexing
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
Business Source License 1.1 (BSL-1.1) - See [LICENSE](../../LICENSE) for details.
|
|
196
|
+
|
|
197
|
+
Converts to MIT license on the Change Date specified in the license.
|
|
198
|
+
|
|
199
|
+
## Related Packages
|
|
200
|
+
|
|
201
|
+
- [@signaltree/core](../core) - Core SignalTree functionality
|
|
202
|
+
- [@signaltree/ng-forms](../ng-forms) - Angular forms integration
|
|
203
|
+
- [@signaltree/callable-syntax](../callable-syntax) - Callable syntax transform
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@signaltree/enterprise",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.9",
|
|
4
4
|
"description": "Enterprise-grade optimizations for SignalTree. Provides diff-based updates, bulk operation optimization, and advanced change tracking for large-scale applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { ChangeType, DiffEngine } from './diff-engine';
|
|
2
|
+
|
|
3
|
+
describe('DiffEngine', () => {
|
|
4
|
+
let engine: DiffEngine;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
engine = new DiffEngine();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
describe('primitive values', () => {
|
|
11
|
+
it('should detect primitive changes', () => {
|
|
12
|
+
const current = { value: 42 };
|
|
13
|
+
const updates = { value: 43 };
|
|
14
|
+
|
|
15
|
+
const diff = engine.diff(current, updates);
|
|
16
|
+
|
|
17
|
+
expect(diff.hasChanges).toBe(true);
|
|
18
|
+
expect(diff.changes).toHaveLength(1);
|
|
19
|
+
expect(diff.changes[0]).toEqual({
|
|
20
|
+
type: ChangeType.UPDATE,
|
|
21
|
+
path: ['value'],
|
|
22
|
+
value: 43,
|
|
23
|
+
oldValue: 42,
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should detect no changes when values are equal', () => {
|
|
28
|
+
const current = { value: 42 };
|
|
29
|
+
const updates = { value: 42 };
|
|
30
|
+
|
|
31
|
+
const diff = engine.diff(current, updates);
|
|
32
|
+
|
|
33
|
+
expect(diff.hasChanges).toBe(false);
|
|
34
|
+
expect(diff.changes).toHaveLength(0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should detect additions', () => {
|
|
38
|
+
const current = {};
|
|
39
|
+
const updates = { newField: 'hello' };
|
|
40
|
+
|
|
41
|
+
const diff = engine.diff(current, updates);
|
|
42
|
+
|
|
43
|
+
expect(diff.hasChanges).toBe(true);
|
|
44
|
+
expect(diff.changes[0].type).toBe(ChangeType.ADD);
|
|
45
|
+
expect(diff.changes[0].value).toBe('hello');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should detect null values correctly', () => {
|
|
49
|
+
const current = { value: null };
|
|
50
|
+
const updates = { value: 'not null' };
|
|
51
|
+
|
|
52
|
+
const diff = engine.diff(current, updates);
|
|
53
|
+
|
|
54
|
+
expect(diff.hasChanges).toBe(true);
|
|
55
|
+
expect(diff.changes[0].oldValue).toBeNull();
|
|
56
|
+
expect(diff.changes[0].value).toBe('not null');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('nested objects', () => {
|
|
61
|
+
it('should detect deep changes', () => {
|
|
62
|
+
const current = {
|
|
63
|
+
user: {
|
|
64
|
+
profile: {
|
|
65
|
+
name: 'Alice',
|
|
66
|
+
age: 30,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const updates = {
|
|
72
|
+
user: {
|
|
73
|
+
profile: {
|
|
74
|
+
name: 'Alice',
|
|
75
|
+
age: 31, // Changed
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const diff = engine.diff(current, updates);
|
|
81
|
+
|
|
82
|
+
expect(diff.hasChanges).toBe(true);
|
|
83
|
+
expect(diff.changes).toHaveLength(1);
|
|
84
|
+
expect(diff.changes[0]).toEqual({
|
|
85
|
+
type: ChangeType.UPDATE,
|
|
86
|
+
path: ['user', 'profile', 'age'],
|
|
87
|
+
value: 31,
|
|
88
|
+
oldValue: 30,
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should detect multiple changes', () => {
|
|
93
|
+
const current = {
|
|
94
|
+
name: 'Alice',
|
|
95
|
+
age: 30,
|
|
96
|
+
city: 'NYC',
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const updates = {
|
|
100
|
+
name: 'Alice',
|
|
101
|
+
age: 31, // Changed
|
|
102
|
+
city: 'SF', // Changed
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const diff = engine.diff(current, updates);
|
|
106
|
+
|
|
107
|
+
expect(diff.hasChanges).toBe(true);
|
|
108
|
+
expect(diff.changes).toHaveLength(2);
|
|
109
|
+
|
|
110
|
+
const paths = diff.changes.map((c) => c.path.join('.'));
|
|
111
|
+
expect(paths).toContain('age');
|
|
112
|
+
expect(paths).toContain('city');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should handle partial updates', () => {
|
|
116
|
+
const current = {
|
|
117
|
+
user: {
|
|
118
|
+
name: 'Alice',
|
|
119
|
+
age: 30,
|
|
120
|
+
email: 'alice@example.com',
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const updates = {
|
|
125
|
+
user: {
|
|
126
|
+
age: 31, // Only updating age
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const diff = engine.diff(current, updates);
|
|
131
|
+
|
|
132
|
+
expect(diff.hasChanges).toBe(true);
|
|
133
|
+
expect(diff.changes).toHaveLength(1);
|
|
134
|
+
expect(diff.changes[0].path).toEqual(['user', 'age']);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('arrays', () => {
|
|
139
|
+
it('should detect array element changes (ordered)', () => {
|
|
140
|
+
const current = { items: [1, 2, 3] };
|
|
141
|
+
const updates = { items: [1, 4, 3] }; // Changed index 1
|
|
142
|
+
|
|
143
|
+
const diff = engine.diff(current, updates);
|
|
144
|
+
|
|
145
|
+
expect(diff.hasChanges).toBe(true);
|
|
146
|
+
expect(diff.changes).toHaveLength(1);
|
|
147
|
+
expect(diff.changes[0]).toEqual({
|
|
148
|
+
type: ChangeType.UPDATE,
|
|
149
|
+
path: ['items', 1],
|
|
150
|
+
value: 4,
|
|
151
|
+
oldValue: 2,
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should detect array additions', () => {
|
|
156
|
+
const current = { items: [1, 2] };
|
|
157
|
+
const updates = { items: [1, 2, 3] }; // Added element
|
|
158
|
+
|
|
159
|
+
const diff = engine.diff(current, updates);
|
|
160
|
+
|
|
161
|
+
expect(diff.hasChanges).toBe(true);
|
|
162
|
+
const addition = diff.changes.find((c) => c.type === ChangeType.ADD);
|
|
163
|
+
expect(addition).toBeDefined();
|
|
164
|
+
expect(addition?.path).toEqual(['items', 2]);
|
|
165
|
+
expect(addition?.value).toBe(3);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('should handle empty arrays', () => {
|
|
169
|
+
const current = { items: [] };
|
|
170
|
+
const updates = { items: [1] };
|
|
171
|
+
|
|
172
|
+
const diff = engine.diff(current, updates);
|
|
173
|
+
|
|
174
|
+
expect(diff.hasChanges).toBe(true);
|
|
175
|
+
expect(diff.changes[0].type).toBe(ChangeType.ADD);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should handle nested arrays', () => {
|
|
179
|
+
const current = {
|
|
180
|
+
matrix: [
|
|
181
|
+
[1, 2],
|
|
182
|
+
[3, 4],
|
|
183
|
+
],
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const updates = {
|
|
187
|
+
matrix: [
|
|
188
|
+
[1, 2],
|
|
189
|
+
[3, 5], // Changed [1][1]
|
|
190
|
+
],
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const diff = engine.diff(current, updates);
|
|
194
|
+
|
|
195
|
+
expect(diff.hasChanges).toBe(true);
|
|
196
|
+
expect(diff.changes[0].path).toEqual(['matrix', 1, 1]);
|
|
197
|
+
expect(diff.changes[0].value).toBe(5);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('type changes', () => {
|
|
202
|
+
it('should detect object to primitive as update', () => {
|
|
203
|
+
const current = { value: { nested: 'object' } };
|
|
204
|
+
const updates = { value: 'string' }; // Type changed
|
|
205
|
+
|
|
206
|
+
const diff = engine.diff(current, updates);
|
|
207
|
+
|
|
208
|
+
expect(diff.hasChanges).toBe(true);
|
|
209
|
+
// Nested object to primitive is UPDATE, not REPLACE
|
|
210
|
+
expect(diff.changes[0].type).toBe(ChangeType.UPDATE);
|
|
211
|
+
expect(diff.changes[0].path).toEqual(['value']);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('should replace object with array', () => {
|
|
215
|
+
const current = { data: { a: 1 } };
|
|
216
|
+
const updates = { data: [1, 2, 3] };
|
|
217
|
+
|
|
218
|
+
const diff = engine.diff(current, updates);
|
|
219
|
+
|
|
220
|
+
expect(diff.changes[0].type).toBe(ChangeType.REPLACE);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should replace array with object', () => {
|
|
224
|
+
const current = { data: [1, 2, 3] };
|
|
225
|
+
const updates = { data: { a: 1 } };
|
|
226
|
+
|
|
227
|
+
const diff = engine.diff(current, updates);
|
|
228
|
+
|
|
229
|
+
expect(diff.changes[0].type).toBe(ChangeType.REPLACE);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('options', () => {
|
|
234
|
+
it('should respect maxDepth option', () => {
|
|
235
|
+
const current = {
|
|
236
|
+
level1: {
|
|
237
|
+
level2: {
|
|
238
|
+
level3: {
|
|
239
|
+
level4: {
|
|
240
|
+
deep: 'value',
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const updates = {
|
|
248
|
+
level1: {
|
|
249
|
+
level2: {
|
|
250
|
+
level3: {
|
|
251
|
+
level4: {
|
|
252
|
+
deep: 'changed',
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const diff = engine.diff(current, updates, { maxDepth: 2 });
|
|
260
|
+
|
|
261
|
+
// Should stop at depth 2, so level3+ changes won't be detected
|
|
262
|
+
expect(diff.changes).toHaveLength(0);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('should detect deletions when enabled', () => {
|
|
266
|
+
const current = { a: 1, b: 2, c: 3 };
|
|
267
|
+
const updates = { a: 1, b: 2 }; // 'c' deleted
|
|
268
|
+
|
|
269
|
+
const diff = engine.diff(current, updates, { detectDeletions: true });
|
|
270
|
+
|
|
271
|
+
const deletion = diff.changes.find((c) => c.type === ChangeType.DELETE);
|
|
272
|
+
expect(deletion).toBeDefined();
|
|
273
|
+
expect(deletion?.path).toEqual(['c']);
|
|
274
|
+
expect(deletion?.oldValue).toBe(3);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should not detect deletions by default', () => {
|
|
278
|
+
const current = { a: 1, b: 2 };
|
|
279
|
+
const updates = { a: 1 }; // 'b' deleted
|
|
280
|
+
|
|
281
|
+
const diff = engine.diff(current, updates);
|
|
282
|
+
|
|
283
|
+
const deletion = diff.changes.find((c) => c.type === ChangeType.DELETE);
|
|
284
|
+
expect(deletion).toBeUndefined();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('should use custom equality function', () => {
|
|
288
|
+
const current = { value: '42' };
|
|
289
|
+
const updates = { value: 42 }; // Different type, same value
|
|
290
|
+
|
|
291
|
+
// Default behavior - detects change
|
|
292
|
+
const diff1 = engine.diff(current, updates);
|
|
293
|
+
expect(diff1.hasChanges).toBe(true);
|
|
294
|
+
|
|
295
|
+
// Custom equality - coerce types
|
|
296
|
+
const diff2 = engine.diff(current, updates, {
|
|
297
|
+
equalityFn: (a, b) => String(a) === String(b),
|
|
298
|
+
});
|
|
299
|
+
expect(diff2.hasChanges).toBe(false);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('should handle ignoreArrayOrder option', () => {
|
|
303
|
+
const current = { items: [1, 2, 3] };
|
|
304
|
+
const updates = { items: [3, 2, 1] }; // Same values, different order
|
|
305
|
+
|
|
306
|
+
// With ignoreArrayOrder, should detect no changes
|
|
307
|
+
const diff = engine.diff(current, updates, { ignoreArrayOrder: true });
|
|
308
|
+
|
|
309
|
+
expect(diff.hasChanges).toBe(false);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
describe('edge cases', () => {
|
|
314
|
+
it('should handle circular references', () => {
|
|
315
|
+
interface Circular {
|
|
316
|
+
a: number;
|
|
317
|
+
self?: Circular;
|
|
318
|
+
}
|
|
319
|
+
const circular: Circular = { a: 1 };
|
|
320
|
+
circular.self = circular;
|
|
321
|
+
|
|
322
|
+
const updates = { a: 2, self: circular };
|
|
323
|
+
|
|
324
|
+
// Should not throw or hang
|
|
325
|
+
expect(() => {
|
|
326
|
+
engine.diff(circular, updates);
|
|
327
|
+
}).not.toThrow();
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('should handle undefined values', () => {
|
|
331
|
+
const current = { value: undefined };
|
|
332
|
+
const updates = { value: 'defined' };
|
|
333
|
+
|
|
334
|
+
const diff = engine.diff(current, updates);
|
|
335
|
+
|
|
336
|
+
expect(diff.hasChanges).toBe(true);
|
|
337
|
+
expect(diff.changes[0].oldValue).toBeUndefined();
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe('performance', () => {
|
|
342
|
+
it('should handle large objects efficiently', () => {
|
|
343
|
+
const current = {
|
|
344
|
+
items: Array.from({ length: 1000 }, (_, i) => ({
|
|
345
|
+
id: i,
|
|
346
|
+
value: i,
|
|
347
|
+
})),
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const updates = {
|
|
351
|
+
items: Array.from({ length: 1000 }, (_, i) => ({
|
|
352
|
+
id: i,
|
|
353
|
+
value: i === 500 ? 999 : i, // Only change one item
|
|
354
|
+
})),
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const start = performance.now();
|
|
358
|
+
const diff = engine.diff(current, updates);
|
|
359
|
+
const duration = performance.now() - start;
|
|
360
|
+
|
|
361
|
+
// Should detect only the one change
|
|
362
|
+
expect(diff.changes).toHaveLength(1);
|
|
363
|
+
expect(diff.changes[0].path).toEqual(['items', 500, 'value']);
|
|
364
|
+
|
|
365
|
+
// Should be reasonably fast
|
|
366
|
+
expect(duration).toBeLessThan(100); // < 100ms for 1000 items
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('should skip dangerous keys during diff', () => {
|
|
370
|
+
const current = {};
|
|
371
|
+
const updates = {
|
|
372
|
+
__proto__: { polluted: true },
|
|
373
|
+
safe: 'value',
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const diff = engine.diff(current, updates, {
|
|
377
|
+
keyValidator: (key) => !['__proto__', 'constructor'].includes(key),
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
expect(diff.changes.map((c) => c.path[0])).not.toContain('__proto__');
|
|
381
|
+
expect(diff.changes.map((c) => c.path[0])).toContain('safe');
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
});
|