1 |
|
2 | function Worker(){
|
3 | var Threads;
|
4 | Threads = this;
|
5 | return (function(){
|
6 | var prototype = constructor.prototype;
|
7 | function constructor(code){
|
8 | var t, this$ = this;
|
9 | this.thread = t = Threads.create();
|
10 | t.on('message', function(args){
|
11 | return typeof this$.onmessage == 'function' ? this$.onmessage({
|
12 | data: args
|
13 | }) : void 8;
|
14 | });
|
15 | t.on('error', function(args){
|
16 | return typeof this$.onerror == 'function' ? this$.onerror(args) : void 8;
|
17 | });
|
18 | t.on('close', function(){
|
19 | return t.destroy();
|
20 | });
|
21 | this.terminate = function(){
|
22 | return t.destroy();
|
23 | };
|
24 | this.addEventListener = function(event, cb){
|
25 | if (event === 'message') {
|
26 | return this$.onmessage = cb;
|
27 | } else {
|
28 | return t.on(event, cb);
|
29 | }
|
30 | };
|
31 | this.dispatchEvent = function(event){
|
32 | return t.emitSerialized(event.type, event);
|
33 | };
|
34 | this.postMessage = function(data){
|
35 | return t.emitSerialized('message', {
|
36 | data: data
|
37 | });
|
38 | };
|
39 | if (typeof code === 'function') {
|
40 | t.eval("(" + code + ")()");
|
41 | } else if (code != null) {
|
42 | t.load(code);
|
43 | }
|
44 | }
|
45 | return constructor;
|
46 | }());
|
47 | }
|