Skip to content

Instantly share code, notes, and snippets.

@lsm
Created June 28, 2010 16:50
Show Gist options
  • Save lsm/456082 to your computer and use it in GitHub Desktop.
Save lsm/456082 to your computer and use it in GitHub Desktop.
var Pool = Base(EventEmitter, {
init: function(getConnection, size) {
this._super();
this._pool = [];
this._queue = [];
this.size = size;
var me = this;
getConnection(size, function(conn) {
me.emit('back', conn);
});
this.addListener('back', function(conn) {
var task = me._queue.shift();
if (task) {
task(conn);
} else {
me._pool.push(conn);
}
});
},
give: function(callback) {
var conn = this._pool.shift();
if (conn) {
callback(conn);
} else {
this._queue.push(callback);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment