Created
March 9, 2019 00:18
-
-
Save aharpole/2cec1c866c082375ec09b11de9bdc8c0 to your computer and use it in GitHub Desktop.
proposed genserver with new API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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