Method: ReactiveManager#with_and_options

Defined in:
lib/volt/reactive/reactive_value.rb

#with_and_options(args, &block) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/volt/reactive/reactive_value.rb', line 366

def with_and_options(args, &block)
  getter = @getter
  setter = @setter
  scope = @scope

  if block
    # If a block was passed in, the getter now becomes a proc that calls
    # the passed in block with the right arguments.
    getter = ::Proc.new do
      # TODO: Calling cur every time
      current_val = self.cur

      if current_val.is_a?(Exception)
        current_val
      else
        block.call(current_val, args)
      end
    end

    # TODO: Make this work with custom setters
    setter = nil

    # Scope also gets set to nil, because now we should always retrigger this
    # method because we don't know enough about what methods its calling.
    scope = nil
  end

  new_val = ReactiveValue.new(getter, setter, scope)

  # Add the ReactiveValue we're building from
  new_val.reactive_manager.add_parent!(self)

  # Add any reactive arguments as parents
  args.select(&:reactive?).each do |arg|
    new_val.reactive_manager.add_parent!(arg.reactive_manager)
  end

  return new_val
end