prioritypool.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * @fileoverview Datastructure: Priority Pool.
  16. *
  17. *
  18. * An extending of Pool that handles queueing and prioritization.
  19. */
  20. goog.provide('goog.structs.PriorityPool');
  21. goog.require('goog.structs.Pool');
  22. goog.require('goog.structs.PriorityQueue');
  23. /**
  24. * A generic pool class. If min is greater than max, an error is thrown.
  25. * @param {number=} opt_minCount Min. number of objects (Default: 0).
  26. * @param {number=} opt_maxCount Max. number of objects (Default: 10).
  27. * @constructor
  28. * @extends {goog.structs.Pool<VALUE>}
  29. * @template VALUE
  30. */
  31. goog.structs.PriorityPool = function(opt_minCount, opt_maxCount) {
  32. /**
  33. * The key for the most recent timeout created.
  34. * @private {number|undefined}
  35. */
  36. this.delayTimeout_ = undefined;
  37. /**
  38. * Queue of requests for pool objects.
  39. * @private {goog.structs.PriorityQueue<VALUE>}
  40. */
  41. this.requestQueue_ = new goog.structs.PriorityQueue();
  42. // Must break convention of putting the super-class's constructor first. This
  43. // is because the super-class constructor calls adjustForMinMax, which this
  44. // class overrides. In this class's implementation, it assumes that there
  45. // is a requestQueue_, and will error if not present.
  46. goog.structs.Pool.call(this, opt_minCount, opt_maxCount);
  47. };
  48. goog.inherits(goog.structs.PriorityPool, goog.structs.Pool);
  49. /**
  50. * Default priority for pool objects requests.
  51. * @type {number}
  52. * @private
  53. */
  54. goog.structs.PriorityPool.DEFAULT_PRIORITY_ = 100;
  55. /** @override */
  56. goog.structs.PriorityPool.prototype.setDelay = function(delay) {
  57. goog.structs.PriorityPool.base(this, 'setDelay', delay);
  58. // If the pool hasn't been accessed yet, no need to do anything.
  59. if (!goog.isDefAndNotNull(this.lastAccess)) {
  60. return;
  61. }
  62. goog.global.clearTimeout(this.delayTimeout_);
  63. this.delayTimeout_ = goog.global.setTimeout(
  64. goog.bind(this.handleQueueRequests_, this),
  65. this.delay + this.lastAccess - goog.now());
  66. // Handle all requests.
  67. this.handleQueueRequests_();
  68. };
  69. /**
  70. * Get a new object from the the pool, if there is one available, otherwise
  71. * return undefined.
  72. * @param {Function=} opt_callback The function to callback when an object is
  73. * available. This could be immediately. If this is not present, then an
  74. * object is immediately returned if available, or undefined if not.
  75. * @param {number=} opt_priority The priority of the request. A smaller value
  76. * means a higher priority.
  77. * @return {VALUE|undefined} The new object from the pool if there is one
  78. * available and a callback is not given. Otherwise, undefined.
  79. * @override
  80. */
  81. goog.structs.PriorityPool.prototype.getObject = function(
  82. opt_callback, opt_priority) {
  83. if (!opt_callback) {
  84. var result = goog.structs.PriorityPool.base(this, 'getObject');
  85. if (result && this.delay) {
  86. this.delayTimeout_ = goog.global.setTimeout(
  87. goog.bind(this.handleQueueRequests_, this), this.delay);
  88. }
  89. return result;
  90. }
  91. var priority = goog.isDef(opt_priority) ?
  92. opt_priority :
  93. goog.structs.PriorityPool.DEFAULT_PRIORITY_;
  94. this.requestQueue_.enqueue(priority, opt_callback);
  95. // Handle all requests.
  96. this.handleQueueRequests_();
  97. return undefined;
  98. };
  99. /**
  100. * Handles the request queue. Tries to fires off as many queued requests as
  101. * possible.
  102. * @private
  103. */
  104. goog.structs.PriorityPool.prototype.handleQueueRequests_ = function() {
  105. var requestQueue = this.requestQueue_;
  106. while (requestQueue.getCount() > 0) {
  107. var obj = this.getObject();
  108. if (!obj) {
  109. return;
  110. } else {
  111. var requestCallback = requestQueue.dequeue();
  112. requestCallback.apply(this, [obj]);
  113. }
  114. }
  115. };
  116. /**
  117. * Adds an object to the collection of objects that are free. If the object can
  118. * not be added, then it is disposed.
  119. *
  120. * NOTE: This method does not remove the object from the in use collection.
  121. *
  122. * @param {VALUE} obj The object to add to the collection of free objects.
  123. * @override
  124. */
  125. goog.structs.PriorityPool.prototype.addFreeObject = function(obj) {
  126. goog.structs.PriorityPool.superClass_.addFreeObject.call(this, obj);
  127. // Handle all requests.
  128. this.handleQueueRequests_();
  129. };
  130. /**
  131. * Adjusts the objects held in the pool to be within the min/max constraints.
  132. *
  133. * NOTE: It is possible that the number of objects in the pool will still be
  134. * greater than the maximum count of objects allowed. This will be the case
  135. * if no more free objects can be disposed of to get below the minimum count
  136. * (i.e., all objects are in use).
  137. * @override
  138. */
  139. goog.structs.PriorityPool.prototype.adjustForMinMax = function() {
  140. goog.structs.PriorityPool.superClass_.adjustForMinMax.call(this);
  141. // Handle all requests.
  142. this.handleQueueRequests_();
  143. };
  144. /** @override */
  145. goog.structs.PriorityPool.prototype.disposeInternal = function() {
  146. goog.structs.PriorityPool.superClass_.disposeInternal.call(this);
  147. goog.global.clearTimeout(this.delayTimeout_);
  148. this.requestQueue_.clear();
  149. this.requestQueue_ = null;
  150. };