-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathFamousEngine.js
More file actions
471 lines (419 loc) · 13.6 KB
/
FamousEngine.js
File metadata and controls
471 lines (419 loc) · 13.6 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Famous Industries Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var Clock = require('./Clock');
var Scene = require('./Scene');
var Channel = require('./Channel');
var Dispatch = require('./Dispatch');
var UIManager = require('../renderers/UIManager');
var Compositor = require('../renderers/Compositor');
var RequestAnimationFrameLoop = require('../render-loops/RequestAnimationFrameLoop');
var TransformSystem = require('./TransformSystem');
var OpacitySystem = require('./OpacitySystem');
var SizeSystem = require('./SizeSystem');
var Commands = require('./Commands');
var ENGINE_START = [Commands.ENGINE, Commands.START];
var ENGINE_STOP = [Commands.ENGINE, Commands.STOP];
var TIME_UPDATE = [Commands.TIME, null];
/**
* Famous has two responsibilities, one to act as the highest level
* updater and another to send messages over to the renderers. It is
* a singleton.
*
* @class FamousEngine
* @constructor
*/
function FamousEngine() {
var _this = this;
Dispatch._setUpdater(this);
this._updateQueue = []; // The updateQueue is a place where nodes
// can place themselves in order to be
// updated on the frame.
this._nextUpdateQueue = []; // the nextUpdateQueue is used to queue
// updates for the next tick.
// this prevents infinite loops where during
// an update a node continuously puts itself
// back in the update queue.
this._scenes = {}; // a hash of all of the scenes's that the FamousEngine
// is responsible for.
this._messages = TIME_UPDATE; // a queue of all of the draw commands to
// send to the the renderers this frame.
this._inUpdate = false; // when the famous is updating this is true.
// all requests for updates will get put in the
// nextUpdateQueue
this._clock = new Clock(); // a clock to keep track of time for the scene
// graph.
this._channel = new Channel();
this._channel.onMessage = function (message) {
_this.handleMessage(message);
};
}
/**
* An init script that initializes the FamousEngine with options
* or default parameters.
*
* @method
*
* @param {Object} options a set of options containing a compositor and a render loop
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.init = function init(options) {
if (typeof window === 'undefined') {
throw new Error(
'FamousEngine#init needs to have access to the global window object. ' +
'Instantiate Compositor and UIManager manually in the UI thread.'
);
}
this.compositor = options && options.compositor || new Compositor();
this.renderLoop = options && options.renderLoop || new RequestAnimationFrameLoop();
this.uiManager = new UIManager(this.getChannel(), this.compositor, this.renderLoop);
return this;
};
/**
* Sets the channel that the engine will use to communicate to
* the renderers.
*
* @method
*
* @param {Channel} channel The channel to be used for communicating with
* the `UIManager`/ `Compositor`.
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.setChannel = function setChannel(channel) {
this._channel = channel;
return this;
};
/**
* Returns the channel that the engine is currently using
* to communicate with the renderers.
*
* @method
*
* @return {Channel} channel The channel to be used for communicating with
* the `UIManager`/ `Compositor`.
*/
FamousEngine.prototype.getChannel = function getChannel () {
return this._channel;
};
/**
* _update is the body of the update loop. The frame consists of
* pulling in appending the nextUpdateQueue to the currentUpdate queue
* then moving through the updateQueue and calling onUpdate with the current
* time on all nodes. While _update is called _inUpdate is set to true and
* all requests to be placed in the update queue will be forwarded to the
* nextUpdateQueue.
*
* @method
*
* @return {undefined} undefined
*/
FamousEngine.prototype._update = function _update () {
this._inUpdate = true;
var time = this._clock.now();
var nextQueue = this._nextUpdateQueue;
var queue = this._updateQueue;
var item;
this._messages[1] = time;
SizeSystem.update();
TransformSystem.update();
OpacitySystem.update();
while (nextQueue.length) queue.unshift(nextQueue.pop());
while (queue.length) {
item = queue.shift();
if (item && item.update) item.update(time);
if (item && item.onUpdate) item.onUpdate(time);
}
this._inUpdate = false;
};
/**
* requestUpdates takes a class that has an onUpdate method and puts it
* into the updateQueue to be updated at the next frame.
* If FamousEngine is currently in an update, requestUpdate
* passes its argument to requestUpdateOnNextTick.
*
* @method
*
* @param {Object} requester an object with an onUpdate method
*
* @return {undefined} undefined
*/
FamousEngine.prototype.requestUpdate = function requestUpdate (requester) {
if (!requester)
throw new Error(
'requestUpdate must be called with a class to be updated'
);
if (this._inUpdate) this.requestUpdateOnNextTick(requester);
else this._updateQueue.push(requester);
};
/**
* requestUpdateOnNextTick is requests an update on the next frame.
* If FamousEngine is not currently in an update than it is functionally equivalent
* to requestUpdate. This method should be used to prevent infinite loops where
* a class is updated on the frame but needs to be updated again next frame.
*
* @method
*
* @param {Object} requester an object with an onUpdate method
*
* @return {undefined} undefined
*/
FamousEngine.prototype.requestUpdateOnNextTick = function requestUpdateOnNextTick (requester) {
this._nextUpdateQueue.push(requester);
};
/**
* postMessage sends a message queue into FamousEngine to be processed.
* These messages will be interpreted and sent into the scene graph
* as events if necessary.
*
* @method
*
* @param {Array} messages an array of commands.
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.handleMessage = function handleMessage (messages) {
if (!messages)
throw new Error(
'onMessage must be called with an array of messages'
);
var command;
while (messages.length > 0) {
command = messages.shift();
switch (command) {
case Commands.WITH:
this.handleWith(messages);
break;
case Commands.FRAME:
this.handleFrame(messages);
break;
default:
throw new Error('received unknown command: ' + command);
}
}
return this;
};
/**
* handleWith is a method that takes an array of messages following the
* WITH command. It'll then issue the next commands to the path specified
* by the WITH command.
*
* @method
*
* @param {Array} messages array of messages.
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.handleWith = function handleWith (messages) {
var path = messages.shift();
var command = messages.shift();
switch (command) {
case Commands.TRIGGER: // the TRIGGER command sends a UIEvent to the specified path
var type = messages.shift();
var ev = messages.shift();
Dispatch.dispatchUIEvent(path, type, ev);
break;
default:
throw new Error('received unknown command: ' + command);
}
return this;
};
/**
* handleFrame is called when the renderers issue a FRAME command to
* FamousEngine. FamousEngine will then step updating the scene graph to the current time.
*
* @method
*
* @param {Array} messages array of messages.
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.handleFrame = function handleFrame (messages) {
if (!messages) throw new Error('handleFrame must be called with an array of messages');
if (!messages.length) throw new Error('FRAME must be sent with a time');
this.step(messages.shift());
return this;
};
/**
* step updates the clock and the scene graph and then sends the draw commands
* that accumulated in the update to the renderers.
*
* @method
*
* @param {Number} time current engine time
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.step = function step (time) {
if (time == null) throw new Error('step must be called with a time');
this._clock.step(time);
this._update();
if (this._messages.length) {
this._channel.sendMessage(this._messages);
while (this._messages.length > 2) this._messages.pop();
}
return this;
};
/**
* returns the context of a particular path. The context is looked up by the selector
* portion of the path and is listed from the start of the string to the first
* '/'.
*
* @method
*
* @param {String} selector the path to look up the context for.
*
* @return {Context | Undefined} the context if found, else undefined.
*/
FamousEngine.prototype.getContext = function getContext (selector) {
if (!selector) throw new Error('getContext must be called with a selector');
var index = selector.indexOf('/');
selector = index === -1 ? selector : selector.substring(0, index);
return this._scenes[selector];
};
/**
* Returns the instance of clock used by the FamousEngine.
*
* @method
*
* @return {Clock} FamousEngine's clock
*/
FamousEngine.prototype.getClock = function getClock () {
return this._clock;
};
/**
* Enqueues a message to be transfered to the renderers.
*
* @method
*
* @param {Any} command Draw Command
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.message = function message (command) {
this._messages.push(command);
return this;
};
/**
* Creates a scene under which a scene graph could be built.
*
* @method
*
* @param {String} selector a dom selector for where the scene should be placed
*
* @return {Scene} a new instance of Scene.
*/
FamousEngine.prototype.createScene = function createScene (selector) {
selector = selector || 'body';
if (this._scenes[selector]) this._scenes[selector].dismount();
this._scenes[selector] = new Scene(selector, this);
return this._scenes[selector];
};
/**
* Introduce an already instantiated scene to the engine.
*
* @method
*
* @param {Scene} scene the scene to reintroduce to the engine
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.addScene = function addScene (scene) {
var selector = scene._selector;
var current = this._scenes[selector];
if (current && current !== scene) current.dismount();
if (!scene.isMounted()) scene.mount(scene.getSelector());
this._scenes[selector] = scene;
return this;
};
/**
* Remove a scene.
*
* @method
*
* @param {Scene} scene the scene to remove from the engine
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.removeScene = function removeScene (scene) {
var selector = scene._selector;
var current = this._scenes[selector];
if (current && current === scene) {
if (scene.isMounted()) scene.dismount();
delete this._scenes[selector];
}
return this;
};
/**
* Starts the engine running in the Main-Thread.
* This effects **every** updateable managed by the Engine.
*
* @method
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.startRenderLoop = function startRenderLoop() {
this._channel.sendMessage(ENGINE_START);
return this;
};
/**
* Stops the engine running in the Main-Thread.
* This effects **every** updateable managed by the Engine.
*
* @method
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.stopRenderLoop = function stopRenderLoop() {
this._channel.sendMessage(ENGINE_STOP);
return this;
};
/**
* @method
* @deprecated Use {@link FamousEngine#startRenderLoop} instead!
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.startEngine = function startEngine() {
console.warn(
'FamousEngine.startEngine is deprecated! Use ' +
'FamousEngine.startRenderLoop instead!'
);
return this.startRenderLoop();
};
/**
* @method
* @deprecated Use {@link FamousEngine#stopRenderLoop} instead!
*
* @return {FamousEngine} this
*/
FamousEngine.prototype.stopEngine = function stopEngine() {
console.warn(
'FamousEngine.stopEngine is deprecated! Use ' +
'FamousEngine.stopRenderLoop instead!'
);
return this.stopRenderLoop();
};
module.exports = new FamousEngine();