wrap method

Computed<V> wrap(
  1. K key,
  2. V computation()
)

Returns a computation that creates and uses a leader for the given key.

The leader, in turn, runs the given computation. For each key there is at most a single leader. This means the given computation will be run at most once per upstream changes no matter how many wrap calls with the same key was made.

Implementation

Computed<V> wrap(K key, V Function() computation) {
  final cachedComputation = _m[key];
  if (cachedComputation != null) return cachedComputation;
  late final Computed<V> newComputation;

  void onCancel() {
    final cachedComputation = _m[key];
    if (identical(cachedComputation, newComputation)) {
      final removed = _m.remove(key);
      assert(removed != null);
    }
    if (_onCancel != null) _onCancel();
  }

  newComputation = Computed(() {
    final cachedComputation = _m[key];
    if (cachedComputation != null &&
        !identical(cachedComputation, newComputation)) {
      return cachedComputation.use;
    }
    // No cached result
    _m[key] = newComputation;
    return computation();
  },
      memoized: _memoized,
      assertIdempotent: _assertIdempotent,
      dispose: _dispose,
      onCancel: onCancel);
  return newComputation;
}