Base class for a pool.

Instance Properties
PropertyTypeDefault ValueDescription
countNumber

the total number of objects in the pool, including free and in use objects.

freeCountNumber

the number of free objects in this pool.

inUseCountNumber

the number of objects in use in this pool.

maxObjectsNumber1

the maximum number of objects this pool should contain

minObjectsNumber0

the minimum number of objects this pool should contain.

Constructor

Defined collections/Pool.js

createObject Function Public


Defined collections/Pool.js

Creates a new object for this pool.

Returns Source
function (){
   return {};
           
}
    

getObject Function Public


Defined collections/Pool.js

Retrieves an object from this pool. `

Returns Source
function (){
   var ret;
   if (this.count <= this.__maxObjects && this.freeCount > 0) {
       ret = this.__freeObjects.dequeue();
       this.__inUseObjects.push(ret);
   } else if (this.count < this.__maxObjects) {
       ret = this.createObject();
       this.__inUseObjects.push(ret);
   }
   return ret;
           
}
    

removeObject Function Public


Defined collections/Pool.js

Removes an object from the pool, this can be overriden to provide any teardown of objects that needs to take place.

Arguments Returns Source
function (obj){
   var index;
   if (this.__freeObjects.contains(obj)) {
       this.__freeObjects.remove(obj);
   } else if ((index = this.__inUseObjects.indexOf(obj)) > -1) {
       this.__inUseObjects.splice(index, 1);
   }
   //otherwise its not contained in this pool;
   return obj;
           
}
    

returnObject Function Public


Defined collections/Pool.js

Returns an object to this pool. The object is validated before it is returned to the pool, if the validation fails then it is removed from the pool;

Arguments Source
function (obj){
   var index;
   if (this.validate(obj) && this.count <= this.__maxObjects && (index = this.__inUseObjects.indexOf(obj)) > -1) {
       this.__freeObjects.enqueue(obj);
       this.__inUseObjects.splice(index, 1);
   } else {
       this.removeObject(obj);
   }
           
}
    

validate Function Public


Defined collections/Pool.js

Validates an object in this pool.
THIS SHOULD BE OVERRIDDEN TO VALIDATE

Arguments Source
function (obj){
   return true;
           
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta