updateBy<M> method

FutureOr<M?> updateBy<M>(
  1. FutureOr<M> updater(
    1. M old
    ), {
  2. String tag = '',
  3. dynamic onError(
    1. Object e,
    2. StackTrace s
    )?,
  4. int slowlyMs = 100,
  5. Object? debounceTag,
  6. Object? throttleTag,
  7. Object? mutexTag,
  8. OnLogging<M>? logging,
})

Implementation

FutureOr<M?> updateBy<M>(
  FutureOr<M> Function(M old) updater, {
  String tag = '',
  Function(Object e, StackTrace s)? onError,
  int slowlyMs = 100,
  Object? debounceTag,
  Object? throttleTag,
  Object? mutexTag,
  OnLogging<M>? logging,
}) {
  final rst = update(
    (old) {
      final r = updater(old.modelValue<M>(tag));
      if (r is Future<M>) {
        return r.then(
          (r) => old.copyWith({
            FrUnion.modelKey<M>(tag: tag): r as FrUnionModel,
          }),
        );
      } else {
        return old.copyWith({
          FrUnion.modelKey<M>(tag: tag): r as FrUnionModel,
        });
      }
    },
    onError: onError,
    slowlyMs: slowlyMs,
    debounceTag: debounceTag,
    throttleTag: throttleTag,
    mutexTag: mutexTag,
    logging:
        logging == null
            ? null
            : (previous, current) => logging.call(
              previous.modelValue<M>(tag),
              current.modelValue<M>(tag),
            ),
  );
  if (rst is Future<FrUnion>) {
    return rst.then((r) => r.modelValue<M>(tag));
  } else if (rst is FrUnion) {
    return rst.modelValue<M>(tag);
  } else {
    return null;
  }
}