This file contains hidden or 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
GenServer.update_config(pid, fn config -> Keyword.update(config, :increment_by, 2) end) |
This file contains hidden or 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
def handle_cast(:increment, %{value: value, increment_by: increment_by}) do | |
{:ok, %{value: value + increment_by}} | |
end |
This file contains hidden or 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 GenServer | |
defstruct [:value, :increment_by] | |
@type t :: %__MODULE__{ | |
value: integer, | |
increment_by: integer | |
} |
This file contains hidden or 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
def handle_cast(:increment, state) do | |
new_state = Map.put(state, :value, &(&1 + state.increment_by)) | |
{:noreply, state} | |
end |
This file contains hidden or 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
def handle_call(:get_data, _, %{value: value} = state) do | |
{:reply, value, state} | |
end |
This file contains hidden or 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
def init(%{increment_by: increment_by}) do | |
{:ok, %__MODULE__{value: 1, increment_by: increment_by}} | |
end |
This file contains hidden or 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
def start_link(opts) do | |
{state_opts, opts} = Keyword.split(opts, [:increment_by]) | |
state_opts = Keyword.merge([increment_by: 1], state_opts) #default to incrementing by 1 still | |
GenServer.start_link(__MODULE__, state_opts, opts) | |
end |
This file contains hidden or 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
@type t :: %__MODULE__{ | |
value: integer, | |
increment_by: integer | |
} |
This file contains hidden or 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
defstruct [:value, :increment_by] |
This file contains hidden or 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 GenServer | |
def start_link() do | |
GenServer.start_link(__MODULE__, []) | |
end | |
def init(_) do | |
{:ok, 1} | |
end |