Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertyID removed #492

Merged
merged 5 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@ private[bindings] trait SeqPropertyModifierUtils[T, E <: ReadableProperty[T]] ex
private var firstElement: Node = _
private var firstElementIsPlaceholder = false
private val producedElementsCount = MArrayBuffer[Int]()
protected val nestedBindingsByProperty: js.Dictionary[js.Array[Binding]] = js.Dictionary.empty
private val nestedBindingsByProperty: MHashMap[E, js.Array[Binding]] = MHashMap.empty

def propertyAwareNestedInterceptor(p: E)(binding: Binding): Binding = {
super.nestedInterceptor(binding)
binding.setup { b =>
val id: String = p.id.toString
if (!nestedBindingsByProperty.contains(id)) {
nestedBindingsByProperty(id) = js.Array()
}
nestedBindingsByProperty(id).push(b)
nestedBindingsByProperty.getOrElseUpdate(p, js.Array()).push(b)
}
}

def clearPropertyAwareNestedInterceptor(p: E): Unit = {
val id = p.id.toString
if (nestedBindingsByProperty.contains(id)) {
nestedBindingsByProperty(id).foreach(_.kill())
nestedBindingsByProperty(id).length = 0
nestedBindingsByProperty.remove(id)
nestedBindingsByProperty.remove(p).foreach { bindings =>
bindings.foreach(_.kill())
bindings.length = 0
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import io.udash.properties.single.{Property, ReadableProperty}
import io.udash.utils.Registration

private[properties] class ImmutableProperty[A](value: A) extends ReadableProperty[A] {
/** Unique property ID. */
override val id: PropertyId = PropertyCreator.newID()

/** @return Current property value. */
@inline override def get: A = value
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/scala/io/udash/properties/PropertyCreator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ object PropertyCreator extends PropertyCreatorImplicits {

def apply[T](implicit ev: PropertyCreator[T]): PropertyCreator[T] = ev

def newID(): PropertyId = PropertyIdGenerator.next()

implicit final val Double: PropertyCreator[Double] = new SinglePropertyCreator
implicit final val Float: PropertyCreator[Float] = new SinglePropertyCreator
implicit final val Long: PropertyCreator[Long] = new SinglePropertyCreator
Expand All @@ -48,15 +46,15 @@ object PropertyCreator extends PropertyCreatorImplicits {

final class SinglePropertyCreator[T] extends PropertyCreator[T] {
protected def create(prt: ReadableProperty[_]): CastableProperty[T] =
new DirectPropertyImpl[T](prt, PropertyCreator.newID())
new DirectPropertyImpl[T](prt)

override def newImmutableProperty(value: T): ImmutableProperty[T] = new ImmutableProperty[T](value)
}

final class SeqPropertyCreator[A: PropertyCreator, SeqTpe[T] <: BSeq[T]](implicit fac: Factory[A, SeqTpe[A]])
extends PropertyCreator[SeqTpe[A]] {
protected def create(parent: ReadableProperty[_]): CastableProperty[SeqTpe[A]] =
new DirectSeqPropertyImpl[A, SeqTpe](parent, PropertyCreator.newID()).asInstanceOf[CastableProperty[SeqTpe[A]]]
new DirectSeqPropertyImpl[A, SeqTpe](parent).asInstanceOf[CastableProperty[SeqTpe[A]]]

override def newImmutableProperty(value: SeqTpe[A]): ImmutableProperty[SeqTpe[A]] =
new ImmutableSeqProperty[A, SeqTpe](value).asInstanceOf[ImmutableProperty[SeqTpe[A]]]
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/scala/io/udash/properties/PropertyId.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package model
import io.udash.properties.single.{CastableProperty, Property, ReadableProperty}


abstract class ModelPropertyImpl[A](val parent: ReadableProperty[_], override val id: PropertyId)
abstract class ModelPropertyImpl[A](val parent: ReadableProperty[_])
extends ModelProperty[A] with CastableProperty[A] with ModelPropertyMacroApi[A] {

/** False if subproperties were not created yet. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.udash.properties.seq

import com.avsystem.commons._
import io.udash.properties.PropertyCreator
import io.udash.properties.single.{CastableProperty, ReadableProperty}
import io.udash.properties.{PropertyCreator, PropertyId}
import io.udash.utils.CrossCollections

import scala.collection.compat._

private[properties] class DirectSeqPropertyImpl[A: PropertyCreator, SeqTpe[T] <: BSeq[T]](
val parent: ReadableProperty[_], override val id: PropertyId)(implicit fac: Factory[A, SeqTpe[A]])
val parent: ReadableProperty[_])(implicit fac: Factory[A, SeqTpe[A]])
extends AbstractSeqProperty[A, CastableProperty[A]] with CastableProperty[BSeq[A]] {

private val properties = CrossCollections.createArray[CastableProperty[A]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private[properties] class FilteredSeqProperty[A, ElemType <: ReadableProperty[A]

val patch = (oldIdx, matches) match {
case (old, false) if old != -1 =>
CrossCollections.replace(lastValue, old, 1)
lastValue.remove(old, 1)
Patch[ElemType](old, Seq(p), Seq.empty, filteredProps.isEmpty)
case (-1, true) =>
val originProps = origin.elemProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private[properties] trait ForwarderWithLocalCopy[A, B, ElemType <: ReadablePrope

override protected def onListenerInit(): Unit = {
val fromOrigin = CrossCollections.toCrossArray(elementsFromOrigin())
if (!(transformedElements.iterator.map(_.id) sameElements fromOrigin.iterator.map(_.id))) {
if (!(transformedElements == fromOrigin)) {
fireElementsListeners[ElemType](Patch[ElemType](0, transformedElements.toSeq, fromOrigin.toSeq, fromOrigin.isEmpty), structureListeners)
valueChanged()
} else if (transformedElements.map(_.get) != fromOrigin.map(_.get)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.udash.properties.seq

import com.avsystem.commons._
import io.udash.properties.ImmutableProperty
import io.udash.properties.Properties.ReadableProperty
import io.udash.properties.{ImmutableProperty, PropertyCreator, PropertyId}
import io.udash.utils.Registration

private[properties] class PropertySeqCombinedReadableSeqProperty[A](value: ISeq[ReadableProperty[A]])
extends AbstractReadableSeqProperty[A, ReadableProperty[A]] {

override val id: PropertyId = PropertyCreator.newID()
override protected[properties] val parent: ReadableProperty[_] = null

private val children = value.map(_.readable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private[properties] trait AbstractReadableSeqProperty[A, +ElemType <: ReadablePr
): Unit = {
val originalListeners = structureListeners.toSet
CallbackSequencer().queue(
s"${this.id.toString}:fireElementsListeners:${patch.hashCode()}",
s"$hashCode:fireElementsListeners:${patch.hashCode()}",
() => structureListeners.foreach { listener => if (originalListeners.contains(listener)) listener(patch) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ private[properties] abstract class BaseReadableSeqPropertyFromSingleValue[A, B:
origin: ReadableProperty[A], transformer: A => BSeq[B], listenChildren: Boolean
) extends AbstractReadableSeqProperty[B, ElemType] {

override final val id: PropertyId = PropertyCreator.newID()
override final protected[properties] def parent: ReadableProperty[_] = null

private final val children = CrossCollections.createArray[Property[B]]
private final val childrenRegistrations = MHashMap.empty[PropertyId, Registration]
private final val childrenRegistrations = MHashMap.empty[Property[B], Registration]
private final var originListenerRegistration: Registration = _
private final var lastOriginValue: Opt[A] = Opt.empty

Expand All @@ -40,7 +39,7 @@ private[properties] abstract class BaseReadableSeqPropertyFromSingleValue[A, B:

private def structureChanged(patch: Patch[ElemType]): Unit =
CallbackSequencer().queue(
s"${this.id.toString}:fireElementsListeners:${patch.hashCode()}",
s"$hashCode:fireElementsListeners:${patch.hashCode()}",
() => structureListeners.foreach(_.apply(patch))
)

Expand All @@ -62,18 +61,18 @@ private[properties] abstract class BaseReadableSeqPropertyFromSingleValue[A, B:
val added: Seq[CastableProperty[B]] = Seq.tabulate(transformed.size - current.size) { idx =>
PropertyCreator[B].newProperty(transformed(current.size + idx), this)
}
if (listenChildren) childrenRegistrations ++= added.map(p => p.id -> p.listen(_ => valueChanged()))
if (listenChildren) childrenRegistrations ++= added.iterator.map(p => p -> p.listen(_ => valueChanged()))
CrossCollections.replaceSeq(children, commonBegin, 0, added)
Some(Patch[ElemType](commonBegin, Seq(), added.map(toElemProp), clearsProperty = false))
} else if (transformed.size < current.size) {
val removed = CrossCollections.slice(children, commonBegin, commonBegin + current.size - transformed.size)
if (listenChildren) removed.foreach(p => childrenRegistrations.remove(p.id).get.cancel())
CrossCollections.replace(children, commonBegin, current.size - transformed.size)
if (listenChildren) removed.foreach(p => childrenRegistrations.remove(p).get.cancel())
children.remove(commonBegin, current.size - transformed.size)
Some(Patch[ElemType](commonBegin, removed.map(toElemProp).toSeq, Seq(), transformed.isEmpty))
} else None

CallbackSequencer().sequence {
transformed.zip(children)
transformed.iterator.zip(children.iterator)
.slice(commonBegin, math.max(commonBegin + transformed.size - current.size, transformed.size - commonEnd))
.foreach { case (pv, p) => p.set(pv) }
patch.foreach(structureChanged)
Expand Down Expand Up @@ -117,7 +116,7 @@ private[properties] abstract class BaseReadableSeqPropertyFromSingleValue[A, B:
override def restart(): Unit = {
initOriginListeners()
if (listenChildren && childrenRegistrations.isEmpty) {
childrenRegistrations ++= children.map(p => p.id -> p.listen(_ => valueChanged()))
childrenRegistrations ++= children.iterator.map(p => p -> p.listen(_ => valueChanged()))
}
reg.restart()
}
Expand Down Expand Up @@ -157,7 +156,7 @@ private[properties] final class SeqPropertyFromSingleValue[A, B: PropertyCreator
protected def toElemProp(p: Property[B]): Property[B] = p

override protected[properties] def valueChanged(): Unit = {
CallbackSequencer().queue(s"revertSet:$id", () => {
CallbackSequencer().queue(s"revertSet:$hashCode", () => {
origin.set(revert(get))
})
super.valueChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ private[properties] abstract class ZippedSeqPropertyUtils[O](
sources: ISeq[ReadableSeqProperty[_, _ <: ReadableProperty[_]]]
) extends AbstractReadableSeqProperty[O, ReadableProperty[O]] {

override final val id: PropertyId = PropertyCreator.newID()
override final protected[properties] def parent: ReadableProperty[_] = null

private final val children = CrossCollections.createArray[ReadableProperty[O]]
Expand All @@ -28,7 +27,7 @@ private[properties] abstract class ZippedSeqPropertyUtils[O](
CrossCollections.replaceSeq(children, patch.idx, removed.size, added)
val mappedPatch = Patch(patch.idx, removed.toSeq, added, patch.clearsProperty)
CallbackSequencer().queue(
s"${this.id.toString}:fireElementsListeners:${patch.hashCode()}",
s"$hashCode:fireElementsListeners:${patch.hashCode()}",
() => structureListeners.foreach(_.apply(mappedPatch))
)
valueChanged()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.udash.properties.single

import io.udash.properties.PropertyId

private[properties] class DirectPropertyImpl[A](val parent: ReadableProperty[_], override val id: PropertyId)
private[properties] class DirectPropertyImpl[A](val parent: ReadableProperty[_])
extends AbstractProperty[A] with CastableProperty[A] {

private var value: A = _
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package io.udash.properties.single

import io.udash.properties.{PropertyCreator, PropertyId}

private[properties] trait ForwarderReadableProperty[A] extends AbstractReadableProperty[A] {
protected def origin: ReadableProperty[_]

override val id: PropertyId = PropertyCreator.newID()
override protected[properties] def parent: ReadableProperty[_] = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import io.udash.utils.Registration

/** Base interface of every Property in Udash. */
trait ReadableProperty[+A] {
/** Unique property ID. */
val id: PropertyId

/** @return Current property value. */
def get: A
Expand Down Expand Up @@ -130,7 +128,7 @@ private[properties] trait AbstractReadableProperty[A] extends ReadableProperty[A

protected[properties] override def valueChanged(): Unit = {
val originalListeners = listeners.toSet
CallbackSequencer().queue(s"${this.id.toString}:fireValueListeners", () => {
CallbackSequencer().queue(s"$hashCode:valueChanged", () => {
val value = get
listeners.foreach { listener => if (originalListeners.contains(listener)) listener(value) }
oneTimeListeners.foreach(_.cancel())
Expand Down
12 changes: 6 additions & 6 deletions core/src/test/scala/io/udash/properties/PropertyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ class PropertyTest extends UdashCoreTest {
}

"transform to ReadableSeqProperty" in {
val elemListeners = MMap.empty[PropertyId, Registration]
val elemListeners = MMap.empty[ReadableProperty[_], Registration]
var elementsUpdated = 0
def registerElementListener(props: BSeq[ReadableProperty[_]]) =
props.foreach { p =>
elemListeners(p.id) = p.listen(_ => elementsUpdated += 1)
elemListeners(p) = p.listen(_ => elementsUpdated += 1)
}

val p = Property("1,2,3,4,5")
Expand All @@ -553,7 +553,7 @@ class PropertyTest extends UdashCoreTest {
val r2 = s.listenStructure { p =>
registerElementListener(p.added)
p.removed.foreach { p =>
elemListeners(p.id).cancel()
elemListeners(p).cancel()
}
lastPatch = p
}
Expand Down Expand Up @@ -707,11 +707,11 @@ class PropertyTest extends UdashCoreTest {
}

"transform to SeqProperty" in {
val elemListeners = MMap.empty[PropertyId, Registration]
val elemListeners = MMap.empty[ReadableProperty[_], Registration]
var elementsUpdated = 0
def registerElementListener(props: BSeq[ReadableProperty[_]]): Unit =
props.foreach { p =>
elemListeners(p.id) = p.listen(_ => elementsUpdated += 1)
elemListeners(p) = p.listen(_ => elementsUpdated += 1)
}

val p = Property("1,2,3,4,5")
Expand All @@ -728,7 +728,7 @@ class PropertyTest extends UdashCoreTest {
val r2 = s.listenStructure(p => {
registerElementListener(p.added)
p.removed.foreach { p =>
elemListeners(p.id).cancel()
elemListeners(p).cancel()
}
lastPatch = p
})
Expand Down
2 changes: 1 addition & 1 deletion macros/src/main/scala/io/udash/macros/PropertyMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class PropertyMacros(val ctx: blackbox.Context) extends AbstractMacroCommons(ctx
private def generateModelProperty(tpe: Type): c.Tree = {
def impl(members: Map[TermName, Type], getCreator: Tree): Tree = {
q"""
new $ModelPropertyImplCls[$tpe](prt, $PropertyCreatorCompanion.newID()) {
new $ModelPropertyImplCls[$tpe](prt) {
override protected def initialize(): Unit = {
..${
members.map {
Expand Down