clearScope method

void clearScope(
  1. String scopeId
)

Clear all queries and stream queries in a specific scope

Implementation

void clearScope(String scopeId) {
  final scopeQueries = _scopeQueries[scopeId];
  final scopeStreams = _scopeStreamQueries[scopeId];
  if (scopeQueries == null && scopeStreams == null) return;

  int clearedCount = 0;
  if (scopeQueries != null) {
    final keysToRemove = List<String>.from(scopeQueries);
    for (final key in keysToRemove) {
      _cache[key]?.expiryTimer?.cancel();
      _cache.remove(key);
      _queries.remove(key);
      _pendingFetches.remove(key);
      _unindexTags(key);
    }
    clearedCount += keysToRemove.length;
    _scopeQueries.remove(scopeId);
  }

  if (scopeStreams != null) {
    final streamKeys = List<String>.from(scopeStreams);
    for (final key in streamKeys) {
      _cache[key]?.expiryTimer?.cancel();
      _cache.remove(key);
      final streamQuery = _streamQueries.remove(key);
      _unindexTags(key);
      if (streamQuery != null && !streamQuery.isDisposed) {
        streamQuery.dispose();
      }
    }
    clearedCount += streamKeys.length;
    _scopeStreamQueries.remove(scopeId);
  }

  ZenLogger.logDebug('Cleared $clearedCount queries from scope: $scopeId');
}