Class: Rubyfox::Client::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyfox/client/event_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(smartfox) ⇒ EventHandler

Returns a new instance of EventHandler.



6
7
8
9
# File 'lib/rubyfox/client/event_handler.rb', line 6

def initialize(smartfox)
  @handler = Hash.new { |hash, type| hash[type] = [] }
  @smartfox = smartfox
end

Instance Method Details

#add(*names, &block) ⇒ Object



21
22
23
24
25
# File 'lib/rubyfox/client/event_handler.rb', line 21

def add(*names, &block)
  names.each do |name|
    @handler[event_type(name)] << block
  end
end

#dispatch(event) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubyfox/client/event_handler.rb', line 33

def dispatch(event)
  type = event.type

  handlers = @handler[:any] + @handler[type]
  handlers.each do |handler|
    handler.call(event)
  end
end

#registerObject



11
12
13
14
15
# File 'lib/rubyfox/client/event_handler.rb', line 11

def register
  Event.types.each do |type|
    @smartfox.add_event_listener Event[type], self
  end
end

#remove(*names) ⇒ Object



27
28
29
30
31
# File 'lib/rubyfox/client/event_handler.rb', line 27

def remove(*names)
  names.each do |name|
    @handler[event_type(name)].clear
  end
end

#unregisterObject



17
18
19
# File 'lib/rubyfox/client/event_handler.rb', line 17

def unregister
  @smartfox.remove_all_event_listeners
end