refactor(wyatt_architecture): upgrade usecases to add NoParams

This commit is contained in:
Hugo Pointcheval 2024-02-18 22:49:11 +01:00
parent 41382aad2f
commit 59bca9045e
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
31 changed files with 220 additions and 385 deletions

View File

@ -88,8 +88,8 @@ class PhotoRepositoryImpl extends PhotoRepository {
}
return Ok(response);
} catch (_) {
return const Err(
ClientException('Cannot retrieve all photos from favorites.'),
return Err(
const ClientException('Cannot retrieve all photos from favorites.'),
);
}
}

View File

@ -17,7 +17,7 @@
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
abstract class FavoriteLocalDataSource extends BaseLocalDataSource {
abstract class FavoriteLocalDataSource extends BaseDataSource {
Future<void> addPhotoToFavorites(Photo photo);
Future<void> deletePhotoFromFavorites(int id);
Future<List<int>> getAllPhotosFromFavorites();

View File

@ -17,7 +17,7 @@
import 'package:architecture_example/domain/entities/album.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
abstract class AlbumRemoteDataSource extends BaseRemoteDataSource {
abstract class AlbumRemoteDataSource extends BaseDataSource {
Future<Album> getAlbum(int id);
Future<List<Album>> getAllAlbums({int? start, int? limit});
}

View File

@ -17,7 +17,7 @@
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
abstract class PhotoRemoteDataSource extends BaseRemoteDataSource {
abstract class PhotoRemoteDataSource extends BaseDataSource {
Future<Photo> getPhoto(int id);
Future<List<Photo>> getAllPhotos({int? start, int? limit});
Future<List<Photo>> getPhotosFromAlbum(int albumId, {int? start, int? limit});

View File

@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class AddPhotoToFavorites extends AsyncUseCase<Photo, List<Photo>> {
AddPhotoToFavorites(this._photoRepository);
const AddPhotoToFavorites(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<List<Photo>> execute(Photo? params) async {
await _photoRepository.addPhotoToFavorites(params!);
FutureOrResult<List<Photo>> execute(Photo params) async {
await _photoRepository.addPhotoToFavorites(params);
return _photoRepository.getAllPhotosFromFavorites();
}
@override
FutureOr<void> onStart(Photo? params) {
if (params == null) {
throw const ClientException('Photo cannot be null');
}
}
}

View File

@ -14,23 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class CheckIfPhotoIsInFavorites extends AsyncUseCase<int, bool> {
CheckIfPhotoIsInFavorites(this._photoRepository);
const CheckIfPhotoIsInFavorites(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<bool> execute(int? params) async =>
_photoRepository.checkIfPhotoIsInFavorites(params!);
@override
FutureOr<void> onStart(int? params) {
if (params == null) {
throw const ClientException('id cannot be null');
}
}
FutureOrResult<bool> execute(int params) async =>
_photoRepository.checkIfPhotoIsInFavorites(params);
}

View File

@ -18,12 +18,12 @@ import 'package:architecture_example/domain/entities/photo.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class DisplayFavorites extends AsyncUseCase<NoParam, List<Photo>> {
DisplayFavorites(this._photoRepository);
class DisplayFavorites extends NoParamsAsyncUseCase<List<Photo>> {
const DisplayFavorites(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<List<Photo>> execute(void params) {
FutureOrResult<List<Photo>> execute() {
final photos = _photoRepository.getAllPhotosFromFavorites();
return photos;
}

View File

@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class DisplayPhoto extends AsyncUseCase<int, Photo> {
DisplayPhoto(this._photoRepository);
const DisplayPhoto(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<Photo> execute(int? params) {
final photo = _photoRepository.getPhoto(params!);
FutureOrResult<Photo> execute(int params) {
final photo = _photoRepository.getPhoto(params);
return photo;
}
@override
FutureOr<void> onStart(int? params) {
if (params == null) {
throw const ClientException('id cannot be null');
}
}
}

View File

@ -14,32 +14,23 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class OpenAlbum extends AsyncUseCase<QueryParameters, List<Photo>> {
OpenAlbum(this._photoRepository);
const OpenAlbum(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<List<Photo>> execute(QueryParameters? params) {
FutureOrResult<List<Photo>> execute(QueryParameters params) {
final photos = _photoRepository.getPhotosFromAlbum(
params!.albumId,
params.albumId,
start: params.start,
limit: params.limit,
);
return photos;
}
@override
FutureOr<void> onStart(QueryParameters? params) {
if (params == null) {
throw const ClientException('params cannot be null');
}
}
}

View File

@ -14,26 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:architecture_example/domain/entities/photo.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class RemovePhotoFromFavorites extends AsyncUseCase<int, List<Photo>> {
RemovePhotoFromFavorites(this._photoRepository);
const RemovePhotoFromFavorites(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<List<Photo>> execute(int? params) async {
await _photoRepository.deletePhotoFromFavorites(params!);
FutureOrResult<List<Photo>> execute(int params) async {
await _photoRepository.deletePhotoFromFavorites(params);
return _photoRepository.getAllPhotosFromFavorites();
}
@override
FutureOr<void> onStart(int? params) {
if (params == null) {
throw const ClientException('id cannot be null');
}
}
}

View File

@ -14,30 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:architecture_example/domain/entities/album.dart';
import 'package:architecture_example/domain/repositories/photo_repository.dart';
import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
class RetrieveAllAlbums extends AsyncUseCase<QueryParameters, List<Album>> {
RetrieveAllAlbums(this._photoRepository);
const RetrieveAllAlbums(this._photoRepository);
final PhotoRepository _photoRepository;
@override
FutureOrResult<List<Album>> execute(QueryParameters? params) {
FutureOrResult<List<Album>> execute(QueryParameters params) {
final albums = _photoRepository.getAllAlbums(
start: params!.start,
start: params.start,
limit: params.limit,
);
return albums;
}
@override
FutureOr<void> onStart(QueryParameters? params) {
if (params == null) {
throw const ClientException('params cannot be null');
}
}
}

View File

@ -15,3 +15,5 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'exceptions/exceptions.dart';
export 'extensions/object_extension.dart';
export 'mixins/observers.dart';

View File

@ -14,8 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
/// {@template app_exception}
/// [AppException] is a base class for all exceptions in the wyatt architecture.
/// {@endtemplate}
@ -27,7 +25,7 @@ abstract class AppException implements Exception {
@override
String toString() {
if (message.isNotNullOrEmpty) {
if (message?.isNotEmpty ?? false) {
return '$runtimeType: $message';
} else {
return '$runtimeType: An exception occured';

View File

@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2024 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'base_data_source.dart';
export 'local/local.dart';
export 'remote/remote.dart';
import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart';
extension ObjectExtension on Object? {
AppException toException() => this is AppException
? this! as AppException
: ClientException(this?.toString());
}

View File

@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2024 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@ -16,30 +16,26 @@
import 'dart:async';
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart';
import 'package:wyatt_architecture/src/domain/usecase.dart';
/// Usecase observers
mixin Observer<Parameters, ReturnType> {
mixin Observer<I, O> {
/// Called before usecase is runned.
/// Useful to check the preconditions
FutureOr<void> onStart(Parameters? params) {}
/// Called when error occures during main scenario
/// Useful to run alternative scenario
FutureOr<void> onError(AppException? error) {}
FutureOr<void> onStart(I? params) {}
}
/// Specific observer for classic usecase
mixin AsyncObserver<ReturnType> {
/// Called when usecase is completed
FutureOr<void> onComplete(ReturnType? data) {}
/// Exception observer
mixin ExceptionObserver<O> {
/// Called on error
/// Useful to handle exceptions
FutureOr<void> onError(AppException e) {}
}
/// Specific observer for stream case usecase
mixin StreamObserver<ReturnType> {
/// Replaces the data event handler of this subscription.
void onDone() {}
/// Replaces the done event handler of this subscription.
void onData(ReturnType? data) {}
/// Completer observer
mixin CompleterObserver<O> {
/// Called after usecase is runned.
/// Useful to log the result
FutureOr<void> onComplete(Res<O>? result) {}
}

View File

@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2024 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify

View File

@ -1,26 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/src/domain/data_sources/base_data_source.dart';
/// {@template base_local_data_source}
/// [BaseLocalDataSource] is a base class for all local data sources in the
/// wyatt architecture.
/// {@endtemplate}
abstract class BaseLocalDataSource extends BaseDataSource {
/// {@macro base_local_data_source}
const BaseLocalDataSource();
}

View File

@ -1,17 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'base_local_data_source.dart';

View File

@ -1,26 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/src/domain/data_sources/base_data_source.dart';
/// {@template base_remote_data_source}
/// [BaseRemoteDataSource] is a base class for all remote data sources in the
/// wyatt architecture.
/// {@endtemplate}
abstract class BaseRemoteDataSource extends BaseDataSource {
/// {@macro base_remote_data_source}
const BaseRemoteDataSource();
}

View File

@ -1,17 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'base_remote_data_source.dart';

View File

@ -1,4 +1,4 @@
// Copyright (C) 2022 WYATT GROUP
// Copyright (C) 2024 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'data_sources/data_sources.dart';
export 'entities/entities.dart';
export 'repositories/repositories.dart';
export 'usecases/usecases.dart';
export 'base_repository.dart';
export 'entity.dart';
export 'usecase.dart';

View File

@ -1,17 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'entity.dart';

View File

@ -1,17 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'base_repository.dart';

View File

@ -0,0 +1,153 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:generic_usecase/generic_usecase.dart' as generic;
import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart';
import 'package:wyatt_architecture/src/core/extensions/object_extension.dart';
import 'package:wyatt_architecture/src/core/mixins/observers.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
typedef Res<T> = Result<T, AppException>;
typedef FutureOrResult<T> = FutureOr<Res<T>>;
typedef StreamResult<T> = Stream<Res<T>>;
/// A wrapper around [Result] to make it easier to use with AppException.
Future<Res<T>> unsafe<T>(FutureOr<T> Function() fn) =>
Result.tryCatchAsync<T, AppException, Object>(
() => Future.sync(fn),
(e) => e.toException(),
);
abstract class AsyncUseCase<I, O> extends generic.Usecase<I, Res<O>>
with Observer<I, O>, ExceptionObserver<O>, CompleterObserver<O> {
const AsyncUseCase() : super();
@override
FutureOr<generic.ConditionsResult> checkPreconditions(I? params) async {
try {
await onStart(params);
return generic.ConditionsResult(isValid: true);
} catch (e) {
return generic.ConditionsResult(isValid: false, message: e.toString());
}
}
@override
FutureOr<generic.ConditionsResult> checkPostconditions(Res<O>? result) async {
try {
await onComplete(result);
return generic.ConditionsResult(isValid: true);
} catch (e) {
return generic.ConditionsResult(isValid: false, message: e.toString());
}
}
@override
FutureOrResult<O> onException(Object e) async {
final exception = e.toException();
await onError(exception);
return Err(exception);
}
}
abstract class NoParamsAsyncUseCase<O> extends generic.NoParamsUsecase<Res<O>>
with ExceptionObserver<O>, CompleterObserver<O> {
const NoParamsAsyncUseCase() : super();
@override
FutureOr<generic.ConditionsResult> checkPostconditions(Res<O>? result) async {
try {
await onComplete(result);
return generic.ConditionsResult(isValid: true);
} catch (e) {
return generic.ConditionsResult(isValid: false, message: e.toString());
}
}
@override
FutureOrResult<O> onException(Object e) async {
final exception = e.toException();
await onError(exception);
return Err(exception);
}
}
abstract class StreamUseCase<I, O> extends generic.StreamUsecase<I, Res<O>>
with Observer<I, O>, ExceptionObserver<O> {
const StreamUseCase() : super();
@override
FutureOr<generic.ConditionsResult> checkPreconditions(I? params) async {
try {
await onStart(params);
return generic.ConditionsResult(isValid: true);
} catch (e) {
return generic.ConditionsResult(isValid: false, message: e.toString());
}
}
StreamSubscription<Res<O>> listen(
I params,
void Function(Res<O>)? onData, {
Function? onError,
void Function()? onDone,
bool? cancelOnError,
}) =>
super.call(params).listen(
onData,
onError: onError,
onDone: onDone,
cancelOnError: cancelOnError,
);
@override
FutureOrResult<O> onException(Object e) async {
final exception = e.toException();
await onError(exception);
return Err(exception);
}
}
abstract class NoParamsStreamUseCase<O>
extends generic.NoParamsStreamUsecase<Res<O>> with ExceptionObserver<O> {
const NoParamsStreamUseCase() : super();
StreamSubscription<Res<O>> listen(
void Function(Res<O>)? onData, {
Function? onError,
void Function()? onDone,
bool? cancelOnError,
}) =>
super.call().listen(
onData,
onError: onError,
onDone: onDone,
cancelOnError: cancelOnError,
);
@override
FutureOrResult<O> onException(Object e) async {
final exception = e.toException();
await onError(exception);
return Err(exception);
}
}

View File

@ -1,26 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/src/domain/entities/entity.dart';
/// {@template no_param}
/// [NoParam] is a class that is used when a use case does not require any
/// parameters.
/// {@endtemplate}
class NoParam extends Entity {
/// {@macro no_param}
const NoParam();
}

View File

@ -1,100 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:async';
import 'package:wyatt_architecture/src/core/exceptions/exceptions.dart';
import 'package:wyatt_architecture/src/domain/usecases/observers.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
typedef FutureOrResult<T> = FutureOr<Result<T, AppException>>;
typedef StreamResult<T> = Stream<Result<T, AppException>>;
/// {@template base_usecase}
/// Abstract class of any use case.
/// {@endtemplate}
abstract class BaseUseCase<Parameters, ReturnType> {
/// {@macro base_usecase}
const BaseUseCase();
/// Run use case scenarios
ReturnType call(Parameters parameters);
/// Private function to implement main scenario
/// of your usecase.
ReturnType execute(Parameters params);
}
/// {@template usecase}
/// Abstract class of a use case that deals specifically
/// with the response and its state.
/// {@endtemplate}
abstract class UseCase<Parameters, ReturnType>
extends BaseUseCase<Parameters?, FutureOrResult<ReturnType>>
with Observer<Parameters, ReturnType> {
/// {@macro usecase}
const UseCase();
FutureOr<void> _onSuccess(ReturnType data);
/// Supports the result of the main scenario and integrates
/// some alternative scenarios if necessary.
@override
FutureOrResult<ReturnType> call(Parameters? parameters) async {
try {
await onStart(parameters);
final response = await execute(parameters);
if (response.isErr) {
await onError(response.err);
} else if (response.isOk && response.ok != null) {
await _onSuccess(response.ok as ReturnType);
}
return response;
} catch (e) {
return Err(ClientException(e.toString()));
}
}
}
/// {@template async_usecase}
/// Abtstract classic usecase bases on futures
/// {@endtemplate}
abstract class AsyncUseCase<Parameters, ReturnType>
extends UseCase<Parameters?, ReturnType> with AsyncObserver<ReturnType> {
/// {@macro async_usecase}
const AsyncUseCase();
@override
FutureOr<void> _onSuccess(ReturnType data) => onComplete(data);
}
/// {@template stream_usecase}
/// Abstract specific usecase bases on streams
/// {@endtemplate}
abstract class StreamUseCase<Parameters, ReturnType>
extends UseCase<Parameters?, Stream<ReturnType>>
with StreamObserver<ReturnType> {
/// {@macro stream_usecase}
const StreamUseCase();
@override
FutureOr<void> _onSuccess(Stream<ReturnType> data) {
data.listen(
onData,
onDone: onDone,
);
}
}

View File

@ -1,19 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'no_param.dart';
export 'observers.dart';
export 'usecase.dart';

View File

@ -15,4 +15,5 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'core/core.dart';
export 'data/base_data_source.dart';
export 'domain/domain.dart';

View File

@ -22,9 +22,10 @@ version: 0.2.0+1
publish_to: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub
environment:
sdk: ">=2.17.0 <3.0.0"
sdk: "^3.0.0"
dependencies:
generic_usecase: ^3.0.0
wyatt_type_utils:
hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub
version: ^0.0.5
@ -32,4 +33,4 @@ dependencies:
dev_dependencies:
wyatt_analysis:
hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub
version: ^2.5.0
version: ^2.6.1