user_notification.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'dart:async';
  2. import 'dart:typed_data';
  3. import 'package:appflowy_backend/protobuf/flowy-notification/protobuf.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  5. import 'package:dartz/dartz.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:appflowy_backend/rust_stream.dart';
  8. import 'notification_helper.dart';
  9. // User
  10. typedef UserNotificationCallback = void Function(
  11. UserNotification,
  12. Either<Uint8List, FlowyError>,
  13. );
  14. class UserNotificationParser
  15. extends NotificationParser<UserNotification, FlowyError> {
  16. UserNotificationParser({
  17. required String id,
  18. required UserNotificationCallback callback,
  19. }) : super(
  20. id: id,
  21. callback: callback,
  22. tyParser: (ty) => UserNotification.valueOf(ty),
  23. errorParser: (bytes) => FlowyError.fromBuffer(bytes),
  24. );
  25. }
  26. typedef UserNotificationHandler = Function(
  27. UserNotification ty,
  28. Either<Uint8List, FlowyError> result,
  29. );
  30. class UserNotificationListener {
  31. StreamSubscription<SubscribeObject>? _subscription;
  32. UserNotificationParser? _parser;
  33. UserNotificationListener({
  34. required String objectId,
  35. required UserNotificationHandler handler,
  36. }) : _parser = UserNotificationParser(id: objectId, callback: handler) {
  37. _subscription =
  38. RustStreamReceiver.listen((observable) => _parser?.parse(observable));
  39. }
  40. Future<void> stop() async {
  41. _parser = null;
  42. await _subscription?.cancel();
  43. }
  44. }