Class: ReactiveManager

Inherits:
Object show all
Includes:
Events
Defined in:
lib/volt/reactive/reactive_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Events

#add_event_follower, #add_event_to_chains, #add_following, #all_listeners_for, #event_chain, #event_followers, #event_followings, #listeners, #on, #remove_event_follower, #remove_event_from_chains, #remove_following, #remove_listener, #trigger!, #trigger_by_scope!

Constructor Details

#initialize(getter, setter = nil, scope = nil) ⇒ ReactiveManager

When created, ReactiveValue’s get a getter (a proc)



206
207
208
209
210
211
212
213
214
# File 'lib/volt/reactive/reactive_value.rb', line 206

def initialize(getter, setter=nil, scope=nil)
  @getter = getter
  @setter = setter
  @scope = scope
  
  @parents = []
  
  object_tracker.enable!
end

Instance Attribute Details

#parentsObject (readonly)

Returns the value of attribute parents.



203
204
205
# File 'lib/volt/reactive/reactive_value.rb', line 203

def parents
  @parents
end

#scopeObject (readonly)

Returns the value of attribute scope.



203
204
205
# File 'lib/volt/reactive/reactive_value.rb', line 203

def scope
  @scope
end

Instance Method Details

#add_parent!(parent) ⇒ Object



343
344
345
346
# File 'lib/volt/reactive/reactive_value.rb', line 343

def add_parent!(parent)
  @parents << parent
  event_chain.add_object(parent)
end

#curObject

Fetch the current value



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/volt/reactive/reactive_value.rb', line 250

def cur
  # if @cached_obj && ObjectTracker.cache_version == @cached_version
  #   return @cached_obj
  # end
  
  if @getter.class == ::Proc
    # Get the current value, capture any errors
    begin
      result = @getter.call
    rescue => e
      result = e
    end
  else
    # getter is just an object, return it
    result = @getter
  end
  
  if result.reactive?
    # Unwrap any stored reactive values
    result = result.cur
  end
  
  # if ObjectTracker.cache_enabled
    # @cached_obj = result
    # @cached_version = ObjectTracker.cache_version
  # end
  
  return result
end

#cur=(val) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/volt/reactive/reactive_value.rb', line 280

def cur=(val)
  if @setter
    @setter.call(val)
  elsif @scope == nil
    @getter = val
    @setter = nil
    
    trigger!('changed')
  else
    raise "Value can not be updated"
  end
end

#inspectObject



220
221
222
# File 'lib/volt/reactive/reactive_value.rb', line 220

def inspect
  "@<#{self.class.to_s}:#{reactive_object_id} #{cur.inspect}>"
end

#object_trackerObject

def event_added(event, scope, first)

# When the first event is registered, we need to start listening on our current object
# for it to publish events.
# object_tracker.enable! if first

end

def event_removed(event, last)

# If no one is listening on the reactive value, then we don't need to listen on our
# current object for events, because no one cares.
# Note: when we're tracking the current object, it will have one registered changed 
# event.
# update_current_object(true) if last && !has_non_tracking_listeners?
# object_tracker.disable! if @listeners.size == 0

end



244
245
246
# File 'lib/volt/reactive/reactive_value.rb', line 244

def object_tracker
  @object_tracker ||= ::ObjectTracker.new(self)
end

#reactive?Boolean

Returns:



216
217
218
# File 'lib/volt/reactive/reactive_value.rb', line 216

def reactive?
  true
end

#reactive_object_idObject



224
225
226
# File 'lib/volt/reactive/reactive_value.rb', line 224

def reactive_object_id
  @reactive_object_id ||= rand(100000)
end

#remove_parent!(parent) ⇒ Object



348
349
350
351
# File 'lib/volt/reactive/reactive_value.rb', line 348

def remove_parent!(parent)
  @parents.delete(parent)
  event_chain.remove_object(parent)
end

#set_scope(new_scope) ⇒ Object



359
360
361
# File 'lib/volt/reactive/reactive_value.rb', line 359

def set_scope(new_scope)
  dup.scope!(new_scope)
end

#set_scope!(new_scope) ⇒ Object



353
354
355
356
357
# File 'lib/volt/reactive/reactive_value.rb', line 353

def set_scope!(new_scope)
  @scope = new_scope
  
  self
end

#setter!(setter = nil, &block) ⇒ Object

Sets the setter



364
365
366
# File 'lib/volt/reactive/reactive_value.rb', line 364

def setter!(setter=nil, &block)
  @setter = setter || block
end

#with(*args, &block) ⇒ Object

With returns a new reactive value dependent on any arguments passed in. If a block is passed in, the getter is the block its self, which will be passed the .cur and the .cur of any reactive arguments.



296
297
298
# File 'lib/volt/reactive/reactive_value.rb', line 296

def with(*args, &block)
  return with_and_options(args, false, &block)
end

#with_and_options(args, pass_reactive, &block) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/volt/reactive/reactive_value.rb', line 300

def with_and_options(args, pass_reactive, &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
      # Unwrap arguments if the method doesn't want reactive values
      pass_args = pass_reactive ? args : args.map{|v| v.cur }
      
      # TODO: Calling cur every time
      current_val = self.cur
      
      if current_val.is_a?(Exception)
        current_val
      else
        block.call(current_val, pass_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