Agent.update
update
, go back to Agent module for more information.
Specs
Updates the agent state via the given anonymous function.
The function fun
is sent to the agent
which invokes the function
passing the agent state. The return value of fun
becomes the new
state of the agent.
This function always returns :ok
.
timeout
is an integer greater than zero which specifies how many
milliseconds are allowed before the agent executes the function and returns
the result value, or the atom :infinity
to wait indefinitely. If no result
is received within the specified time, the function call fails and the caller
exits.
Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.update(pid, fn state -> state + 1 end)
:ok
iex> Agent.get(pid, fn state -> state end)
43
Specs
Updates the agent state via the given function.
Same as update/3
but a module, function, and arguments are expected
instead of an anonymous function. The state is added as first
argument to the given list of arguments.
Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.update(pid, Kernel, :+, [12])
:ok
iex> Agent.get(pid, fn state -> state end)
54