mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 13:36:59 +01:00
Fix comprehension rendering
This commit is contained in:
parent
ccdd22b61a
commit
1bd65fd49c
3 changed files with 30 additions and 20 deletions
|
|
@ -8,7 +8,7 @@ enum Diff:
|
|||
case Tag(
|
||||
static: Seq[String] = Seq.empty,
|
||||
dynamic: Seq[Diff.Dynamic] = Seq.empty)
|
||||
case Split(
|
||||
case Comprehension(
|
||||
static: Seq[String] = Seq.empty,
|
||||
entries: Seq[Diff.Dynamic] = Seq.empty)
|
||||
case Value(value: String)
|
||||
|
|
@ -16,7 +16,7 @@ enum Diff:
|
|||
case Deleted
|
||||
|
||||
object Diff:
|
||||
given JsonEncoder[Diff] = JsonEncoder[Json].contramap(toJson)
|
||||
given JsonEncoder[Diff] = JsonEncoder[Json].contramap(toJson(_))
|
||||
|
||||
private def toJson(diff: Diff): Json =
|
||||
diff match
|
||||
|
|
@ -29,17 +29,18 @@ object Diff:
|
|||
dynamic.map(d => d.key -> toJson(d.diff))
|
||||
)
|
||||
)
|
||||
case Diff.Split(static, entries) =>
|
||||
case Diff.Comprehension(static, entries) =>
|
||||
Json.Obj(
|
||||
Option
|
||||
.when(static.nonEmpty)("s" -> Json.Arr(static.map(Json.Str(_))*))
|
||||
.to(Chunk)
|
||||
.appendedAll(
|
||||
Option.when(entries.nonEmpty)(
|
||||
"d" ->
|
||||
Json.Obj(
|
||||
entries.map(d => d.key -> toJson(d.diff))*
|
||||
)
|
||||
"k" ->
|
||||
Json
|
||||
.Obj(
|
||||
entries.map(d => d.key -> toJson(d.diff))*
|
||||
).add("kc", Json.Num(entries.length))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ object DiffBuilder:
|
|||
entries.collectFirst { case (_, Some(el)) => el.static }.getOrElse(List.empty)
|
||||
List(
|
||||
Some(
|
||||
Diff.Split(
|
||||
Diff.Comprehension(
|
||||
static = if trackUpdates then Seq.empty else static,
|
||||
entries = entries.map {
|
||||
case (key, Some(el)) =>
|
||||
|
|
|
|||
|
|
@ -8,28 +8,37 @@ import scala.util.Random
|
|||
final case class Socket[Cmd](lv: LiveView[Cmd]):
|
||||
|
||||
private var clientInitialized = false
|
||||
val id: String = s"phx-${Base64.getEncoder().encodeToString(Random().nextBytes(8))}"
|
||||
private val token = Token.sign("secret", id, "")
|
||||
private val element = lv.el.prepended(idAttr := id, dataAttr("phx-session") := token)
|
||||
val id: String =
|
||||
s"phx-${Base64.getUrlEncoder().withoutPadding().encodeToString(Random().nextBytes(8))}"
|
||||
private val token = Token.sign("secret", id, "")
|
||||
|
||||
element.syncAll()
|
||||
lv.el.syncAll()
|
||||
|
||||
def receiveCommand(cmd: Cmd): Unit =
|
||||
lv.handleCommand(cmd)
|
||||
|
||||
def renderHtml(rootLayout: HtmlElement => HtmlElement = identity): String =
|
||||
element.syncAll()
|
||||
HtmlBuilder.build(rootLayout(element))
|
||||
lv.el.syncAll()
|
||||
HtmlBuilder.build(
|
||||
rootLayout(
|
||||
div(
|
||||
idAttr := id,
|
||||
dataAttr("phx-session") := token,
|
||||
lv.el
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
def syncClient: Unit =
|
||||
element.syncAll()
|
||||
println(DiffBuilder.build(element, trackUpdates = clientInitialized).toJsonPretty)
|
||||
lv.el.syncAll()
|
||||
println(DiffBuilder.build(lv.el, trackUpdates = clientInitialized).toJsonPretty)
|
||||
clientInitialized = true
|
||||
element.setAllUnchanged()
|
||||
lv.el.setAllUnchanged()
|
||||
|
||||
def diff: Diff =
|
||||
element.syncAll()
|
||||
val diff = DiffBuilder.build(element, trackUpdates = clientInitialized)
|
||||
lv.el.syncAll()
|
||||
val diff = DiffBuilder.build(lv.el, trackUpdates = clientInitialized)
|
||||
clientInitialized = true
|
||||
element.setAllUnchanged()
|
||||
lv.el.setAllUnchanged()
|
||||
diff
|
||||
end Socket
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue