mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 13:36:59 +01:00
Complete redesign to allow assigns style state
This commit is contained in:
parent
ae0dc04a9e
commit
bb062b9679
18 changed files with 802 additions and 739 deletions
|
|
@ -9,9 +9,10 @@ import zio.http.template.Html
|
|||
|
||||
object Example extends ZIOAppDefault:
|
||||
|
||||
val lv =
|
||||
LiveView(
|
||||
TestView,
|
||||
val s = Socket(
|
||||
TestView,
|
||||
LiveState.empty.set(
|
||||
TestView.model,
|
||||
MyModel(
|
||||
List(
|
||||
NestedModel("a", 10),
|
||||
|
|
@ -20,6 +21,7 @@ object Example extends ZIOAppDefault:
|
|||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
val socketApp: WebSocketApp[Any] =
|
||||
Handler.webSocket { channel =>
|
||||
|
|
@ -66,7 +68,7 @@ object Example extends ZIOAppDefault:
|
|||
val routes: Routes[Any, Response] =
|
||||
Routes(
|
||||
Method.GET / "" -> handler { (_: Request) =>
|
||||
Response.html(Html.raw(HtmlBuilder.build(lv)))
|
||||
Response.html(Html.raw(s.renderHtml))
|
||||
},
|
||||
Method.GET / "live" / "ws" -> handler(socketApp.toResponse)
|
||||
)
|
||||
|
|
@ -77,13 +79,14 @@ end Example
|
|||
final case class MyModel(elems: List[NestedModel], cls: String = "text-xs")
|
||||
final case class NestedModel(name: String, age: Int)
|
||||
|
||||
object TestView extends View[MyModel]:
|
||||
val root: HtmlElement[MyModel] =
|
||||
object TestView extends LiveView:
|
||||
val model = LiveState.Key[MyModel]
|
||||
val render =
|
||||
div(
|
||||
idAttr := "42",
|
||||
cls := model(_.cls),
|
||||
ul(
|
||||
model.splitByIndex(_.elems)(elem =>
|
||||
model(_.elems).splitByIndex(elem =>
|
||||
li(
|
||||
"Nom: ",
|
||||
elem(_.name),
|
||||
|
|
|
|||
13
zio/src/scalive/RootLayout.scala
Normal file
13
zio/src/scalive/RootLayout.scala
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package scalive
|
||||
|
||||
import scalive.HtmlElement
|
||||
|
||||
object RootLayout:
|
||||
def apply[RootModel](content: HtmlElement): HtmlElement =
|
||||
htmlRootTag(
|
||||
lang := "en",
|
||||
metaTag(charset := "utf-8"),
|
||||
bodyTag(
|
||||
// content
|
||||
)
|
||||
)
|
||||
30
zio/src/scalive/ScaliveZio.scala
Normal file
30
zio/src/scalive/ScaliveZio.scala
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package scalive
|
||||
|
||||
import zio.http.Response
|
||||
import zio.http.template.Html
|
||||
|
||||
// trait LiveRouter:
|
||||
// type RootModel
|
||||
// private lazy val viewsMap: Map[String, View] = views.map(r => (r.name, r.view)).toMap
|
||||
// def rootLayout: HtmlElement[RootModel]
|
||||
// def views: Seq[LiveRoute]
|
||||
//
|
||||
// final case class LiveRoute(name: String, view: View)
|
||||
|
||||
object ZioLiveApp:
|
||||
// 1 Request to live route
|
||||
// 2 Create live view with stateless token containing user id if connected, http params, live view id
|
||||
// 3 Response with HTML and token
|
||||
// 4 Websocket connection with token
|
||||
// 5 Recreate exact same liveview as before using token data
|
||||
|
||||
// val testRoute = LiveRoute("test", TestView)
|
||||
// val router = new LiveRouter:
|
||||
// val rootLayout = htmlRootTag()
|
||||
// val views = Seq(testRoute)
|
||||
|
||||
// def htmlRender(v: View, model: v.Model) =
|
||||
// val lv = LiveView(v, model)
|
||||
// Response.html(Html.raw(HtmlBuilder.build(lv, isRoot = true)))
|
||||
|
||||
private val socketApp = ???
|
||||
Loading…
Add table
Add a link
Reference in a new issue