feat: use result instead of either (#4724)

* feat: use result instead of either

* chore: remove dartz
This commit is contained in:
Lucas.Xu
2024-02-24 20:54:10 +07:00
committed by GitHub
parent 236b5bfe90
commit 2abb396467
190 changed files with 1813 additions and 1527 deletions

View File

@ -1,37 +1,37 @@
import 'dart:async';
import 'dart:convert' show utf8;
import 'dart:ffi';
import 'package:dartz/dartz.dart';
import 'dart:typed_data';
import 'package:appflowy_backend/ffi.dart' as ffi;
import 'package:appflowy_backend/log.dart';
// ignore: unnecessary_import
import 'package:appflowy_backend/protobuf/dart-ffi/ffi_response.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:isolates/isolates.dart';
import 'package:isolates/ports.dart';
import 'package:ffi/ffi.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy_backend/ffi.dart' as ffi;
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_backend/protobuf/dart-ffi/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-document/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:ffi/ffi.dart';
import 'package:flutter/services.dart';
import 'package:isolates/isolates.dart';
import 'package:isolates/ports.dart';
import 'package:protobuf/protobuf.dart';
import 'dart:convert' show utf8;
import '../protobuf/flowy-config/entities.pb.dart';
import '../protobuf/flowy-config/event_map.pb.dart';
import 'error.dart';
import '../protobuf/flowy-date/entities.pb.dart';
import '../protobuf/flowy-date/event_map.pb.dart';
import 'error.dart';
part 'dart_event/flowy-config/dart_event.dart';
part 'dart_event/flowy-database2/dart_event.dart';
part 'dart_event/flowy-date/dart_event.dart';
part 'dart_event/flowy-document/dart_event.dart';
part 'dart_event/flowy-folder/dart_event.dart';
part 'dart_event/flowy-user/dart_event.dart';
part 'dart_event/flowy-database2/dart_event.dart';
part 'dart_event/flowy-document/dart_event.dart';
part 'dart_event/flowy-config/dart_event.dart';
part 'dart_event/flowy-date/dart_event.dart';
enum FFIException {
RequestIsEmpty,
@ -43,7 +43,8 @@ class DispatchException implements Exception {
}
class Dispatch {
static Future<Either<Uint8List, Uint8List>> asyncRequest(FFIRequest request) {
static Future<FlowyResult<Uint8List, Uint8List>> asyncRequest(
FFIRequest request) {
// FFIRequest => Rust SDK
final bytesFuture = _sendToRust(request);
@ -57,43 +58,43 @@ class Dispatch {
}
}
Future<Either<Uint8List, Uint8List>> _extractPayload(
Future<Either<FFIResponse, FlowyInternalError>> responseFuture) {
Future<FlowyResult<Uint8List, Uint8List>> _extractPayload(
Future<FlowyResult<FFIResponse, FlowyInternalError>> responseFuture) {
return responseFuture.then((result) {
return result.fold(
(response) {
switch (response.code) {
case FFIStatusCode.Ok:
return left(Uint8List.fromList(response.payload));
return FlowySuccess(Uint8List.fromList(response.payload));
case FFIStatusCode.Err:
return right(Uint8List.fromList(response.payload));
return FlowyFailure(Uint8List.fromList(response.payload));
case FFIStatusCode.Internal:
final error = utf8.decode(response.payload);
Log.error("Dispatch internal error: $error");
return right(emptyBytes());
return FlowyFailure(emptyBytes());
default:
Log.error("Impossible to here");
return right(emptyBytes());
return FlowyFailure(emptyBytes());
}
},
(error) {
Log.error("Response should not be empty $error");
return right(emptyBytes());
return FlowyFailure(emptyBytes());
},
);
});
}
Future<Either<FFIResponse, FlowyInternalError>> _extractResponse(
Future<FlowyResult<FFIResponse, FlowyInternalError>> _extractResponse(
Completer<Uint8List> bytesFuture) {
return bytesFuture.future.then((bytes) {
try {
final response = FFIResponse.fromBuffer(bytes);
return left(response);
return FlowySuccess(response);
} catch (e, s) {
final error = StackTraceError(e, s);
Log.error('Deserialize response failed. ${error.toString()}');
return right(error.asFlowyError());
return FlowyFailure(error.asFlowyError());
}
});
}

View File

@ -14,11 +14,12 @@ dependencies:
ffi: ^2.0.2
isolates: ^3.0.3+8
protobuf: ^3.1.0
dartz: ^0.10.1
freezed_annotation:
logger: ^2.0.0
plugin_platform_interface: ^2.1.3
json_annotation: ^4.7.0
appflowy_result:
path: ../appflowy_result
dev_dependencies:
flutter_test: