notifyDependents method

  1. @protected
void notifyDependents()

Notifies all dependents and listeners that this node changed.

Implementation

@protected
void notifyDependents() {
  if (_isDisposed) return;

  if (ReactiveScope.isBatching) {
    ReactiveScope.schedulePending(this);
    return;
  }

  final deps = _dependents;
  final listeners = _listeners;

  // Fast path: nothing to notify.
  if (deps == null && listeners == null) return;

  _isNotifying = true;

  // Notify dependent reactive nodes.
  // We must snapshot because dependents may re-register during notification
  // (computed nodes clear and re-track their dependencies).
  if (deps != null && deps.isNotEmpty) {
    final snapshot = deps.toList(growable: false);
    for (var i = 0; i < snapshot.length; i++) {
      final dep = snapshot[i];
      if (!dep.isDisposed) {
        dep.onDependencyChanged(this);
      }
    }
  }

  // Notify imperative listeners (iterate by index — no allocation).
  // Listeners that remove themselves during callback are handled safely
  // via snapshot if the list was modified during iteration.
  if (listeners != null && listeners.isNotEmpty) {
    for (var i = 0; i < listeners.length; i++) {
      listeners[i]();
    }
  }

  _isNotifying = false;
}