Created
June 28, 2010 16:50
-
-
Save lsm/456082 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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