Sha256: 6ef7e9a57d73fc56dca55bf42a1886fac1dae73f7a57ef58fbf81c12b6ba17ba

Contents?: true

Size: 642 Bytes

Versions: 16

Compression:

Stored size: 642 Bytes

Contents

module LaunchDarkly
  # Simple implementation of a thread-safe memoized value whose generator function will never be
  # run more than once, and whose value can be overridden by explicit assignment.
  class MemoizedValue
    def initialize(&generator)
      @generator = generator
      @mutex = Mutex.new
      @inited = false
      @value = nil
    end

    def get
      @mutex.synchronize do
        if !@inited
          @value = @generator.call
          @inited = true
        end
      end
      @value
    end

    def set(value)
      @mutex.synchronize do
        @value = value
        @inited = true
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ldclient-rb-5.4.3 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.4.2 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.4.1 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.3.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.2.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.1.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.0.1 lib/ldclient-rb/memoized_value.rb
ldclient-rb-5.0.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-4.0.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-3.0.3 lib/ldclient-rb/memoized_value.rb
ldclient-rb-3.0.2 lib/ldclient-rb/memoized_value.rb
ldclient-rb-3.0.1 lib/ldclient-rb/memoized_value.rb
ldclient-rb-3.0.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-2.5.0 lib/ldclient-rb/memoized_value.rb
ldclient-rb-2.4.1 lib/ldclient-rb/memoized_value.rb
ldclient-rb-2.4.0 lib/ldclient-rb/memoized_value.rb