Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library async.async_memoizer; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 /// A class for running an asynchronous method body exactly once and caching its | |
|
Lasse Reichstein Nielsen
2015/07/08 07:37:34
method body -> function
nweiz
2015/07/09 00:45:13
Done.
| |
| 10 /// result. | |
| 11 /// | |
| 12 /// This should be stored as an instance variable, and [runOnce] should be | |
|
Lasse Reichstein Nielsen
2015/07/08 07:37:34
Drop the usage of "this", or at least don't make i
nweiz
2015/07/09 00:45:13
Done.
| |
| 13 /// called when the method is invoked with the uncached method body. The first | |
| 14 /// time, it runs the body; after that, it returns the future from the first | |
| 15 /// run. | |
| 16 /// | |
| 17 /// This is useful for methods like `close()` and getters that need to do | |
| 18 /// asynchronous work. For example: | |
| 19 /// | |
| 20 /// ```dart | |
|
Lasse Reichstein Nielsen
2015/07/08 07:37:34
Does DartDoc understand "```dart" now! If so, yey!
nweiz
2015/07/09 00:45:13
I think so? I'm not 100% sure, but I'll keep an ey
| |
| 21 /// class SomeResource { | |
| 22 /// final _closeMemo = new AsyncMemoizer(); | |
| 23 /// | |
| 24 /// Future close() => _closeMemo.runOnce(() { | |
| 25 /// // ... | |
| 26 /// }); | |
| 27 /// } | |
| 28 /// ``` | |
| 29 class AsyncMemoizer<T> { | |
| 30 /// The future containing the method's result. | |
| 31 /// | |
| 32 /// This will be `null` if [run] hasn't been called yet. | |
| 33 Future<T> _future; | |
| 34 | |
| 35 /// Whether [run] has been called yet. | |
| 36 bool get hasRun => _future != null; | |
| 37 | |
| 38 /// Runs the method body, [fn], if it hasn't been run before. | |
|
Lasse Reichstein Nielsen
2015/07/08 07:37:34
method body -> function.
nweiz
2015/07/09 00:45:13
Done.
| |
| 39 /// | |
| 40 /// If [run] has already been called, this returns the original result. | |
| 41 Future<T> runOnce(fn()) { | |
|
Lasse Reichstein Nielsen
2015/07/08 07:37:34
Rename "fn" to "computation".
This matches the arg
nweiz
2015/07/09 00:45:13
Done.
| |
| 42 if (_future == null) _future = new Future.sync(fn); | |
| 43 return _future; | |
| 44 } | |
| 45 } | |
| OLD | NEW |