OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 | 7 |
8 import 'package:async/async.dart'; | 8 import 'package:async/async.dart'; |
9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
10 | 10 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 _requestedResources.add(completer); | 99 _requestedResources.add(completer); |
100 _resetTimer(); | 100 _resetTimer(); |
101 return completer.future; | 101 return completer.future; |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 /// Requests a resource for the duration of [callback], which may return a | 105 /// Requests a resource for the duration of [callback], which may return a |
106 /// Future. | 106 /// Future. |
107 /// | 107 /// |
108 /// The return value of [callback] is piped to the returned Future. | 108 /// The return value of [callback] is piped to the returned Future. |
109 Future/*<T>*/ withResource/*<T>*/(/*=T*/ callback()) async { | 109 Future/*<T>*/ withResource/*<T>*/(/*=T*/ callback()) { |
110 if (isClosed) { | 110 if (isClosed) { |
111 throw new StateError( | 111 throw new StateError( |
112 "withResource() may not be called on a closed Pool."); | 112 "withResource() may not be called on a closed Pool."); |
113 } | 113 } |
114 | 114 |
115 var resource = await request(); | 115 // We can't use async/await here because we need to start the request |
116 try { | 116 // synchronously in case the pool is closed immediately afterwards. Async |
117 return await callback(); | 117 // functions have an asynchronous gap between calling and running the body, |
118 } finally { | 118 // and [close] could be called during that gap. See #3. |
119 resource.release(); | 119 return request().then((resource) { |
120 } | 120 return new Future.sync(callback).whenComplete(resource.release); |
| 121 }); |
121 } | 122 } |
122 | 123 |
123 /// Closes the pool so that no more resources are requested. | 124 /// Closes the pool so that no more resources are requested. |
124 /// | 125 /// |
125 /// Existing resource requests remain unchanged. | 126 /// Existing resource requests remain unchanged. |
126 /// | 127 /// |
127 /// Any resources that are marked as releasable using | 128 /// Any resources that are marked as releasable using |
128 /// [PoolResource.allowRelease] are released immediately. Once all resources | 129 /// [PoolResource.allowRelease] are released immediately. Once all resources |
129 /// have been released and any `onRelease` callbacks have completed, the | 130 /// have been released and any `onRelease` callbacks have completed, the |
130 /// returned future completes successfully. If any `onRelease` callback throws | 131 /// returned future completes successfully. If any `onRelease` callback throws |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 /// produce additional information later on. For example, an isolate's task | 258 /// produce additional information later on. For example, an isolate's task |
258 /// may be complete, but it could still emit asynchronous errors. | 259 /// may be complete, but it could still emit asynchronous errors. |
259 void allowRelease(onRelease()) { | 260 void allowRelease(onRelease()) { |
260 if (_released) { | 261 if (_released) { |
261 throw new StateError("A PoolResource may only be released once."); | 262 throw new StateError("A PoolResource may only be released once."); |
262 } | 263 } |
263 _released = true; | 264 _released = true; |
264 _pool._onResourceReleaseAllowed(onRelease); | 265 _pool._onResourceReleaseAllowed(onRelease); |
265 } | 266 } |
266 } | 267 } |
OLD | NEW |