Class: MotionWiretap::Wiretap
- Inherits:
-
Object
- Object
- MotionWiretap::Wiretap
- Defined in:
- lib/motion-wiretap/all/wiretap.rb
Direct Known Subclasses
Signal, WiretapArray, WiretapChild, WiretapNotification, WiretapTarget
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
wiretaps have an intrinsic “current value”.
Instance Method Summary collapse
-
#and_then(wiretap = nil, &block) ⇒ Object
called when no more values are expected.
-
#cancel! ⇒ Object
this is the preferred way to turn off a wiretap; child classes override ‘teardown`, which is only ever called once.
-
#combine(&block) ⇒ Object
Returns a Wiretap that combines all the values into one value (the values are all passed in at the same time).
- #dealloc ⇒ Object
-
#enqueue(&block) ⇒ Object
send a block to the GCD queue.
-
#filter(&block) ⇒ Object
Returns a Wiretap that will only be called if the &condition block returns true.
-
#initialize(&block) ⇒ Wiretap
constructor
A new instance of Wiretap.
-
#listen(wiretap = nil, &block) ⇒ Object
called when the value changes.
-
#map(&block) ⇒ Object
Returns a Wiretap that passes each value through the provided block.
-
#on_error(wiretap = nil, &block) ⇒ Object
called when an error occurs, and no more values are expected.
-
#queue(queue) ⇒ Object
specify the GCD queue that the listeners should be run on.
-
#reduce(memo = nil, &block) ⇒ Object
Returns a Wiretap that passes each value through the block, and also the previous return value (memo).
-
#teardown ⇒ Object
Overridden by subclasses to turn off observation, unregister notifications, etc.
- #trigger_changed(*values) ⇒ Object
-
#trigger_changed_on(block_or_wiretap, values) ⇒ Object
Sends the block or wiretap a changed signal.
- #trigger_completed ⇒ Object
-
#trigger_completed_on(block_or_wiretap) ⇒ Object
Sends the block or wiretap a completed signal.
- #trigger_error(error = SINGLETON) ⇒ Object
-
#trigger_error_on(block_or_wiretap, error) ⇒ Object
Sends the block or wiretap an error value.
Constructor Details
#initialize(&block) ⇒ Wiretap
Returns a new instance of Wiretap.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 15 def initialize(&block) @is_torn_down = false @is_completed = false @is_error = false @queue = nil @value = nil @listener_handlers = [] @completion_handlers = [] @error_handlers = [] listen &block if block end |
Instance Attribute Details
#value ⇒ Object (readonly)
wiretaps have an intrinsic “current value”
13 14 15 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 13 def value @value end |
Instance Method Details
#and_then(wiretap = nil, &block) ⇒ Object
called when no more values are expected
101 102 103 104 105 106 107 108 109 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 101 def and_then(wiretap=nil, &block) raise "Block or Wiretap is expected in #{self.class.name}##{__method__}" unless block || wiretap raise "Only Block *or* Wiretap is expected in #{self.class.name}##{__method__}" if block && wiretap @completion_handlers << (block ? block.weak! : wiretap) if @is_completed trigger_completed_on(block || wiretap) end return self end |
#cancel! ⇒ Object
this is the preferred way to turn off a wiretap; child classes override ‘teardown`, which is only ever called once.
36 37 38 39 40 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 36 def cancel! return if @is_torn_down @is_torn_down = true teardown end |
#combine(&block) ⇒ Object
Returns a Wiretap that combines all the values into one value (the values are all passed in at the same time)
188 189 190 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 188 def combine(&block) return WiretapCombiner.new(self, block) end |
#dealloc ⇒ Object
29 30 31 32 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 29 def dealloc self.cancel! super end |
#enqueue(&block) ⇒ Object
send a block to the GCD queue
54 55 56 57 58 59 60 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 54 def enqueue(&block) if @queue @queue.async(&block) else block.call end end |
#filter(&block) ⇒ Object
Returns a Wiretap that will only be called if the &condition block returns true
178 179 180 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 178 def filter(&block) return WiretapFilter.new(self, block) end |
#listen(wiretap = nil, &block) ⇒ Object
called when the value changes
67 68 69 70 71 72 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 67 def listen(wiretap=nil, &block) raise "Block or Wiretap is expected in #{self.class.name}##{__method__}" unless block || wiretap raise "Only Block *or* Wiretap is expected in #{self.class.name}##{__method__}" if block && wiretap @listener_handlers << (block ? block.weak! : wiretap) self end |
#map(&block) ⇒ Object
Returns a Wiretap that passes each value through the provided block
208 209 210 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 208 def map(&block) return WiretapMapper.new(self, block) end |
#on_error(wiretap = nil, &block) ⇒ Object
called when an error occurs, and no more values are expected
135 136 137 138 139 140 141 142 143 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 135 def on_error(wiretap=nil, &block) raise "Block or Wiretap is expected in #{self.class.name}##{__method__}" unless block || wiretap raise "Only Block *or* Wiretap is expected in #{self.class.name}##{__method__}" if block && wiretap @error_handlers << (block ? block.weak! : wiretap) if @is_error trigger_error_on(block || wiretap, @is_error) end return self end |
#queue(queue) ⇒ Object
specify the GCD queue that the listeners should be run on
48 49 50 51 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 48 def queue(queue) @queue = queue return self end |
#reduce(memo = nil, &block) ⇒ Object
Returns a Wiretap that passes each value through the block, and also the previous return value (memo).
199 200 201 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 199 def reduce(memo=nil, &block) return WiretapReducer.new(self, memo, block) end |
#teardown ⇒ Object
Overridden by subclasses to turn off observation, unregister notifications, etc.
44 45 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 44 def teardown end |
#trigger_changed(*values) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 74 def trigger_changed(*values) return if @is_torn_down || @is_completed || @is_error if values.length == 1 @value = values.first else @value = values end @listener_handlers.each do |block_or_wiretap| trigger_changed_on(block_or_wiretap, values) end return self end |
#trigger_changed_on(block_or_wiretap, values) ⇒ Object
Sends the block or wiretap a changed signal
90 91 92 93 94 95 96 97 98 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 90 def trigger_changed_on(block_or_wiretap, values) if block_or_wiretap.is_a? Wiretap block_or_wiretap.trigger_changed(*values) else enqueue do block_or_wiretap.call(*values) end end end |
#trigger_completed ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 111 def trigger_completed return if @is_torn_down || @is_completed || @is_error @is_completed = true @completion_handlers.each do |block_or_wiretap| trigger_completed_on(block_or_wiretap) end cancel! return self end |
#trigger_completed_on(block_or_wiretap) ⇒ Object
Sends the block or wiretap a completed signal
124 125 126 127 128 129 130 131 132 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 124 def trigger_completed_on(block_or_wiretap) if block_or_wiretap.is_a? Wiretap block_or_wiretap.trigger_completed else enqueue do block_or_wiretap.call end end end |
#trigger_error(error = SINGLETON) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 145 def trigger_error(error=SINGLETON) return if @is_torn_down || @is_completed || @is_error raise "You must pass a truthy value to `trigger_error()`" unless error # convert SINGLETON to a default error value error = true if error == SINGLETON @is_error = error @error_handlers.each do |block_or_wiretap| trigger_error_on(block_or_wiretap, error) end cancel! return self end |
#trigger_error_on(block_or_wiretap, error) ⇒ Object
Sends the block or wiretap an error value
162 163 164 165 166 167 168 169 170 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 162 def trigger_error_on(block_or_wiretap, error) if block_or_wiretap.is_a? Wiretap block_or_wiretap.trigger_error(error) else enqueue do block_or_wiretap.call(error) end end end |