Sha256: 5f85425804f6bf471d9832b5191011c989e3295f4e0acfda9c9eec4fca9741fb

Contents?: true

Size: 976 Bytes

Versions: 9

Compression:

Stored size: 976 Bytes

Contents

module Mittsu
  module EventDispatcher
    Event = Struct.new(:type, :target)

    def add_event_listener(type, listener)
      @_listeners ||= {}
      @_listeners[type] ||= []
      if !@_listeners[type].include? listener
        @_listeners[type] << (listener)
      end
    end

    def has_event_listener(type, listener)
      return false if @_listeners.nil?
      return false if @_listeners[type].nil?
      @_listeners[type].include? listener
    end

    def remove_event_listener(type, listener)
      return if @_listeners.nil?
      listener_array = @_listeners[type]
      if listener_array
        listener_array.delete(listener)
      end
    end

    def dispatch_event(event = {})
      return if @_listeners.nil?
      listener_array = @_listeners[event.type]
      if listener_array
        evt = Event.new(event[:type], self)
        array = listener_array.dup
        array.each do |l|
          l.call(self, evt)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mittsu-0.2.0 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.7 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.6 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.5 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.4 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.3 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.2 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.1 lib/mittsu/core/event_dispatcher.rb
mittsu-0.1.0 lib/mittsu/core/event_dispatcher.rb