Class: MotionWiretap::WiretapArray
- Defined in:
- lib/motion-wiretap/all/wiretap.rb
Instance Attribute Summary collapse
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
Attributes inherited from Wiretap
Instance Method Summary collapse
-
#initialize(targets, &block) ⇒ WiretapArray
constructor
A new instance of WiretapArray.
- #teardown ⇒ Object
- #trigger_changed(*values) ⇒ Object
Methods inherited from Wiretap
#and_then, #cancel!, #combine, #dealloc, #enqueue, #filter, #listen, #map, #on_error, #queue, #reduce, #trigger_changed_on, #trigger_completed, #trigger_completed_on, #trigger_error, #trigger_error_on
Constructor Details
#initialize(targets, &block) ⇒ WiretapArray
Returns a new instance of WiretapArray.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 273 def initialize(targets, &block) raise "Not only is listening to an empty array pointless, it will also cause errors" if targets.length == 0 # the complete trigger isn't called until all the wiretap are complete @uncompleted = targets.length # targets can be an array of Wiretap objects (they will be monitored), or # plain objects (they'll just be included in the sequence) super(&block) # gets assigned to the wiretap value if it's a Wiretap, or the object # itself if it is anything else. @values = [] @initial_is_set = true # maps the wiretap object (which is unique) @wiretaps = {} targets.each_with_index do |wiretap, index| unless wiretap.is_a? Wiretap @values << wiretap # not a wiretap, so doesn't need to be "completed" @uncompleted -= 1 else raise "You cannot store a Wiretap twice in the same sequence (for now - do you really need this?)" if @wiretaps.key?(wiretap) @wiretaps[wiretap] = index @values << wiretap.value wiretap.listen do |*values| indx = @wiretaps[wiretap] @values[indx] = wiretap.value trigger_changed(*@values) end wiretap.on_error do |error| trigger_error(error) end wiretap.and_then do |error| @uncompleted -= 1 if @uncompleted == 0 trigger_completed end end end end end |
Instance Attribute Details
#targets ⇒ Object (readonly)
Returns the value of attribute targets.
271 272 273 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 271 def targets @targets end |
Instance Method Details
#teardown ⇒ Object
321 322 323 324 325 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 321 def teardown cancel = (->(wiretap){ wiretap.cancel! }).weak! @wiretaps.keys.each &cancel super end |
#trigger_changed(*values) ⇒ Object
327 328 329 330 |
# File 'lib/motion-wiretap/all/wiretap.rb', line 327 def trigger_changed(*values) values = @values if values.length == 0 super(*values) end |