append<T> method

T append<T>(
  1. NotifyData data,
  2. T builder()
)

Implementation

T append<T>(NotifyData data, T Function() builder) {
  final oldData = _notifyData;
  final active = data.activeSources;
  active?.clear();
  _notifyData = data;
  try {
    final result = builder();
    final hasSubscriptions = data.disposers.isNotEmpty ||
        (active != null && active.isNotEmpty);
    if (!hasSubscriptions && data.throwException) {
      throw ObxError();
    }
    return result;
  } finally {
    final tracked = data.trackedSubscriptions;
    if (tracked != null &&
        active != null &&
        tracked.length > active.length) {
      List<ListNotifier>? stale;
      for (final source in tracked.keys) {
        if (!active.contains(source)) {
          (stale ??= <ListNotifier>[]).add(source);
        }
      }
      if (stale != null) {
        for (final source in stale) {
          tracked.remove(source)?.call();
        }
      }
    }
    active?.clear();
    _notifyData = oldData;
  }
}