Sha256: e51b2804a9b7f79bf67f1380f0869ffea1435676243c6d071ab316f0c0134121

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.1.0
# @version 0.3.0
class SmartCore::Injection::Locator::Dependency
  # @option memoize [Boolean]
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  # @version 0.3.0
  def initialize(memoize:)
    @binded = false
    @value = nil
    @memoize = memoize
    @barrier = SmartCore::Engine::Lock.new
  end

  # @param block [Block]
  # @return [Any]
  #
  # @api private
  # @since 0.1.0
  def rebind(&block)
    with_barrier do
      @binded = false
      bind(&block)
    end
  end

  # @param block [Block]
  # @return [Any]
  #
  # @api public
  # @since 0.2.0
  # @version 0.3.0
  def bind(&block)
    with_barrier do
      # NOTE:
      #   Temporary disabled old variant of dependency resolving
      #   cuz we need to reivew the canonical way of dependency resolving
      #   and rework the resolving at all on runtime level (under `memoize: true` option).
      #   Old code variant:
      #
      #   if @binded
      #     @value
      #   else
      #     @binded = true
      #     @value = yield
      #   end

      if @memoize
        if @binded
          @value
        else
          @binded = true
          @value = yield
        end
      else
        yield
      end
    end
  end

  private

  # @param block [Block]
  # @return [Any]
  #
  # @api private
  # @since 0.1.0
  def with_barrier(&block)
    @barrier.synchronize(&block)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_injection-0.3.0 lib/smart_core/injection/locator/dependency.rb