Class: Goru::Reactor
- Inherits:
-
Object
- Object
- Goru::Reactor
- Defined in:
- lib/goru/reactor.rb
Overview
- public
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
[public].
Instance Method Summary collapse
-
#adopt_routine(routine) ⇒ Object
[public].
-
#asleep_for(seconds) ⇒ Object
[public].
-
#deregister(routine) ⇒ Object
[public].
-
#finished? ⇒ Boolean
[public].
-
#initialize(queue:, scheduler:) ⇒ Reactor
constructor
A new instance of Reactor.
-
#register(routine) ⇒ Object
[public].
-
#routine_finished(routine) ⇒ Object
[public].
-
#run ⇒ Object
[public].
-
#stop ⇒ Object
[public].
-
#wakeup ⇒ Object
[public].
Constructor Details
#initialize(queue:, scheduler:) ⇒ Reactor
Returns a new instance of Reactor.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/goru/reactor.rb', line 13 def initialize(queue:, scheduler:) @queue = queue @scheduler = scheduler @routines = Set.new @timers = Timers::Group.new @stopped = false @status = nil @selector = NIO::Selector.new @commands = [] end |
Instance Attribute Details
#status ⇒ Object (readonly)
- public
26 27 28 |
# File 'lib/goru/reactor.rb', line 26 def status @status end |
Instance Method Details
#adopt_routine(routine) ⇒ Object
- public
136 137 138 |
# File 'lib/goru/reactor.rb', line 136 def adopt_routine(routine) command(:adopt, routine) end |
#asleep_for(seconds) ⇒ Object
- public
128 129 130 131 132 |
# File 'lib/goru/reactor.rb', line 128 def asleep_for(seconds) @timers.after(seconds) { yield } end |
#deregister(routine) ⇒ Object
- public
154 155 156 |
# File 'lib/goru/reactor.rb', line 154 def deregister(routine) command(:deregister, routine) end |
#finished? ⇒ Boolean
- public
105 106 107 |
# File 'lib/goru/reactor.rb', line 105 def finished? @status == :idle || @status == :stopped end |
#register(routine) ⇒ Object
- public
148 149 150 |
# File 'lib/goru/reactor.rb', line 148 def register(routine) command(:register, routine) end |
#routine_finished(routine) ⇒ Object
- public
142 143 144 |
# File 'lib/goru/reactor.rb', line 142 def routine_finished(routine) command(:cleanup, routine) end |
#run ⇒ Object
- public
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/goru/reactor.rb', line 30 def run set_status(:running) until @stopped tick end ensure @timers.cancel @selector.close set_status(:finished) end |
#stop ⇒ Object
- public
119 120 121 122 123 124 |
# File 'lib/goru/reactor.rb', line 119 def stop @stopped = true wakeup rescue ClosedQueueError # nothing to do end |
#wakeup ⇒ Object
- public
111 112 113 114 115 |
# File 'lib/goru/reactor.rb', line 111 def wakeup @selector.wakeup rescue IOError # nothing to do end |