get method

TimedComputeOnce<V> get(
  1. K key,
  2. ComputeCall<V> call, {
  3. PosComputeCall<V>? posCompute,
  4. bool resolve = true,
})

Returns an existing TimedComputeOnce for key or creates a new one.

  • call is executed at most once per key while retained.
  • posCompute, if provided, is invoked after resolution and before the end of the retention period.
  • If resolve is true, the computation may resolve eagerly.

Implementation

TimedComputeOnce<V> get(K key, ComputeCall<V> call,
    {PosComputeCall<V>? posCompute, bool resolve = true}) {
  var computer = _calls[key];
  if (computer != null) {
    return computer;
  }

  computer = _calls[key] =
      TimedComputeOnce(call, posCompute: posCompute, resolve: resolve);

  computer.whenResolved((v, e, s) {
    final prev = _calls[key];
    if (!identical(prev, computer)) return;

    if (retentionDuration == Duration.zero) {
      _removeInternal(key);
    } else {
      _timers[key]?.cancel();
      _timers[key] = Timer(retentionDuration, () {
        final prev = _calls[key];
        if (identical(prev, computer)) {
          _removeInternal(key);
        }
      });
    }
  });

  return computer;
}