execute method

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

Calls the wrapped handler function with an option input parameter

Implementation

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

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

  _isExecutingSubject.add(true);
  _commandResultsSubject.add(new CommandResult<TResult>(_emitLastResult ? lastResult : null, null, true));

  dynamic thrownException;

  var inputObservable = new Observable(_observableProvider(param))
      .handleError((error) {
          thrownException = error;
      })
      .doOnData((result) => _resultsSubject.add(result))
      .map((result) {
        lastResult = result;
        return new CommandResult(result, null, true);
      });

  _commandResultsSubject.addStream(inputObservable).then((_) {
    if (thrownException != null) {
      if (throwExceptions) {
        _resultsSubject.addError(thrownException);
        _commandResultsSubject.addError(thrownException);
      } else {
        _thrownExceptionsSubject.add(thrownException);
        _commandResultsSubject.add(new CommandResult<TResult>(null, thrownException, false));
      }
    } else {
      _commandResultsSubject.add(CommandResult(lastResult, null, false));
    }

    _isRunning = false;
    _canExecuteSubject.add(!_executionLocked);
  }, onError: (error) {
    print(error);
  });
}