|
69 | 69 | var defaults = { |
70 | 70 | ie10shim: !isNode, |
71 | 71 | path: (isNode ? __dirname + '/' : 'lib/') + 'eval.js', |
72 | | - maxWorkers: isNode ? require('os').cpus().length : 4 |
| 72 | + maxWorkers: isNode ? require('os').cpus().length : 4, |
| 73 | + synchronous: true |
73 | 74 | }; |
74 | 75 |
|
75 | 76 | function Parallel(data, options) { |
|
120 | 121 | wrk = new Worker(this.options.path); |
121 | 122 | wrk.postMessage(src); |
122 | 123 | } else { |
| 124 | + if (Worker === undefined) { |
| 125 | + return undefined; |
| 126 | + } |
| 127 | + |
123 | 128 | try { |
124 | 129 | if (this.requiredScripts.length !== 0) { |
125 | 130 | if (this.options.ie10shim) { |
|
152 | 157 | var newOp = new Operation(); |
153 | 158 | this.operation.then(function () { |
154 | 159 | var wrk = that._spawnWorker(cb); |
155 | | - wrk.onmessage = function (msg) { |
156 | | - wrk.terminate(); |
157 | | - that.data = msg.data; |
158 | | - newOp.resolve(null, that.data); |
159 | | - }; |
160 | | - wrk.postMessage(that.data); |
| 160 | + if (wrk !== undefined) { |
| 161 | + wrk.onmessage = function (msg) { |
| 162 | + wrk.terminate(); |
| 163 | + that.data = msg.data; |
| 164 | + newOp.resolve(null, that.data); |
| 165 | + }; |
| 166 | + wrk.postMessage(that.data); |
| 167 | + } else if (that.options.synchronous) { |
| 168 | + setImmediate(function () { |
| 169 | + that.data = cb(that.data); |
| 170 | + newOp.resolve(null, that.data); |
| 171 | + }); |
| 172 | + } else { |
| 173 | + throw new Error('Workers do not exist and synchronous operation not allowed!'); |
| 174 | + } |
161 | 175 | }); |
162 | 176 | this.operation = newOp; |
163 | 177 | return this; |
|
166 | 180 | Parallel.prototype._spawnMapWorker = function (i, cb, done) { |
167 | 181 | var that = this; |
168 | 182 | var wrk = that._spawnWorker(cb); |
169 | | - wrk.onmessage = function (msg) { |
170 | | - wrk.terminate(); |
171 | | - that.data[i] = msg.data; |
172 | | - done(); |
173 | | - }; |
174 | | - wrk.postMessage(that.data[i]); |
| 183 | + if (wrk !== undefined) { |
| 184 | + wrk.onmessage = function (msg) { |
| 185 | + wrk.terminate(); |
| 186 | + that.data[i] = msg.data; |
| 187 | + done(); |
| 188 | + }; |
| 189 | + wrk.postMessage(that.data[i]); |
| 190 | + } else if (that.options.synchronous) { |
| 191 | + setImmediate(function () { |
| 192 | + that.data[i] = cb(that.data[i]); |
| 193 | + done(); |
| 194 | + }); |
| 195 | + } else { |
| 196 | + throw new Error('Workers do not exist and synchronous operation not allowed!'); |
| 197 | + } |
175 | 198 | }; |
176 | 199 |
|
177 | 200 | Parallel.prototype.map = function (cb) { |
|
203 | 226 | Parallel.prototype._spawnReduceWorker = function (data, cb, done) { |
204 | 227 | var that = this; |
205 | 228 | var wrk = that._spawnWorker(cb); |
206 | | - wrk.onmessage = function (msg) { |
207 | | - wrk.terminate(); |
208 | | - that.data[that.data.length] = msg.data; |
209 | | - done(); |
210 | | - }; |
211 | | - wrk.postMessage(data); |
| 229 | + if (wrk !== undefined) { |
| 230 | + wrk.onmessage = function (msg) { |
| 231 | + wrk.terminate(); |
| 232 | + that.data[that.data.length] = msg.data; |
| 233 | + done(); |
| 234 | + }; |
| 235 | + wrk.postMessage(data); |
| 236 | + } else if (that.options.synchronous) { |
| 237 | + setImmediate(function () { |
| 238 | + that.data[that.data.length] = cb(data); |
| 239 | + done(); |
| 240 | + }); |
| 241 | + } else { |
| 242 | + throw new Error('Workers do not exist and synchronous operation not allowed!'); |
| 243 | + } |
212 | 244 | }; |
213 | 245 |
|
214 | 246 | Parallel.prototype.reduce = function (cb) { |
|
0 commit comments