updateComputed<T> function

bool updateComputed<T>(
  1. ComputedReactiveNode<T> computed
)

Recomputes a ComputedReactiveNode and reports whether the pending value changed.

Parameters:

  • computed: Computed node whose getter should be invoked

Example:

final computedNode = CustomComputedNode<int>(() => 0);
if (updateComputed(computedNode) && computedNode.subs != null) {
  shallowPropagate(computedNode.subs!);
}

Implementation

@pragma("vm:prefer-inline")
@pragma("wasm:prefer-inline")
@pragma("dart2js:prefer-inline")
bool updateComputed<T>(ComputedReactiveNode<T> computed) {
  ++cycle;
  computed
    ..depsTail = null
    ..flags = ReactiveFlags.mutable | ReactiveFlags.recursedCheck;
  final prevSub = setActiveSub(computed);

  try {
    final oldValue = computed.pendingValue;
    final newValue = computed.getter();
    final equals = computed.equals == null
        ? oldValue == newValue
        : computed.equals!(newValue as Object, oldValue as Object);
    if (equals) {
      return false;
    }
    computed.pendingValue = newValue;
    return true;
  } finally {
    activeSub = prevSub;
    computed.flags &= ~ReactiveFlags.recursedCheck;
    purgeDeps(computed);
  }
}