Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Fix/async memoizer storing exception #260

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 2.12.0-wip

- Require Dart 3.4
* Can decide `runOnce` method of `AsyncMemoizer` will store exception or not.
* Require Dart 3.4

## 2.11.0

Expand Down
28 changes: 25 additions & 3 deletions lib/src/async_memoizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import 'dart:async';

/// A class for running an asynchronous function exactly once and caching its
/// result.
/// result. If you doesn't want to cache Exception then you can set
/// [_canCacheException] to false.
///
///
/// An `AsyncMemoizer` is used when some function may be run multiple times in
/// order to get its result, but it only actually needs to be run once for its
Expand All @@ -27,6 +29,9 @@ import 'dart:async';
/// }
/// ```
class AsyncMemoizer<T> {
AsyncMemoizer({bool canCacheException = true}):
_canCacheException = canCacheException;

/// The future containing the method's result.
///
/// This can be accessed at any time, and will fire once [runOnce] is called.
Expand All @@ -36,11 +41,28 @@ class AsyncMemoizer<T> {
/// Whether [runOnce] has been called yet.
bool get hasRun => _completer.isCompleted;

///Default is set to true.
///If we set this variable to false.
///On the initial run, if callback returned the [Exception].
///Next time, we can reRun the callback for the successful attempt.
final bool _canCacheException;

/// Runs the function, [computation], if it hasn't been run before.
///
/// If [runOnce] has already been called, this returns the original result.
Future<T> runOnce(FutureOr<T> Function() computation) {
if (!hasRun) _completer.complete(Future.sync(computation));
Future<T> runOnce(FutureOr<T> Function() computation) async {
if (!hasRun) {
if (_canCacheException) {
_completer.complete(Future.sync(computation));
} else {
try {
var value = await computation();
_completer.complete(value);
} catch (error) {
rethrow;
}
}
}
return future;
}
}
8 changes: 8 additions & 0 deletions test/async_memoizer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ void main() {
late AsyncMemoizer cache;
setUp(() => cache = AsyncMemoizer());

test('should not store exception when callback throws exception', () async {
Future<String> asyncFunctionThatThrows() {
throw Exception();
}
var cacheFuture = cache.runOnce(asyncFunctionThatThrows);
await expectLater(cacheFuture,throwsA(isException));
});

test('runs the function only the first time runOnce() is called', () async {
var count = 0;
expect(await cache.runOnce(() => count++), equals(0));
Expand Down