Sha256: 24675d1255433128a27ef808083a47a24cc7505fd39172f19ed741f2e6bda7e2

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 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.is_a? Integer
        __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)
      type = type.to_sym
      __events.each do |e|
        case e[:type]
        when type
          listener = e[:listener]
          e[:type] = nil if e[:params][:once]
          instance_exec(*data, &listener)
        when :*
          listener = e[:listener]
          e[:type] = nil if e[:params][:once]
          instance_exec(type, *data, &listener)
        end
      end
      __events.each do |e|
        remove_listener e[:id] unless e[:type]
      end
    end

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

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
event_emitter-0.2.6 lib/event_emitter/emitter.rb