package xyz.dynamic.embeddedwebview import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition import java.util.UUID class EmbeddedWebViewModule : Module() { private var emitterToken: UUID? = null override fun definition() = ModuleDefinition { Name("EmbeddedWebView") Events( "onMessage", "onShouldStartLoad", "onLoadError", "onLoadStart", "onLoad", "onLoadEnd", ) OnCreate { emitterToken = EmbeddedWebViewController.attach(appContext) { name, payload -> sendEvent(name, payload) } } OnDestroy { // Only clear if our emitter is still the live one. Prevents a stale // OnDestroy (e.g. during dev hot reload after a newer OnCreate already // ran) from nulling the active emitter. emitterToken?.let { token -> EmbeddedWebViewController.detach(token) emitterToken = null } } AsyncFunction("setUrl") { url: String -> EmbeddedWebViewController.setUrl(url) } AsyncFunction("setVisible") { visible: Boolean -> EmbeddedWebViewController.setVisible(visible) } AsyncFunction("setDebuggingEnabled") { enabled: Boolean -> EmbeddedWebViewController.setDebuggingEnabled(enabled) } AsyncFunction("destroy") { EmbeddedWebViewController.destroy() } AsyncFunction("postMessage") { message: String -> EmbeddedWebViewController.postMessage(message) } AsyncFunction("respondToShouldStartLoad") { id: String, allow: Boolean -> EmbeddedWebViewController.respondToShouldStartLoad(id, allow) } } }