Modules (181)

NodeDomain

Description

Dependencies

Classes

Constructor

NodeDomain

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);
domainName string
Name of the registered Node Domain
domainPath string
Full path of the JavaScript Node domain specification
    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);

Properties

Private

_connectionPromise

A promise that is resolved once the NodeConnection is connected and the domain has been loaded.

Type
?jQuery.Promise
    NodeDomain.prototype._connectionPromise = null;
Private

_domainLoaded

Whether or not the domain has been successfully loaded.

Type
boolean
    NodeDomain.prototype._domainLoaded = false;
Private

_domainName

The name of this domain.

Type
string
    NodeDomain.prototype._domainName = null;
Private

_domainPath

The path at which the Node definition of this domain resides.

Type
string
    NodeDomain.prototype._domainPath = null;

connection

The underlying Node connection object for this domain.

Type
NodeConnection
    NodeDomain.prototype.connection = null;

Methods

Private

_load

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.

Returns: jQuery.Promise
Resolves once the domain is been loaded.
    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));
    };

exec

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.

name string
The name of the domain command to execute
Returns: jQuery.Promise
Resolves with the result of the command
    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;
});

promise

Get a promise that resolves when the connection is open and the domain is loaded.

Returns: jQuery.Promise
    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();
        }
    };

ready

Synchronously determine whether the domain is ready; i.e., whether the connection is open and the domain is loaded.

Returns: boolean
Whether or not the domain is currently ready.
    NodeDomain.prototype.ready = function () {
        return this._domainLoaded && this.connection.connected();
    };