-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcohtml.js
More file actions
452 lines (385 loc) · 15.3 KB
/
Copy pathcohtml.js
File metadata and controls
452 lines (385 loc) · 15.3 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
///<reference path="./cohtml.d.ts"/>
/*jslint browser: true, nomen: true, plusplus: true */
/// @file cohtml.js
/// @namespace engine
/// Coherent UI JavaScript interface.
/// The `engine` module contains all functions for communication between the UI and the game / application.
(function (factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory(global, global.engine, false);
} else {
engine = factory(window, /** @type {any} */ (window).engine, true);
}
})(function (global, engine, hasOnLoad) {
'use strict';
var VERSION = [2, 0, 0, 0];
/**
* Event emitter
*
* @class Emitter
*/
function Emitter() {
this.events = {};
}
/**
* Event handler
*
* @class Handler
*/
function Handler(code, context) {
this.code = code;
this.context = context;
}
Emitter.prototype._createClear = function (object, name, handler, context) {
return function () {
var handlers = object.events[name];
if (handlers) {
var index = -1;
// this was in native previously
if (handler === undefined) {
for (var i = 0; i < handlers.length; ++i) {
if (handlers[i].wasInCPP !== undefined) {
index = i;
break;
}
}
} else {
index = handlers.indexOf(handler);
}
if (index != -1) {
handlers.splice(index, 1);
if (handlers.length === 0) {
delete object.events[name];
}
}
} else {
if (engine.RemoveOnHandler !== undefined) {
engine.RemoveOnHandler(name, handler, context || engine);
}
}
};
};
/**
* Add a handler for an event
*
* @method on
* @param name the event name
* @param callback function to be called when the event is triggered
* @param context this binding for executing the handler, defaults to the Emitter
* @return connection object
*/
Emitter.prototype.on = function (name, callback, context) {
var handlers = this.events[name];
if (handlers === undefined) handlers = this.events[name] = [];
var handler = new Handler(callback, context || this);
handlers.push(handler);
return { clear: this._createClear(this, name, handler, context) };
};
/**
* Remove a handler from an event
*
* @method off
* @param name the event name
* @param handler function to be called when the event is triggered
* @param context this binding for executing the handler, defaults to the Emitter
* @return connection object
*/
Emitter.prototype.off = function (name, handler, context) {
var handlers = this.events[name];
if (handlers !== undefined) {
context = context || this;
var index;
var length = handlers.length;
for (index = 0; index < length; ++index) {
var reg = handlers[index];
if (reg.code == handler && reg.context == context) {
break;
}
}
if (index < length) {
handlers.splice(index, 1);
if (handlers.length === 0) {
delete this.events[name];
}
}
} else {
engine.RemoveOnHandler(name, handler, context || this);
}
};
Emitter.prototype.trigger = function (name) {
var handlers = this.events[name];
if (handlers !== undefined) {
var args = Array.prototype.slice.call(arguments, 1);
handlers.forEach(function (handler) {
handler.code.apply(handler.context, args);
});
return true;
}
return false;
};
var isAttached = engine !== undefined;
engine = engine || {};
/// @var {bool} engine.isAttached
/// Indicates whether the script is currently running inside Cohtml
engine.isAttached = isAttached;
function callAsync(code, context, argument) {
var async = function () {
code.call(context, argument);
};
window.setTimeout(async, 1);
}
if (!engine.isAttached) {
Emitter.prototype.on = function (name, callback, context) {
var handlers = this.events[name];
if (/** @type {any} */ (this).browserCallbackOn) {
/** @type {any} */ (this).browserCallbackOn(name, callback, context);
}
if (handlers === undefined) {
handlers = this.events[name] = [];
}
var handler = new Handler(callback, context || this);
handlers.push(handler);
return { clear: this._createClear(this, name, handler) };
};
Emitter.prototype.off = function (name, handler, context) {
var handlers = this.events[name];
if (handlers !== undefined) {
context = context || this;
var index;
var length = handlers.length;
for (index = 0; index < length; ++index) {
var reg = handlers[index];
if (reg.code == handler && reg.context == context) {
break;
}
}
if (index < length) {
handlers.splice(index, 1);
if (handlers.length === 0) {
delete this.events[name];
if (/** @type {any} */ (this).browserCallbackOff) {
/** @type {any} */ (this).browserCallbackOff(name, handler, context);
}
}
}
}
};
engine.SendMessage = function (name, id) {
var args = Array.prototype.slice.call(arguments, 2);
var deferred = engine._ActiveRequests[id];
delete engine._ActiveRequests[id];
var call = function () {
var mock = engine._mocks[name];
if (mock !== undefined) {
deferred.resolve(mock.apply(engine, args));
}
};
window.setTimeout(call, 16);
};
/// @function engine.TriggerEvent
/// Tries to invoke handlers for an event.
///
/// It will invoke any handler registered in C++ or the only handler registered in JavaScript.
/// engine._trigger will handle the case where more than one event handler is registered in JavaScript
/// or there are handlers from C++ and JavaScript at the same time.
/// @param {String} name name of the event to be fired
/// @param ... any extra parameters to be passed to event handlers
/// @return true if any event handlers have been registered in C++ or exactly one in JavaScript
/// @note this mock-mode version will return true if there is any event handler in JavaScript
engine.TriggerEvent = function () {
var args = Array.prototype.slice.call(arguments);
var trigger = function () {
var mock = engine._mocks[args[0]];
if (mock !== undefined) {
mock.apply(engine, args.slice(1));
}
};
window.setTimeout(trigger, 16);
return engine._mocks[args[0]] !== undefined;
};
engine.BindingsReady = function () {
engine._OnReady();
};
engine._mocks = {};
engine._mockImpl = function (name, mock, isCppCall, isEvent) {
if (mock) {
this._mocks[name] = mock;
}
// Extract the name of the arguments from Function.prototype.toString
var functionStripped = mock.toString().replace('function ' + mock.name + '(', '');
var rightParanthesis = functionStripped.indexOf(')');
var args = functionStripped.substr(0, rightParanthesis);
if (this.browserCallbackMock) {
this.browserCallbackMock(name, args, isCppCall, Boolean(isEvent));
}
};
engine.mock = function (name, mock, isEvent) {
this._mockImpl(name, mock, true, isEvent);
};
}
engine.events = {};
for (var property in Emitter.prototype) {
engine[property] = Emitter.prototype[property];
}
if (engine.isAttached) {
/// @function engine.on
/// Register handler for and event
/// @param {String} name name of the event
/// @param {Function} callback callback function to be executed when the event has been triggered
/// @param context *this* context for the function, by default the engine object
engine.on = function (name, callback, context) {
engine.AddOrRemoveOnHandler(name, callback, context || engine);
return { clear: this._createClear(this, name, callback, context) };
};
}
engine.whenReady = new Promise((resolve) => {
engine.on('Ready', resolve);
});
/// @function engine.off
/// Remove handler for an event
/// @param {String} name name of the event, by default removes all events
/// @param {Function} callback the callback function to be removed, by default removes all callbacks for a given event
/// @param context *this* context for the function, by default all removes all callbacks, regardless of context
/// @warning Removing all handlers for `engine` will remove some *Coherent UI* internal events, breaking some functionality.
/// @function engine.trigger
/// Trigger an event
/// This function will trigger any C++ handler registered for this event with `View::RegisterForEvent`
/// @param {String} name name of the event
/// @param ... any extra arguments to be passed to the event handlers
engine._trigger = Emitter.prototype.trigger;
engine.trigger = function () {
if (!this._trigger.apply(this, arguments)) {
this.TriggerEvent.apply(this, arguments);
}
};
/// @function engine.mock
/// Mocks a C++ function call with the specified function.
/// Only works in the browser. Attempts to use it in Coherent UI will do nothing.
/// @param {String} name name of the event
/// @param {Function} mock a function to be called in-place of your native binding
/// @param {Boolean} isEvent whether you are mocking an event or function call
if (engine.isAttached) {
engine.mock = function () {};
}
engine._BindingsReady = false;
engine._WindowLoaded = false;
engine._RequestId = 0;
engine._ActiveRequests = {};
/// @function engine.call
/// Call asynchronously a C++ handler and retrieve the result
/// The C++ handler must have been registered with `View::BindCall`
/// @param {String} name name of the C++ handler to be called
/// @param ... any extra parameters to be passed to the C++ handler
/// @return ECMAScript 6 promise
engine.call = function () {
engine._RequestId++;
var id = engine._RequestId;
var messageArguments = Array.prototype.slice.call(arguments);
messageArguments.splice(1, 0, id);
var promise = new Promise(function (resolve, reject) {
engine._ActiveRequests[id] = {
resolve: resolve,
reject: reject
};
engine.SendMessage.apply(engine, messageArguments);
});
return promise;
};
engine._Result = function (requestId) {
var deferred = engine._ActiveRequests[requestId];
if (deferred !== undefined) {
delete engine._ActiveRequests[requestId];
var resultArguments = Array.prototype.slice.call(arguments);
resultArguments.shift();
deferred.resolve.apply(deferred, resultArguments);
}
};
engine._Reject = function (requestId) {
var deferred = engine._ActiveRequests[requestId];
if (deferred !== undefined) {
delete engine._ActiveRequests[requestId];
// ChakraCore executes deferred.reject immediately before we can return and have the user attach a rejection callback
requestAnimationFrame(() => deferred.reject('No handler registered'));
}
};
engine._ForEachError = function (errors, callback) {
var length = errors.length;
for (var i = 0; i < length; ++i) {
callback(errors[i].first, errors[i].second);
}
};
engine._TriggerError = function (message) {
engine.trigger('Error', message);
};
engine._OnError = function (requestId, errors) {
if (requestId === null || requestId === 0) {
engine._ForEachError(errors, engine._TriggerError);
} else {
var deferred = engine._ActiveRequests[requestId];
delete engine._ActiveRequests[requestId];
deferred.reject(new Error(errors[0].second));
}
if (errors.length) {
throw new Error(errors[0].second);
}
};
engine._OnReady = function () {
engine._BindingsReady = true;
if (engine._WindowLoaded) {
engine.trigger('Ready');
}
};
engine._OnWindowLoaded = function () {
engine._WindowLoaded = true;
if (engine._BindingsReady) {
engine.trigger('Ready');
}
};
if (hasOnLoad) {
global.addEventListener('load', function () {
engine._OnWindowLoaded();
});
} else {
engine._WindowLoaded = true;
}
engine.on('_Result', engine._Result, engine);
engine.on('_Reject', engine._Reject, engine);
engine.on('_OnReady', engine._OnReady, engine);
engine.on('_OnError', engine._OnError, engine);
//@ts-ignore
engine.dependency = new WeakMap();
engine.hasAttachedUpdateListner = false;
engine.onUpdateWholeModel = (object) => {
let deps = engine.dependency.get(object) || [];
deps.forEach((dep) => engine.updateWholeModel(dep));
};
engine.createObservableModel = (observableName) => {
const handler = {
set: (target, prop, value) => {
engine.updateWholeModel(window[observableName]);
target[prop] = value;
}
};
// @ts-ignore
engine.createJSModel(observableName, new Proxy({}, handler));
};
engine.addSynchronizationDependency = (first, second) => {
if (!engine.hasAttachedUpdateListner) {
// will attach updateWholeModel callback for when there are one or more model dependencies
engine.addDataBindEventListner('updateWholeModel', engine.onUpdateWholeModel);
engine.hasAttachedUpdateListner = true;
}
let deps = engine.dependency.get(first);
if (!deps) {
deps = [];
engine.dependency.set(first, deps);
}
deps.push(second);
};
engine.removeSynchronizationDependency = (first, second) => {
let deps = engine.dependency.get(first) || [];
deps.splice(deps.indexOf(second), 1);
};
engine.BindingsReady(VERSION[0], VERSION[1], VERSION[2], VERSION[3]);
return engine;
});