notification_helper.dart 915 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. void parse(SubscribeObject subject) {
  15. if (id != null) {
  16. if (subject.id != id) {
  17. return;
  18. }
  19. }
  20. final ty = tyParser(subject.ty);
  21. if (ty == null) {
  22. return;
  23. }
  24. if (subject.hasError()) {
  25. final bytes = Uint8List.fromList(subject.error);
  26. final error = errorParser(bytes);
  27. callback(ty, right(error));
  28. } else {
  29. final bytes = Uint8List.fromList(subject.payload);
  30. callback(ty, left(bytes));
  31. }
  32. }
  33. }