Class: Evented
- Inherits:
-
Object
- Object
- Evented
- Defined in:
- lib/evented/evented.rb
Instance Method Summary collapse
- #callbacks ⇒ Object
- #emit(event, *args) ⇒ Object
-
#initialize ⇒ Evented
constructor
A new instance of Evented.
- #on(event, &block) ⇒ Object
- #start ⇒ Object
- #start_threaded ⇒ Object
- #stop ⇒ Object
- #streams ⇒ Object
- #tick ⇒ Object
Constructor Details
#initialize ⇒ Evented
Returns a new instance of Evented.
3 4 5 6 |
# File 'lib/evented/evented.rb', line 3 def initialize @@_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new } @@_streams = [] end |
Instance Method Details
#callbacks ⇒ Object
8 9 10 |
# File 'lib/evented/evented.rb', line 8 def callbacks @@_callbacks end |
#emit(event, *args) ⇒ Object
20 21 22 23 24 |
# File 'lib/evented/evented.rb', line 20 def emit(event, *args) @@_callbacks[event].each do |callback| callback.call(*args) end end |
#on(event, &block) ⇒ Object
16 17 18 |
# File 'lib/evented/evented.rb', line 16 def on(event, &block) @@_callbacks[event] << block end |
#start ⇒ Object
26 27 28 29 30 31 |
# File 'lib/evented/evented.rb', line 26 def start @running = true while @running tick end end |
#start_threaded ⇒ Object
33 34 35 |
# File 'lib/evented/evented.rb', line 33 def start_threaded Thread.new { start } end |
#stop ⇒ Object
37 38 39 |
# File 'lib/evented/evented.rb', line 37 def stop @running = false end |
#streams ⇒ Object
12 13 14 |
# File 'lib/evented/evented.rb', line 12 def streams @@_streams end |
#tick ⇒ Object
41 42 43 44 45 46 |
# File 'lib/evented/evented.rb', line 41 def tick @@_streams.each do |stream| stream.handle_read stream.handle_write end end |