notification_helper.dart 911 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import 'dart:typed_data';
  2. import 'package:appflowy_backend/protobuf/flowy-notification/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({
  10. this.id,
  11. required this.callback,
  12. required this.errorParser,
  13. required this.tyParser,
  14. });
  15. void parse(SubscribeObject subject) {
  16. if (id != null) {
  17. if (subject.id != id) {
  18. return;
  19. }
  20. }
  21. final ty = tyParser(subject.ty);
  22. if (ty == null) {
  23. return;
  24. }
  25. if (subject.hasError()) {
  26. final bytes = Uint8List.fromList(subject.error);
  27. final error = errorParser(bytes);
  28. callback(ty, right(error));
  29. } else {
  30. final bytes = Uint8List.fromList(subject.payload);
  31. callback(ty, left(bytes));
  32. }
  33. }
  34. }