Hi Arkadii. Is there any example of how to set up ...
# decompose
x
Hi Arkadii. Is there any example of how to set up the lifecycle registry on the new wasmJs targets? Having trouble hooking up
Document.visibilityState
as we don't have
asDynamic
over at the wasm targets 😞
Copy code
// jsMain

// Attaches the LifecycleRegistry to the document
private fun LifecycleRegistry.attachToDocument() {
  fun onVisibilityChanged() = if (document.visibilityState == "visible") resume() else stop()
  onVisibilityChanged()
  document.addEventListener(type = "visibilitychange", callback = { onVisibilityChanged() })
}

private val Document.visibilityState: String
  get() = asDynamic().visibilityState.unsafeCast<String>()
I was able to migrate this over kotlinx-wrappers on js targets so that I can use
web.dom.document
as outlined in the quick-start guide.
Copy code
import kotlinx.browser.document
import web.dom.DocumentVisibilityState
import web.dom.document as webDocument

// Attaches the LifecycleRegistry to the document
private fun LifecycleRegistry.attachToDocument() {
  fun onVisibilityChanged() =
    if (webDocument.visibilityState == DocumentVisibilityState.visible) resume()
    else stop()

  onVisibilityChanged()

  document.addEventListener("visibilitychange", callback = { onVisibilityChanged() })
}
But can't really do this for wasmJS 🤔
a
Yeah, not all bindings are available as of now, it needs some manual interop work. Check it out here: https://github.com/arkivanov/Minesweeper/blob/8270ffb0c75bf032b6d4da673c0bb2b01c9496ec/composeApp/src/wasmJsMain/kotlin/Main.kt#L47
🙌 1
thank you color 1
x
Awesome - thank you very much
👍 1