Skip to content

Commit

Permalink
having followup transactions during observation makes weird observer …
Browse files Browse the repository at this point in the history
…timings
  • Loading branch information
rmgk committed Mar 10, 2024
1 parent 7711ffa commit 0ff0bd7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Modules/Example Todolist/src/main/scala/todo/TaskData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ class TaskReferences(toggleAll: Event[dom.Event], storePrefix: String) {
GlobalRegistry.publish(taskID, crdt)
GlobalRegistry.unbuffer(taskID)

val taskData =
crdt.map(x => x.state.data.read.getOrElse(TaskData(desc = "LWW Empty")))
val taskData = Signal {
crdt.value.state.data.read.getOrElse(TaskData(desc = "LWW Empty"))
}

val removeButton =
Event.fromCallback(button(`class` := "destroy", onclick := Event.handle))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ trait Transaction[State[_]] {

def observe(obs: Observation): Unit

def followup(obs: Observation): Unit = observe(obs)

def initializer: Initializer[State]

private[reactives] def discover(source: ReSource.of[State], sink: Derived.of[State]): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object PlanTransactionScope {
case class StaticInTransaction[State[_]](tx: Transaction[State], scheduler: Scheduler[State])
extends PlanTransactionScope[State] {
override def planTransaction(inintialWrites: ReSource.of[State]*)(admission: AdmissionTicket[State] => Unit): Unit =
tx.observe { () =>
tx.followup { () =>
scheduler.forceNewTransaction(inintialWrites*)(admission)
}
}
Expand All @@ -68,7 +68,7 @@ object PlanTransactionScope {
extends PlanTransactionScope[State] {
override def planTransaction(inintialWrites: ReSource.of[State]*)(admission: AdmissionTicket[State] => Unit): Unit =
dynamicScope.maybeTransaction match
case Some(tx) => tx.observe { () =>
case Some(tx) => tx.followup { () =>
scheduler.forceNewTransaction(inintialWrites*)(admission)
}
case None =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ trait Twoversion {

val toCommit = ListBuffer[ReSource]()
val observers = ListBuffer[Observation]()
val followups = ListBuffer[Observation]()
var commitStarted = false

override def schedule(commitable: ReSource): Unit = { toCommit += commitable; () }
Expand All @@ -180,7 +181,17 @@ trait Twoversion {
s"Added observation to transaction (${this}), but it is too late in its lifecycle. " +
s"This may happen due to capturing a transaction reference such that it survives outside of its dynamic scope."
)
observers += f; ()
observers += f
()
}

override def followup(obs: Observation): Unit = {
if (commitStarted) throw new IllegalStateException(
s"Added observation to transaction (${this}), but it is too late in its lifecycle. " +
s"This may happen due to capturing a transaction reference such that it survives outside of its dynamic scope."
)
followups += obs
()
}

override def commitPhase(): Unit = {
Expand All @@ -200,6 +211,12 @@ trait Twoversion {
// we should probably aggregate all of the exceptions,
// but this is not the place to invent exception aggregation
if (failure != null) throw failure

followups.foreach { n =>
try n.execute()
catch { case NonFatal(e) => failure = e }
}
if (failure != null) throw failure
}

final def commitDependencyDiff(node: Derived, current: Set[ReSource])(updated: Set[ReSource]): Unit = {
Expand Down

0 comments on commit 0ff0bd7

Please sign in to comment.