notification_helper.dart 876 B

12345678910111213141516171819202122232425262728293031323334
  1. import 'dart:typed_data';
  2. import 'package:flowy_sdk/protobuf/dart-notify/protobuf.dart';
  3. import 'package:dartz/dartz.dart';
  4. class NotificationParser<T, E> {
  5. String? id;
  6. void Function(T, Either<Uint8List, E>) callback;
  7. T? Function(int) tyParser;
  8. E Function(Uint8List) errorParser;
  9. NotificationParser({this.id, required this.callback, required this.errorParser, required this.tyParser});
  10. void parse(SubscribeObject subject) {
  11. if (id != null) {
  12. if (subject.id != id) {
  13. return;
  14. }
  15. }
  16. final ty = tyParser(subject.ty);
  17. if (ty == null) {
  18. return;
  19. }
  20. if (subject.hasError()) {
  21. final bytes = Uint8List.fromList(subject.error);
  22. final error = errorParser(bytes);
  23. callback(ty, right(error));
  24. } else {
  25. final bytes = Uint8List.fromList(subject.payload);
  26. callback(ty, left(bytes));
  27. }
  28. }
  29. }