Class: Wires::Channel
- Inherits:
-
Object
- Object
- Wires::Channel
- Defined in:
- lib/wires/channel.rb
Constant Summary collapse
- @@channel_hash =
Channel registry hash and star channel reference are values In this Hash with the key being the reference to the Hub
Hash.new
- @@channel_star =
Hash.new
- @@new_lock =
Ensure that there is only one instance of Channel per name
Mutex.new
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#target_list ⇒ Object
readonly
Returns the value of attribute target_list.
Class Method Summary collapse
-
.channel_star ⇒ Object
Give out references to the star channel.
-
.hub ⇒ Object
Redefine this class method to use an alternate Hub.
- .new(*args, &block) ⇒ Object
Instance Method Summary collapse
- #channel_star ⇒ Object
-
#fire(event, blocking: false) ⇒ Object
Fire an event on this channel.
-
#hub ⇒ Object
Don’t redefine this instance method!.
-
#initialize(name) ⇒ Channel
constructor
A new instance of Channel.
-
#register(events, proc) ⇒ Object
Register a proc to be triggered by an event on this channel.
- #relevant_channels ⇒ Object
Constructor Details
#initialize(name) ⇒ Channel
Returns a new instance of Channel.
27 28 29 30 |
# File 'lib/wires/channel.rb', line 27 def initialize(name) @name = name @target_list = Set.new nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/wires/channel.rb', line 24 def name @name end |
#target_list ⇒ Object (readonly)
Returns the value of attribute target_list.
25 26 27 |
# File 'lib/wires/channel.rb', line 25 def target_list @target_list end |
Class Method Details
.channel_star ⇒ Object
Give out references to the star channel
44 |
# File 'lib/wires/channel.rb', line 44 def self.channel_star; @@channel_star[self.hub]; end |
.hub ⇒ Object
Redefine this class method to use an alternate Hub
34 |
# File 'lib/wires/channel.rb', line 34 def self.hub; Hub; end |
.new(*args, &block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wires/channel.rb', line 49 def self.new(*args, &block) (args.include? :recursion_guard) ? (args.delete :recursion_guard) : (@@channel_star[self.hub] ||= self.new('*', :recursion_guard)) @@new_lock.synchronize do @@channel_hash[self.hub] ||= Hash.new @@channel_hash[self.hub][args[0]] ||= super(*args, &block) end end |
Instance Method Details
#channel_star ⇒ Object
45 |
# File 'lib/wires/channel.rb', line 45 def channel_star; @@channel_star[self.hub]; end |
#fire(event, blocking: false) ⇒ Object
Fire an event on this channel
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/wires/channel.rb', line 76 def fire(event, blocking:false) # Create an instance object from one of several acceptable input forms event = Event.new_from event # Fire to each relevant target on each channel for chan in relevant_channels() for target in chan.target_list for string in target[0] & event.class.codestrings self.class.hub << [string, event, blocking, *target[1..-1]] end end end nil end |
#hub ⇒ Object
Don’t redefine this instance method!
36 |
# File 'lib/wires/channel.rb', line 36 def hub; self.class.hub; end |
#register(events, proc) ⇒ Object
Register a proc to be triggered by an event on this channel
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wires/channel.rb', line 61 def register(events, proc) if not proc.is_a?(Proc) then raise SyntaxError, \ "No Proc given to execute on event: #{events}" end # Convert all events to strings events = [events] unless events.is_a? Array events.flatten! events.map! { |e| (e.is_a?(Class) ? e.codestring : e.to_s) } events.uniq! @target_list << [events, proc] nil end |
#relevant_channels ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wires/channel.rb', line 90 def relevant_channels return @@channel_hash[hub].values if self==channel_star if self.name.is_a?(Regexp) then raise TypeError, "Cannot fire on Regexp channel: #{self.name}."\ " Regexp channels can only used in event handlers." end relevant = [channel_star] for c in @@channel_hash[hub].values relevant << c if \ if c.name.is_a?(Regexp) self.name =~ c.name elsif (defined?(c.name.channel_name) and defined?(self.name.channel_name)) self.name.channel_name == c.name.channel_name else self.name.to_s == c.name.to_s end end return relevant.uniq end |