2021-09-07 17:12:03 +08:00
|
|
|
import 'dart:typed_data';
|
2021-12-06 14:41:09 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/dart-notify/protobuf.dart';
|
2021-09-08 13:50:20 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart';
|
2021-11-08 10:25:10 +08:00
|
|
|
import 'package:dartz/dartz.dart';
|
2021-12-06 14:41:09 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-core/errors.pb.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-core/observable.pb.dart';
|
2021-09-07 17:12:03 +08:00
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
typedef UserNotificationCallback = void Function(UserNotification, Either<Uint8List, UserError>);
|
2021-09-08 13:50:20 +08:00
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
class UserNotificationParser extends NotificationParser<UserNotification, UserError> {
|
|
|
|
UserNotificationParser({required String id, required UserNotificationCallback callback})
|
2021-09-08 13:50:20 +08:00
|
|
|
: super(
|
|
|
|
id: id,
|
|
|
|
callback: callback,
|
2021-10-14 14:34:22 +08:00
|
|
|
tyParser: (ty) => UserNotification.valueOf(ty),
|
2021-09-08 13:50:20 +08:00
|
|
|
errorParser: (bytes) => UserError.fromBuffer(bytes),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
typedef NotificationCallback = void Function(WorkspaceNotification, Either<Uint8List, WorkspaceError>);
|
2021-09-08 13:50:20 +08:00
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
class WorkspaceNotificationParser extends NotificationParser<WorkspaceNotification, WorkspaceError> {
|
|
|
|
WorkspaceNotificationParser({String? id, required NotificationCallback callback})
|
2021-09-08 13:50:20 +08:00
|
|
|
: super(
|
|
|
|
id: id,
|
|
|
|
callback: callback,
|
2021-10-14 14:34:22 +08:00
|
|
|
tyParser: (ty) => WorkspaceNotification.valueOf(ty),
|
2021-09-08 13:50:20 +08:00
|
|
|
errorParser: (bytes) => WorkspaceError.fromBuffer(bytes),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:11:45 +08:00
|
|
|
class NotificationParser<T, E> {
|
2021-10-14 14:34:22 +08:00
|
|
|
String? id;
|
2021-09-08 13:50:20 +08:00
|
|
|
void Function(T, Either<Uint8List, E>) callback;
|
2021-09-07 17:12:03 +08:00
|
|
|
|
2021-09-08 13:50:20 +08:00
|
|
|
T? Function(int) tyParser;
|
|
|
|
E Function(Uint8List) errorParser;
|
2021-09-07 17:12:03 +08:00
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
NotificationParser({this.id, required this.callback, required this.errorParser, required this.tyParser});
|
|
|
|
void parse(SubscribeObject subject) {
|
|
|
|
if (id != null) {
|
|
|
|
if (subject.id != id) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-07 17:12:03 +08:00
|
|
|
}
|
|
|
|
|
2021-09-08 13:50:20 +08:00
|
|
|
final ty = tyParser(subject.ty);
|
2021-09-07 17:12:03 +08:00
|
|
|
if (ty == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-17 23:08:39 +08:00
|
|
|
if (subject.hasError()) {
|
2021-09-07 17:12:03 +08:00
|
|
|
final bytes = Uint8List.fromList(subject.error);
|
2021-09-08 13:50:20 +08:00
|
|
|
final error = errorParser(bytes);
|
2021-09-07 17:12:03 +08:00
|
|
|
callback(ty, right(error));
|
|
|
|
} else {
|
2021-10-17 23:08:39 +08:00
|
|
|
final bytes = Uint8List.fromList(subject.payload);
|
|
|
|
callback(ty, left(bytes));
|
2021-09-07 17:12:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|