# Trait `kurogane::StreamHandler`

```rust
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`](https://kurogane-rs.org/docs/api/app/app.md#method.stream) makes a
handler for each stream. In order:

1. [`on_open`](https://kurogane-rs.org/docs/api/ipc/streamhandler.md#method.on_open) accepts or refuses the stream. Nothing can
   be sent yet since the page does not hold the stream.
2. The runtime tells the page the stream is open, which resolves its
   `openStream`, then calls [`on_opened`](https://kurogane-rs.org/docs/api/ipc/streamhandler.md#method.on_opened).
3. [`on_chunk`](https://kurogane-rs.org/docs/api/ipc/streamhandler.md#tymethod.on_chunk) runs for each chunk the page writes.
4. [`on_end`](https://kurogane-rs.org/docs/api/ipc/streamhandler.md#method.on_end) runs when the page ends the stream.

The first [`end`](https://kurogane-rs.org/docs/api/ipc/streamresponder.md#method.end) or
[`error`](https://kurogane-rs.org/docs/api/ipc/streamresponder.md#method.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:

```rust
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();
```

[Self::on_end]: https://kurogane-rs.org/docs/api/ipc/streamhandler.md#method.on_end
[Self::on_opened]: https://kurogane-rs.org/docs/api/ipc/streamhandler.md#method.on_opened
[crate::App::stream]: https://kurogane-rs.org/docs/api/app/app.md#method.stream
[StreamResponder::end]: https://kurogane-rs.org/docs/api/ipc/streamresponder.md#method.end
[StreamResponder::error]: https://kurogane-rs.org/docs/api/ipc/streamresponder.md#method.error
[Self::on_open]: https://kurogane-rs.org/docs/api/ipc/streamhandler.md#method.on_open
[Self::on_chunk]: https://kurogane-rs.org/docs/api/ipc/streamhandler.md#tymethod.on_chunk

## Required methods

### `on_chunk`

```rust
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

### `on_open`

```rust
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.

### `on_opened`

```rust
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.

### `on_end`

```rust
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.

### `on_error`

```rust
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.

Source: https://github.com/0x48piraj/kurogane/blob/584c586118ee53cc04604007ee038557bd859092/kurogane/src/ipc/stream/mod.rs#L219
