diff --git a/lib/flutter_command.dart b/lib/flutter_command.dart index 2f4ff4d..34954f6 100644 --- a/lib/flutter_command.dart +++ b/lib/flutter_command.dart @@ -197,8 +197,8 @@ abstract class Command extends CustomValueNotifier { : null); }); - /// forward busy states to the `isExecuting` Listenable - _commandResult.listen((x, _) => _isExecuting.value = x.isExecuting); + // /// forward busy states to the `isExecuting` Listenable + // _commandResult.listen((x, _) => _isExecuting.value = x.isExecuting); /// Merge the external execution restricting with the internal /// isExecuting which also blocks execution if true @@ -208,6 +208,12 @@ abstract class Command extends CustomValueNotifier { _isExecuting, (restriction, isExecuting) => !restriction && !isExecuting, ) as ValueNotifier; + + /// decouple the async isExecuting from the sync isExecuting + /// so that _canExecute will update immediately + _isExecuting.listen((busy, _) { + _isExecutingAsync.value = busy; + }); } /// Calls the wrapped handler function with an optional input parameter @@ -351,7 +357,7 @@ abstract class Command extends CustomValueNotifier { /// `ValueListenable` that changes its value on any change of the execution /// state change of the command - ValueListenable get isExecuting => _isExecuting; + ValueListenable get isExecuting => _isExecutingAsync; /// `ValueListenable` that changes its value on any change of the current /// executability state of the command. Meaning if the command can be executed or not. @@ -447,8 +453,10 @@ abstract class Command extends CustomValueNotifier { /// properties we make them private and only publish their `ValueListenable` /// interface via getters. late CustomValueNotifier> _commandResult; - final CustomValueNotifier _isExecuting = + final CustomValueNotifier _isExecutingAsync = CustomValueNotifier(false, asyncNotification: true); + final CustomValueNotifier _isExecuting = + CustomValueNotifier(false); late ValueNotifier _canExecute; late final ValueListenable? _restriction; final CustomValueNotifier?> _errors = @@ -475,6 +483,7 @@ abstract class Command extends CustomValueNotifier { _commandResult.dispose(); _canExecute.dispose(); _isExecuting.dispose(); + _isExecutingAsync.dispose(); _errors.dispose(); if (!(_futureCompleter?.isCompleted ?? true)) { _futureCompleter!.complete(null); @@ -550,7 +559,7 @@ abstract class Command extends CustomValueNotifier { } bool get _hasLocalErrorHandler => - _commandResult.listenerCount >= 3 || _errors.hasListeners; + _commandResult.listenerCount >= 2 || _errors.hasListeners; void _handleErrorFiltered( TParam? param, Object error, StackTrace stackTrace) { diff --git a/test/flutter_command_test.dart b/test/flutter_command_test.dart index c61d402..ee32b06 100644 --- a/test/flutter_command_test.dart +++ b/test/flutter_command_test.dart @@ -1245,7 +1245,9 @@ void main() { throw CustomException('Exception From Command'); }, initialValue: 'Initial Value', - ); + )..errors.listen((error, _) { + print('Error: $error'); + }); await tester.pumpWidget( MaterialApp( home: Scaffold( @@ -1345,7 +1347,9 @@ void main() { throw CustomException('Exception From Command'); }, initialValue: 'Initial Value', - ); + )..errors.listen((error, _) { + print('Error: $error'); + }); await tester.pumpWidget( MaterialApp( home: Scaffold(