Forked from
CS-214 / ul2024 / webapp-lib
64 commits behind, 1 commit ahead of the upstream repository.
-
Hugh Coleman authored
Rebasing on top of !27.
Hugh Coleman authoredRebasing on top of !27.
WebClientAppInstance.scala 1.38 KiB
package cs214.webapp
package client
package graphics
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import org.scalajs.dom
import scalatags.JsDom.all.Frag
/** General web graphics UI, basically a wrapper around raw ScalaJS API */
abstract class WebClientAppInstance[Event, View](
userId: UserId,
sendMessage: ujson.Value => Unit,
target: Target
) extends StateMachineClientAppInstance[Event, View](userId, sendMessage), DOMClientUI[View], PatchableUI:
/** The currently-rendered view, if one exists. */
private var currentView: Option[View] = None
/** Renders a [[View]] received from the server. The method also takes a
* [[UserId]] to get information on the context and an [[onEvent]] callback
* which it uses to send server events when specific actions are triggered on
* the UI (click on a button for example)
* @param userId
* The user id used by the client.
* @param view
* The view to render.
* @return
* The rendered view.
*/
protected def render(userId: UserId, view: View): Frag
override def displayView(view: View): Unit =
if Some(view) != currentView then
currentView = Some(view)
patchUI(target, render(userId, view))
override def patchUI(target: dom.Element, fr: Frag): Unit =
target.replaceChildren(fr.render)
trait PatchableUI:
def patchUI(target: dom.Element, fr: Frag): Unit