useStorageWithHandlers<R> method

  1. @protected
  2. @nonVirtual
Future<R> useStorageWithHandlers<R>(
  1. Future<R> block(
    1. S
    ),
  2. FutureOr<void> onSuccess(
    1. R,
    2. S
    )?,
  3. FutureOr<void> onFailure(
    1. RxStorageError,
    2. S
    )?
)

Calling block with S as argument.

Implementation

@protected
@nonVirtual
Future<R> useStorageWithHandlers<R>(
  Future<R> Function(S) block,
  FutureOr<void> Function(R, S)? onSuccess,
  FutureOr<void> Function(RxStorageError, S)? onFailure,
) async {
  assert(_debugAssertNotDisposed());

  final storage = _storage ?? await _storageFuture;
  if (onSuccess == null && onFailure == null) {
    return await block(storage);
  }

  try {
    final value = await block(storage);
    final futureOrVoid = onSuccess?.call(value, storage);
    if (futureOrVoid is Future<void>) {
      await futureOrVoid;
    }
    return value;
  } catch (e, s) {
    final futureOrVoid = onFailure?.call(RxStorageError(e, s), storage);
    if (futureOrVoid is Future<void>) {
      await futureOrVoid;
    }
    rethrow;
  }
}