Sha256: 2430f2367e36c6d0d71635e253a52b3cc93e622f975ac040117430a63910589e

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

module Faye::WebSocket::API
  module EventTarget

    include ::WebSocket::Driver::EventEmitter
    events = %w[open message error close]

    events.each do |event_type|
      define_method "on#{event_type}=" do |handler|
        EventMachine.next_tick do
          flush(event_type, handler)
          instance_variable_set("@on#{event_type}", handler)
        end
      end
    end

    def add_event_listener(event_type, listener, use_capture = false)
      add_listener(event_type, &listener)
    end

    def add_listener(event_type, callable = nil, &block)
      listener = callable || block
      EventMachine.next_tick do
        flush(event_type, listener)
        super(event_type, &listener)
      end
    end

    def remove_event_listener(event_type, listener, use_capture = false)
      remove_listener(event_type, &listener)
    end

    def dispatch_event(event)
      event.target = event.current_target = self
      event.event_phase = Event::AT_TARGET

      listener = instance_variable_get("@on#{ event.type }")
      count    = listener_count(event.type)

      unless listener or count > 0
        event_buffers[event.type].push(event)
      end

      listener.call(event) if listener
      emit(event.type, event)
    end

  private

    def flush(event_type, listener)
      if buffer = event_buffers.delete(event_type.to_s)
        buffer.each { |event| listener.call(event) }
      end
    end

    def event_buffers
      @event_buffers ||= Hash.new { |k,v| k[v] = [] }
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faye-websocket-0.10.8 lib/faye/websocket/api/event_target.rb
faye-websocket-0.10.7 lib/faye/websocket/api/event_target.rb
faye-websocket-0.10.6 lib/faye/websocket/api/event_target.rb
faye-websocket-0.10.5 lib/faye/websocket/api/event_target.rb
faye-websocket-0.10.4 lib/faye/websocket/api/event_target.rb