RxCommand<TParam, TResult> constructor

RxCommand<TParam, TResult>(
  1. Subject<TResult> _resultsSubject,
  2. Stream<bool?>? restriction,
  3. bool _includeLastResultInCommandResults,
  4. bool _resultSubjectIsBehaviourSubject,
  5. TResult? lastResult,
  6. bool noReturnValue,
  7. String? debugName,
  8. bool noParamValue,
)

Implementation

RxCommand(
    this._resultsSubject,
    Stream<bool?>? restriction,
    this._includeLastResultInCommandResults,
    this._resultSubjectIsBehaviourSubject,
    this.lastResult,
    bool noReturnValue,
    String? debugName,
    bool noParamValue)
    : _noReturnValue = noReturnValue,
      _noParamValue = noParamValue,
      _debugName = debugName,
      super(_resultsSubject) {
  _commandResultsSubject = _resultSubjectIsBehaviourSubject
      ? BehaviorSubject<CommandResult<TParam, TResult>>()
      : PublishSubject<CommandResult<TParam, TResult>>();

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

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

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

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