Module: QDA::Broadcaster

Included in:
Application, GUI::WeftClient
Defined in:
lib/weft/broadcaster.rb

Overview

Allows an object such as the global Wx App to broadcast messages about changes in the database and other application states that might require the child to update its appearance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bc__subscribersObject

intended to be module private variables to managed which widgets are subscribing to which events



9
10
11
# File 'lib/weft/broadcaster.rb', line 9

def bc__subscribers
  @bc__subscribers
end

#bc__subscriptionsObject

intended to be module private variables to managed which widgets are subscribing to which events



9
10
11
# File 'lib/weft/broadcaster.rb', line 9

def bc__subscriptions
  @bc__subscriptions
end

Instance Method Details

#add_subscriber(subscriber, *events) ⇒ Object

subscribe the widget subscriber to receive notification of app event types *events (which should be a set of symbols)



27
28
29
30
31
32
33
34
35
36
# File 'lib/weft/broadcaster.rb', line 27

def add_subscriber(subscriber, *events)
  reset_subscriptions if not bc__subscriptions
  if events.length == 1 and events[0] == :all
    events = self.event_types()
  end
  
  events.each do | e |
    bc__subscriptions[e].push(subscriber)
  end
end

#broadcast(event_type, content = nil) ⇒ Object

broadcast an event to all subscribers to event of type event_type, optionally including content



18
19
20
21
22
23
# File 'lib/weft/broadcaster.rb', line 18

def broadcast(event_type, content = nil)
  return unless bc__subscriptions
  for subscriber in bc__subscriptions[event_type]
    subscriber.notify(event_type, content)
  end
end

#delete_subscriber(subscriber) ⇒ Object

accepts a ruby item



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/weft/broadcaster.rb', line 39

def delete_subscriber(subscriber)
  if subscriber.respond_to?(:associated_subscribers)
    subscriber.associated_subscribers.each do | child |
      delete_subscriber(child)
    end
  end
  bc__subscribers.delete(subscriber)
  bc__subscriptions.each_value do | subscriber_set |
    subscriber_set.delete(subscriber)
  end
end

#event_typesObject



12
13
14
# File 'lib/weft/broadcaster.rb', line 12

def event_types()
     self.class.const_get('SUBSCRIBABLE_EVENTS')
end

#reset_subscriptionsObject



51
52
53
54
55
56
57
# File 'lib/weft/broadcaster.rb', line 51

def reset_subscriptions()
  self.bc__subscribers = []
  self.bc__subscriptions = Hash.new do | ev | 
    raise "Cannot subscribe to unknown event #{ev}"
  end
  event_types.each { | ev | self.bc__subscriptions[ev] = [] }
end