Class: Spade::Reactor
- Inherits:
-
Object
- Object
- Spade::Reactor
- Defined in:
- lib/spade/reactor.rb
Overview
The reactor exposes some API
Defined Under Namespace
Classes: Timer
Instance Attribute Summary collapse
-
#holds ⇒ Object
readonly
Returns the value of attribute holds.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
-
#stopped ⇒ Object
readonly
Returns the value of attribute stopped.
-
#timers ⇒ Object
readonly
Returns the value of attribute timers.
Instance Method Summary collapse
-
#add_hold ⇒ Object
Holds.
-
#add_timer(interval, periodic = false, &block) ⇒ Object
Timers.
- #clear_interval(timer) ⇒ Object
- #clear_timeout(timer) ⇒ Object
- #exit(status = 0) ⇒ Object
-
#initialize(ctx) ⇒ Reactor
constructor
A new instance of Reactor.
- #next_tick(&block) ⇒ Object
- #release_hold ⇒ Object
- #remove_timer(timer) ⇒ Object
- #set_interval(callback, interval) ⇒ Object
- #set_timeout(callback, interval) ⇒ Object
-
#start ⇒ Object
Starts the event loop.
- #stop_loop ⇒ Object
Constructor Details
#initialize(ctx) ⇒ Reactor
Returns a new instance of Reactor.
16 17 18 19 20 21 |
# File 'lib/spade/reactor.rb', line 16 def initialize(ctx) @timers = [] @running = false @stopped = false @holds = 0 end |
Instance Attribute Details
#holds ⇒ Object (readonly)
Returns the value of attribute holds.
14 15 16 |
# File 'lib/spade/reactor.rb', line 14 def holds @holds end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
14 15 16 |
# File 'lib/spade/reactor.rb', line 14 def running @running end |
#stopped ⇒ Object (readonly)
Returns the value of attribute stopped.
14 15 16 |
# File 'lib/spade/reactor.rb', line 14 def stopped @stopped end |
#timers ⇒ Object (readonly)
Returns the value of attribute timers.
14 15 16 |
# File 'lib/spade/reactor.rb', line 14 def timers @timers end |
Instance Method Details
#add_hold ⇒ Object
Holds
Prevents the loop from exiting
64 65 66 |
# File 'lib/spade/reactor.rb', line 64 def add_hold @holds = @holds + 1 end |
#add_timer(interval, periodic = false, &block) ⇒ Object
Timers
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/spade/reactor.rb', line 83 def add_timer(interval, periodic = false, &block) add_hold Timer.new(self).tap do |timer| timer.periodic = periodic timer.interval = interval timer.callback = block @timers << timer timer.start if @running end end |
#clear_interval(timer) ⇒ Object
115 116 117 |
# File 'lib/spade/reactor.rb', line 115 def clear_interval(timer) timer.cancel end |
#clear_timeout(timer) ⇒ Object
111 112 113 |
# File 'lib/spade/reactor.rb', line 111 def clear_timeout(timer) timer.cancel end |
#exit(status = 0) ⇒ Object
42 43 44 45 |
# File 'lib/spade/reactor.rb', line 42 def exit(status=0) stop_loop Kernel.exit(status) end |
#next_tick(&block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/spade/reactor.rb', line 47 def next_tick(&block) if @running add_hold EventMachine.next_tick do yield release_hold end else add_timer(0) { yield } end end |
#release_hold ⇒ Object
68 69 70 71 |
# File 'lib/spade/reactor.rb', line 68 def release_hold @holds = @holds - 1 stop_loop if @stopped && @holds <= 0 end |
#remove_timer(timer) ⇒ Object
94 95 96 97 |
# File 'lib/spade/reactor.rb', line 94 def remove_timer(timer) 'remove_timer' release_hold if @timers.delete(timer) end |
#set_interval(callback, interval) ⇒ Object
105 106 107 108 109 |
# File 'lib/spade/reactor.rb', line 105 def set_interval(callback, interval) add_timer interval, true do callback.methodcall(self) end end |
#set_timeout(callback, interval) ⇒ Object
99 100 101 102 103 |
# File 'lib/spade/reactor.rb', line 99 def set_timeout(callback, interval) add_timer interval, false do callback.methodcall(self) end end |
#start ⇒ Object
Starts the event loop
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/spade/reactor.rb', line 24 def start return if @running @running = true add_hold # release when finish is called EventMachine.run do @running = true @timers.each { |t| t.start unless t.running } yield if block_given? @stopped = true release_hold end end |
#stop_loop ⇒ Object
73 74 75 76 |
# File 'lib/spade/reactor.rb', line 73 def stop_loop @running = false EventMachine.stop_event_loop end |