Class: Butler::IRC::Client::ListenerList
- Inherits:
-
Object
- Object
- Butler::IRC::Client::ListenerList
- Includes:
- Enumerable
- Defined in:
- lib/butler/irc/client/listenerlist.rb
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize ⇒ ListenerList
constructor
A new instance of ListenerList.
- #mutated(listener) ⇒ Object
- #subscribe(listener) ⇒ Object
- #synchronized_each_for(symbol, &block) ⇒ Object
- #unsubscribe(listener) ⇒ Object
Methods included from Enumerable
Constructor Details
#initialize ⇒ ListenerList
Returns a new instance of ListenerList.
21 22 23 24 25 |
# File 'lib/butler/irc/client/listenerlist.rb', line 21 def initialize @mutex = Mutex.new @all = {} @per_symbol = Hash.new { |h,k| h[k] = [] } end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
19 20 21 |
# File 'lib/butler/irc/client/listenerlist.rb', line 19 def mutex @mutex end |
Instance Method Details
#each(&block) ⇒ Object
27 28 29 |
# File 'lib/butler/irc/client/listenerlist.rb', line 27 def each(&block) @all.each_key(&block) end |
#mutated(listener) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/butler/irc/client/listenerlist.rb', line 59 def mutated(listener) @mutex.synchronize { listener.symbols.each { |symbol| @per_symbol[s] = @per_symbol[s].sort_by { |l| -l.priority } } } end |
#subscribe(listener) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/butler/irc/client/listenerlist.rb', line 37 def subscribe(listener) @mutex.synchronize { raise "#{listener} already subscribed" if @all[listener] listener.container = self @all[listener] = true listener.symbols.each { |s| @per_symbol[s] << listener @per_symbol[s] = @per_symbol[s].sort_by { |l| -l.priority } } } end |
#synchronized_each_for(symbol, &block) ⇒ Object
31 32 33 34 35 |
# File 'lib/butler/irc/client/listenerlist.rb', line 31 def synchronized_each_for(symbol, &block) @mutex.synchronize { @per_symbol[symbol]+@per_symbol[nil] }.each(&block) end |
#unsubscribe(listener) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/butler/irc/client/listenerlist.rb', line 49 def unsubscribe(listener) @mutex.synchronize { @all.delete(listener) listener.symbols.each { |s| @per_symbol[s].delete(listener) @per_symbol.delete(s) if @per_symbol[s].empty? } } end |