mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 05:26:59 +01:00
Remove the need to wrap every model in ZIO
This commit is contained in:
parent
1da129f855
commit
c19086ef32
6 changed files with 24 additions and 30 deletions
|
|
@ -8,10 +8,10 @@ import zio.stream.ZStream
|
||||||
import TestView.*
|
import TestView.*
|
||||||
class TestView extends LiveView[Msg, Model]:
|
class TestView extends LiveView[Msg, Model]:
|
||||||
|
|
||||||
def init = ZIO.succeed(Model())
|
def init = Model()
|
||||||
|
|
||||||
def update(model: Model) =
|
def update(model: Model) =
|
||||||
case Msg.UpdateModel(f) => ZIO.succeed(f(model))
|
case Msg.UpdateModel(f) => f(model)
|
||||||
|
|
||||||
def view(model: Dyn[Model]) =
|
def view(model: Dyn[Model]) =
|
||||||
div(
|
div(
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,5 @@ trait LiveView[Msg, Model]:
|
||||||
def update(model: Model): Msg => Task[Model]
|
def update(model: Model): Msg => Task[Model]
|
||||||
def view(model: Dyn[Model]): HtmlElement
|
def view(model: Dyn[Model]): HtmlElement
|
||||||
def subscriptions(model: Model): ZStream[Any, Nothing, Msg]
|
def subscriptions(model: Model): ZStream[Any, Nothing, Msg]
|
||||||
|
|
||||||
|
given [T]: Conversion[T, Task[T]] = ZIO.succeed(_)
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,19 @@ import zio.stream.ZStream
|
||||||
|
|
||||||
class CounterLiveView() extends LiveView[Msg, Model]:
|
class CounterLiveView() extends LiveView[Msg, Model]:
|
||||||
|
|
||||||
def init = ZIO.succeed(
|
def init =
|
||||||
Model(
|
Model(
|
||||||
isVisible = true,
|
isVisible = true,
|
||||||
counter = 0
|
counter = 0
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
def update(model: Model) =
|
def update(model: Model) =
|
||||||
case Msg.ToggleCounter =>
|
case Msg.ToggleCounter =>
|
||||||
ZIO.succeed(model.focus(_.isVisible).modify(!_))
|
model.focus(_.isVisible).modify(!_)
|
||||||
case Msg.IncCounter =>
|
case Msg.IncCounter =>
|
||||||
ZIO.succeed(model.focus(_.counter).modify(_ + 1))
|
model.focus(_.counter).modify(_ + 1)
|
||||||
case Msg.DecCounter =>
|
case Msg.DecCounter =>
|
||||||
ZIO.succeed(model.focus(_.counter).modify(_ - 1))
|
model.focus(_.counter).modify(_ - 1)
|
||||||
|
|
||||||
def view(model: Dyn[Model]) =
|
def view(model: Dyn[Model]) =
|
||||||
div(
|
div(
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,9 @@ class HomeLiveView() extends LiveView[String, Unit]:
|
||||||
"/todo" -> "Todo"
|
"/todo" -> "Todo"
|
||||||
)
|
)
|
||||||
|
|
||||||
def init = ZIO.succeed(())
|
def init = ()
|
||||||
|
|
||||||
def update(model: Unit) = _ => ZIO.succeed(model)
|
def update(model: Unit) = _ => model
|
||||||
|
|
||||||
def view(model: Dyn[Unit]) =
|
def view(model: Dyn[Unit]) =
|
||||||
ul(
|
ul(
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import zio.stream.ZStream
|
||||||
|
|
||||||
class ListLiveView(someParam: String) extends LiveView[Msg, Model]:
|
class ListLiveView(someParam: String) extends LiveView[Msg, Model]:
|
||||||
|
|
||||||
def init = ZIO.succeed(
|
def init =
|
||||||
Model(
|
Model(
|
||||||
elems = List(
|
elems = List(
|
||||||
NestedModel("a", 10),
|
NestedModel("a", 10),
|
||||||
|
|
@ -14,11 +14,10 @@ class ListLiveView(someParam: String) extends LiveView[Msg, Model]:
|
||||||
NestedModel("c", 20)
|
NestedModel("c", 20)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
def update(model: Model) =
|
def update(model: Model) =
|
||||||
case Msg.IncAge(value) =>
|
case Msg.IncAge(value) =>
|
||||||
ZIO.succeed(model.focus(_.elems.index(2).age).modify(_ + value))
|
model.focus(_.elems.index(2).age).modify(_ + value)
|
||||||
|
|
||||||
def view(model: Dyn[Model]) =
|
def view(model: Dyn[Model]) =
|
||||||
div(
|
div(
|
||||||
|
|
|
||||||
|
|
@ -6,30 +6,24 @@ import zio.stream.ZStream
|
||||||
|
|
||||||
class TodoLiveView() extends LiveView[Msg, Model]:
|
class TodoLiveView() extends LiveView[Msg, Model]:
|
||||||
|
|
||||||
def init = ZIO.succeed(Model(List(Todo(99, "some task"))))
|
def init = Model(List(Todo(99, "Buy eggs")))
|
||||||
|
|
||||||
def update(model: Model) =
|
def update(model: Model) =
|
||||||
case Msg.Add(text) =>
|
case Msg.Add(text) =>
|
||||||
val nextId = model.todos.maxByOption(_.id).map(_.id).getOrElse(1) + 1
|
val nextId = model.todos.maxByOption(_.id).map(_.id).getOrElse(1) + 1
|
||||||
ZIO.succeed(
|
|
||||||
model
|
model
|
||||||
.focus(_.todos)
|
.focus(_.todos)
|
||||||
.modify(_.appended(Todo(nextId, text)))
|
.modify(_.appended(Todo(nextId, text)))
|
||||||
)
|
|
||||||
case Msg.Remove(id) =>
|
case Msg.Remove(id) =>
|
||||||
ZIO.succeed(
|
|
||||||
model
|
model
|
||||||
.focus(_.todos)
|
.focus(_.todos)
|
||||||
.modify(_.filterNot(_.id == id))
|
.modify(_.filterNot(_.id == id))
|
||||||
)
|
|
||||||
case Msg.ToggleCompletion(id) =>
|
case Msg.ToggleCompletion(id) =>
|
||||||
ZIO.succeed(
|
|
||||||
model
|
model
|
||||||
.focus(_.todos)
|
.focus(_.todos)
|
||||||
.modify(
|
.modify(
|
||||||
_.map(todo => if todo.id == id then todo.copy(completed = todo.completed) else todo)
|
_.map(todo => if todo.id == id then todo.copy(completed = todo.completed) else todo)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
def view(model: Dyn[Model]) =
|
def view(model: Dyn[Model]) =
|
||||||
div(
|
div(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue