Base class for a pool.
Instance PropertiesProperty | Type | Default Value | Description |
count | Number | the total number of objects in the pool, including free and in use objects. | |
freeCount | Number | the number of free objects in this pool. | |
inUseCount | Number | the number of objects in use in this pool. | |
maxObjects | Number | 1 | the maximum number of objects this pool should contain |
minObjects | Number | 0 | the minimum number of objects this pool should contain. |
Creates a new object for this pool.
Object
be default just creates an object.
- function (){
- return {};
- }
Retrieves an object from this pool. `
Returns*
an object to contained in this pool
- 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;
- }
Removes an object from the pool, this can be overriden to provide any teardown of objects that needs to take place.
Argumentsthe object that needs to be removed.
*
the object removed.
- 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;
- }
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;
Argumentsthe object to return to the pool
- 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);
- }
- }
Validates an object in this pool.
THIS SHOULD BE OVERRIDDEN TO VALIDATE
the object to validate.
- function (obj){
- return true;
- }
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git