import 'dart:async'; import 'dart:typed_data'; import 'package:appflowy_backend/protobuf/flowy-notification/protobuf.dart'; import 'package:dartz/dartz.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-grid/notification.pb.dart'; import 'package:appflowy_backend/rust_stream.dart'; import 'notification_helper.dart'; // GridPB typedef GridNotificationCallback = void Function( GridDartNotification, Either); class GridNotificationParser extends NotificationParser { GridNotificationParser( {String? id, required GridNotificationCallback callback}) : super( id: id, callback: callback, tyParser: (ty) => GridDartNotification.valueOf(ty), errorParser: (bytes) => FlowyError.fromBuffer(bytes), ); } typedef GridNotificationHandler = Function( GridDartNotification ty, Either result); class GridNotificationListener { StreamSubscription? _subscription; GridNotificationParser? _parser; GridNotificationListener( {required String objectId, required GridNotificationHandler handler}) : _parser = GridNotificationParser(id: objectId, callback: handler) { _subscription = RustStreamReceiver.listen((observable) => _parser?.parse(observable)); } Future stop() async { _parser = null; await _subscription?.cancel(); _subscription = null; } }