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

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

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.

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.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jsi/util/private/memo_map.rb', line 28

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
    result_value, result_inputs, result_inputs_hash = @results[key]
    if inputs_hash == result_inputs_hash && inputs == result_inputs
      result_value
    else
      value = @block.call(**inputs)
      @results[key] = [value, inputs, inputs_hash]
      value
    end
  end
end