Option name | Type | Description |
---|---|---|
config | Object | configuration object |
it make possible to choose which stage to run according to result of condition
evaluation
condition
desicion function or boolean condition
used to decide what way to go.
success
Stage
or stage function
to run in case of successful evaluation of condition
failed
Stage
or stage function
to run in case of failure evaluation of condition
other confguration as Stage because it is its child Class.
function IfElse(config) {
var self = this;
if (!(self instanceof IfElse)) {
throw new Error('constructor is not a function');
}
Stage.apply(self, arguments);
if (!config) {
config = {};
}
if (config.condition instanceof Function) {
self.condition = config.condition;
}
if (config.success instanceof Stage) {
self.success = config.success;
} else {
if (config.success instanceof Function) {
self.success = new Stage(config.success);
} else {
self.success = new Empty();
}
}
if (config.failed instanceof Stage) {
self.failed = config.failed;
} else {
if (config.failed instanceof Function) {
self.failed = new Stage(config.failed);
} else {
self.failed = new Empty();
}
}
self.name = config.name;
}
internal declaration fo success
IfElse.prototype.success = undefined;
internal declaration fo failure
IfElse.prototype.failure = undefined;
internal declaration fo condition
IfElse.prototype.condition = function(ctx) {
return true;
};
override of reportName
IfElse.prototype.reportName = function() {
var self = this;
return "IFELSE:" + self.name;
};
override of compile
IfElse.prototype.compile = function() {
var self = this;
if (!self.name) {
self.name = "success: " + self.success.reportName() + " failure: " + self.failed.reportName();
}
var run = function(ctx, done) {
if (self.condition(ctx)) {
self.success.execute(ctx, done);
} else {
self.failed.execute(ctx, done);
}
};
self.run = run;
};
Option name | Type | Description |
---|---|---|
context | Context | evaluating context |
[callback] | Context | returning callback |
override of execute
IfElse.prototype.execute = function(context, callback) {
var self = this;
if (!self.run) {
self.compile();
}
IfElse.super_.prototype.execute.apply(self, arguments);
};