Skip to content

Commit 61bce33

Browse files
committed
Refactor spawning the worker.
1 parent c9dcd62 commit 61bce33

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/thread.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,18 @@
8989
}
9090
};
9191

92+
Thread.prototype._spawnWorker = function (cb) {
93+
var wrk = new Worker(this.options.path);
94+
wrk.postMessage(Thread.getWorkerSource(cb));
95+
96+
return wrk;
97+
};
98+
9299
Thread.prototype.spawn = function (cb) {
93100
var that = this;
94101
var newOp = new Operation();
95102
this.operation.then(function () {
96-
var wrk = new Worker(that.options.path);
97-
wrk.postMessage(Thread.getWorkerSource(cb));
103+
var wrk = that._spawnWorker(cb);
98104
wrk.onmessage = function (msg) {
99105
wrk.terminate();
100106
that.data = msg.data;
@@ -108,8 +114,7 @@
108114

109115
Thread.prototype._spawnMapWorker = function (i, cb, done) {
110116
var that = this;
111-
var wrk = new Worker(that.options.path);
112-
wrk.postMessage(Thread.getWorkerSource(cb));
117+
var wrk = that._spawnWorker(cb);
113118
wrk.onmessage = function (msg) {
114119
wrk.terminate();
115120
that.data[i] = msg.data;
@@ -146,8 +151,7 @@
146151

147152
Thread.prototype._spawnReduceWorker = function (data, cb, done) {
148153
var that = this;
149-
var wrk = new Worker(that.options.path);
150-
wrk.postMessage(Thread.getWorkerSource(cb));
154+
var wrk = that._spawnWorker(cb);
151155
wrk.onmessage = function (msg) {
152156
wrk.terminate();
153157
that.data[that.data.length] = msg.data;

0 commit comments

Comments
 (0)