Class: MotionWiretap::Signal
- Defined in:
- lib/motion-wiretap/all/signal.rb
Overview
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.
Instance Method Summary collapse
- #complete ⇒ Object
- #error(error = SINGLETON) ⇒ Object
-
#initialize(value = SINGLETON, &block) ⇒ Signal
constructor
The SINGLETON value does not trigger a ‘change’ event.
-
#listen(wiretap = nil, &block) ⇒ Object
The Signal class always sends an initial value.
-
#next(*values) ⇒ Object
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.
- #value ⇒ Object
Methods inherited from Wiretap
#and_then, #cancel!, #combine, #dealloc, #enqueue, #filter, #map, #on_error, #queue, #reduce, #teardown, #trigger_changed, #trigger_changed_on, #trigger_completed, #trigger_completed_on, #trigger_error, #trigger_error_on
Constructor Details
#initialize(value = SINGLETON, &block) ⇒ Signal
The SINGLETON value does not trigger a ‘change’ event. It is for internal use only.
10 11 12 13 |
# File 'lib/motion-wiretap/all/signal.rb', line 10 def initialize(value=SINGLETON, &block) super(&block) @value = value end |
Instance Method Details
#complete ⇒ Object
41 42 43 |
# File 'lib/motion-wiretap/all/signal.rb', line 41 def complete trigger_completed end |
#error(error = SINGLETON) ⇒ Object
45 46 47 |
# File 'lib/motion-wiretap/all/signal.rb', line 45 def error(error=SINGLETON) trigger_error(error) end |
#listen(wiretap = nil, &block) ⇒ Object
The Signal class always sends an initial value
50 51 52 53 54 55 56 |
# 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) ⇒ Object
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!)
36 37 38 39 |
# 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 ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/motion-wiretap/all/signal.rb', line 15 def value if @value == SINGLETON nil else @value end end |