Skip to content

Commit

Permalink
clean fold branches (macro seems to have fixed itself)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Mar 2, 2024
1 parent a48dc99 commit e6fa173
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
)
Expand Down
19 changes: 8 additions & 11 deletions Modules/Example Todolist/src/main/scala/todo/TaskOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
* )
* }}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e6fa173

Please sign in to comment.