module WebSocket::Driver::EventEmitter - websocket-driver-0.6.5 Documentation

# module WebSocket::Driver::EventEmitter

### Public Class Methods

new() click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 5
    def initialize
      @listeners = Hash.new { |h,k| h[k] = [] }
    end

### Public Instance Methods

add_listener(event, callable = nil, &block) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 9
    def add_listener(event, callable = nil, &block)
      listener = callable || block
      @listeners[event.to_s] << listener
      listener
    end

emit(event, *args) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 37
    def emit(event, *args)
      @listeners[event.to_s].dup.each do |listener|
        listener.call(*args)
      end
    end

listener_count(event) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 43
    def listener_count(event)
      return 0 unless @listeners.has_key?(event.to_s)
      @listeners[event.to_s].size
    end

listeners(event) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 48
    def listeners(event)
      @listeners[event.to_s]
    end

on(event, callable = nil, &block) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 15
    def on(event, callable = nil, &block)
      if callable
        add_listener(event, callable)
      else
        add_listener(event, &block)
      end
    end

remove_all_listeners(event = nil) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 29
    def remove_all_listeners(event = nil)
      if event
        @listeners.delete(event.to_s)
      else
        @listeners.clear
      end
    end

remove_listener(event, callable = nil, &block) click to toggle source

    # File lib/websocket/driver/event_emitter.rb, line 23
    def remove_listener(event, callable = nil, &block)
      listener = callable || block
      @listeners[event.to_s].delete(listener)
      listener
    end