From e6fa1737254bb00c444e7f080ce6c22da04abf09 Mon Sep 17 00:00:00 2001 From: ragnar Date: Sat, 2 Mar 2024 22:20:43 +0100 Subject: [PATCH] clean fold branches (macro seems to have fixed itself) --- .../src/test/scala/example/SocialMedia.scala | 6 ++---- .../src/main/scala/todo/TaskOps.scala | 19 ++++++++----------- .../reactives/operator/EventBundle.scala | 9 ++++----- .../scala/reactives/operator/FoldBundle.scala | 2 +- .../scala/reactives/operator/Interface.scala | 2 +- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Modules/Example Replication/shared/src/test/scala/example/SocialMedia.scala b/Modules/Example Replication/shared/src/test/scala/example/SocialMedia.scala index 8597773ec..91a737954 100644 --- a/Modules/Example Replication/shared/src/test/scala/example/SocialMedia.scala +++ b/Modules/Example Replication/shared/src/test/scala/example/SocialMedia.scala @@ -56,10 +56,8 @@ object SocialMediaTest { val socialMedia: Signal[SocialMedia] = Fold(SocialMedia())( likeEvent act { id => Fold.current.like(id) }, - Fold.branch { - commentEvent.value match - case None => Fold.current - case Some(text) => Fold.current.comment(UI.currentPostID.value, text) + commentEvent.act { text => + Fold.current.comment(UI.currentPostID.value, text) }, postEvent act { text => Fold.current.post(text) } ) diff --git a/Modules/Example Todolist/src/main/scala/todo/TaskOps.scala b/Modules/Example Todolist/src/main/scala/todo/TaskOps.scala index 008838860..dd17b46f6 100644 --- a/Modules/Example Todolist/src/main/scala/todo/TaskOps.scala +++ b/Modules/Example Todolist/src/main/scala/todo/TaskOps.scala @@ -24,17 +24,14 @@ class TaskOps(@unused taskrefs: TaskReferences, replicaID: Uid) { current.clearDeltas().prepend(taskref) } - def handleRemoveAll(removeAll: Event[Any]): Fold.Branch[State] = Fold.branch { - removeAll.value match - case None => Fold.current - case Some(_) => - current.clearDeltas().deleteBy { (taskref: TaskRef) => - val isDone = taskref.task.value.read.exists(_.done) - // todo, move to observer, disconnect during transaction does not respect rollbacks - if (isDone) taskref.task.disconnect() - isDone - } - } + def handleRemoveAll(removeAll: Event[Any]): Fold.Branch[State] = + removeAll.act: _ => + current.clearDeltas().deleteBy { (taskref: TaskRef) => + val isDone = taskref.task.value.read.exists(_.done) + // todo, move to observer, disconnect during transaction does not respect rollbacks + if (isDone) taskref.task.disconnect() + isDone + } def handleRemove(state: State)(id: String): State = { state.clearDeltas().deleteBy { (taskref: TaskRef) => diff --git a/Modules/Reactives/shared/src/main/scala/reactives/operator/EventBundle.scala b/Modules/Reactives/shared/src/main/scala/reactives/operator/EventBundle.scala index 9c53ba169..bf3fedd23 100644 --- a/Modules/Reactives/shared/src/main/scala/reactives/operator/EventBundle.scala +++ b/Modules/Reactives/shared/src/main/scala/reactives/operator/EventBundle.scala @@ -225,11 +225,10 @@ trait Event[+T] extends MacroAccess[Option[T]] with Disconnectable { * @inheritdoc */ final inline def fold[A](init: A)(inline op: (A, T) => A)(implicit ticket: CreationTicket[State]): Signal[A] = - Fold(init)(Fold.branch { - this.value match - case None => Fold.current - case Some(v) => op(Fold.current, v) - }) + Fold(init)( + this.act: v => + op(Fold.current, v) + ) } diff --git a/Modules/Reactives/shared/src/main/scala/reactives/operator/FoldBundle.scala b/Modules/Reactives/shared/src/main/scala/reactives/operator/FoldBundle.scala index 48998e921..e7bed2aac 100644 --- a/Modules/Reactives/shared/src/main/scala/reactives/operator/FoldBundle.scala +++ b/Modules/Reactives/shared/src/main/scala/reactives/operator/FoldBundle.scala @@ -9,7 +9,7 @@ import reactives.structure.{Pulse, SignalImpl} * Example for a counter that can be reset: * {{{ * Fold(0)( - * add act { x => current + v }, + * add act { x => Fold.current + v }, * reset act { _ => 0 } * ) * }}} diff --git a/Modules/Reactives/shared/src/main/scala/reactives/operator/Interface.scala b/Modules/Reactives/shared/src/main/scala/reactives/operator/Interface.scala index 6de6f760e..cdf8fd4e4 100644 --- a/Modules/Reactives/shared/src/main/scala/reactives/operator/Interface.scala +++ b/Modules/Reactives/shared/src/main/scala/reactives/operator/Interface.scala @@ -18,7 +18,7 @@ trait Interface { export Fold.current extension [T](e: Event[T]) { - inline infix def act[S](f: FoldState[S] ?=> T => S): Fold.Branch[S] = Fold.branch { e.value.fold(current)(f) } + inline infix def act[S](inline f: FoldState[S] ?=> T => S): Fold.Branch[S] = Fold.branch { e.value.fold(current)(f) } } val global: GlobalCandidate[GlobalCandidate.selected.State] = GlobalCandidate.selected