Option name | Type | Description |
---|---|---|
config | Object | configuration object |
Timeout: run stage and wait timeout ms for and run overdue stage
configuration
timeout in ms
function Timeout(config) {
var self = this;
if (!(self instanceof Timeout)) {
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 = {};
}
self.timeout = config.timeout || 1000;
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.overdue instanceof Stage) {
self.overdue = config.overdue;
} else if (config.overdue instanceof Function) {
self.overdue = config.overdue;
}
self.overdue = new Stage(self.overdue);
self.name = config.name;
}
internal declaration fo timeout
Timeout.prototype.timeout = undefined;
internal declaration fo stage
Timeout.prototype.stage = undefined;
default implementation of overdue;
Timeout.prototype.overdue = function(ctx, done) {
done(new Error('overdue'));
};
override of reportName
Timeout.prototype.reportName = function() {
return "Timeout:" + this.name;
};
override of compile
Timeout.prototype.compile = function() {
var self = this;
if (!self.name) {
self.name = "success: " + self.stage.reportName() + " failure: " + self.overdue.reportName();
}
var run = function(ctx, done) {
var to;
var localDone = function(err) {
if (to) {
clearTimeout(to);
to = null;
done(err);
}
};
var waitFor;
if (self.timeout instanceof Function) {
waitFor = self.timeout(ctx);
} else {
waitFor = self.timeout;
}
to = setTimeout(function() {
if (to) {
self.overdue.execute(ctx, localDone);
}
else {
here can be some sort of caching operation
}
}, waitFor);
self.stage.execute(ctx, localDone);
};
self.run = run;
};
override of execute
Timeout.prototype.execute = function(context, callback) {
var self = this;
if (!self.run) {
self.compile();
}
Timeout.super_.prototype.execute.apply(self, arguments);
};