Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ The state updates automatically as the command executes:

---

## Getters for State Checks

To simplify state management and improve code readability, the following getters are available:

- **`isIdle`**: Checks if the command is in the idle state.
```dart
bool get isIdle => value is IdleCommand<T>;
```

- **`isRunning`**: Checks if the command is currently running.
```dart
bool get isRunning => value is RunningCommand<T>;
```

- **`isCancelled`**: Checks if the command has been cancelled.
```dart
bool get isCancelled => value is CancelledCommand<T>;
```

- **`isSuccess`**: Checks if the command execution was successful.
```dart
bool get isSuccess => value is SuccessCommand<T>;
```

- **`isFailure`**: Checks if the command execution failed.
```dart
bool get isFailure => value is FailureCommand<T>;
```

These getters allow you to write cleaner and more intuitive code when interacting with commands in your views or controllers.

---

## Examples

### Example 1: Simple Command with No Arguments
Expand Down
16 changes: 13 additions & 3 deletions lib/src/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ abstract class Command<T extends Object> extends ChangeNotifier //
/// The current state of the command.
CommandState<T> _value = IdleCommand<T>();

bool get isIdle => value is IdleCommand<T>;

bool get isRunning => value is RuningCommand<T>;

bool get isCancelled => value is CancelledCommand<T>;

bool get isSuccess => value is SuccessCommand<T>;

bool get isFailure => value is FailureCommand<T>;

@override
CommandState<T> get value => _value;

Expand All @@ -53,7 +63,7 @@ abstract class Command<T extends Object> extends ChangeNotifier //
/// If the command is in the [RuningCommand] state, the [onCancel] callback is invoked,
/// and the state transitions to [CancelledCommand].
void cancel() {
if (value is RuningCommand<T>) {
if (isRunning) {
onCancel?.call();
_setValue(CancelledCommand<T>());
}
Expand All @@ -80,7 +90,7 @@ abstract class Command<T extends Object> extends ChangeNotifier //
///
/// If the command is cancelled during execution, the result is ignored.
Future<void> _execute(CommandAction0<T> action) async {
if (value is RuningCommand<T>) return; // Prevent multiple concurrent executions.
if (isRunning) return; // Prevent multiple concurrent executions.
_setValue(RuningCommand<T>());

Result<T>? result;
Expand All @@ -94,7 +104,7 @@ abstract class Command<T extends Object> extends ChangeNotifier //
.map(SuccessCommand<T>.new)
.mapError(FailureCommand<T>.new)
.fold(identity, identity);
if (value is RuningCommand) {
if (isRunning) {
_setValue(newValue);
}
}
Expand Down