notifyListeners<T> method

void notifyListeners<T>(
  1. String? tag
)

Optimized listener notification with memory safety

Implementation

void notifyListeners<T>(String? tag) {
  final key = _getKey(T, tag);
  final listenerSet = _listeners[key];

  // Early return for empty cases - zero allocations
  if (listenerSet == null || listenerSet.isEmpty) return;

  _notificationCount++;

  // Single listener optimization - no iteration overhead
  if (listenerSet.length == 1) {
    _safeNotify(listenerSet.first, T);
    return;
  }

  // Multiple listeners - direct Set iteration (no toList())
  for (final listener in listenerSet) {
    _safeNotify(listener, T);
  }

  // Periodic cleanup check
  if (_notificationCount % _cleanupThreshold == 0) {
    _performMaintenanceCheck();
  }
}