use_in_case 0.1.0
use_in_case: ^0.1.0 copied to clipboard
An extendable library that provides functionality for use cases
Use-In-Case Interactor Library #
This package contains the implementation required to add interactors to your project.
Simple Usage #
You can read about the usage in many examples inside the examples folder. Here's a quick example on how to get started:
import 'package:use_in_case/use_in_case.dart';
final class Square extends PRInteractor<double, double> {
const Square();
@override
Future<double> execute(double parameter) async {
return parameter * parameter;
}
}
void main() async {
const square = Square();
final result = await square(11).get();
print(result);
}
It's also possible to receive information about the workflow of our invocation.
/// ...
/// Definition on DownloadFileInteractor class
/// ...
void main() async {
const downloadFile = DownloadFileInteractor();
final stopwatch = Stopwatch();
await downloadFile((
sourceUri: Uri.parse('https://www.my_website.com'),
destinationPath: 'downloads/my_website_data.txt',
))
.timeout(const Duration(seconds: 30))
.receiveBusyState((isBusy) {
if (isBusy) {
stopwatch.start();
} else {
stopwatch.stop();
}
})
.onResult((data) => print('Downloading succeeded after ${stopwatch.elapsed} with $data'),)
.onException((exception) => print('Downloading failed after ${stopwatch.elapsed} due to $exception'))
.run();
}
This example shows an example implementation of a ParameterizedResultInteractor / PRInteractor. The first generic type returns the parameter of execute, the second defines the result/return type.
Modifiers usage #
This library provides the ability to add highly customizable modifiers. There are some pre-defined modifiers such as timeout or busy state. More information on these modifiers can be read in their corresponding examples.
// ...
void main() async {
const square = Square();
final result = await square(11)
.timeout(
const Duration(seconds: 5),
'Timeout because the computation took longer than 5 seconds.',
)
.receiveBusyState(
(isBusy) => print('${isBusy ? 'start' : 'stop'} at ${DateTime.now()}'),
)
.get();
print(result);
}
Writing custom modifiers #
Due to the limitations of my few implementations of modifiers provided by the library, i've designed the API publically extendable. For more details you may want to take a look into the implemenation of timeout or busy state modifiers.
You can write your own modifier as follows:
final class CustomModifier<Parameter, Result>
extends ChainedModifier<Parameter, Result> {
const CustomModifier(super._modifier);
@override
EventHandler<Parameter, Result> buildEventHandler() {
return super.buildEventHandler().after((details, event) {
event.whenOrNull(
onStart: (parameter) {
print('${details.calleName} started with $parameter -->');
},
onResult: (result) =>
print('<-- ${details.calleName} succeeded with $result'),
onException: (exception) =>
print('<-- ${details.calleName} failed with $exception'),
);
});
}
}
// ...
void main() async {
const interactor = // ...
final result = await interactor()
.modifier((modifier) => CustomModifier(modifier)).get();
print(result);
}
Note that this api will definitely change in future!