Provides a simple abstraction for executing the commands of a single domain loaded via a NodeConnection. Automatically handles connection management and domain loading, and exposes each command in the domain as a promise-returning method that can safely be called regardless of the current status of the underlying connection. Example usage:
var myDomain = new NodeDomain("someDomain", "/path/to/SomeDomainDef.js"),
$result = myDomain.exec("someCommand", arg1, arg2);
$result.done(function (value) {
// the command succeeded!
});
$result.fail(function (err) {
// the command failed; act accordingly!
});
To handle domain events, just listen for the event on the domain:
myDomain.on("someEvent", someHandler);
function NodeDomain(domainName, domainPath) {
var connection = new NodeConnection();
this.connection = connection;
this._domainName = domainName;
this._domainPath = domainPath;
this._domainLoaded = false;
this._load = this._load.bind(this);
this._connectionPromise = connection.connect(true)
.then(this._load);
connection.on("close", function (event, promise) {
this.connection.off(EVENT_NAMESPACE);
this._domainLoaded = false;
this._connectionPromise = promise.then(this._load);
}.bind(this));
}
EventDispatcher.makeEventDispatcher(NodeDomain.prototype);
A promise that is resolved once the NodeConnection is connected and the domain has been loaded.
NodeDomain.prototype._connectionPromise = null;
Whether or not the domain has been successfully loaded.
NodeDomain.prototype._domainLoaded = false;
Loads the domain via the underlying connection object and exposes the domain's commands as methods on this object. Assumes the underlying connection has already been opened.
NodeDomain.prototype._load = function () {
var connection = this.connection;
return connection.loadDomains(this._domainPath, true)
.done(function () {
this._domainLoaded = true;
this._connectionPromise = null;
var eventNames = Object.keys(connection.domainEvents[this._domainName]);
eventNames.forEach(function (domainEvent) {
var connectionEvent = this._domainName + ":" + domainEvent + EVENT_NAMESPACE;
connection.on(connectionEvent, function () {
var params = Array.prototype.slice.call(arguments, 1);
EventDispatcher.triggerWithArray(this, domainEvent, params);
}.bind(this));
}, this);
}.bind(this))
.fail(function (err) {
console.error("[NodeDomain] Error loading domain \"" + this._domainName + "\": " + err);
}.bind(this));
};
Applies the named command from the domain to a list of parameters, which are passed as extra arguments to this method. If the connection is open and the domain is loaded, the function is applied immediately. Otherwise the function is applied as soon as the connection has been opened and the domain has finished loading.
NodeDomain.prototype.exec = function (name) {
var connection = this.connection,
params = Array.prototype.slice.call(arguments, 1),
execConnected = function () {
var domain = connection.domains[this._domainName],
fn = domain && domain[name],
execResult;
if (fn) {
execResult = fn.apply(domain, params);
} else {
execResult = new $.Deferred().reject().promise();
}
return execResult;
}.bind(this);
var result;
if (this.ready()) {
result = execConnected();
} else if (this._connectionPromise) {
result = this._connectionPromise.then(execConnected);
} else {
result = new $.Deferred.reject().promise();
}
return result;
};
module.exports = NodeDomain;
});
Get a promise that resolves when the connection is open and the domain is loaded.
NodeDomain.prototype.promise = function () {
if (this._connectionPromise) {
return this._connectionPromise;
} else {
var deferred = new $.Deferred();
if (this.ready()) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise();
}
};