Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shun159/gen_batch_server_ex

wrapper for rabbitmq/gen-batch-server
https://github.com/shun159/gen_batch_server_ex

Last synced: 9 days ago
JSON representation

wrapper for rabbitmq/gen-batch-server

Awesome Lists containing this project

README

        

# GenBatchServer

Elixir wrapper of `rabbitmq/gen-batch-server`

usage:

```elixir
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
```