LiveStash.Adapter behaviour (LiveStash v0.1.2)

Copy Markdown View Source

Behaviour for storage backends used by LiveStash.

An adapter is responsible for persisting and restoring selected LiveView assigns for a specific socket identity. LiveStash delegates all storage operations to the adapter chosen during initialization.

Summary

Callbacks

Returns a child specification when the adapter needs supervision.

Initializes adapter state for the given socket.

Attempts to restore previously persisted state to the socket.

Removes stored state associated with the socket and returns it.

Persists selected assign keys for the given socket.

Types

recovery_status()

@type recovery_status() :: :recovered | :not_found | :new | :error

Callbacks

child_spec(args)

(optional)
@callback child_spec(args :: any()) :: Supervisor.child_spec()

Returns a child specification when the adapter needs supervision.

init_stash(socket, session, opts)

@callback init_stash(
  socket :: Phoenix.LiveView.Socket.t(),
  session :: Keyword.t(),
  opts :: Keyword.t()
) ::
  Phoenix.LiveView.Socket.t()

Initializes adapter state for the given socket.

Called during LiveView mount through LiveStash.on_mount/4. Receives session data and adapter options and must return an updated socket.

recover_state(socket)

@callback recover_state(socket :: Phoenix.LiveView.Socket.t()) ::
  {recovery_status(), Phoenix.LiveView.Socket.t()}

Attempts to restore previously persisted state to the socket.

Must return {status, socket} where status is one of recovery_status/0.

reset_stash(socket)

@callback reset_stash(socket :: Phoenix.LiveView.Socket.t()) ::
  Phoenix.LiveView.Socket.t()

Removes stored state associated with the socket and returns it.

stash_assigns(socket, keys)

@callback stash_assigns(socket :: Phoenix.LiveView.Socket.t(), keys :: [atom()]) ::
  Phoenix.LiveView.Socket.t()

Persists selected assign keys for the given socket.

The keys are atoms that reference entries in socket.assigns. Returns an updated socket.