Plushie.Transport behaviour (Plushie v0.7.1)

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:

Summary

Callbacks

Close the transport. Called during terminate.

Handle an incoming message that may belong to this transport.

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

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

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

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

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.