Class: Wee::CallbackRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/wee/core/callback.rb

Overview

The callback registry is the central datastructure where all components of a session register their callbacks.

The format of the internal datastructure is:

@callbacks[type][callback_id] => callback
@obj_to_id_map[type][object] => [id*]

Instance Method Summary collapse

Constructor Details

#initialize(id_generator) ⇒ CallbackRegistry

Returns a new instance of CallbackRegistry.



11
12
13
14
15
# File 'lib/wee/core/callback.rb', line 11

def initialize(id_generator)
  @idgen = id_generator
  @callbacks = Hash.new
  @obj_to_id_map = Hash.new
end

Instance Method Details

#all_of_type(type) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/wee/core/callback.rb', line 55

def all_of_type(type)
  if c = @callbacks[type]
    c
  else
    []
  end
end

#get_callback_for(id, type) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/wee/core/callback.rb', line 47

def get_callback_for(id, type)
  if c = @callbacks[type]
    c[id]
  else
    raise
  end
end

#get_ids_for(object, type) ⇒ Object

                                                                        • -

:section: Friend methods for Wee::CallbackStream

                                                                        • -



39
40
41
42
43
44
45
# File 'lib/wee/core/callback.rb', line 39

def get_ids_for(object, type)
  if o = @obj_to_id_map[type]
    o[object] || []
  else
    []
  end
end

#register_for(object, type, callback) ⇒ Object

Register callback for object under type and return a unique callback id.



19
20
21
# File 'lib/wee/core/callback.rb', line 19

def register_for(object, type, callback)
  register_named_for(object, type, callback, @idgen.next)
end

#register_named_for(object, type, callback, named_id) ⇒ Object

Register callback for object under type with the id named_id.



25
26
27
28
29
30
31
32
33
# File 'lib/wee/core/callback.rb', line 25

def register_named_for(object, type, callback, named_id)
  cid = named_id.to_s
  c = (@callbacks[type] ||= Hash.new)
  o = (@obj_to_id_map[type] ||= Hash.new) 
  raise "duplicate callback id" if c.has_key?(cid)
  c[cid] = callback
  (o[object] ||= []) << cid  
  return cid
end