-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathNodeDomain.js
More file actions
216 lines (192 loc) · 7.29 KB
/
NodeDomain.js
File metadata and controls
216 lines (192 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
define(function (require, exports, module) {
var NodeConnection = require("utils/NodeConnection"),
EventDispatcher = require("utils/EventDispatcher");
// Used to remove all listeners at once when the connection drops
var EVENT_NAMESPACE = ".NodeDomainEvent";
/**
* 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);
*
* @constructor
* @param {string} domainName Name of the registered Node Domain
* @param {string} domainPath 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);
/**
* The underlying Node connection object for this domain.
*
* @type {NodeConnection}
*/
NodeDomain.prototype.connection = null;
/**
* A promise that is resolved once the NodeConnection is connected and the
* domain has been loaded.
*
* @type {?jQuery.Promise}
* @private
*/
NodeDomain.prototype._connectionPromise = null;
/**
* The name of this domain.
*
* @type {string}
* @private
*/
NodeDomain.prototype._domainName = null;
/**
* The path at which the Node definition of this domain resides.
*
* @type {string}
* @private
*/
NodeDomain.prototype._domainPath = null;
/**
* Whether or not the domain has been successfully loaded.
*
* @type {boolean}
* @private
*/
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.
*
* @return {jQuery.Promise} Resolves once the domain is been loaded.
* @private
*/
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));
};
/**
* Synchronously determine whether the domain is ready; i.e., whether the
* connection is open and the domain is loaded.
*
* @return {boolean} Whether or not the domain is currently ready.
*/
NodeDomain.prototype.ready = function () {
return this._domainLoaded && this.connection.connected();
};
/**
* Get a promise that resolves when the connection is open and the domain
* is loaded.
*
* @return {jQuery.Promise}
*/
NodeDomain.prototype.promise = function () {
if (this._connectionPromise) {
return this._connectionPromise;
}
var deferred = new $.Deferred();
if (this.ready()) {
deferred.resolve();
} else {
deferred.reject();
}
return deferred.promise();
};
/**
* 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.
*
* @param {string} name The name of the domain command to execute
* @return {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;
});