Class: Evented

Inherits:
Object
  • Object
show all
Defined in:
lib/evented/evented.rb

Direct Known Subclasses

Server, Stream

Instance Method Summary collapse

Constructor Details

#initializeEvented

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

#callbacksObject



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

#startObject



26
27
28
29
30
31
# File 'lib/evented/evented.rb', line 26

def start
  @running = true
  while @running
    tick
  end
end

#start_threadedObject



33
34
35
# File 'lib/evented/evented.rb', line 33

def start_threaded
  Thread.new { start }
end

#stopObject



37
38
39
# File 'lib/evented/evented.rb', line 37

def stop
  @running = false
end

#streamsObject



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

def streams
  @@_streams
end

#tickObject



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