RxCommand<TParam, TResult> constructor

RxCommand<TParam, TResult>(
  1. Subject<TResult> _resultsSubject,
  2. Stream<bool> canExecuteRestriction,
  3. bool _emitLastResult,
  4. bool _resultSubjectIsBehaviourSubject,
  5. TResult lastResult
)

Implementation

RxCommand(
    this._resultsSubject,
    Stream<bool> canExecuteRestriction,
    this._emitLastResult,
    this._resultSubjectIsBehaviourSubject,
    this.lastResult)
    : super(_resultsSubject) {
  _commandResultsSubject = _resultSubjectIsBehaviourSubject
      ? BehaviorSubject<CommandResult<TResult>>()
      : PublishSubject<CommandResult<TResult>>();

  _commandResultsSubject
      .where((x) => x.hasError)
      .listen((x) => _thrownExceptionsSubject.add(x.error), onError: (x) {});

  _commandResultsSubject.listen((x) => _isExecutingSubject.add(x.isExecuting),
      onError: (x) {});

  final _canExecuteParam = canExecuteRestriction == null
      ? Stream<bool>.value(true)
      : canExecuteRestriction.handleError((error) {
          if (error is Exception) {
            _thrownExceptionsSubject.add(error);
          }
        }).distinct();

  _canExecuteParam.listen((canExecute) {
    _canExecute = canExecute && (!_isRunning);
    _executionLocked = !canExecute;
    _canExecuteSubject.add(_canExecute);
  });
}