module Elixir::Agent

Public Instance Methods

cast(agent, &fun) click to toggle source
# 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) click to toggle source
# File lib/elixir/agent.rb, line 15
def get agent, &fun
  fun.call agent.get.call
end
get_and_update(agent, &fun) click to toggle source
# 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) click to toggle source
# File lib/elixir/agent.rb, line 28
def start &fun
  [:ok, Concurrent::Atomic.new(fun)]
end
update(agent, &fun) click to toggle source
# File lib/elixir/agent.rb, line 32
def update agent, &fun
  agent.update do |value|
    ->{ fun.call value.call }
  end

  :ok
end