mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 05:26:59 +01:00
Fix LiveView methods mixed output
This commit is contained in:
parent
2424d2ff6c
commit
cbd5229025
4 changed files with 15 additions and 8 deletions
|
|
@ -4,9 +4,7 @@ import zio.*
|
|||
import zio.stream.*
|
||||
|
||||
trait LiveView[Msg, Model]:
|
||||
def init: Task[Model]
|
||||
def update(model: Model): Msg => Task[Model]
|
||||
def init: Model | Task[Model]
|
||||
def update(model: Model): Msg => Model | Task[Model]
|
||||
def view(model: Dyn[Model]): HtmlElement
|
||||
def subscriptions(model: Model): ZStream[Any, Nothing, Msg]
|
||||
|
||||
given [T]: Conversion[T, Task[T]] = ZIO.succeed(_)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ final case class LiveRoute[A, Msg, Model](
|
|||
s"phx-${Base64.getUrlEncoder().withoutPadding().encodeToString(Random().nextBytes(12))}"
|
||||
val token = Token.sign("secret", id, "")
|
||||
for
|
||||
initModel <- lv.init
|
||||
initModel <- normalize(lv.init)
|
||||
el = lv.view(Var(initModel))
|
||||
_ = el.syncAll()
|
||||
yield Response.html(
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ object Socket:
|
|||
inbox <- Queue.bounded[(Payload.Event, WebSocketMessage.Meta)](4)
|
||||
outHub <- Hub.unbounded[(Payload, WebSocketMessage.Meta)]
|
||||
|
||||
initModel <- lv.init
|
||||
initModel <- normalize(lv.init)
|
||||
modelVar = Var(initModel)
|
||||
el = lv.view(modelVar)
|
||||
ref <- Ref.make((modelVar, el))
|
||||
|
|
@ -52,7 +52,8 @@ object Socket:
|
|||
s"No binding found for event ID ${event.event}"
|
||||
)
|
||||
)
|
||||
updatedModel <- lv.update(modelVar.currentValue)(f(event.params))
|
||||
updatedModel <-
|
||||
normalize(lv.update(modelVar.currentValue)(f(event.params)))
|
||||
_ = modelVar.set(updatedModel)
|
||||
_ <- lvStreamRef.set(lv.subscriptions(updatedModel))
|
||||
diff = el.diff()
|
||||
|
|
@ -63,7 +64,7 @@ object Socket:
|
|||
serverFiber <- serverMsgStream.runForeach { (msg, meta) =>
|
||||
for
|
||||
(modelVar, el) <- ref.get
|
||||
updatedModel <- lv.update(modelVar.currentValue)(msg)
|
||||
updatedModel <- normalize(lv.update(modelVar.currentValue)(msg))
|
||||
_ = modelVar.set(updatedModel)
|
||||
diff = el.diff()
|
||||
payload = Payload.Diff(diff)
|
||||
|
|
|
|||
8
scalive/zio/src/scalive/ZIOHelpers.scala
Normal file
8
scalive/zio/src/scalive/ZIOHelpers.scala
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package scalive
|
||||
|
||||
import zio.*
|
||||
|
||||
private def normalize[A](value: A | Task[A]): Task[A] =
|
||||
value match
|
||||
case t: Task[?] @unchecked => t.asInstanceOf[Task[A]]
|
||||
case v => ZIO.succeed(v.asInstanceOf[A])
|
||||
Loading…
Add table
Add a link
Reference in a new issue