Class: MotionWiretap::WiretapArray

Inherits:
Wiretap
  • Object
show all
Defined in:
lib/motion-wiretap/all/wiretap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
# File 'lib/motion-wiretap/all/wiretap.rb', line 256

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 |value|
        indx = @wiretaps[wiretap]
        @values[index] = 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

#targetsObject (readonly)

Returns the value of attribute targets.



254
255
256
# File 'lib/motion-wiretap/all/wiretap.rb', line 254

def targets
  @targets
end

Instance Method Details

#teardownObject



304
305
306
307
308
# File 'lib/motion-wiretap/all/wiretap.rb', line 304

def teardown
  cancel = (->(wiretap){ wiretap.cancel! }).weak!
  @wiretaps.keys.each &cancel
  super
end

#trigger_changed(*values) ⇒ Object



310
311
312
313
# File 'lib/motion-wiretap/all/wiretap.rb', line 310

def trigger_changed(*values)
  values = @values if values.length == 0
  super(*values)
end