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

Unified Diff: packages/async/lib/src/async_memoizer.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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 | « packages/async/lib/result.dart ('k') | packages/async/lib/src/delegate/event_sink.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/async/lib/src/async_memoizer.dart
diff --git a/packages/async/lib/src/async_memoizer.dart b/packages/async/lib/src/async_memoizer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..68a6c80e4fac0372e0bef576076aa599e22348c3
--- /dev/null
+++ b/packages/async/lib/src/async_memoizer.dart
@@ -0,0 +1,47 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library async.async_memoizer;
+
+import 'dart:async';
+
+/// A class for running an asynchronous function exactly once and caching its
+/// result.
+///
+/// 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
+/// effect. To memoize the result of an async function, you can create a
+/// memoizer outside the function (for example as an instance field if you want
+/// to memoize the result of a method), and then wrap the function's body in a
+/// call to [runOnce].
+///
+/// This is useful for methods like `close()` and getters that need to do
+/// asynchronous work. For example:
+///
+/// ```dart
+/// class SomeResource {
+/// final _closeMemo = new AsyncMemoizer();
+///
+/// Future close() => _closeMemo.runOnce(() {
+/// // ...
+/// });
+/// }
+/// ```
+class AsyncMemoizer<T> {
+ /// The future containing the method's result.
+ ///
+ /// This will be `null` if [run] hasn't been called yet.
+ Future<T> _future;
+
+ /// Whether [run] has been called yet.
+ bool get hasRun => _future != null;
+
+ /// 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;
+ }
+}
« no previous file with comments | « packages/async/lib/result.dart ('k') | packages/async/lib/src/delegate/event_sink.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698