2010-10-24 01:45:37 +08:00
|
|
|
var events = require('events');
|
2010-12-11 07:32:34 +08:00
|
|
|
var sys = require('sys');
|
2010-10-24 01:45:37 +08:00
|
|
|
|
|
|
|
if(typeof events.EventEmitter.prototype.once !== 'function') {
|
|
|
|
events.EventEmitter.prototype.once = function (type, listener) {
|
|
|
|
var self = this;
|
|
|
|
self.on(type, function g () {
|
|
|
|
self.removeListener(type, g);
|
|
|
|
listener.apply(this, arguments);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2010-12-10 08:10:42 +08:00
|
|
|
var Pool = function(maxSize, createFn) {
|
2010-12-11 07:32:34 +08:00
|
|
|
events.EventEmitter.call(this);
|
2010-12-10 08:10:42 +08:00
|
|
|
this.maxSize = maxSize;
|
|
|
|
this.createFn = createFn;
|
|
|
|
this.items = [];
|
|
|
|
this.waits = [];
|
|
|
|
}
|
2010-12-11 07:32:34 +08:00
|
|
|
sys.inherits(Pool, events.EventEmitter);
|
2010-12-10 08:10:42 +08:00
|
|
|
var p = Pool.prototype;
|
|
|
|
|
|
|
|
p.checkOut = function(callback) {
|
|
|
|
var len = 0;
|
|
|
|
for(var i = 0, len = this.items.length; i < len; i++) {
|
|
|
|
var item = this.items[i];
|
|
|
|
if(item.checkedIn) {
|
|
|
|
return this._pulse(item, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//check if we can create a new item
|
2010-12-11 07:32:34 +08:00
|
|
|
if(this.items.length < this.maxSize && this.createFn) {
|
|
|
|
var result = this.createFn();
|
|
|
|
var item = result;
|
|
|
|
//create function can return item conforming to interface
|
|
|
|
//of stored items to allow for create function to create
|
|
|
|
//checked out items
|
|
|
|
if(typeof item.checkedIn == "undefined") {
|
|
|
|
var item = {ref: result, checkedIn: true}
|
|
|
|
}
|
2010-12-10 08:10:42 +08:00
|
|
|
this.items.push(item);
|
2010-12-11 07:32:34 +08:00
|
|
|
if(item.checkedIn) {
|
|
|
|
return this._pulse(item, callback)
|
|
|
|
}
|
2010-12-10 08:10:42 +08:00
|
|
|
}
|
|
|
|
this.waits.push(callback);
|
|
|
|
return false; //did not execute sync
|
|
|
|
}
|
|
|
|
|
|
|
|
p.checkIn = function(item) {
|
|
|
|
//scan current items
|
|
|
|
for(var i = 0, len = this.items.length; i < len; i++) {
|
|
|
|
var currentItem = this.items[i];
|
|
|
|
if(currentItem.ref == item) {
|
|
|
|
currentItem.checkedIn = true;
|
2010-12-11 07:32:34 +08:00
|
|
|
this._pulse(currentItem);
|
|
|
|
return true;
|
2010-12-10 08:10:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//add new item
|
|
|
|
var newItem = {ref: item, checkedIn: true};
|
|
|
|
this.items.push(newItem);
|
2010-12-11 07:32:34 +08:00
|
|
|
this._pulse(newItem);
|
|
|
|
return false;
|
2010-12-10 08:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p._pulse = function(item, cb) {
|
2010-12-11 07:32:34 +08:00
|
|
|
cb = cb || this.waits.shift()
|
2010-12-10 08:10:42 +08:00
|
|
|
if(cb) {
|
|
|
|
item.checkedIn = false;
|
|
|
|
cb(null, item.ref)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Pool: Pool
|
|
|
|
}
|