Sha256: b2336c73d56d141ab64b67f57cf6389e9e644059ac4ef50ab78f12da651b6743

Contents?: true

Size: 1.83 KB

Versions: 21

Compression:

Stored size: 1.83 KB

Contents

require 'set'

module Volt
  # Dependencies are used to track the current computation so it can be re-run
  # at a later point if this dependency changes.
  #
  # You can also pass an on_dep and on_stop_dep proc's to #initialize.
  class Dependency
    # Setup a new dependency.
    #
    # @param on_dep [Proc] a proc to be called the first time a computation depends
    #   on this dependency.
    # @param on_stop_dep [Proc] a proc to be called when no computations are depending
    #   on this dependency anymore.
    def initialize(on_dep = nil, on_stop_dep = nil)
      @dependencies = Set.new
      @on_dep = on_dep
      @on_stop_dep = on_stop_dep
    end

    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
          # The first time the dependency is depended on by this computation, we call on_dep
          @on_dep.call if @on_dep && @dependencies.size == 1

          current.on_invalidate do
            # If @dependencies is nil, this Dependency has been removed
            if @dependencies
              # For set, .delete returns a boolean if it was deleted
              deleted = @dependencies.delete(current)

              # Call on stop dep if no more deps
              @on_stop_dep.call if @on_stop_dep && deleted && @dependencies.size == 0
            end
          end
        end
      end
    end

    def changed!
      deps = @dependencies

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

      @dependencies = Set.new

      deps.each(&:invalidate!)

      @on_stop_dep.call if @on_stop_dep
    end

    # Called when a dependency is no longer needed
    def remove
      changed!
      @dependencies = nil
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
volt-0.9.5 lib/volt/reactive/dependency.rb
volt-0.9.5.pre12 lib/volt/reactive/dependency.rb
volt-0.9.5.pre11 lib/volt/reactive/dependency.rb
volt-0.9.5.pre9 lib/volt/reactive/dependency.rb
volt-0.9.5.pre8 lib/volt/reactive/dependency.rb
volt-0.9.5.pre7 lib/volt/reactive/dependency.rb
volt-0.9.5.pre6 lib/volt/reactive/dependency.rb
volt-0.9.5.pre5 lib/volt/reactive/dependency.rb
volt-0.9.5.pre4 lib/volt/reactive/dependency.rb
volt-0.9.5.pre3 lib/volt/reactive/dependency.rb
volt-0.9.5.pre2 lib/volt/reactive/dependency.rb
volt-0.9.5.pre1 lib/volt/reactive/dependency.rb
volt-0.9.4 lib/volt/reactive/dependency.rb
volt-0.9.4.pre5 lib/volt/reactive/dependency.rb
volt-0.9.4.pre3 lib/volt/reactive/dependency.rb
volt-0.9.4.pre2 lib/volt/reactive/dependency.rb
volt-0.9.4.pre1 lib/volt/reactive/dependency.rb
volt-0.9.3 lib/volt/reactive/dependency.rb
volt-0.9.3.pre6 lib/volt/reactive/dependency.rb
volt-0.9.3.pre5 lib/volt/reactive/dependency.rb