Skip to content

Commit

Permalink
feaure: isCalculating option on depends_on_async
Browse files Browse the repository at this point in the history
  • Loading branch information
ali77gh committed Mar 28, 2023
1 parent 64a3a5c commit 07a7503
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DependObservableAsyncSampleLayoutState
late Telescope<int> weight;
late Telescope<double> bmi;
late Telescope<String> showingText;
var loadingBMI = Telescope(false);

@override
void initState() {
Expand All @@ -26,11 +27,12 @@ class DependObservableAsyncSampleLayoutState

bmi = Telescope.dependsOnAsync(0, [height, weight], () async {
return await calculateBMI(height.value, weight.value);
});
}, isCalculating: loadingBMI);

showingText = Telescope.dependsOn([height, weight, bmi], () {
showingText = Telescope.dependsOn([height, weight, bmi, loadingBMI], () {
var bmis = bmi.value.toString();
bmis = bmis.length > 5 ? bmis.substring(0, 5) : bmis;
bmis = loadingBMI.value ? "..." : bmis.length > 5 ? bmis.substring(0, 5) : bmis;

return "weight is ${weight.value} and height is ${height.value} so bmi will be $bmis";
});
}
Expand Down
9 changes: 8 additions & 1 deletion lib/src/telescope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ class Telescope<T> {
/// Use this if you need to use await in calculate function
Telescope.dependsOnAsync(
this.holden, List<Telescope> dependencies, Future<T> Function() calculate,
{this.iWillCallNotifyAll = false}) {
{
this.iWillCallNotifyAll = false,
Telescope<bool>? isCalculating,
}) {
isCalculating?.value = true;
calculate().then((value) {
holden = value;
isCalculating?.value = false;
notifyAll();
});
for (var o in dependencies) {
o.subscribe(() {
isCalculating?.value = true;
calculate().then((value) {
holden = value;
isCalculating?.value = false;
notifyAll();
});
});
Expand Down

0 comments on commit 07a7503

Please sign in to comment.