Skip to content

Instantly share code, notes, and snippets.

@akidee
Created December 10, 2010 17:07
Show Gist options
  • Save akidee/736477 to your computer and use it in GitHub Desktop.
Save akidee/736477 to your computer and use it in GitHub Desktop.
We need a way to allow execution only for filters that be parallelized, like map, unlike filter
var async = require('async.js/lib/async')
var sys = require('sys')
async.plugin({
parallel: function(sliceLength) {
if (sliceLength === 1) return this
if (sliceLength < 1) sliceLength = Infinity
var sliceI = -1
var sliceValues
var sliceErrors
var source = this.source
this.next = function(callback) {
sliceI++
if (sliceI === sliceLength) sliceI = 0
var yieldNext = function() {
if (sliceErrors[sliceI] == async.STOP)
callback && callback(async.STOP)
else
callback && callback(sliceErrors[sliceI], sliceValues[sliceI])
}
// Access computed slice
if (sliceI !== 0) {
yieldNext()
}
// Generate next slice in parallel
else {
sliceValues = []
sliceErrors = []
var e = null
var i = -1
var handler = function(j) {
console.log(j);
return function(err, value) {
console.log(' ', j, err == async.STOP, value)
sliceValues[j] = value
sliceErrors[j] = e = err
process.nextTick(function() {
// Yield first item in this slice
console.log(' ', i - 1)
if (--i === -1) yieldNext()
})
}
}
do {
source.next(handler(++i))
} while(i < sliceLength - 1 && e != async.STOP)
}
}
return new this.constructor(this)
}
})
async
.range(0, 10)
.map(function(i, next) {
time = Math.round(Math.random() * 100)
//sys.print(i, 'yet '+time+' - ')
setTimeout(function() {
//sys.print(i, 'now - ');
next(null, i > 5);
}, time)
})
.parallel(5)
.toArray(function(e, list) {
console.log(arguments)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment