Class: Green::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvent

Returns a new instance of Event.



4
5
6
7
# File 'lib/green/event.rb', line 4

def initialize
  @waiters = []
  @setted = false
end

Instance Attribute Details

#waitersObject (readonly)

Returns the value of attribute waiters.



3
4
5
# File 'lib/green/event.rb', line 3

def waiters
  @waiters
end

Instance Method Details

#set(result = nil) ⇒ Object



9
10
11
12
13
# File 'lib/green/event.rb', line 9

def set(result = nil)
  @setted = true
  @result = result
  waiters.each { |v| Green.hub.callback { v.switch } }
end

#waitObject



15
16
17
18
19
20
21
22
23
# File 'lib/green/event.rb', line 15

def wait
  if @setted
    @result
  else
    g = Green.current
    waiters << g
    Green.hub.wait { waiters.delete g }
  end
end