@@ -60,24 +60,34 @@ var Parallel = (function () {
60
60
61
61
var wrap = _ . compose ( wrapFunctions , wrapFiles , wrapMain ) ;
62
62
63
- var RemoteRef = function ( fn ) {
64
- var str = wrap ( fn ) ,
65
- blob = new Blob ( [ str ] , { type : 'text/javascript' } ) ,
66
- url = URL . createObjectURL ( blob ) ,
67
- worker = new Worker ( url ) ;
68
-
69
- worker . onmessage = _ . bind ( this . onWorkerMsg , worker ) ;
70
-
71
- this . worker = worker ;
72
- this . worker . ref = this ;
63
+ var RemoteRef = function ( fn , args ) {
64
+ try {
65
+ var str = wrap ( fn ) ,
66
+ blob = new Blob ( [ str ] , { type : 'text/javascript' } ) ,
67
+ url = URL . createObjectURL ( blob ) ,
68
+ worker = new Worker ( url ) ;
69
+
70
+ worker . onmessage = _ . bind ( this . onWorkerMsg , this ) ;
71
+
72
+ this . worker = worker ;
73
+ this . worker . ref = this ;
74
+
75
+ if ( isNode ) {
76
+ this . worker . postMessage ( JSON . stringify ( [ ] . concat ( args ) ) ) ;
77
+ } else {
78
+ this . worker . postMessage ( [ ] . concat ( args ) ) ;
79
+ }
80
+ } catch ( e ) {
81
+ this . onWorkerMsg ( { data : fn . apply ( window , args ) } ) ;
82
+ }
73
83
} ;
74
84
75
85
RemoteRef . prototype . onWorkerMsg = function ( e ) {
76
86
if ( isNode ) {
77
- this . ref . data = JSON . parse ( e . data ) ;
78
- this . ref . worker . terminate ( ) ;
87
+ this . data = JSON . parse ( e . data ) ;
88
+ this . worker . terminate ( ) ;
79
89
} else {
80
- this . ref . data = e . data ;
90
+ this . data = e . data ;
81
91
}
82
92
} ;
83
93
@@ -97,12 +107,7 @@ var Parallel = (function () {
97
107
} ;
98
108
99
109
return function ( fn , args ) {
100
- var r = new RemoteRef ( fn ) ;
101
- if ( isNode ) {
102
- r . worker . postMessage ( JSON . stringify ( [ ] . concat ( args ) ) ) ;
103
- } else {
104
- r . worker . postMessage ( [ ] . concat ( args ) ) ;
105
- }
110
+ var r = new RemoteRef ( fn , args ) ;
106
111
107
112
return r ;
108
113
} ;
0 commit comments