Skip to main content

HostChannel

Struct HostChannel 

Source
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

Source

pub fn new() -> Self

A channel with no parameters and no handlers.

Source

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).

Source

pub fn params(&self) -> Map<String, JsonValue>

The launch parameters (HostChannel::set_params).

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn remove_open_url_handler(&self)

Removes the URL handler: the system opens the app’s URLs from now on.

Trait Implementations§

Source§

impl Clone for HostChannel

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HostChannel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HostChannel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ParallelSend for T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more