postMessageAndReceiveResult

suspend fun JavaScriptBridge.postMessageAndReceiveResult(type: String, timeout: Duration, vararg args: JSValue): JSValue

Dispatches a typed message to JavaScript and suspends until JavaScript returns one result.

This is the request/response form of postMessage. The function installs a temporary native response handler, dispatches type through window.wvbridge.dispatchEvent, and appends a JavaScript callback after args. The page must register its listener with window.wvbridge.addEventListener(type, listener) before native code calls this API; otherwise the dispatch has no listener to invoke and the call will wait until timeout. The JavaScript listener receives ...args, reply and should call reply(result) once.

The returned value is the callback argument normalized as JSValue. Only JSValue.Undefined, JSValue.Null, and JSValue.Serializable can be used as outgoing args because they must be representable as undefined, null, or a kotlinx.serialization.json.JsonElement in the page.

The temporary response handler is always unregistered before this function returns or throws. If JavaScript does not call the callback before timeout, this function throws kotlinx.coroutines.TimeoutCancellationException.

Return

the value passed by JavaScript to the response callback.

Parameters

type

application-level packet type to dispatch.

timeout

maximum time to wait for the JavaScript callback.

args

payload values delivered to the JavaScript listener before the response callback.