Skip to content

Commit

Permalink
Sprinkle .changes combinator for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Feb 29, 2024
1 parent 8abfc08 commit d5a4cf1
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions todo-mvc/src/main/scala/todomvc/TodoMvc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,10 @@ object TodoMvc extends IOWebApp:
span(
cls := "todo-count",
strong(store.activeCount.map(_.toString)),
store
.activeCount
.map {
case 1 => " item left"
case n => " items left"
}
.changes
store.activeCount.map {
case 1 => " item left"
case n => " items left"
}
),
ul(
cls := "filters",
Expand Down Expand Up @@ -194,9 +191,9 @@ class TodoStore(entries: SignallingSortedMapRef[IO, Long, Todo], nextId: IO[Long
def toggleAll(completed: Boolean): IO[Unit] =
entries.update(sm => SortedMap.from(sm.view.mapValues(_.copy(completed = completed))))

def allCompleted: Signal[IO, Boolean] = entries.map(_.values.forall(_.completed))
def allCompleted: Signal[IO, Boolean] = entries.map(_.values.forall(_.completed)).changes

def hasCompleted: Signal[IO, Boolean] = entries.map(_.values.exists(_.completed))
def hasCompleted: Signal[IO, Boolean] = entries.map(_.values.exists(_.completed)).changes

def clearCompleted: IO[Unit] = entries.update(_.filterNot((_, todo) => todo.completed))

Expand All @@ -208,9 +205,9 @@ class TodoStore(entries: SignallingSortedMapRef[IO, Long, Todo], nextId: IO[Long
def ids(filter: Filter): Signal[IO, List[Long]] =
entries.map(_.filter((_, t) => filter.pred(t)).keySet.toList)

def size: Signal[IO, Int] = entries.map(_.size)
def size: Signal[IO, Int] = entries.map(_.size).changes

def activeCount: Signal[IO, Int] = entries.map(_.values.count(!_.completed))
def activeCount: Signal[IO, Int] = entries.map(_.values.count(!_.completed)).changes

object TodoStore:

Expand Down

0 comments on commit d5a4cf1

Please sign in to comment.