@@ -57,28 +57,46 @@ class CachedAssetReader extends AssetReader {
5757 }
5858
5959 @override
60+ // Can't use async keyword here because we need to update
61+ // `_pendingHasInputChecks` synchronously.
6062 Future <bool > hasInput (AssetId id) {
61- if (_cache.contains (id)) return new Future .value (true );
62-
63- return _pendingHasInputChecks.putIfAbsent (id, () async {
64- var exists = await _reader.hasInput (id);
65- _pendingHasInputChecks.remove (id);
66- return exists;
67- });
63+ try {
64+ if (_cache.contains (id)) return new Future .value (true );
65+
66+ return _pendingHasInputChecks.putIfAbsent (id, () async {
67+ try {
68+ return await _reader.hasInput (id);
69+ } finally {
70+ _pendingHasInputChecks.remove (id);
71+ }
72+ });
73+ } catch (e, s) {
74+ return new Future .error (e, s);
75+ }
6876 }
6977
7078 @override
79+ // Can't use async keyword here because we need to update
80+ // `_pendingReads` synchronously.
7181 Future <String > readAsString (AssetId id, {Encoding encoding: UTF8 }) {
72- if (_cache.contains (id)) {
73- return new Future .value (_cache.get (id).stringContents);
82+ try {
83+ if (_cache.contains (id)) {
84+ return new Future .value (_cache.get (id).stringContents);
85+ }
86+
87+ return _pendingReads.putIfAbsent (id, () async {
88+ try {
89+ var content = await _reader.readAsString (id, encoding: encoding);
90+ _cache.put (new Asset (id, content));
91+ return content;
92+ } finally {
93+ // Make sure we always remove the pending read
94+ _pendingReads.remove (id);
95+ }
96+ });
97+ } catch (e, s) {
98+ return new Future .error (e, s);
7499 }
75-
76- return _pendingReads.putIfAbsent (id, () async {
77- var content = await _reader.readAsString (id, encoding: encoding);
78- _cache.put (new Asset (id, content));
79- _pendingReads.remove (id);
80- return content;
81- });
82100 }
83101
84102 @override
@@ -101,14 +119,26 @@ class CachedAssetWriter extends AssetWriter {
101119 CachedAssetWriter (this ._cache, this ._writer);
102120
103121 @override
122+ // Can't use async keyword here because we need to update `_cache`
123+ // synchronously.
104124 Future writeAsString (Asset asset, {Encoding encoding: UTF8 }) {
105- _cache.put (asset);
106- return _writer.writeAsString (asset, encoding: encoding);
125+ try {
126+ _cache.put (asset);
127+ return _writer.writeAsString (asset, encoding: encoding);
128+ } catch (e, s) {
129+ return new Future .error (e, s);
130+ }
107131 }
108132
109133 @override
134+ // Can't use async keyword here because we need to update `_cache`
135+ // synchronously.
110136 Future delete (AssetId id) {
111- _cache.remove (id);
112- return _writer.delete (id);
137+ try {
138+ _cache.remove (id);
139+ return _writer.delete (id);
140+ } catch (e, s) {
141+ return new Future .error (e, s);
142+ }
113143 }
114144}
0 commit comments