Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change worker.send(type, data) to postMessage(data) #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions debug/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ worker.onmessage = function (type, data) {
if (type === 'bar') {
console.log('main: got bar from worker');
console.log('main: sending baz to worker');
this.send('baz');
this.postMessage({type: 'baz'});
}
};

console.log('main: sending foo to worker');
worker.send('foo');
worker.postMessage({type: 'foo'});

setTimeout(function () {
console.log('main: creating worker2');
var worker2 = new PooledWorker(require('./worker2'));

worker2.onmessage = function (type, data) {
if (type === 'answer') {
console.log('main: got answer ' + data + ' from worker2');
worker2.onmessage = function (e) {
if (e.data.type === 'answer') {
console.log('main: got answer ' + e.data.message + ' from worker2');

console.log('main: terminating workers');
worker.terminate();
Expand All @@ -33,5 +33,5 @@ setTimeout(function () {
}

console.log('main: sending ask to worker2');
worker2.send('ask');
worker2.postMessage({type: 'ask'});
}, 200);
11 changes: 4 additions & 7 deletions debug/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ module.exports = TestWorker;

function TestWorker() {
console.log('worker: created');
TestWorker.sharedState.push('test');
};

TestWorker.sharedState = [];

TestWorker.prototype = {
onmessage: function (type, data) {
if (type === 'foo') {
onmessage: function (e) {
if (e.data.type === 'foo') {
console.log('worker: got foo');
console.log('worker: sending bar');
this.send('bar');
this.postMessage({type: 'bar'});
}
if (type === 'baz') {
if (e.data.type === 'baz') {
console.log('worker: got baz');
}
},
Expand Down
6 changes: 3 additions & 3 deletions debug/src/worker2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function TestWorker2() {
};

TestWorker2.prototype = {
onmessage: function (type, data) {
if (type === 'ask') {
onmessage: function (e) {
if (e.data.type === 'ask') {
console.log('worker2: got ask');
console.log('worker2: sending answer');
this.send('answer', hello + ' ' + answer);
this.postMessage({type: 'answer', message: hello + ' ' + answer});
}
},
onterminate: function () {
Expand Down
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ module.exports = createWorkerPool;
function nativeWorkerFn(self) {
var workersidePooledWorkers = {};

function send(type, data) {
function send(data) {
self.postMessage({
type: type,
data: data,
workerId: this.workerId
workerId: this.workerId,
data: data
});
}

Expand All @@ -30,7 +29,7 @@ function nativeWorkerFn(self) {
if (Worker.prototype.workerId) {
throw new Error('Pooled worker class should not have a workerId property.');
}
Worker.prototype.send = send;
Worker.prototype.postMessage = send;
Worker.prototype.workerId = workerId;

workersidePooledWorkers[workerId] = new Worker();
Expand All @@ -42,22 +41,25 @@ function nativeWorkerFn(self) {

if (data.bundle) { // add missing dependencies
self.importScripts(data.bundle);
}
if (data.moduleId) { // create workerside pooled worker

} else if (data.moduleId) { // create workerside pooled worker
createWorkersidePooledWorker(data.moduleId, data.workerId);
}
if (data.type) { // process message to the worker

} else if (data.data) { // process message to the worker
worker = workersidePooledWorkers[data.workerId];
if (worker.onmessage) {
worker.onmessage(data.type, data.data);
worker.onmessage({data: data.data});
}
}
if (data.terminate) { // terminate the worker

} else if (data.terminate) { // terminate the worker
worker = workersidePooledWorkers[data.workerId];
delete workersidePooledWorkers[data.workerId];
if (worker.onterminate) {
worker.onterminate();
}

} else {
throw new Error('Unexpected message: ' + e);
}
};
}
Expand Down Expand Up @@ -93,10 +95,9 @@ function createWorkerPool(workerCount) {

PooledWorker.prototype = {

send: function (type, data) {
postMessage: function (data) {
this.worker.postMessage({
workerId: this.id,
type: type,
data: data
});
},
Expand All @@ -121,7 +122,7 @@ function createWorkerPool(workerCount) {
function handleWorkerMessage(e) {
var worker = pooledWorkers[e.data.workerId];
if (worker) {
worker.onmessage(e.data.type, e.data.data);
worker.onmessage(e.data);
}
}

Expand Down
20 changes: 11 additions & 9 deletions test/basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ test('roundtrip messages to one worker', function (t) {
var worker = new PooledWorker(TestWorker);
t.pass('main: create worker');

worker.onmessage = function (type, data) {
worker.onmessage = function (e) {
var type = e.data.type;

if (type === 'bar') {
t.pass('main: got bar');

this.send('baz');
this.postMessage({type: 'baz'});
t.pass('main: send baz');

} else if (type === 'end') {
t.equal(data, 'Hello', 'main: got end');
t.equal(e.data.message, 'Hello', 'main: got end');
worker.terminate();
t.pass('worker terminated');
t.end();
Expand All @@ -29,7 +31,7 @@ test('roundtrip messages to one worker', function (t) {
}
};

worker.send('foo');
worker.postMessage({type: 'foo'});
t.pass('main: send foo');
});

Expand All @@ -38,19 +40,19 @@ test('delayed worker2 creation and more messages', function (t) {
var worker2 = new PooledWorker(TestWorker2);
t.pass('main: create worker2');

worker2.onmessage = function (type, data) {
if (type === 'answer') {
t.equal(data, '100 Hello 42', 'main: got answer');
worker2.onmessage = function (e) {
if (e.data.type === 'answer') {
t.equal(e.data.message, '100 Hello 42', 'main: got answer');
worker2.terminate();
t.pass('worker2 terminated');
t.end();

} else {
t.fail('main: unexpected message ' + type);
t.fail('main: unexpected message ' + e.data.type);
}
};

worker2.send('ask', 100);
worker2.postMessage({type: 'ask', message: 100});
t.pass('main: send ask');
}, 200);
});
9 changes: 5 additions & 4 deletions test/basic/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ module.exports = TestWorker;
function TestWorker() {}

TestWorker.prototype = {
onmessage: function (type) {
onmessage: function (e) {
var type = e.data.type;
if (type === 'foo') {
this.send('bar');
this.postMessage({type: 'bar'});
} else if (type === 'baz') {
this.send('end', hello);
this.postMessage({type: 'end', message: hello});
} else {
this.send('error');
this.postMessage({type: 'error'});
}
}
};
8 changes: 4 additions & 4 deletions test/basic/worker2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module.exports = TestWorker2;
function TestWorker2() {}

TestWorker2.prototype = {
onmessage: function (type, data) {
if (type === 'ask') {
this.send('answer', data + ' ' + hello + ' ' + answer);
onmessage: function (e) {
if (e.data.type === 'ask') {
this.postMessage({type: 'answer', message: e.data.message + ' ' + hello + ' ' + answer});
} else {
this.send('error');
this.postMessage({type: 'error'});
}
}
};