maybeRebroadcastQueries method

bool maybeRebroadcastQueries({
  1. ObservableQuery<Object?>? exclude,
  2. bool force = false,
})

Rebroadcast cached queries with changed underlying data if cache.broadcastRequested or force.

Push changed data from cache to query streams. exclude is used to skip a query if it was recently executed (normally the query that caused the rebroadcast)

Returns whether a broadcast was executed, which depends on the state of the cache. If there are multiple in-flight cache updates, we wait until they all complete

Note on internal implementation details: There is sometimes confusion on when this is called, but rebroadcasts are requested from every addQueryResult where result.isNotLoading as an OnData callback from ObservableQuery.

Implementation

bool maybeRebroadcastQueries({
  ObservableQuery<Object?>? exclude,
  bool force = false,
}) {
  if (rebroadcastLocked && !force) {
    return false;
  }

  final shouldBroadast = cache.shouldBroadcast(claimExecution: true);

  if (!shouldBroadast && !force) {
    return false;
  }

  for (var query in queries.values) {
    if (query != exclude && query.isRebroadcastSafe) {
      final cachedData = cache.readQuery(
        query.options.asRequest,
        optimistic: query.options.policies.mergeOptimisticData,
      );
      if (_cachedDataHasChangedFor(query, cachedData)) {
        query.addResult(
          mapFetchResultToQueryResult(
            Response(data: cachedData, response: {}),
            query.options,
            source: QueryResultSource.cache,
          ),
          fromRebroadcast: true,
        );
      }
    }
  }
  return true;
}