Created
June 11, 2021 05:57
-
-
Save kitlangton/ffce44d09bbc36c5996bbc9fa4ab8a62 to your computer and use it in GitHub Desktop.
Example of basic HMR with Scala.js & Laminar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main { | |
val css: Css.type = Css | |
println(s"PRODUCTION MODE: ${scala.scalajs.LinkingInfo.productionMode}") | |
def main(args: Array[String]): Unit = | |
waitForLoad { | |
val appContainer = dom.document.querySelector("#app") | |
appContainer.innerHTML = "" | |
unmount() | |
println("RENDER") | |
val rootNode = render(appContainer, Frontend.view) | |
storeUnmount(rootNode) | |
} | |
def waitForLoad(f: => Any): Unit = | |
if (dom.window.asInstanceOf[js.Dynamic].documentLoaded == null) | |
documentEvents.onDomContentLoaded.foreach { _ => | |
dom.window.asInstanceOf[js.Dynamic].documentLoaded = true | |
f | |
}(unsafeWindowOwner) | |
else | |
f | |
def unmount(): Unit = | |
if (scala.scalajs.LinkingInfo.developmentMode) { | |
Option(dom.window.asInstanceOf[js.Dynamic].__laminar_root_unmount) | |
.collect { | |
case x if !js.isUndefined(x) => | |
println("UNMOUNT") | |
x.asInstanceOf[js.Function0[Unit]] | |
} | |
.foreach { _.apply() } | |
} | |
def storeUnmount(rootNode: RootNode): Unit = { | |
val unmountFunction: js.Function0[Any] = () => { | |
rootNode.unmount() | |
} | |
println("STORE UNMOUNT") | |
dom.window.asInstanceOf[js.Dynamic].__laminar_root_unmount = unmountFunction | |
} | |
if (`import`.meta.hot != null) { | |
println("HOT RELOADING ENABLED") | |
`import`.meta.hot.accept { (_: Any) => } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment