Class: Spade::Reactor

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

Overview

The reactor exposes some API

Defined Under Namespace

Classes: Timer

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#holdsObject (readonly)

Returns the value of attribute holds.



14
15
16
# File 'lib/spade/reactor.rb', line 14

def holds
  @holds
end

#runningObject (readonly)

Returns the value of attribute running.



14
15
16
# File 'lib/spade/reactor.rb', line 14

def running
  @running
end

#stoppedObject (readonly)

Returns the value of attribute stopped.



14
15
16
# File 'lib/spade/reactor.rb', line 14

def stopped
  @stopped
end

#timersObject (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_holdObject

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_holdObject



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

#startObject

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_loopObject



73
74
75
76
# File 'lib/spade/reactor.rb', line 73

def stop_loop
  @running = false
  EventMachine.stop_event_loop
end