| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Datastructure: Priority Pool. |
| 17 |
* |
| 18 |
* |
| 19 |
* An extending of Pool that handles queueing and prioritization. |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
goog.provide('goog.structs.PriorityPool'); |
| 24 |
|
| 25 |
goog.require('goog.structs.Pool'); |
| 26 |
goog.require('goog.structs.PriorityQueue'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* A generic pool class. If min is greater than max, an error is thrown. |
| 32 |
* @param {number=} opt_minCount Min. number of objects (Default: 0). |
| 33 |
* @param {number=} opt_maxCount Max. number of objects (Default: 10). |
| 34 |
* @constructor |
| 35 |
* @extends {goog.structs.Pool<VALUE>} |
| 36 |
* @template VALUE |
| 37 |
*/ |
| 38 |
goog.structs.PriorityPool = function(opt_minCount, opt_maxCount) { |
| 39 |
/** |
| 40 |
* The key for the most recent timeout created. |
| 41 |
* @private {number|undefined} |
| 42 |
*/ |
| 43 |
this.delayTimeout_ = undefined; |
| 44 |
|
| 45 |
/** |
| 46 |
* Queue of requests for pool objects. |
| 47 |
* @private {goog.structs.PriorityQueue<VALUE>} |
| 48 |
*/ |
| 49 |
this.requestQueue_ = new goog.structs.PriorityQueue(); |
| 50 |
|
| 51 |
// Must break convention of putting the super-class's constructor first. This |
| 52 |
// is because the super-class constructor calls adjustForMinMax, which this |
| 53 |
// class overrides. In this class's implementation, it assumes that there |
| 54 |
// is a requestQueue_, and will error if not present. |
| 55 |
goog.structs.Pool.call(this, opt_minCount, opt_maxCount); |
| 56 |
}; |
| 57 |
goog.inherits(goog.structs.PriorityPool, goog.structs.Pool); |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Default priority for pool objects requests. |
| 62 |
* @type {number} |
| 63 |
* @private |
| 64 |
*/ |
| 65 |
goog.structs.PriorityPool.DEFAULT_PRIORITY_ = 100; |
| 66 |
|
| 67 |
|
| 68 |
/** @override */ |
| 69 |
goog.structs.PriorityPool.prototype.setDelay = function(delay) { |
| 70 |
goog.structs.PriorityPool.base(this, 'setDelay', delay); |
| 71 |
|
| 72 |
// If the pool hasn't been accessed yet, no need to do anything. |
| 73 |
if (!goog.isDefAndNotNull(this.lastAccess)) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
goog.global.clearTimeout(this.delayTimeout_); |
| 78 |
this.delayTimeout_ = goog.global.setTimeout( |
| 79 |
goog.bind(this.handleQueueRequests_, this), |
| 80 |
this.delay + this.lastAccess - goog.now()); |
| 81 |
|
| 82 |
// Handle all requests. |
| 83 |
this.handleQueueRequests_(); |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Get a new object from the the pool, if there is one available, otherwise |
| 89 |
* return undefined. |
| 90 |
* @param {Function=} opt_callback The function to callback when an object is |
| 91 |
* available. This could be immediately. If this is not present, then an |
| 92 |
* object is immediately returned if available, or undefined if not. |
| 93 |
* @param {number=} opt_priority The priority of the request. A smaller value |
| 94 |
* means a higher priority. |
| 95 |
* @return {VALUE|undefined} The new object from the pool if there is one |
| 96 |
* available and a callback is not given. Otherwise, undefined. |
| 97 |
* @override |
| 98 |
*/ |
| 99 |
goog.structs.PriorityPool.prototype.getObject = function( |
| 100 |
opt_callback, opt_priority) { |
| 101 |
if (!opt_callback) { |
| 102 |
var result = goog.structs.PriorityPool.base(this, 'getObject'); |
| 103 |
if (result && this.delay) { |
| 104 |
this.delayTimeout_ = goog.global.setTimeout( |
| 105 |
goog.bind(this.handleQueueRequests_, this), this.delay); |
| 106 |
} |
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
var priority = goog.isDef(opt_priority) ? |
| 111 |
opt_priority : |
| 112 |
goog.structs.PriorityPool.DEFAULT_PRIORITY_; |
| 113 |
this.requestQueue_.enqueue(priority, opt_callback); |
| 114 |
|
| 115 |
// Handle all requests. |
| 116 |
this.handleQueueRequests_(); |
| 117 |
|
| 118 |
return undefined; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Handles the request queue. Tries to fires off as many queued requests as |
| 124 |
* possible. |
| 125 |
* @private |
| 126 |
*/ |
| 127 |
goog.structs.PriorityPool.prototype.handleQueueRequests_ = function() { |
| 128 |
var requestQueue = this.requestQueue_; |
| 129 |
while (requestQueue.getCount() > 0) { |
| 130 |
var obj = this.getObject(); |
| 131 |
|
| 132 |
if (!obj) { |
| 133 |
return; |
| 134 |
} else { |
| 135 |
var requestCallback = requestQueue.dequeue(); |
| 136 |
requestCallback.apply(this, [obj]); |
| 137 |
} |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Adds an object to the collection of objects that are free. If the object can |
| 144 |
* not be added, then it is disposed. |
| 145 |
* |
| 146 |
* NOTE: This method does not remove the object from the in use collection. |
| 147 |
* |
| 148 |
* @param {VALUE} obj The object to add to the collection of free objects. |
| 149 |
* @override |
| 150 |
*/ |
| 151 |
goog.structs.PriorityPool.prototype.addFreeObject = function(obj) { |
| 152 |
goog.structs.PriorityPool.superClass_.addFreeObject.call(this, obj); |
| 153 |
|
| 154 |
// Handle all requests. |
| 155 |
this.handleQueueRequests_(); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Adjusts the objects held in the pool to be within the min/max constraints. |
| 161 |
* |
| 162 |
* NOTE: It is possible that the number of objects in the pool will still be |
| 163 |
* greater than the maximum count of objects allowed. This will be the case |
| 164 |
* if no more free objects can be disposed of to get below the minimum count |
| 165 |
* (i.e., all objects are in use). |
| 166 |
* @override |
| 167 |
*/ |
| 168 |
goog.structs.PriorityPool.prototype.adjustForMinMax = function() { |
| 169 |
goog.structs.PriorityPool.superClass_.adjustForMinMax.call(this); |
| 170 |
|
| 171 |
// Handle all requests. |
| 172 |
this.handleQueueRequests_(); |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
/** @override */ |
| 177 |
goog.structs.PriorityPool.prototype.disposeInternal = function() { |
| 178 |
goog.structs.PriorityPool.superClass_.disposeInternal.call(this); |
| 179 |
goog.global.clearTimeout(this.delayTimeout_); |
| 180 |
this.requestQueue_.clear(); |
| 181 |
this.requestQueue_ = null; |
| 182 |
}; |