service-model
Version:
An object oriented web service framework inspired by Windows Communication Foundation.
57 lines (56 loc) • 2.02 kB
JavaScript
/**
* @hidden
*/
var DefaultOperationInvoker = (function () {
function DefaultOperationInvoker(description) {
/**
* Timeout for operation. Defaults to 60,000ms (1 minute).
*/
this.timeout = 60000;
if (!description) {
throw new Error("Missing required argument 'operationDescription'.");
}
this._method = description.method;
var parameters = description.method.parameters || [];
// do not include callback in parameter count
this._parameterCount = parameters.length - 1;
}
DefaultOperationInvoker.prototype.invoke = function (instance, args, callback) {
var _this = this;
if (!instance) {
throw new Error("Missing required argument 'instance'.");
}
if (!args) {
throw new Error("Missing required argument 'args'.");
}
if (args.length != this._parameterCount) {
process.nextTick(function () { return callback(new Error("Wrong number of arguments for operation.")); });
return;
}
// asynchronous invoke
var timeout = false, finished = false;
if (this.timeout) {
var timeoutHandle = setTimeout(function () {
if (finished)
return;
timeout = true;
callback(new Error("Timeout of " + _this.timeout + "ms exceeded while invoking operation."));
}, this.timeout);
}
var done = function (err, result) {
if (timeout)
return;
if (timeoutHandle) {
clearTimeout(timeoutHandle);
}
if (finished) {
throw new Error("Callback already called.");
}
finished = true;
callback(err, result);
};
this._method.invoke(instance, args.concat(done));
};
return DefaultOperationInvoker;
})();
exports.DefaultOperationInvoker = DefaultOperationInvoker;