watchAll method

  1. @protected
  2. @visibleForTesting
DataStateNotifier<List<T>> watchAll({
  1. bool? remote,
  2. Map<String, dynamic>? params,
  3. Map<String, String>? headers,
  4. bool? syncLocal,
  5. bool filterLocal(
    1. T
    )?,
})
inherited

Implementation

@protected
@visibleForTesting
DataStateNotifier<List<T>> watchAll({
  bool? remote,
  Map<String, dynamic>? params,
  Map<String, String>? headers,
  bool? syncLocal,
  bool Function(T)? filterLocal,
}) {
  _assertInit();
  remote ??= _remote;
  syncLocal ??= false;
  filterLocal ??= (_) => true;

  final _notifier = DataStateNotifier<List<T>>(
    data: DataState(localAdapter
        .findAll()
        .where(filterLocal)
        .map((m) => initializeModel(m, save: true))
        .filterNulls
        .toList()),
    reload: (notifier) async {
      if (!notifier.mounted) {
        return;
      }
      try {
        final _future = findAll(
          params: params,
          headers: headers,
          remote: remote,
          syncLocal: syncLocal,
          filterLocal: filterLocal,
        );
        if (remote!) {
          notifier.updateWith(isLoading: true);
        }
        await _future;
        // trigger doneLoading to ensure state is updated with isLoading=false
        graph._notify([internalType], DataGraphEventType.doneLoading);
      } on DataException catch (e) {
        // we're only interested in notifying errors
        // as models will pop up via the graph notifier
        // (we can catch the exception as we are NOT supplying
        // an `onError` to `findAll`)
        if (notifier.mounted) {
          notifier.updateWith(isLoading: false, exception: e);
        } else {
          rethrow;
        }
      }
    },
  );

  // kick off
  _notifier.reload();

  final _dispose = throttledGraph.addListener((events) {
    if (!_notifier.mounted) {
      return;
    }

    final models =
        localAdapter.findAll().where(filterLocal!).toImmutableList();
    final modelChanged =
        !const DeepCollectionEquality().equals(models, _notifier.data.model);
    // ensure the done signal belongs to this notifier
    final doneLoading = events.singleWhereOrNull((e) =>
            e.type == DataGraphEventType.doneLoading &&
            e.keys.first == internalType) !=
        null;
    if (modelChanged || doneLoading) {
      _notifier.updateWith(model: models, isLoading: false, exception: null);
    }
  });

  _notifier.onDispose = _dispose;
  return _notifier;
}