class MotionWiretap::Signal
a Wiretap::Signal is much like a Promise in functional programming. A Signal
is triggered with a new value, or it is completed, or canceled with an error event.
Public Class Methods
new(value=SINGLETON, &block)
click to toggle source
The SINGLETON value does not trigger a ‘change’ event. It is for internal use only.
Calls superclass method
# File lib/motion-wiretap/all/signal.rb, line 10 def initialize(value=SINGLETON, &block) super(&block) @value = value end
Public Instance Methods
complete()
click to toggle source
# File lib/motion-wiretap/all/signal.rb, line 41 def complete trigger_completed end
error(error=SINGLETON)
click to toggle source
# File lib/motion-wiretap/all/signal.rb, line 45 def error(error=SINGLETON) trigger_error(error) end
listen(wiretap=nil, &block)
click to toggle source
The Signal
class always sends an initial value
Calls superclass method
# File lib/motion-wiretap/all/signal.rb, line 50 def listen(wiretap=nil, &block) super unless @value == SINGLETON trigger_changed(@value) end return self end
next(*values)
click to toggle source
If you pass multiple values to this method, the ‘value’ will be the array of all the values, but they will be passed on to ‘trigger_changed’ using the splat operator, and so will be passed to listener blocks as individual arguments.
Example:
signal = Signal.new signal.listen do |a, b| @added = a + b end signal.next(1, 5) # works great, @added will be 6 signal.next([1, 5]) # this works, too, because of how args are assigned to blocks in ruby signal.next(1) # a will be 1 and b will be nil (error!)
# File lib/motion-wiretap/all/signal.rb, line 36 def next(*values) raise "don't do that please" if values.include? SINGLETON trigger_changed(*values) end
value()
click to toggle source
# File lib/motion-wiretap/all/signal.rb, line 15 def value if @value == SINGLETON nil else @value end end