Sha256: 7f3fc0405152512d2d2de677afe6fa5e38fc080357b9d4a18f7ed7a428714ef0

Contents?: true

Size: 1.36 KB

Versions: 25

Compression:

Stored size: 1.36 KB

Contents

require 'celluloid'

class Eye::Utils::CelluloidChain
  include Celluloid

  def initialize(target)
    @target = target
    @calls = []
    @running = false
    @target_class = @target.class
  end

  def add(method_name, *args, &block)
    @calls << {:method_name => method_name, :args => args, :block => block}
    ensure_process
  end

  def add_wo_dups(method_name, *args, &block)
    h = {:method_name => method_name, :args => args, :block => block}
    if @calls[-1] != h
      @calls << h
      ensure_process
    end
  end

  def add_wo_dups_current(method_name, *args, &block)
    h = {:method_name => method_name, :args => args, :block => block}
    if !@calls.include?(h) && @call != h
      @calls << h
      ensure_process
    end
  end

  def list
    @calls
  end

  def names_list
    list.map{|el| el[:method_name].to_sym }
  end

  def clear
    @calls = []
  end

  alias :clear_pending_list :clear

  # need, because of https://github.com/celluloid/celluloid/issues/22
  def inspect
    "Celluloid::Chain(#{@target_class}: #{@calls.size})"
  end

  attr_reader :running

private

  def ensure_process
    unless @running
      @running = true
      async.process
    end
  end

  def process
    while @call = @calls.shift
      @running = true
      @target.send(@call[:method_name], *@call[:args], &@call[:block]) if @target.alive?
    end
    @running = false
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
eye-0.8.pre2 lib/eye/utils/celluloid_chain.rb
eye-0.8.pre lib/eye/utils/celluloid_chain.rb
eye-0.7 lib/eye/utils/celluloid_chain.rb
eye-0.7.pre lib/eye/utils/celluloid_chain.rb
eye-0.6.4 lib/eye/utils/celluloid_chain.rb
eye-0.6.3 lib/eye/utils/celluloid_chain.rb
eye-0.6.2 lib/eye/utils/celluloid_chain.rb
eye-0.6.2.pre lib/eye/utils/celluloid_chain.rb
ace-eye-0.6.5 lib/eye/utils/celluloid_chain.rb
ace-eye-0.6.4 lib/eye/utils/celluloid_chain.rb
eye-0.6.1 lib/eye/utils/celluloid_chain.rb
ace-eye-0.6.3 lib/eye/utils/celluloid_chain.rb
ace-eye-0.6.2 lib/eye/utils/celluloid_chain.rb
ace-eye-0.6.1 lib/eye/utils/celluloid_chain.rb
eye-0.6 lib/eye/utils/celluloid_chain.rb
reel-eye-0.5.2.1 lib/eye/utils/celluloid_chain.rb
reel-eye-0.5.2 lib/eye/utils/celluloid_chain.rb
eye-0.5.2 lib/eye/utils/celluloid_chain.rb
reel-eye-0.5.1 lib/eye/utils/celluloid_chain.rb
eye-0.5.1 lib/eye/utils/celluloid_chain.rb