unreadNotificationCountStream property

  1. @override
Stream<int> get unreadNotificationCountStream
override

The live unread count. See AppwinCommunity.unreadNotificationCountStream.

Implementation

@override
Stream<int> get unreadNotificationCountStream {
  return Stream.multi((listener) {
    _unreadWatchers.add(listener);
    // Native emits the known count only to a fresh subscription, which a
    // second listener does not cause.
    final known = _unreadLatest;
    if (known != null) listener.add(known);
    _unreadNative ??= unreadCountChannel.receiveBroadcastStream().listen(
      (raw) {
        if (raw is! int || raw == _unreadLatest) return;
        _unreadLatest = raw;
        for (final watcher in _unreadWatchers.toList()) {
          watcher.add(raw);
        }
      },
      onError: (Object error) {
        if (kDebugMode) {
          debugPrint('[Appwin] unreadNotificationCountStream: bridge error: $error');
        }
      },
    );
    listener.onCancel = () {
      _unreadWatchers.remove(listener);
      if (_unreadWatchers.isNotEmpty) return;
      _unreadNative?.cancel();
      _unreadNative = null;
      _unreadLatest = null;
    };
  });
}