GenServer.Proxy behaviour (GenServer Proxy v0.1.26) View Source

Invokes call, cast or stop in GenServer with a registered server. Will wait a bit if the server is not yet registered on restarts.

Link to this section Summary

Functions

Either aliases GenServer.Proxy (this module) and requires the alias or imports GenServer.Proxy. In the latter case, you could instead simply import GenServer.Proxy.

Makes a synchronous call to the registered server identified by server_id. Will wait a bit if the server is not yet registered on restarts.

Sends an async request to the registered server identified by server_id. Will wait a bit if the server is not yet registered on restarts.

Synchronously stops the registered server identified by server_id. Will wait a bit if the server is not yet registered on restarts.

Callbacks

Converts server_id into a server name like a via tuple.

Called when the server remains unregistered despite waiting a bit.

Link to this section Functions

Link to this macro

__using__(options)

View Source (macro)

Either aliases GenServer.Proxy (this module) and requires the alias or imports GenServer.Proxy. In the latter case, you could instead simply import GenServer.Proxy.

Examples

use GenServer.Proxy, alias: Proxy

use GenServer.Proxy

import GenServer.Proxy
Link to this macro

call(request, server_id, module \\ nil)

View Source (macro)

Makes a synchronous call to the registered server identified by server_id. Will wait a bit if the server is not yet registered on restarts.

The given module (or by default <caller's_module>.GenServerProxy) must implement the 2 callbacks of GenServer.Proxy (this module).

Examples

# Assuming the following callback module:

defmodule Game.Engine.GenServerProxy do
  @behaviour GenServer.Proxy

  @impl GenServer.Proxy
  def server_name(game_name),
    do: {:via, Registry, {:registry, game_name}}

  @impl GenServer.Proxy
  def server_unregistered(game_name),
    do: IO.puts("Game #{game_name} not started.")
end

# We could use the call macro like so:

defmodule Game.Engine do
  use GenServer.Proxy

  def summary(game_name), do: call(:summary, game_name)
  ...
end
Link to this macro

cast(request, server_id, module \\ nil)

View Source (macro)

Sends an async request to the registered server identified by server_id. Will wait a bit if the server is not yet registered on restarts.

The given module (or by default <caller's_module>.GenServerProxy) must implement the 2 callbacks of GenServer.Proxy (this module).

Link to this macro

stop(reason, server_id, module \\ nil)

View Source (macro)

Synchronously stops the registered server identified by server_id. Will wait a bit if the server is not yet registered on restarts.

The given module (or by default <caller's_module>.GenServerProxy) must implement the 2 callbacks of GenServer.Proxy (this module).

Link to this section Callbacks

Specs

server_name(server_id :: term()) :: GenServer.name()

Converts server_id into a server name like a via tuple.

Examples

@impl GenServer.Proxy
def server_name(game_name),
  do: {:via, Registry, {:registry, game_name}}

@impl GenServer.Proxy
def server_name(game_name),
  do: {:global, {GameServer, game_name}}
Link to this callback

server_unregistered(server_id)

View Source

Specs

server_unregistered(server_id :: term()) :: term()

Called when the server remains unregistered despite waiting a bit.

Examples

@impl GenServer.Proxy
def server_unregistered(game_name),
  do: IO.puts("Game #{game_name} not started.")