watch method
Finish building a Query creating a stream that issues events whenever queried entity changes. Streamed query is persisted between stream events and closed when you cancel the subscription.
if you pass TRUE as the triggerImmediately
argument, a single stream
event will be sent immediately after subscription. You can use this to get
access to the query object before any data changes.
Implementation
Stream<Query<T>> watch({bool triggerImmediately = false}) {
final queriedEntities = HashSet<Type>();
_fillQueriedEntities(queriedEntities);
final query = build();
late StreamSubscription<void> subscription;
late StreamController<Query<T>> controller;
final subscribe = () {
subscription = _store.entityChanges.listen((List<Type> entityTypes) {
if (entityTypes.any(queriedEntities.contains)) {
controller.add(query);
}
});
};
controller = StreamController<Query<T>>(
onListen: subscribe,
onResume: subscribe,
onPause: () => subscription.pause(),
onCancel: () => subscription.cancel());
if (triggerImmediately) controller.add(query);
return controller.stream;
}