Option name | Type | Description |
---|---|---|
Object | onfi | configuration object |
Process staging in sequential way
stage
evaluating stage
split
function that split existing stage into smalls parts, it needed
combine
if any result combining is need, this can be used to combine splited parts and update context
Note
Split does not require combine --- b/c it will return parent context;
in cases that have no declaration forsplit
configured or default will be used
function Sequential(config) {
var self = this;
if (!(self instanceof Sequential)) {
throw new Error('constructor is not a function');
}
if (config && config.run instanceof Function) {
config.stage = new Stage(config.run);
delete config.run;
}
Stage.apply(self, arguments);
if (!config) {
config = {};
}
if (config instanceof Stage) {
config = {
stage: config
};
}
if (config.stage instanceof Stage) {
self.stage = config.stage;
} else {
if (config.stage instanceof Function) {
self.stage = new Stage(config.stage);
} else {
self.stage = new Empty();
}
}
if (config.split instanceof Function) {
self.split = config.split;
}
if (config.combine instanceof Function) {
self.combine = config.combine;
}
self.name = config.name;
}
internal declaration fo stage
Sequential.prototype.stage = undefined;
internal declaration fo split
Sequential.prototype.split = function(ctx) {
return [ctx];
};
Option name | Type | Description |
---|---|---|
Context | t | main context |
Context[] | hildre | list of all children contexts |
internal declaration fo combine
Sequential.prototype.combine = function(ctx, children) {
};
override of reportName
Sequential.prototype.reportName = function() {
var self = this;
return "SEQ:" + self.name;
};
override of compile
split all and run all
Sequential.prototype.compile = function() {
var self = this;
if (!self.name) {
self.name = self.stage.reportName();
}
var run = function(err, ctx, done) {
var iter = -1;
var children = self.split(ctx);
var len = children ? children.length : 0;
var errors = [];
function finish() {
if (errors.length > 0) {
done(new ErrorList(errors));
} else {
self.combine(ctx, children);
done();
}
}
function logError(err, index) {
errors.push({
stage: self.name,
index: index,
err: err,
stack: err.stack,
ctx: children[index]
});
}
var next = function(err, retCtx) {
iter++;
if (err) {
logError(err, iter-1);
} else if (iter > 0) {
children[iter-1] = retCtx;
}
if (iter >= len) {
finish();
} else {
self.stage.execute(ctx.ensureIsChild(children[iter]), next);
}
};
if (len === 0) {
finish();
} else {
next();
}
};
self.run = run;
};
override of execute
Sequential.prototype.execute = function(context, callback) {
var self = this;
if (!self.run) {
self.compile();
}
Sequential.super_.prototype.execute.apply(self, arguments);
};