forked from cytoscape/cytoscape.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.js
482 lines (369 loc) · 12 KB
/
thread.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
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
472
473
474
475
476
477
478
479
480
481
482
/*! Weaver licensed under MIT (https://tldrlegal.com/license/mit-license), copyright Max Franz */
// cross-env thread/worker
// NB : uses (heavyweight) processes on nodejs so best not to create too many threads
'use strict';
var window = require('./window');
var util = require('./util');
var Promise = require('./promise');
var Event = require('./event');
var define = require('./define');
var is = require('./is');
var Thread = function( opts ){
if( !(this instanceof Thread) ){
return new Thread( opts );
}
var _p = this._private = {
requires: [],
files: [],
queue: null,
pass: [],
disabled: false
};
if( is.plainObject(opts) ){
if( opts.disabled != null ){
_p.disabled = !!opts.disabled;
}
}
};
var thdfn = Thread.prototype; // short alias
var stringifyFieldVal = function( val ){
var valStr = is.fn( val ) ? val.toString() : "JSON.parse('" + JSON.stringify(val) + "')";
return valStr;
};
// allows for requires with prototypes and subobjs etc
var fnAsRequire = function( fn ){
var req;
var fnName;
if( is.object(fn) && fn.fn ){ // manual fn
req = fnAs( fn.fn, fn.name );
fnName = fn.name;
fn = fn.fn;
} else if( is.fn(fn) ){ // auto fn
req = fn.toString();
fnName = fn.name;
} else if( is.string(fn) ){ // stringified fn
req = fn;
} else if( is.object(fn) ){ // plain object
if( fn.proto ){
req = '';
} else {
req = fn.name + ' = {};';
}
fnName = fn.name;
fn = fn.obj;
}
req += '\n';
var protoreq = function( val, subname ){
if( val.prototype ){
var protoNonempty = false;
for( var prop in val.prototype ){ protoNonempty = true; break; } // jshint ignore:line
if( protoNonempty ){
req += fnAsRequire( {
name: subname,
obj: val,
proto: true
}, val );
}
}
};
// pull in prototype
if( fn.prototype && fnName != null ){
for( var name in fn.prototype ){
var protoStr = '';
var val = fn.prototype[ name ];
var valStr = stringifyFieldVal( val );
var subname = fnName + '.prototype.' + name;
protoStr += subname + ' = ' + valStr + ';\n';
if( protoStr ){
req += protoStr;
}
protoreq( val, subname ); // subobject with prototype
}
}
// pull in properties for obj/fns
if( !is.string(fn) ){ for( var name in fn ){
var propsStr = '';
if( fn.hasOwnProperty(name) ){
var val = fn[ name ];
var valStr = stringifyFieldVal( val );
var subname = fnName + '["' + name + '"]';
propsStr += subname + ' = ' + valStr + ';\n';
}
if( propsStr ){
req += propsStr;
}
protoreq( val, subname ); // subobject with prototype
} }
return req;
};
var isPathStr = function( str ){
return is.string(str) && str.match(/\.js$/);
};
util.extend(thdfn, {
instanceString: function(){ return 'thread'; },
require: function( fn, as ){
var requires = this._private.requires;
if( isPathStr(fn) ){
this._private.files.push( fn );
return this;
}
if( as ){
if( is.fn(fn) ){
fn = { name: as, fn: fn };
} else {
fn = { name: as, obj: fn };
}
} else {
if( is.fn(fn) ){
if( !fn.name ){
throw 'The function name could not be automatically determined. Use thread.require( someFunction, "someFunction" )';
}
fn = { name: fn.name, fn: fn };
}
}
requires.push( fn );
return this; // chaining
},
pass: function( data ){
this._private.pass.push( data );
return this; // chaining
},
run: function( fn, pass ){ // fn used like main()
var self = this;
var _p = this._private;
pass = pass || _p.pass.shift();
if( _p.stopped ){
throw 'Attempted to run a stopped thread! Start a new thread or do not stop the existing thread and reuse it.';
}
if( _p.running ){
return ( _p.queue = _p.queue.then(function(){ // inductive step
return self.run( fn, pass );
}) );
}
var useWW = window != null && !_p.disabled;
var useNode = !window && typeof module !== 'undefined' && !_p.disabled;
self.trigger('run');
var runP = new Promise(function( resolve, reject ){
_p.running = true;
var threadTechAlreadyExists = _p.ran;
var fnImplStr = is.string( fn ) ? fn : fn.toString();
// worker code to exec
var fnStr = '\n' + ( _p.requires.map(function( r ){
return fnAsRequire( r );
}) ).concat( _p.files.map(function( f ){
if( useWW ){
var wwifyFile = function( file ){
if( file.match(/^\.\//) || file.match(/^\.\./) ){
return window.location.origin + window.location.pathname + file;
} else if( file.match(/^\//) ){
return window.location.origin + '/' + file;
}
return file;
};
return 'importScripts("' + wwifyFile(f) + '");';
} else if( useNode ) {
return 'eval( require("fs").readFileSync("' + f + '", { encoding: "utf8" }) );';
} else {
throw 'External file `' + f + '` can not be required without any threading technology.';
}
}) ).concat([
'( function(){',
'var ret = (' + fnImplStr + ')(' + JSON.stringify(pass) + ');',
'if( ret !== undefined ){ resolve(ret); }', // assume if ran fn returns defined value (incl. null), that we want to resolve to it
'} )()\n'
]).join('\n');
// because we've now consumed the requires, empty the list so we don't dupe on next run()
_p.requires = [];
_p.files = [];
if( useWW ){
var fnBlob, fnUrl;
// add normalised thread api functions
if( !threadTechAlreadyExists ){
var fnPre = fnStr + '';
fnStr = [
'function _ref_(o){ return eval(o); };',
'function broadcast(m){ return message(m); };', // alias
'function message(m){ postMessage(m); };',
'function listen(fn){',
' self.addEventListener("message", function(m){ ',
' if( typeof m === "object" && (m.data.$$eval || m.data === "$$start") ){',
' } else { ',
' fn( m.data );',
' }',
' });',
'};',
'self.addEventListener("message", function(m){ if( m.data.$$eval ){ eval( m.data.$$eval ); } });',
'function resolve(v){ postMessage({ $$resolve: v }); };',
'function reject(v){ postMessage({ $$reject: v }); };'
].join('\n');
fnStr += fnPre;
fnBlob = new Blob([ fnStr ], {
type: 'application/javascript'
});
fnUrl = window.URL.createObjectURL( fnBlob );
}
// create webworker and let it exec the serialised code
var ww = _p.webworker = _p.webworker || new Worker( fnUrl );
if( threadTechAlreadyExists ){ // then just exec new run() code
ww.postMessage({
$$eval: fnStr
});
}
// worker messages => events
var cb;
ww.addEventListener('message', cb = function( m ){
var isObject = is.object(m) && is.object( m.data );
if( isObject && ('$$resolve' in m.data) ){
ww.removeEventListener('message', cb); // done listening b/c resolve()
resolve( m.data.$$resolve );
} else if( isObject && ('$$reject' in m.data) ){
ww.removeEventListener('message', cb); // done listening b/c reject()
reject( m.data.$$reject );
} else {
self.trigger( new Event(m, { type: 'message', message: m.data }) );
}
}, false);
if( !threadTechAlreadyExists ){
ww.postMessage('$$start'); // start up the worker
}
} else if( useNode ){
// create a new process
if( !_p.child ){
_p.child = ( require('child_process').fork( require('path').join(__dirname, 'thread-node-fork') ) );
}
var child = _p.child;
// child process messages => events
var cb;
child.on('message', cb = function( m ){
if( is.object(m) && ('$$resolve' in m) ){
child.removeListener('message', cb); // done listening b/c resolve()
resolve( m.$$resolve );
} else if( is.object(m) && ('$$reject' in m) ){
child.removeListener('message', cb); // done listening b/c reject()
reject( m.$$reject );
} else {
self.trigger( new Event({}, { type: 'message', message: m }) );
}
});
// ask the child process to eval the worker code
child.send({
$$eval: fnStr
});
} else { // use a fallback mechanism using a timeout
var promiseResolve = resolve;
var promiseReject = reject;
var timer = _p.timer = _p.timer || {
listeners: [],
exec: function(){
// as a string so it can't be mangled by minifiers and processors
fnStr = [
'function _ref_(o){ return eval(o); };',
'function broadcast(m){ return message(m); };',
'function message(m){ self.trigger( new Event({}, { type: "message", message: m }) ); };',
'function listen(fn){ timer.listeners.push( fn ); };',
'function resolve(v){ promiseResolve(v); };',
'function reject(v){ promiseReject(v); };'
].join('\n') + fnStr;
// the .run() code
eval( fnStr ); // jshint ignore:line
},
message: function( m ){
var ls = timer.listeners;
for( var i = 0; i < ls.length; i++ ){
var fn = ls[i];
fn( m );
}
}
};
timer.exec();
}
}).then(function( v ){
_p.running = false;
_p.ran = true;
self.trigger('ran');
return v;
});
if( _p.queue == null ){
_p.queue = runP; // i.e. first step of inductive promise chain (for queue)
}
return runP;
},
// send the thread a message
message: function( m ){
var _p = this._private;
if( _p.webworker ){
_p.webworker.postMessage( m );
}
if( _p.child ){
_p.child.send( m );
}
if( _p.timer ){
_p.timer.message( m );
}
return this; // chaining
},
stop: function(){
var _p = this._private;
if( _p.webworker ){
_p.webworker.terminate();
}
if( _p.child ){
_p.child.kill();
}
if( _p.timer ){
// nothing we can do if we've run a timeout
}
_p.stopped = true;
return this.trigger('stop'); // chaining
},
stopped: function(){
return this._private.stopped;
}
});
// turns a stringified function into a (re)named function
var fnAs = function( fn, name ){
var fnStr = fn.toString();
fnStr = fnStr.replace(/function\s*?\S*?\s*?\(/, 'function ' + name + '(');
return fnStr;
};
var defineFnal = function( opts ){
opts = opts || {};
return function fnalImpl( fn, arg1 ){
var fnStr = fnAs( fn, '_$_$_' + opts.name );
this.require( fnStr );
return this.run( [
'function( data ){',
' var origResolve = resolve;',
' var res = [];',
' ',
' resolve = function( val ){',
' res.push( val );',
' };',
' ',
' var ret = data.' + opts.name + '( _$_$_' + opts.name + ( arguments.length > 1 ? ', ' + JSON.stringify(arg1) : '' ) + ' );',
' ',
' resolve = origResolve;',
' resolve( res.length > 0 ? res : ret );',
'}'
].join('\n') );
};
};
util.extend(thdfn, {
reduce: defineFnal({ name: 'reduce' }),
reduceRight: defineFnal({ name: 'reduceRight' }),
map: defineFnal({ name: 'map' })
});
// aliases
var fn = thdfn;
fn.promise = fn.run;
fn.terminate = fn.halt = fn.stop;
fn.include = fn.require;
// pull in event apis
util.extend(thdfn, {
on: define.on(),
one: define.on({ unbindSelfOnTrigger: true }),
off: define.off(),
trigger: define.trigger()
});
define.eventAliasesOn( thdfn );
module.exports = Thread;