commonMain.com.multiplatform.webview.jsbridge.JsMessageDispatcher.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compose-webview-multiplatform Show documentation
Show all versions of compose-webview-multiplatform Show documentation
WebView for JetBrains Compose Multiplatform
package com.multiplatform.webview.jsbridge
import androidx.compose.runtime.Immutable
import com.multiplatform.webview.web.WebViewNavigator
/**
* Created By Kevin Zou On 2023/10/31
*/
/**
* A message dispatched from JS to native.
*/
@Immutable
internal class JsMessageDispatcher {
private val jsHandlerMap = mutableMapOf()
fun registerJSHandler(handler: IJsMessageHandler) {
jsHandlerMap[handler.methodName()] = handler
}
fun dispatch(
message: JsMessage,
navigator: WebViewNavigator? = null,
callback: (String) -> Unit,
) {
jsHandlerMap[message.methodName]?.handle(message, navigator, callback)
}
fun canHandle(id: String) = jsHandlerMap.containsKey(id)
fun unregisterJSHandler(handler: IJsMessageHandler) {
jsHandlerMap.remove(handler.methodName())
}
fun clear() {
jsHandlerMap.clear()
}
}