fragment<T> method

  1. @protected
T fragment<T>(
  1. FragmentBuilder<T> builder, {
  2. required Iterable deps,
  3. Object? group,
})

Create a cached subtree.

builder will be called only when deps is different from deps provided in previous build (defined by IterableEquality().equals).

deps would be compared in order, e.g. the first fragment call in current build compares its deps with the first fragment call in previous build, the second fragment call to previous' second, etc.

If group is provided, fragments within the same group (defined by ==) would be compared with the above logic.

If current build is the first pass or the cached value is not of type V, builder would be called with null prevValue and prevDeps.

Implementation

@protected
T fragment<T>(FragmentBuilder<T> builder,
    {required Iterable deps, Object? group}) {
  _didPrepareBuild = false;

  group ??= const Object();
  final Iterator<_Cached>? prevDepsIter =
      _cursors[group] ??= _previous[group]?.iterator;

  final _Cached? cached =
      prevDepsIter?.moveNext() != null ? prevDepsIter?.current : null;
  if (cached is _Cached<T> && shallowEquals(cached.deps, deps)) {
    (_next[group] ??= []).add(cached);
    return cached.value;
  }

  final value =
      builder(cached?.value is T ? cached?.value : null, cached?.deps);
  (_next[group] ??= []).add(_Cached(deps, value));
  return value;
}