Module: Faye::Publisher

Included in:
Channel, Client, Engine::Proxy, Transport
Defined in:
lib/faye/mixins/publisher.rb

Instance Method Summary collapse

Instance Method Details

#bind(event_type, &listener) ⇒ Object



9
10
11
12
13
# File 'lib/faye/mixins/publisher.rb', line 9

def bind(event_type, &listener)
  @subscribers ||= {}
  list = @subscribers[event_type] ||= []
  list << listener
end

#count_listeners(event_type) ⇒ Object



4
5
6
7
# File 'lib/faye/mixins/publisher.rb', line 4

def count_listeners(event_type)
  return 0 unless @subscribers and @subscribers[event_type]
  @subscribers[event_type].size
end

#trigger(event_type, *args) ⇒ Object



22
23
24
25
26
# File 'lib/faye/mixins/publisher.rb', line 22

def trigger(event_type, *args)
  return unless @subscribers and @subscribers[event_type]
  listeners = @subscribers[event_type].dup
  listeners.each { |listener| listener.call(*args) }
end

#unbind(event_type, &listener) ⇒ Object



15
16
17
18
19
20
# File 'lib/faye/mixins/publisher.rb', line 15

def unbind(event_type, &listener)
  return unless @subscribers and @subscribers[event_type]
  return @subscribers.delete(event_type) unless listener
  
  @subscribers[event_type].delete_if(&listener.method(:==))
end