Skip to content

Commit 83df561

Browse files
committed
fix(types): correct type management in Deep class
- Fix type index management in set type method - Add Watch functionality for tracking changes - Add tests for Watch functionality The type setter now properly manages internal indices by always unsetting the previous type before setting a new one. This fixes issues with stale references in the _many index. Resolves type tracking issues in Deep class.
1 parent c4dc424 commit 83df561

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/deep.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,34 @@ export class Deep {
937937
return difference;
938938
});
939939

940+
deep.Watch = deep.contains.Watch = deep.new((difference: Deep) => {
941+
const watch = deep.Watch.new(() => {
942+
const newPatch = difference.call();
943+
watch.to = newPatch;
944+
watch.emit(newPatch);
945+
return newPatch;
946+
});
947+
watch.from = difference;
948+
const selection = difference.from;
949+
selection.on((event: Event) => {
950+
console.log('watch on', event);
951+
if (
952+
['new', 'change', 'kill'].includes(event.name) &&
953+
event.deep.type != deep.Selection
954+
) {
955+
watch.call();
956+
}
957+
// if (['add', 'update', 'remove'].includes(event.name)) {
958+
// watch.call();
959+
// }
960+
});
961+
962+
// Make initial call to set initial state
963+
watch.to = difference.to;
964+
965+
return watch;
966+
});
967+
940968
deep._events = true;
941969
}
942970
}
@@ -1079,8 +1107,8 @@ export class Deep {
10791107
*/
10801108
set type(it: Deep | undefined) {
10811109
const previous = this.type;
1082-
if (isUndefined(it)) this.deep.memory.types.unset(this);
1083-
else this.deep.memory.types.set(this, it);
1110+
this.deep.memory.types.unset(this);
1111+
if (!isUndefined(it)) this.deep.memory.types.set(this, it);
10841112
if (!this.deep._events) return;
10851113
if ((this.type != this.deep.Selection && this.type != this.deep.Id) && previous !== it) {
10861114
this.on.emit(this._createChangeEvent('change', 'type', previous, it));

src/tests/sync.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,43 @@ test('selection events', () => {
312312
]);
313313
});
314314

315+
test('Watch functionality', () => {
316+
const deep = new Deep();
317+
318+
const A = deep.new();
319+
320+
const a1 = deep.new();
321+
const a2 = A.new();
322+
const a3 = A.new();
323+
324+
// Create selection to watch
325+
const selection = deep.select({ type: A });
326+
327+
// Create difference for selection
328+
const difference = deep.Difference.call(selection);
329+
330+
// Create watch for difference
331+
const watch = deep.Watch.call(difference);
332+
333+
// Test add event
334+
a1.type = A;
335+
assert.equal(difference.to.call.added.length, 0);
336+
assert.equal(difference.to.call.updated.length, 1);
337+
assert.equal(difference.to.call.removed.length, 0);
338+
339+
// Test update event
340+
a1.value = 'test';
341+
assert.equal(difference.to.call.added.length, 0);
342+
assert.equal(difference.to.call.updated.length, 1);
343+
assert.equal(difference.to.call.removed.length, 0);
344+
345+
// Test remove event
346+
a1.type = a3;
347+
assert.equal(difference.to.call.added.length, 0);
348+
assert.equal(difference.to.call.updated.length, 0);
349+
assert.equal(difference.to.call.removed.length, 1);
350+
});
351+
315352
// test('sync json file', async () => {
316353
// // Create first Deep instance and setup initial data
317354
// const deep1 = new Deep();

0 commit comments

Comments
 (0)