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 library pool; | 5 library pool; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 _resetTimer(); | 80 _resetTimer(); |
81 return completer.future; | 81 return completer.future; |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 /// Requests a resource for the duration of [callback], which may return a | 85 /// Requests a resource for the duration of [callback], which may return a |
86 /// Future. | 86 /// Future. |
87 /// | 87 /// |
88 /// The return value of [callback] is piped to the returned Future. | 88 /// The return value of [callback] is piped to the returned Future. |
89 Future withResource(callback()) { | 89 Future withResource(callback()) { |
90 return request().then((resource) => | 90 return request().then((resource) => new Future.sync(callback).whenComplete(r esource.release)); |
nweiz
2015/07/01 00:44:34
Long line.
| |
91 Chain.track(new Future.sync(callback)).whenComplete(resource.release)); | |
92 } | 91 } |
93 | 92 |
94 /// If there are any pending requests, this will fire the oldest one. | 93 /// If there are any pending requests, this will fire the oldest one. |
95 void _onResourceReleased() { | 94 void _onResourceReleased() { |
96 _resetTimer(); | 95 _resetTimer(); |
97 | 96 |
98 if (_requestedResources.isEmpty) { | 97 if (_requestedResources.isEmpty) { |
99 _allocatedResources--; | 98 _allocatedResources--; |
100 return; | 99 return; |
101 } | 100 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 /// may be complete, but it could still emit asynchronous errors. | 196 /// may be complete, but it could still emit asynchronous errors. |
198 void allowRelease(onRelease()) { | 197 void allowRelease(onRelease()) { |
199 if (_released) { | 198 if (_released) { |
200 throw new StateError("A PoolResource may only be released once."); | 199 throw new StateError("A PoolResource may only be released once."); |
201 } | 200 } |
202 _released = true; | 201 _released = true; |
203 _pool._onResourceReleaseAllowed(onRelease); | 202 _pool._onResourceReleaseAllowed(onRelease); |
204 } | 203 } |
205 } | 204 } |
206 | 205 |
OLD | NEW |