Class: Dependency

Inherits:
Object show all
Defined in:
lib/volt/reactive/dependency.rb

Instance Method Summary collapse

Constructor Details

#initializeDependency

Returns a new instance of Dependency.



31
32
33
# File 'lib/volt/reactive/dependency.rb', line 31

def initialize
  @dependencies = Set.new
end

Instance Method Details

#changed!Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/volt/reactive/dependency.rb', line 53

def changed!
  deps = @dependencies

  # If no deps, dependency has been removed
  return unless deps

  @dependencies = Set.new

  deps.each do |dep|
    dep.invalidate!
  end
end

#dependObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/volt/reactive/dependency.rb', line 35

def depend
  # If there is no @dependencies, don't depend because it has been removed
  return unless @dependencies

  current = Computation.current
  if current
    added = @dependencies.add?(current)

    if added
      # puts "Added #{self.inspect} to #{current.inspect}"
      current.on_invalidate do
        # If @dependencies is nil, this Dependency has been removed
        @dependencies.delete(current) if @dependencies
      end
    end
  end
end

#removeObject

Called when a dependency is no longer needed



67
68
69
70
# File 'lib/volt/reactive/dependency.rb', line 67

def remove
  changed!
  @dependencies = nil
end