Sha256: 0dd809fc8c3e81daab3c0436afccd3f34dbdcbdcb3665c859c94d0697d914969

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module EventEmitter
  def self.included(klass)
    klass.extend ClassMethods
    klass.__send__ :include, InstanceMethods
  end

  def self.apply(object)
    object.extend InstanceMethods
  end

  module ClassMethods
  end

  module InstanceMethods
    def events
      @events ||= []
    end

    def add_listener(type, params={}, &block)
      raise ArgumentError, 'listener block not given' unless block_given?
      id = events.empty? ? 0 : events.last[:id]+1
      events << {
        :type => type.to_sym,
        :listener => block,
        :params => params,
        :id => id
      }
      id
    end

    alias :on :add_listener

    def remove_listener(id_or_type)
      if id_or_type.class == Fixnum
        events.delete_if do |e|
          e[:id] == id_or_type
        end
      elsif [String, Symbol].include? id_or_type.class
        events.delete_if do |e|
          e[:type] == id_or_type.to_sym
        end
      end
    end

    def emit(type, data)
      events.each do |e|
        if e[:type] == type.to_sym
          listener = e[:listener]
          remove_listener e[:id] if e[:params][:once]
          instance_exec(data, &listener)
        end
      end
    end

    def once(type, &block)
      add_listener type, {:once => true}, &block
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
event_emitter-0.0.6 lib/event_emitter/emitter.rb
event_emitter-0.0.5 lib/event_emitter/emitter.rb
event_emitter-0.0.4 lib/event_emitter/emitter.rb