Skip to content

shun159/gen_batch_server_ex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GenBatchServer

Elixir wrapper of rabbitmq/gen-batch-server

usage:

defmodule Stack do
  @moduledoc """
  Example for a very simple-minded Stack server
  """

  use GenBatchServer

  def push(element) do
    GenBatchServer.cast(__MODULE__, {:push, element})
  end

  def pop do
    GenBatchServer.call(__MODULE__, :pop)
  end

  @spec start_link() :: GenBatchServer.on_start()
  def start_link do
    GenBatchServer.start_link(__MODULE__, [:hello], name: __MODULE__)
  end

  @impl GenBatchServer
  def init(stack) do
    {:ok, stack}
  end

  @impl GenBatchServer
  def handle_batch(commands, state) do
    handle_commands([], commands, state)
  end

  # private functions

  defp handle_commands(actions, [], state),
    do: {:ok, actions, state}

  defp handle_commands(actions, [command | rest], state) do
    case command do
      {:cast, {:push, element}} ->
        handle_commands(actions, rest, [element | state])

      {:call, from, :pop} ->
        action = {:reply, from, hd(state)}
        handle_commands([action | actions], rest, tl(state))
    end
  end
end

About

wrapper for rabbitmq/gen-batch-server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages