Skip to content

Commit 4009f5d

Browse files
committed
fixes to mutation event system: do not deduplicate legitimate events coming through different paths, if no cycles are formed
1 parent a93e535 commit 4009f5d

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

src/data/model/immutable/HashedObject.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,28 +314,29 @@ abstract class HashedObject {
314314
}
315315
}
316316

317-
getMutationEventSource(seen?: Set<HashedObject>): EventRelay<HashedObject> {
317+
getMutationEventSource(): EventRelay<HashedObject> {
318318

319319
if (this._mutationEventSource === undefined) {
320320

321-
this._mutationEventSource = this.createMutationEventSource(seen);
321+
this._mutationEventSource = this.createMutationEventSource();
322322

323323
}
324324

325325
return this._mutationEventSource;
326326

327327
}
328328

329-
protected createMutationEventSource(seen=new Set<HashedObject>()): EventRelay<HashedObject> {
330-
331-
seen.add(this);
329+
protected createMutationEventSource(): EventRelay<HashedObject> {
332330

333331
const subObservers = new Map<string, EventRelay<HashedObject>>();
334332

335333
for (const [fieldName, subobj] of this.getDirectSubObjects().entries()) {
336-
if (!seen.has(subobj)) {
337-
subObservers.set(fieldName, subobj.getMutationEventSource(seen));
338-
}
334+
//if (!seen.has(subobj)) {
335+
// console.log('adding subobject ' + fieldName + ' to ' + this.getLastHash() + '( a ' + this.getClassName() + ')');
336+
subObservers.set(fieldName, subobj.getMutationEventSource());
337+
//} else {
338+
// console.log('NOT adding subobject ' + fieldName + ' to ' + this.getLastHash() + '( a ' + this.getClassName() + ')');
339+
//}
339340
}
340341

341342
return new EventRelay(this, subObservers);

src/data/model/mutable/MutableObject.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,28 +542,28 @@ abstract class MutableObject extends HashedObject {
542542
return before;
543543
}
544544

545-
protected createMutationEventSource(seen=new Set<HashedObject>()): EventRelay<HashedObject> {
545+
protected createMutationEventSource(): EventRelay<HashedObject> {
546546

547-
const ownMutationEventSource = super.createMutationEventSource(seen);
547+
const ownMutationEventSource = super.createMutationEventSource();
548548

549-
this.updateCascadeMutableContentRelays(ownMutationEventSource, seen);
549+
this.updateCascadeMutableContentRelays(ownMutationEventSource);
550550

551551
return ownMutationEventSource;
552552

553553
}
554554

555-
private updateCascadeMutableContentRelays(ownMutationEventSource?: EventRelay<HashedObject>, seen=new Set<HashedObject>()) {
555+
private updateCascadeMutableContentRelays(ownMutationEventSource?: EventRelay<HashedObject>) {
556556

557557
if (ownMutationEventSource !== undefined) {
558558

559559
if (this.isCascadingMutableContentEvents()) {
560560
ownMutationEventSource.addObserver(this._cascadeMutableContentObserver);
561561

562-
this.addEventRelaysForContents(ownMutationEventSource, seen);
562+
this.addEventRelaysForContents(ownMutationEventSource);
563563
} else {
564564
ownMutationEventSource.removeObserver(this._cascadeMutableContentObserver);
565565

566-
this.removeEventRelaysForContents(ownMutationEventSource, seen);
566+
this.removeEventRelaysForContents(ownMutationEventSource);
567567
}
568568
}
569569
}

src/util/events.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,31 @@ class EventRelay<E extends hashable, D=any> {
7979

8080
EventRelay.logger.debug('adding upstream ' + name + ' (' + upstream.emitter.getLastHash() + ') to ' + this.emitter.getLastHash());
8181

82-
//if (!this.wouldCreateACycle(upstream.emitterHash)) {
8382
if (!upstream.wouldCreateACycle(this.emitter.getLastHash())) {
8483

8584
const observer = (upstreamEv: Event<E, D>) => {
8685

87-
const upstreamEmitters = upstreamEv.path === undefined? [] : Array.from(upstreamEv.path)
86+
// if the original emitter and the current emitter are the same,it means that the event has
87+
// somehow propagated back to its origin, we do not need to forward the event any more
88+
if (upstreamEv.emitter.getLastHash() !== this.emitter.getLastHash()) {
8889

89-
upstreamEmitters.push({name: name, emitter: this.emitter});
90+
const upstreamEmitters = upstreamEv.path === undefined? [] : Array.from(upstreamEv.path)
9091

91-
const ev: Event<E, D> = {
92-
emitter: upstreamEv.emitter,
93-
path: upstreamEmitters,
94-
action: upstreamEv.action,
95-
data: upstreamEv.data
96-
};
97-
98-
EventRelay.logger.debug('upstream from ' + this.emitter.getLastHash() + ' name: ' + name);
99-
100-
this.emit(ev);
92+
upstreamEmitters.push({name: name, emitter: this.emitter});
93+
94+
const ev: Event<E, D> = {
95+
emitter: upstreamEv.emitter,
96+
path: upstreamEmitters,
97+
action: upstreamEv.action,
98+
data: upstreamEv.data
99+
};
100+
101+
EventRelay.logger.debug('upstream from ' + this.emitter.getLastHash() + ' name: ' + name);
102+
103+
this.emit(ev);
104+
}
101105

102106
return false;
103-
104107
};
105108

106109
this.upstreamRelays.set(name, [upstream, observer]);

0 commit comments

Comments
 (0)