Skip to main content

HostExtension

Trait HostExtension 

Source
pub trait HostExtension:
    Send
    + Sync
    + 'static {
    // Required methods
    fn namespace(&self) -> &str;
    fn channel(&self) -> &str;
    fn inject_script(&self) -> &str;
    fn handle_message(&self, method: &str, params: &str) -> Option<String>;

    // Provided method
    fn drain_events(&self) -> Vec<HostPushEvent> { ... }
}
Expand description

A host extension contributes a JS injection script and a message handler.

Extensions register as window.host.ext.<namespace> in the SPA WebView. Each extension gets its own WKScriptMessageHandler channel, its own pending-promise map, and its own resolve/push callbacks — fully independent of the core window.host bridge.

Required Methods§

Source

fn namespace(&self) -> &str

Sub-namespace under window.host.ext.* (e.g. “data” → window.host.ext.data)

Source

fn channel(&self) -> &str

WKScriptMessageHandler channel name (e.g. “hostDataBridge”)

Source

fn inject_script(&self) -> &str

JavaScript injected after HOST_API_SCRIPT. Must:

  1. Define its own _call function using its own message handler channel
  2. Define window.__host{Name}Resolve for response resolution
  3. Define window.__host{Name}Push for push events
  4. Set window.host.ext.<namespace> = Object.freeze({...})
Source

fn handle_message(&self, method: &str, params: &str) -> Option<String>

Handle a message from the JS bridge. method is the method name from the JS _call. params is the JSON params object as a string. Returns a serialized JSON result, or None for fire-and-forget.

Provided Methods§

Source

fn drain_events(&self) -> Vec<HostPushEvent>

Drain queued push events destined for window.host.on(...) listeners.

The returned payload is the canonical JSON string for the event’s shared Rust/TypeScript payload shape.

Implementors§