-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
127 additions
and
160 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# dist | ||
dist/ | ||
|
||
# ignore dependencies | ||
node_modules/ | ||
|
||
|
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
|
||
var callbackCount = require('callback-count') | ||
var nextTick = require('next-tick') | ||
|
||
function Parallizer (max, col, paused) { | ||
if (!(this instanceof Parallizer)) return new Parallizer(max, col, paused) | ||
var pm = parseInt(max, 10) | ||
this._max = (!isNaN(pm) && pm > 0) ? pm : 1 | ||
this._running = 0 | ||
this._queue = [] | ||
this._paused = !!paused | ||
this._col = null | ||
if (typeof col === 'function') { | ||
this._col = callbackCount(nextTick.apply.bind(nextTick, null, [col])) | ||
} | ||
} | ||
|
||
Parallizer.prototype.add = function (fn, args, cb, scope, high) { | ||
var self = this | ||
if (!(args instanceof Array)) args = [] | ||
if (typeof cb !== 'function') cb = function () {} | ||
args.push(function () { | ||
cb.apply(this, Array.prototype.slice.call(arguments)) | ||
if (self._col) self._col.next() | ||
self._running-- | ||
self._check() | ||
}) | ||
fn = (typeof fn === 'function') ? fn : function () {} | ||
var fno = fn.apply.bind(fn, scope || null, args || []) | ||
self._queue[high ? 'unshift' : 'push'](fno) | ||
if (self._col) self._col.inc() | ||
self._check() | ||
} | ||
|
||
Parallizer.prototype.sadd = function () { | ||
var fa = Array.prototype.slice.call(arguments) | ||
if (fa.length > 0) { | ||
var fn = fa.shift() | ||
var cb = function () {} | ||
if (fa.length > 0 && typeof fa[fa.length - 1] === 'function') cb = fa.pop() | ||
this.add(fn, fa, cb) | ||
} | ||
} | ||
|
||
Parallizer.prototype._check = function () { | ||
if (!this._paused && this._running < this._max && this._queue.length > 0) { | ||
this._running++ | ||
nextTick(this._queue.shift()) | ||
} | ||
} | ||
|
||
Parallizer.prototype.pause = function () { | ||
this._paused = true | ||
} | ||
|
||
Parallizer.prototype.start = function () { | ||
this._paused = false | ||
this._check() | ||
} | ||
|
||
// backwards compatibility | ||
Parallizer.Parallel = Parallizer | ||
|
||
module.exports = Parallizer |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.