lib/functionpool/pool.js
function Pool(options, fn) {
  if (typeof options == 'function') {
    fn = options;
    options = {};
  }
  options = options || {};
  
  if (!fn) { throw new Error('Pool requires a worker function') };
  if (fn.length < 1) { throw new Error('Pool worker function must accept done callback as an argument') };
  
  EventEmitter.call(this);
  this._fn = fn;
  this._workers = this._createWorkers(options.size || 5);
  this._working = 0;
  this._queue = [];
  
  var self = this;
  this.on('task', function() {
    if (self._workers.length == self._working) { return; }
    self._dispatch();
  });
  this.on('done', function() {
    // TODO: emit a `drain` event if the pool was previously queuing, and now
    //       has slots available
    if (self._working == 0 && self._queue.length == 0) { self.emit('idle'); }
    if (self._queue.length == 0) { return; }
    self._dispatch();
  })
}
function Pool(options, fn) {
  if (typeof options == 'function') {
    fn = options;
    options = {};
  }
  options = options || {};
  
  if (!fn) { throw new Error('Pool requires a worker function') };
  if (fn.length < 1) { throw new Error('Pool worker function must accept done callback as an argument') };
  
  EventEmitter.call(this);
  this._fn = fn;
  this._workers = this._createWorkers(options.size || 5);
  this._working = 0;
  this._queue = [];
  
  var self = this;
  this.on('task', function() {
    if (self._workers.length == self._working) { return; }
    self._dispatch();
  });
  this.on('done', function() {
    // TODO: emit a `drain` event if the pool was previously queuing, and now
    //       has slots available
    if (self._working == 0 && self._queue.length == 0) { self.emit('idle'); }
    if (self._queue.length == 0) { return; }
    self._dispatch();
  })
}
function Pool ( options , fn ) { if ( typeof options == 'function' ) { fn = options ; options = { } ; } options = options || { } ; if ( ! fn ) { throw new Error ( 'Pool requires a worker function' ) } ; if ( fn . length < 1 ) { throw new Error ( 'Pool worker function must accept done callback as an argument' ) } ; EventEmitter . call ( this ) ; this . _fn = fn ; this . _workers = this . _createWorkers ( options . size || 5 ) ; this . _working = 0 ; this . _queue = [ ] ; var self = this ; this . on ( 'task' , function ( ) { if ( self . _workers . length == self . _working ) { return ; } self . _dispatch ( ) ; } ) ; this . on ( 'done' , function ( ) { // TODO: emit a `drain` event if the pool was previously queuing, and now //       has slots available if ( self . _working == 0 && self . _queue . length == 0 ) { self . emit ( 'idle' ) ; } if ( self . _queue . length == 0 ) { return ; } self . _dispatch ( ) ; } ) }
`Pool` constructor.

Assigns `fn` to a pool used to execute (potentially expensive) tasks.  The
pool has a limited size (defaulting to 5), which controls the maximum number
of tasks that will be allowed to execute concurrently.  After the limit has
been reached, tasks will be queued and executed when prior tasks complete.

`fn` must accept a `done` callback, which it must invoke with a result (or
error) upon completion.

Options:
- `size`  maximum number of tasks allowed to execute concurrently (default: 5)

Examples:

new Pool(function(name, done) {
setTimeout(function() { done(null, 'Hello ' + name) }, 1000);
});

new Pool({ size: 3 }, function(x, y, done) {
setTimeout(function() { done(null, x + y) }, 5000);
});

@param {Object} options
@param {Function} fn
@api public
Pool constructor .