Skip to content

Instantly share code, notes, and snippets.

@aharpole
Created March 9, 2019 00:18
Show Gist options
  • Save aharpole/2cec1c866c082375ec09b11de9bdc8c0 to your computer and use it in GitHub Desktop.
Save aharpole/2cec1c866c082375ec09b11de9bdc8c0 to your computer and use it in GitHub Desktop.
proposed genserver with new API
defmodule IncrementableValue do
use StateMapGenServer
def start_link(config \\ []) do
config = Keyword.merge([increment_by: 1], config)
GenServer.start_link(__MODULE__, config, [])
end
def init(config) do
{:ok, %{value: 1}, config}
end
def handle_call(:get_data, _from, %{value: value}, _config) do
{:reply, value} #no need to reply with a new state since nothing is changing
end
def handle_cast(:increment, %{value: value}, %{increment_by: increment_by}) do
{:noreply, %{value: value + increment_by}}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment