Peeper.GenServer behaviour (peeper v0.3.0)

View Source

A drop-in replacement for use GenServer

Summary

Functions

Declares a Peeper.GenServer behaviour, injects start_link/1 function and the child spec.

Starts a Peeper sub-supervision process tree linked to the current process.

Callbacks

code_change(old_vsn, state, extra)

(optional)
@callback code_change(old_vsn, state :: term(), extra :: term()) ::
  {:ok, new_state :: term()} | {:error, reason :: term()}
when old_vsn: term() | {:down, term()}

See: GenServer.code_change/3

handle_call(request, from, state)

(optional)
@callback handle_call(request :: term(), GenServer.from(), state :: term()) ::
  {:reply, reply, new_state}
  | {:reply, reply, new_state,
     timeout() | :hibernate | {:continue, continue_arg :: term()}}
  | {:noreply, new_state}
  | {:noreply, new_state,
     timeout() | :hibernate | {:continue, continue_arg :: term()}}
  | {:stop, reason, reply, new_state}
  | {:stop, reason, new_state}
when reply: term(), new_state: term(), reason: term()

See: GenServer.handle_call/3

handle_cast(request, state)

(optional)
@callback handle_cast(request :: term(), state :: term()) ::
  {:noreply, new_state}
  | {:noreply, new_state,
     timeout() | :hibernate | {:continue, continue_arg :: term()}}
  | {:stop, reason :: term(), new_state}
when new_state: term()

See: GenServer.handle_cast/2

handle_continue(continue_arg, state)

(optional)
@callback handle_continue(continue_arg, state :: term()) ::
  {:noreply, new_state}
  | {:noreply, new_state, timeout() | :hibernate | {:continue, continue_arg}}
  | {:stop, reason :: term(), new_state}
when new_state: term(), continue_arg: term()

See: GenServer.handle_continue/2

handle_info(msg, state)

(optional)
@callback handle_info(msg :: :timeout | term(), state :: term()) ::
  {:noreply, new_state}
  | {:noreply, new_state,
     timeout() | :hibernate | {:continue, continue_arg :: term()}}
  | {:stop, reason :: term(), new_state}
when new_state: term()

See: GenServer.handle_info/2

init(init_arg)

(optional)
@callback init(init_arg :: term()) :: {:ok, new_state} when new_state: term()

Almost the same as GenServer.init/1 but cannot return anything but {:ok, new_state} tuple and is being invoked from handle_continue/2 followed init/1 of the state keeper process.

See: GenServer.init/1

terminate(reason, state)

(optional)
@callback terminate(reason, state :: term()) :: term()
when reason: :normal | :shutdown | {:shutdown, term()} | term()

See: GenServer.terminate/2

Functions

__using__(opts \\ [])

(macro)

Declares a Peeper.GenServer behaviour, injects start_link/1 function and the child spec.

Example

defmodule MyGenServer do
  use Peeper.GenServer

  @impl Peeper.GenServer
  def handle_call(:state, _from, state),
    do: {:reply, state, state}

  @impl Peeper.GenServer
  def handle_cast(:inc, state),
    do: {:noreply, state, {:continue, :inc}}

  @impl Peeper.GenServer
  def handle_continue(:inc, state),
    do: {:noreply, state + 1}
  end

start_link(module, opts)

Starts a Peeper sub-supervision process tree linked to the current process.