|
106 | 106 | return this; |
107 | 107 | }; |
108 | 108 |
|
| 109 | + Parallel.prototype._spawnMapWorker = function (i, cb, done) { |
| 110 | + var that = this; |
| 111 | + var wrk = new Worker(that.options.path); |
| 112 | + wrk.postMessage(Parallel.getWorkerSource(cb)); |
| 113 | + wrk.postMessage(that.data[i]); |
| 114 | + wrk.onmessage = function (msg) { |
| 115 | + wrk.terminate(); |
| 116 | + that.data[i] = msg.data; |
| 117 | + done(); |
| 118 | + }; |
| 119 | + }; |
| 120 | + |
109 | 121 | Parallel.prototype.map = function (cb) { |
110 | 122 | if (!this.data.length) { |
111 | 123 | return this.spawn(cb); |
112 | 124 | } |
113 | 125 |
|
114 | | - function spawnWorker(i) { |
115 | | - var wrk = new Worker(that.options.path); |
116 | | - wrk.postMessage(Parallel.getWorkerSource(cb)); |
117 | | - wrk.postMessage(that.data[i]); |
118 | | - wrk.onmessage = function (msg) { |
119 | | - wrk.terminate(); |
120 | | - that.data[i] = msg.data; |
121 | | - done(); |
122 | | - }; |
123 | | - } |
124 | | - |
125 | 126 | var that = this; |
126 | 127 | var startedOps = 0; |
127 | 128 | var doneOps = 0; |
128 | 129 | function done() { |
129 | 130 | if (++doneOps === that.data.length) { |
130 | 131 | newOp.resolve(null, that.data); |
131 | 132 | } else if (startedOps < that.data.length) { |
132 | | - spawnWorker(++startedOps); |
| 133 | + this._spawnMapWorker(++startedOps, cb, done); |
133 | 134 | } |
134 | 135 | } |
135 | 136 |
|
136 | 137 | var newOp = new Operation(); |
137 | 138 | this.operation.then(function () { |
138 | 139 | for (; startedOps - doneOps < that.options.maxWorkers && startedOps < that.data.length; ++startedOps) { |
139 | | - spawnWorker(startedOps); |
| 140 | + that._spawnMapWorker(startedOps, cb, done); |
140 | 141 | } |
141 | 142 | }); |
142 | 143 | this.operation = newOp; |
|
0 commit comments