execute method

  1. @override
void execute([
  1. TParam? param
])
override

Calls the wrapped handler function with an option input parameter

Implementation

@override
void execute([TParam? param]) {
  if (!_canExecute) {
    return;
  }

  if (_isRunning) {
    return;
  } else {
    _isRunning = true;
    _canExecuteSubject.add(false);
  }

  _commandResultsSubject.add(CommandResult<TParam, TResult>(param,
      _includeLastResultInCommandResults ? lastResult : null, null, true));

  TResult? result;
  late CommandResult<TParam, TResult> commandResult;
  try {
    if (_noParamValue) {
      assert(_funcNoParam != null);
      result = _funcNoParam!();
    } else {
      assert(_func != null);
      assert(param != null || null is TParam,
          'You passed a null value to the command ${_debugName ?? ''} that has a non-nullable type as TParam');
      result = _func!(param as TParam);
    }
    if (_noReturnValue) {
      result = null;
    }
    lastResult = result;
    if (result != null || null is TResult) {
    _resultsSubject.add(result as TResult);
    }
    commandResult =
        CommandResult<TParam, TResult>(param, result, null, false);
    _commandResultsSubject.add(commandResult);
  } catch (error) {
    if (error is AssertionError) rethrow;
    if (throwExceptions) {
      _resultsSubject.addError(error);
      _commandResultsSubject.addError(error);
      _isExecutingSubject.add(
          false); // Has to be done because in this case no command result is queued
      return;
    }

    commandResult = CommandResult<TParam, TResult>(param,
        _includeLastResultInCommandResults ? lastResult : null, error, false);
    _commandResultsSubject.add(commandResult);

    RxCommand.globalExceptionHandler
        ?.call(_debugName, CommandError(param, error));
  } finally {
    _isRunning = false;
    _canExecute = !_executionLocked;
    _canExecuteSubject.add(!_executionLocked);
    RxCommand.loggingHandler?.call(_debugName, commandResult);
  }
}