Class: JSI::Util::Private::MemoMap::Mutable Private

Inherits:
JSI::Util::Private::MemoMap show all
Defined in:
lib/jsi/util/private/memo_map.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from JSI::Util::Private::MemoMap

#initialize, #key_for

Constructor Details

This class inherits a constructor from JSI::Util::Private::MemoMap

Instance Method Details

#[](**inputs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jsi/util/private/memo_map.rb', line 37

def [](**inputs)
  key = key_for(inputs)

  result_mutex = @result_mutexes_mutex.synchronize do
    @result_mutexes[key] ||= Mutex.new
  end

  result_mutex.synchronize do
    inputs_hash = inputs.hash
    if @results.key?(key) && inputs_hash == @results[key].inputs_hash && inputs == @results[key].inputs
      @results[key].value
    else
      value = @block.call(**inputs)
      @results[key] = Result.new(value: value, inputs: inputs, inputs_hash: inputs_hash)
      value
    end
  end
end