Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Unified Diff: lib/src/async_memoizer.dart

Issue 1324743003: Add AsyncMemoizer.future. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Mark as -dev Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/async_memoizer.dart
diff --git a/lib/src/async_memoizer.dart b/lib/src/async_memoizer.dart
index 68a6c80e4fac0372e0bef576076aa599e22348c3..41c3dae87a46054708456ced01a04d582891cb07 100644
--- a/lib/src/async_memoizer.dart
+++ b/lib/src/async_memoizer.dart
@@ -31,17 +31,18 @@ import 'dart:async';
class AsyncMemoizer<T> {
/// The future containing the method's result.
///
- /// This will be `null` if [run] hasn't been called yet.
- Future<T> _future;
+ /// This can be accessed at any time, and will fire once [runOnce] is called.
+ Future<T> get future => _completer.future;
+ final _completer = new Completer();
kevmoo 2015/09/03 11:58:33 Completer<T>
/// Whether [run] has been called yet.
- bool get hasRun => _future != null;
+ bool get hasRun => _completer.isCompleted;
/// Runs the function, [computation], if it hasn't been run before.
///
/// If [run] has already been called, this returns the original result.
Future<T> runOnce(computation()) {
- if (_future == null) _future = new Future.sync(computation);
- return _future;
+ if (!hasRun) _completer.complete(new Future.sync(computation));
kevmoo 2015/09/03 11:58:33 new Future<T> ?
+ return future;
}
}
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698