pub struct HostChannel { /* private fields */ }Expand description
The embedding app’s side of the channel with the apps one of its views
runs: launch parameters, events to the app (host.on in TSX), and
handlers of the app’s calls (host.call) and posts (host.post).
Cheap to clone (the clones are one channel); usable from any thread. One per view, shared by the apps it runs one after another.
use nsh_engine::{HostChannel, JsonValue};
let channel = HostChannel::new();
// What the next app reads synchronously at start (`host.params`).
let mut params = serde_json::Map::new();
params.insert("conversation".into(), JsonValue::from("c-42"));
channel.set_params(params);
// `await host.call("profile", { id })` in the app.
channel.handle("profile", |call| {
let id = call.data()["id"].clone();
call.resolve(serde_json::json!({ "id": id, "name": "Ada" }));
});
// `host.on("user.changed", ...)` in the app.
channel.send("user.changed", serde_json::json!({ "id": 7 })).unwrap();Implementations§
Source§impl HostChannel
impl HostChannel
Sourcepub fn set_params(&self, params: Map<String, JsonValue>)
pub fn set_params(&self, params: Map<String, JsonValue>)
The launch parameters of the apps started from now on
(host.params, read synchronously when they start; a running app
keeps the ones it started with). Later requests are events
(HostChannel::send).
Sourcepub fn params(&self) -> Map<String, JsonValue>
pub fn params(&self) -> Map<String, JsonValue>
The launch parameters (HostChannel::set_params).
Sourcepub fn send(
&self,
name: impl Into<String>,
data: JsonValue,
) -> Result<(), String>
pub fn send( &self, name: impl Into<String>, data: JsonValue, ) -> Result<(), String>
Sends event name with data to the running app (host.on(name, listener)). The app keeps events of names it does not listen to
yet until it does; while no app runs, events wait for the next one
(up to nsh_core::host_channel::MAX_QUEUED_EVENTS each, then the
oldest go). Any thread; it never waits for the app. Err for a name
that is empty or longer than
nsh_core::host_channel::MAX_NAME_LEN bytes.
Sourcepub fn handle(
&self,
name: impl Into<String>,
handler: impl Fn(HostCall) + Send + Sync + 'static,
)
pub fn handle( &self, name: impl Into<String>, handler: impl Fn(HostCall) + Send + Sync + 'static, )
Handles the app’s calls (host.call(name, data)) and posts
(host.post(name, data)) of name, replacing the handler it had.
handler runs on the engine thread and must return quickly: it
answers a call with HostCall::resolve / HostCall::reject at
once, or moves the call to where its answer comes from and answers
there, from any thread. A post’s answer goes nowhere. A call that
is dropped unanswered fails in the app.
Sourcepub fn remove_handler(&self, name: &str)
pub fn remove_handler(&self, name: &str)
Removes the handler of name: the app’s calls of it fail at once
from now on, its posts are dropped.
Sourcepub fn handle_open_url(
&self,
handler: impl Fn(OpenUrlRequest) + Send + Sync + 'static,
)
pub fn handle_open_url( &self, handler: impl Fn(OpenUrlRequest) + Send + Sync + 'static, )
Takes the URLs the app opens (openUrl of nsh/device, a
TextView’s links) before the system does, replacing the handler
it had: a host routes them through its own router. The URL passed
the URL check and the app’s openUrl capability
(nsh_core::open_url). handler runs on the engine thread and
must return quickly: it answers with OpenUrlRequest::handled
(the app’s openUrl resolves true) or
OpenUrlRequest::decline (the system opens it), at once or from
any thread later. A request dropped unanswered is declined.
Sourcepub fn remove_open_url_handler(&self)
pub fn remove_open_url_handler(&self)
Removes the URL handler: the system opens the app’s URLs from now on.