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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 _resetTimer(); | 176 _resetTimer(); |
177 | 177 |
178 if (_requestedResources.isNotEmpty) { | 178 if (_requestedResources.isNotEmpty) { |
179 var pending = _requestedResources.removeFirst(); | 179 var pending = _requestedResources.removeFirst(); |
180 pending.complete(_runOnRelease(onRelease)); | 180 pending.complete(_runOnRelease(onRelease)); |
181 } else if (isClosed) { | 181 } else if (isClosed) { |
182 _closeGroup.add(new Future.sync(onRelease)); | 182 _closeGroup.add(new Future.sync(onRelease)); |
183 _allocatedResources--; | 183 _allocatedResources--; |
184 if (_allocatedResources == 0) _closeGroup.close(); | 184 if (_allocatedResources == 0) _closeGroup.close(); |
185 } else { | 185 } else { |
186 _onReleaseCallbacks.add( | 186 _onReleaseCallbacks.add(Zone.current.bindCallback(onRelease)); |
187 Zone.current.bindCallback(onRelease, runGuarded: false)); | |
188 } | 187 } |
189 } | 188 } |
190 | 189 |
191 /// Runs [onRelease] and returns a Future that completes to a resource once an | 190 /// Runs [onRelease] and returns a Future that completes to a resource once an |
192 /// [onRelease] callback completes. | 191 /// [onRelease] callback completes. |
193 /// | 192 /// |
194 /// Futures returned by [_runOnRelease] always complete in the order they were | 193 /// Futures returned by [_runOnRelease] always complete in the order they were |
195 /// created, even if earlier [onRelease] callbacks take longer to run. | 194 /// created, even if earlier [onRelease] callbacks take longer to run. |
196 Future<PoolResource> _runOnRelease(onRelease()) { | 195 Future<PoolResource> _runOnRelease(onRelease()) { |
197 new Future.sync(onRelease).then((value) { | 196 new Future.sync(onRelease).then((value) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 /// produce additional information later on. For example, an isolate's task | 265 /// produce additional information later on. For example, an isolate's task |
267 /// may be complete, but it could still emit asynchronous errors. | 266 /// may be complete, but it could still emit asynchronous errors. |
268 void allowRelease(onRelease()) { | 267 void allowRelease(onRelease()) { |
269 if (_released) { | 268 if (_released) { |
270 throw new StateError("A PoolResource may only be released once."); | 269 throw new StateError("A PoolResource may only be released once."); |
271 } | 270 } |
272 _released = true; | 271 _released = true; |
273 _pool._onResourceReleaseAllowed(onRelease); | 272 _pool._onResourceReleaseAllowed(onRelease); |
274 } | 273 } |
275 } | 274 } |
OLD | NEW |