Sha256: d9ea5b83b67895cc4bd2f5d63beed46aa698f9e82b9efda60d29ac7bc9335592

Contents?: true

Size: 941 Bytes

Versions: 19

Compression:

Stored size: 941 Bytes

Contents

module WebSocket
  class Driver

    module EventEmitter
      def initialize
        @listeners = Hash.new { |h,k| h[k] = [] }
      end

      def add_listener(event, &listener)
        @listeners[event.to_s] << listener
      end

      def on(event, &listener)
        add_listener(event, &listener)
      end

      def remove_listener(event, &listener)
        @listeners[event.to_s].delete(listener)
      end

      def remove_all_listeners(event = nil)
        if event
          @listeners.delete(event.to_s)
        else
          @listeners.clear
        end
      end

      def emit(event, *args)
        @listeners[event.to_s].dup.each do |listener|
          listener.call(*args)
        end
      end

      def listener_count(event)
        return 0 unless @listeners.has_key?(event.to_s)
        @listeners[event.to_s].size
      end

      def listeners(event)
        @listeners[event.to_s]
      end
    end

  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/websocket-driver-0.5.4/lib/websocket/driver/event_emitter.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/websocket-driver-0.5.4/lib/websocket/driver/event_emitter.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/websocket-driver-0.5.4/lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.4-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.4 lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.3-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.3 lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.2-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.2 lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.1-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.1 lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.0 lib/websocket/driver/event_emitter.rb
websocket-driver-0.5.0-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.4.0-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.4.0 lib/websocket/driver/event_emitter.rb
websocket-driver-0.3.5 lib/websocket/driver/event_emitter.rb
websocket-driver-0.3.5-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.3.4-java lib/websocket/driver/event_emitter.rb
websocket-driver-0.3.4 lib/websocket/driver/event_emitter.rb