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

Side by Side Diff: lib/src/async_memoizer.dart

Issue 1777453002: Modernize the package's style. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Created 4 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library async.async_memoizer;
6
7 import 'dart:async'; 5 import 'dart:async';
8 6
9 /// A class for running an asynchronous function exactly once and caching its 7 /// A class for running an asynchronous function exactly once and caching its
10 /// result. 8 /// result.
11 /// 9 ///
12 /// An `AsyncMemoizer` is used when some function may be run multiple times in 10 /// An `AsyncMemoizer` is used when some function may be run multiple times in
13 /// order to get its result, but it only actually needs to be run once for its 11 /// order to get its result, but it only actually needs to be run once for its
14 /// effect. To memoize the result of an async function, you can create a 12 /// effect. To memoize the result of an async function, you can create a
15 /// memoizer outside the function (for example as an instance field if you want 13 /// memoizer outside the function (for example as an instance field if you want
16 /// to memoize the result of a method), and then wrap the function's body in a 14 /// to memoize the result of a method), and then wrap the function's body in a
(...skipping 11 matching lines...) Expand all
28 /// }); 26 /// });
29 /// } 27 /// }
30 /// ``` 28 /// ```
31 class AsyncMemoizer<T> { 29 class AsyncMemoizer<T> {
32 /// The future containing the method's result. 30 /// The future containing the method's result.
33 /// 31 ///
34 /// This can be accessed at any time, and will fire once [runOnce] is called. 32 /// This can be accessed at any time, and will fire once [runOnce] is called.
35 Future<T> get future => _completer.future; 33 Future<T> get future => _completer.future;
36 final _completer = new Completer(); 34 final _completer = new Completer();
37 35
38 /// Whether [run] has been called yet. 36 /// Whether [runOnce] has been called yet.
39 bool get hasRun => _completer.isCompleted; 37 bool get hasRun => _completer.isCompleted;
40 38
41 /// Runs the function, [computation], if it hasn't been run before. 39 /// Runs the function, [computation], if it hasn't been run before.
42 /// 40 ///
43 /// If [run] has already been called, this returns the original result. 41 /// If [run] has already been called, this returns the original result.
Lasse Reichstein Nielsen 2016/03/08 10:40:37 run->runOnce
nweiz 2016/03/08 20:23:39 Done.
44 Future<T> runOnce(computation()) { 42 Future<T> runOnce(computation()) {
45 if (!hasRun) _completer.complete(new Future.sync(computation)); 43 if (!hasRun) _completer.complete(new Future.sync(computation));
46 return future; 44 return future;
47 } 45 }
48 } 46 }
OLDNEW
« no previous file with comments | « lib/result.dart ('k') | lib/src/cancelable_operation.dart » ('j') | lib/src/future_group.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698