checkDirty method

bool checkDirty(
  1. Link checkLink,
  2. ReactiveNode sub
)

Checks if a node or any of its dependencies are dirty and need updating.

This method:

  • Traverses the dependency graph starting from checkLink
  • Checks if sub or any of its dependencies are dirty (ReactiveFlags.dirty)
  • Updates nodes as needed during the traversal
  • Returns true if any dirty nodes were found, false otherwise

Implementation

bool checkDirty(Link checkLink, ReactiveNode sub) {
  Stack<Link>? stack;
  int checkDepth = 0;
  bool dirty = false;
  Link? link = checkLink;

  top:
  do {
    final dep = link!.dep;
    final flags = dep.flags;

    if ((sub.flags & 16 /* Dirty */) != 0) {
      dirty = true;
    } else if ((flags & 17 /* Mutable | Dirty */) ==
        17 /* Mutable | Dirty */) {
      if (update(dep)) {
        final subs = dep.subs;
        if (subs?.nextSub != null) {
          shallowPropagate(subs!);
        }
        dirty = true;
      }
    } else if ((flags & 33 /* Mutable | Pending */) ==
        33 /* Mutable | Pending */) {
      if (link.nextSub != null || link.prevSub != null) {
        stack = Stack(value: link, prev: stack);
      }
      link = dep.deps!;
      sub = dep;
      ++checkDepth;
      continue;
    }

    if (!dirty) {
      final nextDep = link.nextDep;
      if (nextDep != null) {
        link = nextDep;
        continue;
      }
    }

    while ((checkDepth--) > 0) {
      final firstSub = sub.subs!;
      final hasMultipleSubs = firstSub.nextSub != null;
      if (hasMultipleSubs) {
        link = stack!.value;
        stack = stack.prev;
      } else {
        link = firstSub;
      }
      if (dirty) {
        if (update(sub)) {
          if (hasMultipleSubs) {
            shallowPropagate(firstSub);
          }
          sub = link.sub;
          continue;
        }
        dirty = false;
      } else {
        sub.flags &= -33 /* ~Pending */;
      }
      sub = link.sub;
      final nextDep = link.nextDep;
      if (nextDep != null) {
        link = nextDep;
        continue top;
      }
    }

    return dirty;
  } while (true);
}