update method

  1. @visibleForTesting
  2. @protected
FutureOr<T?> update(
  1. FutureOr<T> update(
    1. T old
    ), {
  2. dynamic onError(
    1. Object e,
    2. StackTrace s
    )?,
  3. int slowlyMs = 100,
  4. Object? debounceTag,
  5. Object? throttleTag,
  6. dynamic ignoreSkipError = true,
})

update if return value, will call put if return null, will not call put/putError onError if set null,will call putError if set function value, will not call putError, you can invoke putError manually slowlyMs if set <=0 value, will ignore debounce/throttleTag debounceTag enable debounce, require unique within the VM scope throttleTag enable throttle, require unique within the VM scope ignoreSkipError ref runCatching.ignoreSkipError

Implementation

@visibleForTesting
@protected
FutureOr<T?> update(
  FutureOr<T> Function(T old) update, {
  Function(Object e, StackTrace s)? onError,
  int slowlyMs = 100,
  Object? debounceTag,
  Object? throttleTag,
  ignoreSkipError = true,
}) async {
  if (slowlyMs > 0) {
    if (debounceTag != null) {
      /// debounce
      final deFunc = await slowly.debounce(
        debounceTag,
        update,
        duration: Duration(milliseconds: slowlyMs),
      );
      if (deFunc is! FutureOr<T> Function(T)) return null;
      update = deFunc;
    } else if (throttleTag != null) {
      /// throttle
      final thFunc = slowly.throttle(
        throttleTag,
        update,
        duration: Duration(milliseconds: slowlyMs),
      );
      if (thFunc is! FutureOr<T> Function(T)) return null;
      update = thFunc;
    }
  }
  return await runCatching<T>(
    () => update(value),
    onSuccess: (r) => put(r),
    onFailure: (e, s) => (onError ?? putError).call(e, s),
    ignoreSkipError: ignoreSkipError,
  );
}