queryListStream<T> method

Stream<List<T>> queryListStream<T>(
  1. String sql, {
  2. List<Object>? arguments,
  3. required String queryableName,
  4. required bool isView,
  5. required T mapper(
    1. Map<String, Object?>
    ),
})

Executes a SQLite query that returns a stream of multiple query results.

Implementation

Stream<List<T>> queryListStream<T>(
  final String sql, {
  final List<Object>? arguments,
  required final String queryableName,
  required final bool isView,
  required final T Function(Map<String, Object?>) mapper,
}) {
  // ignore: close_sinks
  final changeListener = ArgumentError.checkNotNull(_changeListener);
  final controller = StreamController<List<T>>.broadcast();

  Future<void> executeQueryAndNotifyController() async {
    final result = await queryList(sql, arguments: arguments, mapper: mapper);
    controller.add(result);
  }

  controller.onListen = () async => executeQueryAndNotifyController();

  // Views listen on all events, Entities only on events that changed the same entity.
  final subscription = changeListener.stream
      .where((updatedTable) =>
          isView || updatedTable.equals(queryableName, ignoreCase: true))
      .listen(
        (_) async => executeQueryAndNotifyController(),
        onDone: () => controller.close(),
      );

  controller.onCancel = () => subscription.cancel();

  return controller.stream;
}