watch<EntityT> method

Stream<void> watch<EntityT>()

Create a stream to data changes on EntityT (stored Entity class).

The stream receives an event whenever an object of EntityT is created or changed or deleted. Make sure to cancel() the subscription after you're done with it to avoid hanging change listeners.

Implementation

Stream<void> watch<EntityT>() {
  if (_entityChanges != null) {
    return _entityChanges!
        .where((List<Type> entities) => entities.contains(EntityT))
        .map((_) {});
  }

  final observer = _Observer<void>();
  final entityId = _entityDef<EntityT>().model.id.id;

  // We're listening to events on single entity so there's no argument.
  // Ideally, controller.add() would work but it doesn't, even though we're
  // using StreamController<Void> so the argument type is `void`.
  observer.receivePort.listen((dynamic _) => observer.controller.add(null));

  observer.init(() {
    observer.cObserver =
        C.dartc_observe_single_type(_ptr, entityId, observer.nativePort);
  });

  return observer.stream;
}