Command<TParam, TResult> constructor

Command<TParam, TResult>({
  1. required TResult initialValue,
  2. required ValueListenable<bool>? restriction,
  3. required ExecuteInsteadHandler<TParam>? ifRestrictedExecuteInstead,
  4. required bool includeLastResultInCommandResults,
  5. required bool noReturnValue,
  6. required bool notifyOnlyWhenValueChanges,
  7. ErrorFilter? errorFilter,
  8. required String? debugName,
  9. required bool noParamValue,
})

Implementation

Command({
  required TResult initialValue,
  required ValueListenable<bool>? restriction,
  required ExecuteInsteadHandler<TParam>? ifRestrictedExecuteInstead,
  required bool includeLastResultInCommandResults,
  required bool noReturnValue,
  required bool notifyOnlyWhenValueChanges,
  ErrorFilter? errorFilter,
  required String? debugName,
  required bool noParamValue,
})  : _restriction = restriction,
      _ifRestrictedExecuteInstead = ifRestrictedExecuteInstead,
      _noReturnValue = noReturnValue,
      _noParamValue = noParamValue,
      _includeLastResultInCommandResults = includeLastResultInCommandResults,
      _errorFilter = errorFilter ?? errorFilterDefault,
      _debugName = debugName,
      super(
        initialValue,
        mode: notifyOnlyWhenValueChanges
            ? CustomNotifierMode.normal
            : CustomNotifierMode.always,
      ) {
  _commandResult = CustomValueNotifier<CommandResult<TParam?, TResult>>(
    CommandResult.data(null, initialValue),
  );

  /// forward error states to the `errors` Listenable
  _commandResult.where((x) => x.hasError).listen((x, _) {
    _errors.value = CommandError<TParam>(
      x.paramData,
      x.error,
      command: this,
      commandName: this._debugName,
    );
    _errors.notifyListeners();
  });

  /// forward busy states to the `isExecuting` Listenable
  _commandResult.listen((x, _) => _isExecuting.value = x.isExecuting);

  /// Merge the external execution restricting with the internal
  /// isExecuting which also blocks execution if true
  _canExecute = (_restriction == null)
      ? _isExecuting.map((val) => !val) as ValueNotifier<bool>
      : _restriction!.combineLatest<bool, bool>(
          _isExecuting,
          (restriction, isExecuting) => !restriction && !isExecuting,
        ) as ValueNotifier<bool>;
}