pipeline.js

Timeout

function
Timeout()

Option name Type Description
config Object

configuration object

Timeout: run stage and wait timeout ms for and run overdue stage
configuration

  • timeout
     timeout in ms
    
    • stage
      main stage
    • overdue
      overdue stage optional. if no overdue is configured.
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;
}

timeout

property
Timeout.prototype.timeout

internal declaration fo timeout

Timeout.prototype.timeout = undefined;

stage

property
Timeout.prototype.stage

internal declaration fo stage

Timeout.prototype.stage = undefined;

overdue

method
Timeout.prototype.overdue()

default implementation of overdue;

Timeout.prototype.overdue = function(ctx, done) {
	done(new Error('overdue'));
};

reportName

method
Timeout.prototype.reportName()

override of reportName

Timeout.prototype.reportName = function() {
	return "Timeout:" + this.name;
};

compile

method
Timeout.prototype.compile()

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;
};

execute

method
Timeout.prototype.execute()

override of execute

Timeout.prototype.execute = function(context, callback) {
	var self = this;
	if (!self.run) {
		self.compile();
	}
	Timeout.super_.prototype.execute.apply(self, arguments);
};