-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
384 lines (340 loc) · 10.7 KB
/
index.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
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
/* global window AFRAME THREE */
import Session from './session'
import storeEngine from 'store/src/store-engine'
import localStorage from 'store/storages/localStorage'
export default class Application {
/**
* Main application constructor
* Yo may want to set properties and their defaults here before application is sealed
*/
constructor(conf) {
this.session = new Session(conf)
// generic logger
this.log = this.session.getLogger('app')
// set true after initial setup
this.initialized = false
// state is application playing
this.isPlaying = false
// state is application shutting down so most of calls should be blocked
this.isDisposing = false
// A-Frame is available in window
this.hasAframe = false
// will hold registered addons
this.addons = {}
// Reference to <a-scene> if AFRAME is present
this.aframe = null
// Used when A-Frame is not available
this.rAFreq = null
// Register background worker
this.bgWorker = null
// localStorage
this.store = null
// initialize
this.init()
// preventing new properties from being added to application and marking all
// existing properties as non-configurable.
// Do throw TypeError: can't define property "x": Object is not extensible
// That will prevent you add by accident properties which may not be accessible
// after this.stop
Object.seal(this)
}
/**
* initialize application instance
*/
init() {
// check that application is not in shutdown progress
if (this.disposing('init'))
return
// setup localstorage
this.store = storeEngine.createStore(localStorage)
/* eslint consistent-this: ["error", "app"] */
const app = this
// Application initialization
if (typeof AFRAME === 'undefined') {
this.hasAframe = false
this.polyfill()
this.tick = this.tick.bind(this)
} else {
// use A-Frame render loop
this.hasAframe = true
const runtimeSysName = `${this.session.get('config').name}-runtime}`
this.log.debug(`register ${runtimeSysName} system for A-Frame`)
AFRAME.registerSystem(runtimeSysName, {
init() {
app.aframe = this.el
},
tick() {
app.tick()
},
})
}
this.registerServiceWorker()
// Application initialized
this.log.debug('application is initialized')
this.log.info(`Version: ${this.session.get('config').version}`)
this.log.info(`A-Frame: ${this.hasAframe ? AFRAME.version : 'not included'}`)
this.log.info(`three.js: ${typeof THREE === 'undefined' ? 'not included' : THREE.REVISION}`)
this.initialized = true
}
/**
* Play starts the Application
*
* @return {[type]} [description]
*/
play() {
// check that application is not in shutdown progress
if (this.disposing('play'))
return
// verfify that application is initialized
if (!this.initialized || this.isPlaying) {
this.log.error('can not play, application not initialized correctly or already playing.')
return
}
// play A-Frame
if (this.hasAframe && this.session.get('config').ppaframe) {
this.aframe.play()
}
this.log.debug(this.session.get('config').ppaframe)
for (const [name, addon] of Object.entries(this.addons)) {
// initialize addons if thats first call to start
if (addon.app && addon.enabled) {
// call addon start
this.addons[name].isPlaying = true
this.addons[name].play()
}
}
this.log.ok('application is playing')
this.isPlaying = true
// call first tick
this.tick()
}
/**
* Pause application
*/
pause() {
// check that application is not in shutdown progress
if (this.disposing('pause'))
return
// pause A-Frame
if (this.hasAframe && this.session.get('config').ppaframe) {
this.aframe.pause()
}
for (const [name, addon] of Object.entries(this.addons)) {
// initialize addons if thats first call to start
if (addon.app) {
// call addon pause
this.addons[name].isPlaying = false
this.addons[name].pause()
}
}
this.isPlaying = false
this.log.ok('application is paused')
}
/**
* Power on appplication engine
*
* Start the app engine based on configuration
* @return {Promise} Promise which is resolved when everthing is started up
*/
start() {
// check that application is not in shutdown progress
if (this.disposing('start'))
return new Promise((resolve, reject) => reject(Error("blocked")))
return new Promise((resolve, reject) => {
this.registerWorkers()
for (const [name, addon] of Object.entries(this.addons)) {
// initialize addons if thats first call to start
if (!addon.app && addon.enabled) {
this.addons[name].app = this
this.addons[name].setup()
// preventing new properties from being added to addon and marking all
// existing properties as non-configurable.
// Do throw TypeError: can't define property "x": Object is not extensible
// That will prevent you add by accident properties which may not be accessible
// after this.stop
Object.seal(this.addons[name])
}
// call addon start
this.addons[name].start()
}
if (this.initialized) {
this.play()
}
if (this.isPlaying) {
this.log.debug('started')
resolve(this.session.getLogger('app'))
} else {
const err = 'startup failed'
this.log.error(err)
reject(Error(err))
}
})
}
/**
* Power off system
*
* @return {Promise} Promise which is resolved when everthing is cleaned up
*/
stop() {
// check that application is not in shutdown progress already
if (this.disposing('stop'))
return new Promise((resolve, reject) => reject(Error("blocked")))
this.isDisposing = true
this.isPlaying = false
this.log.debug('preparing shutdown')
return new Promise((resolve, reject) => {
let down = false
if (this.bgWorker) {
this.bgWorker.terminate()
this.bgWorker = null
}
if (this.rAFreq) {
this.log.info('stopping engine', this.rAFreq)
window.cancelAnimationFrame(this.rAFreq)
this.rAFreq = null
}
// teardown addons
for (const [name, addon] of Object.entries(this.addons)) {
// initialize addons if thats first call to start
if (addon.app) {
// call addon pause
this.addons[name].isPlaying = false
this.addons[name].dispose()
}
}
down = true
if (down) {
this.log.ok('shutdown complete')
this.isDisposing = false
resolve(this.session.getLogger('app'))
} else {
const err = 'failed to shutdown'
this.log.error(err)
this.isDisposing = false
reject(Error(err))
}
})
}
/**
* Appplication engine loop
*/
tick() {
if (!this.isPlaying || this.isDisposing) {
return
}
for (const [name, addon] of Object.entries(this.addons)) {
if (addon.enabled && addon.isPlaying) {
this.addons[name].tick()
}
}
if (!this.hasAframe) {
this.rAFreq = window.requestAnimationFrame(this.tick)
}
}
/**
* Helper to prevent you calling any application methods when Application
* is shutting down
*
* @param {[type]} fnName reference to where check was called
* @return {[type]} [description]
*/
disposing(fnName) {
if (this.isDisposing) {
this.log.error(`can not call ${fnName} while webapp is shutting down`)
return true
}
return false
}
/**
* registerAddon description
* @param {[type]} addon
* @param {[type]} data Addon configuration
* @return {[type]} [description]
*/
registerAddon(addon, data) {
if (!('name' in addon)) {
this.log.error('addon mus have a name')
return
}
if (this.addons[addon.name]) {
this.log.error(`The addon ${addon.name} has been already registered.`)
return
}
addon.app = null
addon.aframeRequired = false
// perhaps should do a deep merge of data and addon defaults instead of replace
addon.data = data ? data : addon.data
addon.enabled = true
addon.isPlaying = false
addon.log = this.session.getLogger(addon.name)
this.addons[addon.name] = addon
}
/**
* polyfill
*/
polyfill() {
// requestAnimationFrame and cancelAnimationFrame polyfill
if (window.requestAnimationFrame && window.cancelAnimationFrame) {
return
}
this.log.warn('using requestAnimationFrame and cancelAnimationFrame polyfill')
let lastTime = 0
const v = ['ms', 'moz', 'webkit', 'o']
for (let x = 0; x < v.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[`${v[x] }RequestAnimationFrame`]
window.cancelAnimationFrame = window[`${v[x] }CancelAnimationFrame`] ||
window[`${v[x] }CancelRequestAnimationFrame`]
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(cb) {
const currTime = new Date().getTime()
/* eslint no-magic-numbers: off*/
const timeToCall = Math.max(0, 16 - (currTime - lastTime))
const cbTime = currTime + timeToCall
const id = window.setTimeout(() => {
cb(cbTime)
}, timeToCall)
lastTime = currTime + timeToCall
return id
}
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id)
}
}
}
/**
* registerServiceWorker if needed
*/
registerServiceWorker() {
// // ServiceWorker is a progressive technology. Ignore unsupported browsers
// if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator && PROJECT.serviceWorker) {
// info('CLIENT: service worker registration in progress.')
// navigator.serviceWorker.register('/app/js/service-worker.js').then(() => {
// info('CLIENT: service worker registration complete.')
// }, () => {
// info('CLIENT: service worker registration failure.')
// })
// } else {
// info('CLIENT: service worker is not registered.')
// }
}
/**
* register Workers if needed
*/
registerWorkers() {
// if (window.Worker) {
// this.bgWorker = new Worker('/app/js/background.worker.js')
// this.bgWorker.onmessage = event => {
// this.log.debug(event)
// }
// this.bgWorker.onmessageerror = error => {
// this.log.error(error)
// }
// this.bgWorker.onerror = error => {
// this.log.error(error)
// }
// }
}
}