-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnombo-client.js
330 lines (266 loc) · 7.51 KB
/
nombo-client.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
This script provides the core client-side functionality of Nombo.
*/
var $n = {
MAX_ID: Math.pow(2, 53) - 2,
socket: null,
_timeout: 10000,
_appURL: null,
_curID: 1,
_frameworkURL: null,
_jsLibsURL: null,
_frameworkStylesURL: null,
_scriptsRouterURL: null,
_appScriptsURL: null,
_appStylesURL: null,
_appAssetsURL: null,
_appFilesURL: null,
_cacheSeverCalls: false,
_cacheVersion: null,
_callbacks: {},
_releaseMode: false,
_plugins: {},
initIO: function () {
$n.socket = NOMBO_SOCKET;
$n.local = new $n.LocalInterface($n.socket);
},
init: function () {
var appDefinition = $loader.getAppDefinition();
$n._frameworkURL = appDefinition.frameworkURL;
$n._appURL = appDefinition.appURL;
$n._jsLibsURL = appDefinition.jsLibsURL;
$n._frameworkStylesURL = appDefinition.frameworkStylesURL;
$n._scriptsRouterURL = location.href.replace(/\?.*/, '');
$n._appScriptsURL = appDefinition.appScriptsURL;
$n._appStylesURL = appDefinition.appStylesURL;
$n._appAssetsURL = appDefinition.appAssetsURL;
$n._appFilesURL = appDefinition.appFilesURL;
$n._releaseMode = appDefinition.releaseMode;
$n._timeout = appDefinition.timeout;
// global variable from session.js
$n._cacheVersion = NOMBO_CACHE_VERSION;
$n.initIO();
},
getAppDefinition: $loader.getAppDefinition,
session: {
end: function (callback) {
NOMBO_SESSION_MANAGER.endSession(callback);
}
},
_genID: function () {
$n._curID++;
$n._curID = $n._curID % $n.MAX_ID;
return 'l' + $n._curID;
},
_globalEval: $loader.grab._globalEval,
EventEmitter: $loader.EventEmitter,
getBasicType: function (variable) {
var classType = {}.toString
var typeRegex = /[^0-9A-Za-z]*([A-Z][a-zA-Z0-9]*)/;
var typeString = classType.call(variable);
return typeString.match(typeRegex)[1];
},
/**
Bind a callback function to Nombo's ready event. The specified function will be called when Nombo is ready to begin processing.
*/
ready: $loader.grab.ready,
/**
Bind a callback function to Nombo's fail event. The specified function will be called when Nombo fails to load a resource.
The callback can accept a parameter which indicates the URL of the resource which failed to load.
*/
fail: $loader.grab.fail,
/**
This object holds error functions to handle various client-side error types that can occur within the system.
Each function handles a specific type of error and can accept any suitable number of parameters
in order to generate the appropriate error message.
*/
errors: {
serverInterfaceError: function (message) {
return "ServerInterfaceError: " + message;
},
loadError: function (resourceURL) {
return "LoadError: Failed to load resource: " + resourceURL;
}
},
/**
Get the URL of Nombo's root directory.
*/
getRootURL: function () {
return $n._frameworkURL;
},
/**
Navigate to another script.
*/
navigateToScript: function (scriptName) {
location.href = $n._scriptsRouterURL + (scriptName ? "?" + scriptName : "");
},
caching: {
/**
Enable/disable default caching for server interface AJAX calls performed by Nombo.
Server call caching is disabled by default.
*/
cacheServerCalls: function (bool) {
$n._cacheSeverCalls = bool;
}
},
/**
Grab allows you to include external scripts, CSS stylesheets and templates into your JavaScript.
Some grab methods allow you to load resources either synchronously or asynchronously.
*/
grab: $loader.grab,
serverInterfaceDescription: {},
_asRemoteClientURL: function (host, port, secure) {
return (secure ? 'https://' : 'http://') + host + ":" + port;
},
_remoteClientMap: {}
};
$n.LocalInterface = function (wsSocket) {
$n.EventEmitter.call(this);
var self = this;
this.sock = wsSocket;
self.connected = wsSocket.connected;
wsSocket.on('disconnect', function () {
self.connected = false;
self.emit('disconnect');
});
wsSocket.on('connect', function () {
self.connected = true;
self.emit('connect');
});
wsSocket.on('error', function (err) {
self.emit('error', err);
});
self.exec = function () {
var serverInterface = arguments[0];
var method = arguments[1];
var callback = null;
var request = {
sim: serverInterface,
method: method
};
if (arguments[3]) {
request.data = arguments[2];
callback = arguments[3];
} else if (arguments[2] !== undefined) {
if (arguments[2] instanceof Function) {
callback = arguments[2];
} else {
request.data = arguments[2];
}
}
wsSocket.emit('rpc', request, callback);
};
self.watch = function (event, handler) {
wsSocket.on(event, handler);
};
self.watchOnce = function (event, handler) {
wsSocket.once(event, handler);
};
self.unwatch = function (event, handler) {
if (event && handler) {
wsSocket.removeListener(event, handler);
} else {
wsSocket.removeAllListeners(event);
}
};
self.watchers = function (event) {
wsSocket.listeners(event);
};
};
$n.LocalInterface.prototype = Object.create($n.EventEmitter.prototype);
$n.RemoteInterface = function (url, wsSocket) {
$n.EventEmitter.call(this);
var self = this;
if (!wsSocket) {
wsSocket = NOMBO_SOCKET_ENGINE.connect({
url: url,
forceJSONP: true
});
}
self.connected = wsSocket.connected;
wsSocket.on('disconnect', function () {
self.connected = false;
self.emit('disconnect');
});
wsSocket.on('connect', function () {
self.connected = true;
self.emit('connect');
});
wsSocket.on('error', function (err) {
self.emit('error', err);
});
self.exec = function () {
var serverInterface = arguments[0];
var method = arguments[1];
var callback = null;
var request = {
sim: serverInterface,
method: method
};
if (arguments[3]) {
request.data = arguments[2];
callback = arguments[3];
} else if (arguments[2] !== undefined) {
if (arguments[2] instanceof Function) {
callback = arguments[2];
} else {
request.data = arguments[2];
}
}
wsSocket.emit('rpc', request, callback);
};
self.watch = function (event, handler) {
wsSocket.on(event, handler);
};
self.watchOnce = function (event, handler) {
wsSocket.once(event, handler);
};
self.unwatch = function (event, handler) {
if (event && handler) {
wsSocket.removeListener(event, handler);
} else {
wsSocket.removeAllListeners(event);
}
};
self.watchers = function (event) {
wsSocket.listeners(event);
};
};
$n.RemoteInterface.prototype = Object.create($n.EventEmitter.prototype);
$n.remote = function (host, port, secure) {
secure = secure || false;
var url = $n._asRemoteClientURL(host, port, secure);
if (!$n._remoteClientMap.hasOwnProperty(url)) {
$n._remoteClientMap[url] = new $n.RemoteInterface(url);
}
return $n._remoteClientMap[url];
};
$n.destroyRemote = function (host, port, secure) {
var url = $n._asRemoteClientURL(host, port, secure);
delete $n._remoteClientMap[url];
};
/*
registerPlugin(name, plugin)
Register a client-side plugin.
@param {String} name The name of the plugin.
@param {Object} plugin A plugin Object or Function to associate with the specified plugin name
*/
$n.registerPlugin = function (name, plugin) {
if ($n._plugins[name] == null) {
$n._plugins[name] = plugin;
} else {
throw new Error('A plugin with the name "' + name + '" already exists.');
}
};
/*
plugin(name)
Access a plugin with the specified name.
@param {String} name The name of the plugin.
*/
$n.plugin = function (name) {
if ($n._plugins[name] == null) {
throw new Error('The requested "' + name + '" plugin could not be found.');
}
return $n._plugins[name];
};
$n.init();