Module: Elixir::Agent
- Defined in:
- lib/elixir/agent.rb
Class Method Summary collapse
- .cast(agent, &fun) ⇒ Object
- .get(agent, &fun) ⇒ Object
- .get_and_update(agent, &fun) ⇒ Object
- .start(&fun) ⇒ Object
- .update(agent, &fun) ⇒ Object
Class Method Details
.cast(agent, &fun) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/elixir/agent.rb', line 7 def cast agent, &fun agent.try_update do |value| ->{ fun.call value.call } end :ok end |
.get(agent, &fun) ⇒ Object
15 16 17 |
# File 'lib/elixir/agent.rb', line 15 def get agent, &fun fun.call agent.get.call end |
.get_and_update(agent, &fun) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/elixir/agent.rb', line 19 def get_and_update agent, &fun old_lambda = agent.get old_value = old_lambda.call new_lambda = ->{ fun.call old_value } agent.compare_and_set old_lambda, new_lambda old_value end |
.start(&fun) ⇒ Object
28 29 30 |
# File 'lib/elixir/agent.rb', line 28 def start &fun [:ok, Concurrent::Atomic.new(fun)] end |
.update(agent, &fun) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/elixir/agent.rb', line 32 def update agent, &fun agent.update do |value| ->{ fun.call value.call } end :ok end |