Skip to content

Commit

Permalink
workers: replace message types string by constants
Browse files Browse the repository at this point in the history
This change can prevent typos and redundant strings in code.

PR-URL: #21537
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
starkwang authored and targos committed Jun 29, 2018
1 parent eb6dcf2 commit 49706b4
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const kIncrementsPortRef = Symbol('kIncrementsPortRef');

const debug = util.debuglog('worker');

const messageTypes = {
UP_AND_RUNNING: 'upAndRunning',
COULD_NOT_SERIALIZE_ERROR: 'couldNotSerializeError',
ERROR_MESSAGE: 'errorMessage',
STDIO_PAYLOAD: 'stdioPayload',
STDIO_WANTS_MORE_DATA: 'stdioWantsMoreData',
LOAD_SCRIPT: 'loadScript'
};

// A communication channel consisting of a handle (that wraps around an
// uv_async_t) which can receive information from other threads and emits
// .onmessage events, and a function used for sending data to a MessagePort
Expand Down Expand Up @@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable {
}

this[kPort].postMessage({
type: 'stdioWantsMoreData',
type: messageTypes.STDIO_WANTS_MORE_DATA,
stream: this[kName]
});
}
Expand All @@ -174,7 +183,7 @@ class WritableWorkerStdio extends Writable {

_write(chunk, encoding, cb) {
this[kPort].postMessage({
type: 'stdioPayload',
type: messageTypes.STDIO_PAYLOAD,
stream: this[kName],
chunk,
encoding
Expand All @@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable {

_final(cb) {
this[kPort].postMessage({
type: 'stdioPayload',
type: messageTypes.STDIO_PAYLOAD,
stream: this[kName],
chunk: null
});
Expand Down Expand Up @@ -258,7 +267,7 @@ class Worker extends EventEmitter {
this[kPublicPort].on('message', (message) => this.emit('message', message));
setupPortReferencing(this[kPublicPort], this, 'message');
this[kPort].postMessage({
type: 'loadScript',
type: messageTypes.LOAD_SCRIPT,
filename,
doEval: !!options.eval,
workerData: options.workerData,
Expand Down Expand Up @@ -289,18 +298,18 @@ class Worker extends EventEmitter {

[kOnMessage](message) {
switch (message.type) {
case 'upAndRunning':
case messageTypes.UP_AND_RUNNING:
return this.emit('online');
case 'couldNotSerializeError':
case messageTypes.COULD_NOT_SERIALIZE_ERROR:
return this[kOnCouldNotSerializeErr]();
case 'errorMessage':
case messageTypes.ERROR_MESSAGE:
return this[kOnErrorMessage](message.error);
case 'stdioPayload':
case messageTypes.STDIO_PAYLOAD:
{
const { stream, chunk, encoding } = message;
return this[kParentSideStdio][stream].push(chunk, encoding);
}
case 'stdioWantsMoreData':
case messageTypes.STDIO_WANTS_MORE_DATA:
{
const { stream } = message;
return this[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
Expand Down Expand Up @@ -396,7 +405,7 @@ function setupChild(evalScript) {
const publicWorker = require('worker_threads');

port.on('message', (message) => {
if (message.type === 'loadScript') {
if (message.type === messageTypes.LOAD_SCRIPT) {
const { filename, doEval, workerData, publicPort, hasStdin } = message;
publicWorker.parentPort = publicPort;
setupPortReferencing(publicPort, publicPort, 'message');
Expand All @@ -408,19 +417,19 @@ function setupChild(evalScript) {
debug(`[${threadId}] starts worker script ${filename} ` +
`(eval = ${eval}) at cwd = ${process.cwd()}`);
port.unref();
port.postMessage({ type: 'upAndRunning' });
port.postMessage({ type: messageTypes.UP_AND_RUNNING });
if (doEval) {
evalScript('[worker eval]', filename);
} else {
process.argv[1] = filename; // script filename
require('module').runMain();
}
return;
} else if (message.type === 'stdioPayload') {
} else if (message.type === messageTypes.STDIO_PAYLOAD) {
const { stream, chunk, encoding } = message;
workerStdio[stream].push(chunk, encoding);
return;
} else if (message.type === 'stdioWantsMoreData') {
} else if (message.type === messageTypes.STDIO_WANTS_MORE_DATA) {
const { stream } = message;
workerStdio[stream][kStdioWantsMoreDataCallback]();
return;
Expand Down Expand Up @@ -451,9 +460,12 @@ function setupChild(evalScript) {
} catch {}
debug(`[${threadId}] fatal exception serialized = ${!!serialized}`);
if (serialized)
port.postMessage({ type: 'errorMessage', error: serialized });
port.postMessage({
type: messageTypes.ERROR_MESSAGE,
error: serialized
});
else
port.postMessage({ type: 'couldNotSerializeError' });
port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
clearAsyncIdStack();
}
}
Expand Down

0 comments on commit 49706b4

Please sign in to comment.