Trait
StreamHandler
pub trait StreamHandler: Send + 'static {
fn on_open(&mut self, metadata: &str) -> Result<(), IpcError> { ... }
fn on_opened(&mut self, responder: &StreamResponder) -> Result<(), IpcError> { ... }
fn on_chunk(
&mut self,
data: &[u8],
responder: &StreamResponder,
) -> Result<(), IpcError>;
fn on_end(
&mut self,
result: &str,
responder: StreamResponder,
) -> Result<(), IpcError> { ... }
fn on_error(&mut self, message: &str) { ... }
}Handles one stream a page opens.
The factory registered with App::stream makes a
handler for each stream. In order:
on_openaccepts or refuses the stream. Nothing can be sent yet since the page does not hold the stream.- The runtime tells the page the stream is open, which resolves its
openStream, then callson_opened. on_chunkruns for each chunk the page writes.on_endruns when the page ends the stream.
The first end or
error sent closes the stream, whichever clone
of the responder sends it; later sends fail with stream closed.
An Err from on_open rejects the open with the error’s message and
code, as a command’s error does; an Err from on_opened, on_chunk or
on_end fails the stream, whose onError receives the message, unless it
is already closed. A panic in any of them counts as an Err with the
message handler panicked. The page’s end is always answered. When
on_end sends neither the runtime ends the stream with an empty result.
The runtime drops the handler once the stream is closed. A stream closed from another thread is noticed at the next stream open, or when its document goes.
Threads
Every callback runs on the UI thread. That thread also runs every window and receives every message from the page. Return promptly; CEF says not to block this thread. The responder may be cloned and used from any thread.
Examples
A handler that greets the page, then counts the bytes the page writes:
use kurogane::{App, IpcError, StreamHandler, StreamResponder};
#[derive(Default)]
struct Counter {
bytes: usize,
}
impl StreamHandler for Counter {
fn on_opened(&mut self, responder: &StreamResponder) -> Result<(), IpcError> {
responder.send_data(b"ready")
}
fn on_chunk(&mut self, data: &[u8], _: &StreamResponder) -> Result<(), IpcError> {
self.bytes += data.len();
Ok(())
}
fn on_end(&mut self, _: &str, responder: StreamResponder) -> Result<(), IpcError> {
responder.end(&self.bytes.to_string())
}
}
App::new("frontend").stream("count", Counter::default).run_or_exit();
Required methods
#fn on_chunk(
&mut self,
data: &[u8],
responder: &StreamResponder,
) -> Result<(), IpcError>
Called for each chunk the page writes. An Err fails the stream.
Provided methods
#fn on_open(&mut self, metadata: &str) -> Result<(), IpcError>
Accepts or refuses the stream. metadata is the second argument of
the page’s openStream. An Err rejects the openStream with its
message and code.
#fn on_opened(&mut self, responder: &StreamResponder) -> Result<(), IpcError>
Called once the page has been told the stream is open. It is the place
to send initial data, to end or fail the stream or to hand a clone of
responder to a thread that produces data. An Err fails the stream.
#fn on_end(
&mut self,
result: &str,
responder: StreamResponder,
) -> Result<(), IpcError>
Called when the page ends the stream with result. Once it returns
Ok, the runtime ends the stream with an empty result unless the
handler has ended or failed it; an Err fails the stream instead.
#fn on_error(&mut self, message: &str)
Called when the renderer reports that the stream failed; not for a failure the handler or the runtime sends.