6
6
7
7
// proxy to/from worker
8
8
9
+ #if ENVIRONMENT_MAY_BE_NODE
10
+ var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process . versions == 'object' && typeof process . versions . node == 'string' ;
11
+ if ( ENVIRONMENT_IS_NODE ) {
12
+ let nodeWorkerThreads ;
13
+ try {
14
+ nodeWorkerThreads = require ( 'worker_threads' ) ;
15
+ } catch ( e ) {
16
+ console . error ( 'The "worker_threads" module is not supported in this node.js build - perhaps a newer version is needed?' ) ;
17
+ throw e ;
18
+ }
19
+ global . Worker = nodeWorkerThreads . Worker ;
20
+ var Module = Module || { }
21
+ } else
22
+ #endif
9
23
if ( typeof Module == 'undefined' ) {
10
24
console . warn ( 'no Module object defined - cannot proxy canvas rendering and input events, etc.' ) ;
11
25
Module = {
12
26
canvas : {
13
- addEventListener : function ( ) { } ,
14
- getBoundingClientRect : function ( ) { return { bottom : 0 , height : 0 , left : 0 , right : 0 , top : 0 , width : 0 } } ,
27
+ addEventListener : ( ) => { } ,
28
+ getBoundingClientRect : ( ) => { return { bottom : 0 , height : 0 , left : 0 , right : 0 , top : 0 , width : 0 } ; } ,
15
29
} ,
16
30
} ;
17
31
}
18
32
19
33
if ( ! Module . hasOwnProperty ( 'print' ) ) {
20
- Module [ 'print' ] = function ( x ) {
21
- console . log ( x ) ;
22
- } ;
34
+ Module [ 'print' ] = ( x ) => console . log ( x ) ;
23
35
}
24
36
25
37
if ( ! Module . hasOwnProperty ( 'printErr' ) ) {
26
- Module [ 'printErr' ] = function ( x ) {
27
- console . error ( x ) ;
28
- } ;
38
+ Module [ 'printErr' ] = ( x ) => console . error ( x ) ;
29
39
}
30
40
31
41
// utils
@@ -34,7 +44,7 @@ function FPSTracker(text) {
34
44
var last = 0 ;
35
45
var mean = 0 ;
36
46
var counter = 0 ;
37
- this . tick = function ( ) {
47
+ this . tick = ( ) => {
38
48
var now = Date . now ( ) ;
39
49
if ( last > 0 ) {
40
50
var diff = now - last ;
@@ -52,7 +62,7 @@ function FPSTracker(text) {
52
62
function GenericTracker(text) {
53
63
var mean = 0;
54
64
var counter = 0;
55
- this.tick = function (value) {
65
+ this.tick = (value) => {
56
66
mean = 0.99*mean + 0.01*value;
57
67
if (counter++ === 60) {
58
68
counter = 0;
@@ -79,16 +89,18 @@ function renderFrame() {
79
89
renderFrameData = null ;
80
90
}
81
91
82
- window . requestAnimationFrame = window . requestAnimationFrame || window . mozRequestAnimationFrame ||
83
- window . webkitRequestAnimationFrame || window . msRequestAnimationFrame ||
84
- renderFrame ;
92
+ if ( typeof window != 'undefined' ) {
93
+ window . requestAnimationFrame = window . requestAnimationFrame || window . mozRequestAnimationFrame ||
94
+ window . webkitRequestAnimationFrame || window . msRequestAnimationFrame ||
95
+ renderFrame ;
96
+ }
85
97
86
98
/*
87
99
(function() {
88
100
var trueRAF = window.requestAnimationFrame;
89
101
var tracker = new FPSTracker('client');
90
- window.requestAnimationFrame = function (func) {
91
- trueRAF(function() {
102
+ window.requestAnimationFrame = (func) => {
103
+ trueRAF(() => {
92
104
tracker.tick();
93
105
func();
94
106
});
@@ -100,7 +112,7 @@ window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequest
100
112
101
113
// IDBStore
102
114
103
- var IDBStore = { { { IDBStore . js } } } ;
115
+ #include " IDBStore.js"
104
116
105
117
// Frame throttling
106
118
@@ -114,7 +126,7 @@ var SUPPORT_BASE64_EMBEDDING;
114
126
115
127
var filename ;
116
128
if ( ! filename ) {
117
- filename = '{{{ filename }}} ' ;
129
+ filename = '<<< filename >>> ' ;
118
130
}
119
131
120
132
var workerURL = filename ;
@@ -126,9 +138,14 @@ if (SUPPORT_BASE64_EMBEDDING) {
126
138
}
127
139
var worker = new Worker ( workerURL ) ;
128
140
141
+ #if ENVIRONMENT_MAY_BE_NODE
142
+ if ( ENVIRONMENT_IS_NODE ) {
143
+ worker . postMessage ( { target : 'worker-init' } ) ;
144
+ } else {
145
+ #endif
129
146
WebGLClient . prefetch ( ) ;
130
147
131
- setTimeout ( function ( ) {
148
+ setTimeout ( ( ) => {
132
149
worker . postMessage ( {
133
150
target : 'worker-init' ,
134
151
width : Module . canvas . width ,
@@ -138,10 +155,13 @@ setTimeout(function() {
138
155
currentScriptUrl : filename ,
139
156
preMain : true } ) ;
140
157
} , 0 ) ; // delay til next frame, to make sure html is ready
158
+ #if ENVIRONMENT_MAY_BE_NODE
159
+ }
160
+ #endif
141
161
142
162
var workerResponded = false ;
143
163
144
- worker . onmessage = function worker_onmessage ( event ) {
164
+ worker . onmessage = ( event ) => {
145
165
//dump('\nclient got ' + JSON.stringify(event.data).substr(0, 150) + '\n');
146
166
if ( ! workerResponded ) {
147
167
workerResponded = true ;
@@ -230,7 +250,7 @@ worker.onmessage = function worker_onmessage(event) {
230
250
case 'IDBStore' : {
231
251
switch ( data . method ) {
232
252
case 'loadBlob' : {
233
- IDBStore . getFile ( data . db , data . id , function ( error , blob ) {
253
+ IDBStore . getFile ( data . db , data . id , ( error , blob ) => {
234
254
worker . postMessage ( {
235
255
target : 'IDBStore' ,
236
256
method : 'response' ,
@@ -240,7 +260,7 @@ worker.onmessage = function worker_onmessage(event) {
240
260
break ;
241
261
}
242
262
case 'storeBlob' : {
243
- IDBStore . setFile ( data . db , data . id , data . blob , function ( error ) {
263
+ IDBStore . setFile ( data . db , data . id , data . blob , ( error ) => {
244
264
worker . postMessage ( {
245
265
target : 'IDBStore' ,
246
266
method : 'response' ,
@@ -282,6 +302,10 @@ function cloneObject(event) {
282
302
return ret ;
283
303
} ;
284
304
305
+ #if ENVIRONMENT_MAY_BE_NODE
306
+ if ( ! ENVIRONMENT_IS_NODE ) {
307
+ #endif
308
+
285
309
// Only prevent default on backspace/tab because we don't want unexpected navigation.
286
310
// Do not prevent default on the rest as we need the keypress event.
287
311
function shouldPreventDefault ( event ) {
@@ -292,26 +316,30 @@ function shouldPreventDefault(event) {
292
316
}
293
317
} ;
294
318
295
- [ 'keydown' , 'keyup' , 'keypress' , 'blur' , 'visibilitychange' ] . forEach ( function ( event ) {
296
- document . addEventListener ( event , function ( event ) {
319
+
320
+ [ 'keydown' , 'keyup' , 'keypress' , 'blur' , 'visibilitychange' ] . forEach ( ( event ) => {
321
+ document . addEventListener ( event , ( event ) => {
297
322
worker . postMessage ( { target : 'document' , event : cloneObject ( event ) } ) ;
298
-
323
+
299
324
if ( shouldPreventDefault ( event ) ) {
300
325
event . preventDefault ( ) ;
301
326
}
302
327
} ) ;
303
328
} ) ;
304
329
305
- [ 'unload' ] . forEach ( function ( event ) {
306
- window . addEventListener ( event , function ( event ) {
330
+ [ 'unload' ] . forEach ( ( event ) => {
331
+ window . addEventListener ( event , ( event ) => {
307
332
worker . postMessage ( { target : 'window' , event : cloneObject ( event ) } ) ;
308
333
} ) ;
309
334
} ) ;
310
335
311
- [ 'mousedown' , 'mouseup' , 'mousemove' , 'DOMMouseScroll' , 'mousewheel' , 'mouseout' ] . forEach ( function ( event ) {
312
- Module . canvas . addEventListener ( event , function ( event ) {
336
+ [ 'mousedown' , 'mouseup' , 'mousemove' , 'DOMMouseScroll' , 'mousewheel' , 'mouseout' ] . forEach ( ( event ) => {
337
+ Module . canvas . addEventListener ( event , ( event ) => {
313
338
worker . postMessage ( { target : 'canvas' , event : cloneObject ( event ) } ) ;
314
339
event . preventDefault ( ) ;
315
340
} , true ) ;
316
341
} ) ;
317
342
343
+ #if ENVIRONMENT_MAY_BE_NODE
344
+ }
345
+ #endif
0 commit comments