execute method

Future<void> execute({
  1. required Future<TResult> operation(),
  2. Id? targetId,
})

Runs operation with automatic loading, success, and error state handling. Ignores the call if an operation is already in progress.

Implementation

Future<void> execute({
  required Future<TResult> Function() operation,
  Id? targetId,
}) async {
  if (state is OperationInProgress<Id>) return;
  emit(OperationInProgress<Id>(targetId: targetId));
  try {
    final result = await operation();
    if (isClosed) return;
    emit(OperationSuccess<TResult, Id>(targetId: targetId, result: result));
  } catch (e, stack) {
    if (isClosed) return;
    emit(OperationFailure<Id>(
      targetId: targetId,
      error: ErrorMapper.map(e, stack),
    ));
  }
}