Comparing version 0.0.5 to 0.0.6
454
lib/nue.js
@@ -1,22 +0,190 @@ | ||
var EventEmitter = require('events').EventEmitter; | ||
'use strict'; | ||
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var SENTINEL = {}; | ||
exports.DEFAULT_BATCH_SIZE = 3; | ||
exports.name = 'nue'; | ||
exports.version = '0.0.6'; | ||
exports.batchSize = exports.DEFAULT_BATCH_SIZE; | ||
exports.flow = flow; | ||
exports.forEach = forEach; | ||
exports.map = map; | ||
exports.filter = filter; | ||
exports.every = every; | ||
exports.some = some; | ||
exports.parallel = parallel; | ||
exports.parallelForEach = parallelForEach; | ||
exports.parallelMap = parallelMap; | ||
exports.parallelFilter = parallelFilter; | ||
exports.parallelEvery = parallelEvery; | ||
exports.parallelSome = parallelSome; | ||
function flow() { | ||
function flow(batchSize) { | ||
var tasks = Array.prototype.slice.call(arguments, 1); | ||
var defer = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
runFlow(batchSize, tasks, defer, args, this); | ||
}; | ||
attachEmitter(defer); | ||
return defer; | ||
} | ||
return wrapOrApply(flow, arguments); | ||
} | ||
function forEach() { | ||
function forEach(batchSize) { | ||
return doSerial(batchSize, arguments[1], strategies.forEach); | ||
} | ||
return wrapOrApply(forEach, arguments); | ||
} | ||
function map() { | ||
function map(batchSize) { | ||
return doSerial(batchSize, arguments[1], strategies.map); | ||
} | ||
return wrapOrApply(map, arguments); | ||
} | ||
function filter() { | ||
function filter(batchSize) { | ||
return doSerial(batchSize, arguments[1], strategies.filter); | ||
} | ||
return wrapOrApply(filter, arguments); | ||
} | ||
function every() { | ||
function every(batchSize) { | ||
return doSerial(batchSize, arguments[1], strategies.every); | ||
} | ||
return wrapOrApply(every, arguments); | ||
} | ||
function some() { | ||
function some(batchSize) { | ||
return doSerial(batchSize, arguments[1], strategies.some); | ||
} | ||
return wrapOrApply(some, arguments); | ||
} | ||
function doSerial(batchSize, worker, strategy) { | ||
if (typeof worker !== 'function') { | ||
throw new Error('The worker must be a function.'); | ||
} | ||
var defer = function () { | ||
runSerial(batchSize, worker, normalizeArgs(arguments), this, strategy); | ||
}; | ||
attachEmitter(defer); | ||
return defer; | ||
} | ||
function parallel() { | ||
function parallel(batchSize) { | ||
var tasks = normalizeArgs(Array.prototype.slice.call(arguments, 1)); | ||
var workers = tasks.map(function (task, i) { | ||
return { task: task, index: i }; | ||
}); | ||
var defer = function () { | ||
runParallel(batchSize, workers, normalizeArgs(arguments), this, strategies.map); | ||
}; | ||
attachEmitter(defer); | ||
return defer; | ||
} | ||
return wrapOrApply(parallel, arguments); | ||
} | ||
function parallelForEach() { | ||
function parallelForEach(batchSize) { | ||
return doParallel(batchSize, arguments[1], strategies.forEach); | ||
} | ||
return wrapOrApply(parallelForEach, arguments); | ||
} | ||
function parallelMap() { | ||
function parallelMap(batchSize) { | ||
return doParallel(batchSize, arguments[1], strategies.map); | ||
} | ||
return wrapOrApply(parallelMap, arguments); | ||
} | ||
function parallelFilter() { | ||
function parallelFilter(batchSize) { | ||
return doParallel(batchSize, arguments[1], strategies.filter); | ||
} | ||
return wrapOrApply(parallelFilter, arguments); | ||
} | ||
function parallelEvery() { | ||
function parallelEvery(batchSize) { | ||
return doParallel(batchSize, arguments[1], strategies.every); | ||
} | ||
return wrapOrApply(parallelEvery, arguments); | ||
} | ||
function parallelSome() { | ||
function parallelSome(batchSize) { | ||
return doParallel(batchSize, arguments[1], strategies.some); | ||
} | ||
return wrapOrApply(parallelSome, arguments); | ||
} | ||
function doParallel(batchSize, worker, strategy) { | ||
if (typeof worker !== 'function') { | ||
throw new Error('The worker must be a function.'); | ||
} | ||
var defer = function () { | ||
var values = normalizeArgs(arguments); | ||
var workers = values.map(function (value, i) { | ||
return { | ||
task: function () { | ||
worker.apply(this, arguments); | ||
}, | ||
index: i | ||
}; | ||
}); | ||
runParallel(batchSize, workers, values, this, strategy); | ||
}; | ||
attachEmitter(defer); | ||
return defer; | ||
} | ||
function wrapOrApply(fn, args) { | ||
if (typeof args[0] === 'number') { | ||
return function () { | ||
return fn.apply(null, [args[0]].concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
} | ||
return fn.apply(null, [null].concat(Array.prototype.slice.call(args))) | ||
} | ||
function attachEmitter(target) { | ||
target.events = new EventEmitter(); | ||
target.__nue__ = true; | ||
} | ||
function normalizeArgs(args) { | ||
if (args.length === 1 && Array.isArray(args[0])) { | ||
return args[0] | ||
} else { | ||
return Array.isArray(args) ? args : Array.prototype.slice.call(args); | ||
} | ||
} | ||
var strategies = { | ||
forEach: function (value, result, i, results, isLast, next) { | ||
forEach: function (original, result, i, results, isLast, next) { | ||
results[i] = result; | ||
next(); | ||
}, | ||
map: function (value, result, i, results, isLast, next) { | ||
map: function (original, result, i, results, isLast, next) { | ||
results[i] = result; | ||
next(); | ||
}, | ||
filter: function (value, result, i, results, isLast, next) { | ||
filter: function (original, result, i, results, isLast, next) { | ||
if (result) { | ||
results.push(value); | ||
results.push(original); | ||
} | ||
next(); | ||
}, | ||
every: function (value, result, i, results, isLast, next, end) { | ||
every: function (original, result, i, results, isLast, next, end) { | ||
if (!result) { | ||
@@ -30,3 +198,3 @@ end(false); | ||
}, | ||
some: function (value, result, i, results, isLast, next, end) { | ||
some: function (original, result, i, results, isLast, next, end) { | ||
if (result) { | ||
@@ -52,4 +220,2 @@ end(true); | ||
this.err = flow.err; | ||
this.next = this.next.bind(this); | ||
this.end = this.end.bind(this); | ||
} | ||
@@ -79,3 +245,2 @@ | ||
this.err = flow.err; | ||
this.next = this.next.bind(this); | ||
} | ||
@@ -97,2 +262,6 @@ | ||
LastStepContext.prototype.end = function () { | ||
throw new Error('not supported.'); | ||
}; | ||
function FlowRunner(batchSize, tasks, caller, callerArgs, callerContext) { | ||
@@ -110,6 +279,6 @@ this.batchSize = batchSize; | ||
process.nextTick(function () { | ||
emitter.emit('__done__', flow); | ||
emitter.emit('done', flow); | ||
}); | ||
} else { | ||
emitter.emit('__done__', flow); | ||
emitter.emit('done', flow); | ||
} | ||
@@ -127,3 +296,3 @@ }; | ||
if (typeof task !== 'function') { | ||
throw new Error('not function'); | ||
throw new Error('not function. ' + typeof task); | ||
} | ||
@@ -136,3 +305,3 @@ if (task.__nue__) { | ||
}; | ||
Nue.attachEmitter(fn); | ||
attachEmitter(fn); | ||
return fn; | ||
@@ -145,13 +314,7 @@ }); | ||
if (i < len - 1) { | ||
if (step.__nue__) { | ||
if (i == len - 2 ) { | ||
step.events.once('__done__', function(flow) { | ||
self.startStep(new LastStepContext(self, next, flow), next, flow); | ||
}); | ||
} else { | ||
step.events.once('__done__', function(flow) { | ||
self.startStep(new StepContext(self, next, flow), next, flow); | ||
}); | ||
} | ||
} | ||
(function (C) { | ||
step.events.once('done', function(flow) { | ||
self.startStep(new C(self, next, flow), next, flow); | ||
}); | ||
}(i == len - 2 ? LastStepContext : StepContext)); | ||
} | ||
@@ -163,3 +326,3 @@ }); | ||
}); | ||
Nue.attachEmitter(steps[0]); | ||
attachEmitter(steps[0]); | ||
} | ||
@@ -170,3 +333,2 @@ return steps; | ||
FlowRunner.prototype.startFlow = function () { | ||
var inFlow = this.callerContext instanceof ContextBase; | ||
var steps = this.makeSteps(); | ||
@@ -176,7 +338,7 @@ var firstStep = steps[0]; | ||
var self = this; | ||
lastStep.events.once('__done__', function (flow) { | ||
if (inFlow) { | ||
lastStep.events.once('done', function (flow) { | ||
if (self.callerContext instanceof ContextBase) { | ||
self.callerContext.next.apply(self.callerContext, flow.args); | ||
} else { | ||
self.caller.events.emit('__done__', flow); | ||
self.caller.events.emit('done', flow); | ||
} | ||
@@ -189,3 +351,3 @@ }); | ||
lastStep: lastStep, | ||
batchSize: this.batchSize || (inFlow && self.callerContext.batchSize) || exports.batchSize, | ||
batchSize: this.batchSize || exports.batchSize || exports.DEFAULT_BATCH_SIZE, | ||
callCount: 0 | ||
@@ -220,4 +382,2 @@ }; | ||
this.data = runner.callerContext.data; | ||
this.next = this.next.bind(this); | ||
this.end = this.end.bind(this); | ||
} | ||
@@ -252,6 +412,3 @@ | ||
function SerialRunner(batchSize, worker, values, callerContext, strategy) { | ||
this.batchSize = batchSize || callerContext.batchSize || exports.batchSize; | ||
if (!this.batchSize) { | ||
throw new Error('illegal batch size'); | ||
} | ||
this.batchSize = batchSize || callerContext.batchSize || exports.batchSize || exports.DEFAULT_BATCH_SIZE; | ||
this.worker = worker; | ||
@@ -284,2 +441,5 @@ this.values = values; | ||
return function (batchSize, worker, values, callerContext, strategy) { | ||
if (!(callerContext instanceof ContextBase)) { | ||
throw new Error('The context is illegal. This function is out of the flow.'); | ||
} | ||
var runner = new SerialRunner(batchSize, worker, values, callerContext, strategy); | ||
@@ -299,4 +459,2 @@ runner.processValues(); | ||
this.data = runner.callerContext.data; | ||
this.next = this.next.bind(this); | ||
this.end = this.end.bind(this); | ||
} | ||
@@ -323,6 +481,3 @@ | ||
function ParallelRunner(batchSize, workers, values, callerContext, strategy) { | ||
this.batchSize = batchSize || callerContext.batchSize || exports.batchSize; | ||
if (!this.batchSize) { | ||
throw new Error('illegal batch size'); | ||
} | ||
this.batchSize = batchSize || callerContext.batchSize || exports.batchSize || exports.DEFAULT_BATCH_SIZE; | ||
this.workers = workers; | ||
@@ -374,2 +529,5 @@ this.values = values; | ||
return function (batchSize, workers, values, callerContext, strategy) { | ||
if (!(callerContext instanceof ContextBase)) { | ||
throw new Error('The context is illegal. This function is out of the flow.'); | ||
} | ||
var runner = new ParallelRunner(batchSize, workers, values, callerContext, strategy); | ||
@@ -382,118 +540,5 @@ process.nextTick(function () { | ||
function Nue(batchSize) { | ||
this.batchSize = batchSize; | ||
} | ||
Nue.prototype.flow = function () { | ||
var tasks = Array.prototype.slice.call(arguments); | ||
var batchSize = this.batchSize; | ||
var fn = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
runFlow(batchSize, tasks, fn, args, this); | ||
}; | ||
Nue.attachEmitter(fn); | ||
return fn; | ||
}; | ||
Nue.prototype.runSerial = function (worker, strategy) { | ||
var batchSize = this.batchSize; | ||
if (typeof worker !== 'function') { | ||
throw new Error('The worker must be a function.'); | ||
} | ||
var fn = function () { | ||
if (!(this instanceof ContextBase)) { | ||
throw new Error('The context is illegal. This function is out of the flow.'); | ||
} | ||
runSerial(batchSize, worker, normalizeArguments(arguments), this, strategy); | ||
}; | ||
Nue.attachEmitter(fn); | ||
return fn; | ||
}; | ||
Nue.prototype.forEach = function (worker) { | ||
return this.runSerial(worker, strategies.forEach); | ||
}; | ||
Nue.prototype.map = function (worker) { | ||
return this.runSerial(worker, strategies.map); | ||
}; | ||
Nue.prototype.filter = function (worker) { | ||
return this.runSerial(worker, strategies.filter); | ||
}; | ||
Nue.prototype.every = function (worker) { | ||
return this.runSerial(worker, strategies.every); | ||
}; | ||
Nue.prototype.some = function (worker) { | ||
return this.runSerial(worker, strategies.some); | ||
}; | ||
Nue.prototype.parallel = function () { | ||
var batchSize = this.batchSize; | ||
var tasks = normalizeArguments(arguments); | ||
var workers = tasks.map(function (task, i) { | ||
return { task: task, index: i }; | ||
}); | ||
var fn = function () { | ||
if (!(this instanceof ContextBase)) { | ||
throw new Error('The context is illegal. This function is out of the flow.'); | ||
} | ||
runParallel(batchSize, workers, normalizeArguments(arguments), this, strategies.map); | ||
}; | ||
Nue.attachEmitter(fn); | ||
return fn; | ||
}; | ||
Nue.prototype.runParallel = function (worker, strategy) { | ||
if (typeof worker !== 'function') { | ||
throw new Error('The worker must be a function.'); | ||
} | ||
var batchSize = this.batchSize; | ||
var fn = function () { | ||
if (!(this instanceof ContextBase)) { | ||
throw new Error('The context is illegal. This function is out of the flow.'); | ||
} | ||
var values = normalizeArguments(arguments); | ||
var workers = values.map(function (value, i) { | ||
return { | ||
task: function () { | ||
worker.apply(this, arguments); | ||
}, | ||
index: i | ||
}; | ||
}); | ||
runParallel(batchSize, workers, values, this, strategy); | ||
}; | ||
Nue.attachEmitter(fn); | ||
return fn; | ||
}; | ||
Nue.prototype.parallelForEach = function (worker) { | ||
return this.runParallel(worker, strategies.forEach); | ||
}; | ||
Nue.prototype.parallelMap = function (worker) { | ||
return this.runParallel(worker, strategies.map); | ||
}; | ||
Nue.prototype.parallelFilter = function (worker) { | ||
return this.runParallel(worker, strategies.filter); | ||
}; | ||
Nue.prototype.parallelEvery = function (worker) { | ||
return this.runParallel(worker, strategies.every); | ||
}; | ||
Nue.prototype.parallelSome = function (worker) { | ||
return this.runParallel(worker, strategies.some); | ||
}; | ||
Nue.attachEmitter = function (fn) { | ||
fn.events = new EventEmitter(); | ||
fn.__nue__ = true; | ||
}; | ||
function ContextBase() { | ||
this.next = this.next.bind(this); | ||
this.end = this.end.bind(this); | ||
this.async = this.async.bind(this); | ||
@@ -563,4 +608,2 @@ this.queue = this.queue.bind(this); | ||
this.isLast = this.index === this.queue.isAddingCompleted && this.index === this.queue.length - 1; | ||
this.next = this.next.bind(this); | ||
this.end = this.end.bind(this); | ||
} | ||
@@ -647,2 +690,4 @@ | ||
ParallelQueue.SENTINEL = {}; | ||
ParallelQueue.prototype.push = (function () { | ||
@@ -657,4 +702,2 @@ | ||
this.data = this.queue.data; | ||
this.next = this.next.bind(this); | ||
this.end = this.end.bind(this); | ||
} | ||
@@ -706,3 +749,3 @@ | ||
value = queue.values.shift(); | ||
if (value.value === SENTINEL) { | ||
if (value.value === ParallelQueue.SENTINEL) { | ||
process.nextTick(function () { | ||
@@ -738,85 +781,4 @@ self.endParallelQueue(); | ||
ParallelQueue.prototype.complete = function() { | ||
this.push(SENTINEL); | ||
this.push(ParallelQueue.SENTINEL); | ||
this.isAddingCompleted = true; | ||
}; | ||
exports.DEFAULT_BATCH_SIZE = 3; | ||
exports.name = 'nue'; | ||
exports.version = '0.0.5'; | ||
exports.batchSize = exports.DEFAULT_BATCH_SIZE; | ||
exports.flow = function () { | ||
return wrapOrApply(Nue.prototype.flow, arguments); | ||
}; | ||
exports.forEach = function () { | ||
return wrapOrApply(Nue.prototype.forEach, arguments); | ||
}; | ||
exports.map = function () { | ||
return wrapOrApply(Nue.prototype.map, arguments); | ||
}; | ||
exports.filter = function () { | ||
return wrapOrApply(Nue.prototype.filter, arguments); | ||
}; | ||
exports.every = function () { | ||
return wrapOrApply(Nue.prototype.every, arguments); | ||
}; | ||
exports.some = function () { | ||
return wrapOrApply(Nue.prototype.some, arguments); | ||
}; | ||
exports.queue = function () { | ||
return wrapOrApply(Nue.prototype.queue, arguments); | ||
}; | ||
exports.parallel = function () { | ||
return wrapOrApply(Nue.prototype.parallel, arguments); | ||
}; | ||
exports.parallelForEach = function () { | ||
return wrapOrApply(Nue.prototype.parallelForEach, arguments); | ||
}; | ||
exports.parallelMap = function () { | ||
return wrapOrApply(Nue.prototype.parallelMap, arguments); | ||
}; | ||
exports.parallelFilter = function () { | ||
return wrapOrApply(Nue.prototype.parallelFilter, arguments); | ||
}; | ||
exports.parallelEvery = function () { | ||
return wrapOrApply(Nue.prototype.parallelEvery, arguments); | ||
}; | ||
exports.parallelSome = function () { | ||
return wrapOrApply(Nue.prototype.parallelSome, arguments); | ||
}; | ||
exports.parallelQueue = function () { | ||
return wrapOrApply(Nue.prototype.parallelQueue, arguments); | ||
}; | ||
function wrapOrApply (fn, args) { | ||
if (typeof args[0] === 'number') { | ||
return function () { | ||
return fn.apply(new Nue(args[0]), arguments) | ||
}; | ||
} | ||
return fn.apply(new Nue(null), args) | ||
} | ||
function normalizeArguments(args) { | ||
if (args.length === 1 && Array.isArray(args[0])) { | ||
return args[0] | ||
} else { | ||
return Array.prototype.slice.call(args); | ||
} | ||
} |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version" : "0.0.5" | ||
"version" : "0.0.6" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30189
762