From 002fff4ebdea678a31c610c2e54a58be258d696b Mon Sep 17 00:00:00 2001 From: andre-djsystem Date: Mon, 16 Dec 2024 08:11:42 -0300 Subject: [PATCH] Fix command.dart doesn't match the Dart formatter, history.dart doesn't match the Dart formatter, add documentation for getters --- lib/src/command.dart | 35 ++++++++++++++++++++++++++--------- lib/src/history.dart | 3 ++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/src/command.dart b/lib/src/command.dart index 7bc4a6e..c9824f7 100644 --- a/lib/src/command.dart +++ b/lib/src/command.dart @@ -13,7 +13,9 @@ part 'types.dart'; /// /// This class supports state management, notifications, and execution /// with optional cancellation and history tracking. -abstract class Command extends ChangeNotifier with CommandHistoryManager implements ValueListenable> { +abstract class Command extends ChangeNotifier + with CommandHistoryManager + implements ValueListenable> { /// Callback executed when the command is cancelled. final void Function()? onCancel; @@ -25,14 +27,19 @@ abstract class Command extends ChangeNotifier with CommandHist /// The current state of the command. CommandState _value = IdleCommand(); + ///[isIdle]: Checks if the command is in the idle state. bool get isIdle => value is IdleCommand; + ///[isRunning]: Checks if the command is currently running. bool get isRunning => value is RunningCommand; + ///[isCancelled]: Checks if the command has been cancelled. bool get isCancelled => value is CancelledCommand; + ///[isSuccess]:Checks if the command execution was successful. bool get isSuccess => value is SuccessCommand; + ///[isFailure]: Checks if the command execution failed. bool get isFailure => value is FailureCommand; @override @@ -47,10 +54,12 @@ abstract class Command extends ChangeNotifier with CommandHist try { onCancel?.call(); } catch (e) { - _setValue(FailureCommand(e is Exception ? e : Exception('$e')), metadata: metadata); + _setValue(FailureCommand(e is Exception ? e : Exception('$e')), + metadata: metadata); return; } - _setValue(CancelledCommand(), metadata: metadata ?? {'reason': 'Manually cancelled'}); + _setValue(CancelledCommand(), + metadata: metadata ?? {'reason': 'Manually cancelled'}); } } @@ -58,7 +67,8 @@ abstract class Command extends ChangeNotifier with CommandHist /// /// This clears the current state, allowing the command to be reused. void reset({Map? metadata}) { - _setValue(IdleCommand(), metadata: metadata ?? {'reason': 'Command reset'}); + _setValue(IdleCommand(), + metadata: metadata ?? {'reason': 'Command reset'}); } /// Executes the given [action] and updates the command state accordingly. @@ -87,11 +97,15 @@ abstract class Command extends ChangeNotifier with CommandHist } } catch (e, stackTrace) { hasError = true; - _setValue(FailureCommand(Exception('Unexpected error: $e')), metadata: {'error': '$e', 'stackTrace': stackTrace.toString()}); + _setValue(FailureCommand(Exception('Unexpected error: $e')), + metadata: {'error': '$e', 'stackTrace': stackTrace.toString()}); return; } finally { if (!hasError) { - final newValue = result.map(SuccessCommand.new).mapError(FailureCommand.new).fold(identity, identity); + final newValue = result + .map(SuccessCommand.new) + .mapError(FailureCommand.new) + .fold(identity, identity); if (isRunning) { _setValue(newValue); } @@ -118,7 +132,8 @@ final class Command0 extends Command { final CommandAction0 _action; /// Creates a [Command0] with the specified [action] and optional [onCancel] callback. - Command0(this._action, {void Function()? onCancel, int maxHistoryLength = 10}) : super(onCancel, maxHistoryLength); + Command0(this._action, {void Function()? onCancel, int maxHistoryLength = 10}) + : super(onCancel, maxHistoryLength); /// Executes the action and updates the command state. Future execute({Duration? timeout}) async { @@ -132,7 +147,8 @@ final class Command1 extends Command { final CommandAction1 _action; /// Creates a [Command1] with the specified [action] and optional [onCancel] callback. - Command1(this._action, {void Function()? onCancel, int maxHistoryLength = 10}) : super(onCancel, maxHistoryLength); + Command1(this._action, {void Function()? onCancel, int maxHistoryLength = 10}) + : super(onCancel, maxHistoryLength); /// Executes the action with the given [argument] and updates the command state. Future execute(A argument, {Duration? timeout}) async { @@ -146,7 +162,8 @@ final class Command2 extends Command { final CommandAction2 _action; /// Creates a [Command2] with the specified [action] and optional [onCancel] callback. - Command2(this._action, {void Function()? onCancel, int maxHistoryLength = 10}) : super(onCancel, maxHistoryLength); + Command2(this._action, {void Function()? onCancel, int maxHistoryLength = 10}) + : super(onCancel, maxHistoryLength); /// Executes the action with the given [argument1] and [argument2], /// and updates the command state. diff --git a/lib/src/history.dart b/lib/src/history.dart index 2bc9b2a..52c9c98 100644 --- a/lib/src/history.dart +++ b/lib/src/history.dart @@ -37,7 +37,8 @@ mixin CommandHistoryManager { } /// Provides read-only access to the state change history. - List> get stateHistory => List.unmodifiable(_stateHistory); + List> get stateHistory => + List.unmodifiable(_stateHistory); /// Adds a new entry to the history and ensures the history length limit. void addHistoryEntry(CommandHistoryEntry entry) {