Search documentation of Plushie

# Plushie.Transport behaviour (Plushie v0.7.2)

Copy Markdown View Source

Behaviour for renderer transport backends.

A transport handles the low-level I/O between the bridge and the renderer process: opening the connection, sending data, receiving incoming messages, and closing the connection.

Two implementations are provided:

- `Plushie.Transport.Port` handles `:spawn` (child process via Erlang Port) and `:stdio` (BEAM's own stdin/stdout) modes.
- `Plushie.Transport.IOStream` handles `{:iostream, pid}` mode for custom transports (SSH, TCP, WebSocket adapters).

## Summary

### Types

t()

### Callbacks

close(state)

Close the transport. Called during terminate.

handle_info(message, state)

Handle an incoming message that may belong to this transport.

init(opts)

Initialize the transport. Returns `{:ok, state}` on success.

reopen(state)

Re-open the transport connection (e.g. after crash or dev rebuild).

restartable?(state)

Whether the transport supports restart (e.g. re-spawning the binary).

send_data(state, data)

Send iodata to the renderer. Returns `{:ok, state}` on success.

transport_ready?(state)

Whether the transport is ready to accept outgoing data.

## Types

# t()

    @type t() :: struct()

## Callbacks

# close(state)

    @callback close(state :: t()) :: :ok

Close the transport. Called during terminate.

# handle_info(message, state)

    @callback handle_info(message :: term(), state :: t()) ::
      {:data, binary(), t()} | {:closed, term(), t()} | :ignore

Handle an incoming message that may belong to this transport.

Returns:

- `{:data, binary, state}` when the message contains renderer data
- `{:closed, reason, state}` when the transport has closed
- `:ignore` when the message is not relevant to this transport

# init(opts)

    @callback init(opts :: keyword()) :: {:ok, t()} | {:error, term()}

Initialize the transport. Returns `{:ok, state}` on success.

Called once during bridge init to open the connection.

# reopen(state)

(optional)

    @callback reopen(state :: t()) :: {:ok, t()} | {:error, term()}

Re-open the transport connection (e.g. after crash or dev rebuild).

Only meaningful for transports where `restartable?/1` returns `true`. Returns `{:ok, new_state}` or `{:error, reason}`.

# restartable?(state)

    @callback restartable?(state :: t()) :: boolean()

Whether the transport supports restart (e.g. re-spawning the binary).

Only `:spawn` mode supports this. Stdio and iostream transports cannot be restarted because they don't own the renderer lifecycle.

# send_data(state, data)

    @callback send_data(state :: t(), data :: iodata()) :: {:ok, t()} | {:error, term()}

Send iodata to the renderer. Returns `{:ok, state}` on success.

# transport_ready?(state)

    @callback transport_ready?(state :: t()) :: boolean()

Whether the transport is ready to accept outgoing data.