diff --git a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalImpl.kt b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalImpl.kt index 0c08d26c..4cb3bfa3 100644 --- a/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalImpl.kt +++ b/reactor/Core/src/jetbrains/mps/logic/reactor/core/internal/LogicalImpl.kt @@ -131,11 +131,7 @@ internal class LogicalImpl : MutableLogical { otherRepr.setValue(thisVal); } - // then set parent - otherRepr.setParent(thisRepr) - thisRepr.mergeParentObservers(otherRepr) - - // last, reconcile the values/merge observers + // reconcile the values/merge value observers if (thisVal == null && otherVal == null) { // var var thisRepr.mergeValueObservers(otherRepr); @@ -147,10 +143,20 @@ internal class LogicalImpl : MutableLogical { // copy usages thisRepr.usagesCount += otherRepr.usagesCount + + // save other observers + val otherObservers = ArrayList(otherRepr.parentObservers) + mergeParentObservers(thisRepr, otherRepr) + + // finally set parent and notify observers + otherRepr._parent = thisRepr + for (p in otherObservers) { + p.second.parentUpdated(p.first) + } } override fun union(other: MutableLogical) { - union(other, { a, b -> if (a != b) throw IllegalStateException("$a does not equal to $b")}) + union(other, { a, b -> if (a != b) throw IllegalStateException("$a does not equal to $b") }) } override fun addObserver(observer: LogicalObserver) { @@ -183,13 +189,17 @@ internal class LogicalImpl : MutableLogical { private fun rank(): Int = rank - private fun incRank() { rank++ } + private fun incRank() { + rank++ + } - private fun incUsages() { usagesCount++ } + private fun incUsages() { + usagesCount++ + } - private fun setParent(parent: LogicalImpl) { - this._parent = parent - notifyParentUpdated() + private fun mergeParentObservers(thisRepr: LogicalImpl, otherRepr: LogicalImpl) { + thisRepr.parentObservers.addAll(otherRepr.parentObservers) + otherRepr.parentObservers.clear() } private fun mergeValueObservers(mergeFrom: MutableLogical) { @@ -198,12 +208,6 @@ internal class LogicalImpl : MutableLogical { other.valueObservers.clear() } - private fun mergeParentObservers(mergeFrom: MutableLogical) { - val other = mergeFrom as LogicalImpl - this.find().parentObservers.addAll(other.parentObservers) - other.parentObservers.clear() - } - private fun notifyValueUpdated() { val obs = ArrayList(valueObservers) this.valueObservers.clear() @@ -212,16 +216,9 @@ internal class LogicalImpl : MutableLogical { } } - private fun notifyParentUpdated() { - val obs = ArrayList(parentObservers) - for (p in obs) { - p.second.parentUpdated(p.first) - } - } - override fun toString(): String = - if (_parent != null) "${name}(^${_parent.toString()})" - else name + if (_parent != null) "${name}(^${_parent.toString()})" + else name }