workers: replace message types string by constants
This change can prevent typos and redundant strings in code. PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
d6397afbac
commit
ebf5b58bec
@ -47,6 +47,15 @@ const kIncrementsPortRef = Symbol('kIncrementsPortRef');
|
|||||||
|
|
||||||
const debug = util.debuglog('worker');
|
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
|
// A communication channel consisting of a handle (that wraps around an
|
||||||
// uv_async_t) which can receive information from other threads and emits
|
// uv_async_t) which can receive information from other threads and emits
|
||||||
// .onmessage events, and a function used for sending data to a MessagePort
|
// .onmessage events, and a function used for sending data to a MessagePort
|
||||||
@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this[kPort].postMessage({
|
this[kPort].postMessage({
|
||||||
type: 'stdioWantsMoreData',
|
type: messageTypes.STDIO_WANTS_MORE_DATA,
|
||||||
stream: this[kName]
|
stream: this[kName]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -174,7 +183,7 @@ class WritableWorkerStdio extends Writable {
|
|||||||
|
|
||||||
_write(chunk, encoding, cb) {
|
_write(chunk, encoding, cb) {
|
||||||
this[kPort].postMessage({
|
this[kPort].postMessage({
|
||||||
type: 'stdioPayload',
|
type: messageTypes.STDIO_PAYLOAD,
|
||||||
stream: this[kName],
|
stream: this[kName],
|
||||||
chunk,
|
chunk,
|
||||||
encoding
|
encoding
|
||||||
@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable {
|
|||||||
|
|
||||||
_final(cb) {
|
_final(cb) {
|
||||||
this[kPort].postMessage({
|
this[kPort].postMessage({
|
||||||
type: 'stdioPayload',
|
type: messageTypes.STDIO_PAYLOAD,
|
||||||
stream: this[kName],
|
stream: this[kName],
|
||||||
chunk: null
|
chunk: null
|
||||||
});
|
});
|
||||||
@ -258,7 +267,7 @@ class Worker extends EventEmitter {
|
|||||||
this[kPublicPort].on('message', (message) => this.emit('message', message));
|
this[kPublicPort].on('message', (message) => this.emit('message', message));
|
||||||
setupPortReferencing(this[kPublicPort], this, 'message');
|
setupPortReferencing(this[kPublicPort], this, 'message');
|
||||||
this[kPort].postMessage({
|
this[kPort].postMessage({
|
||||||
type: 'loadScript',
|
type: messageTypes.LOAD_SCRIPT,
|
||||||
filename,
|
filename,
|
||||||
doEval: !!options.eval,
|
doEval: !!options.eval,
|
||||||
workerData: options.workerData,
|
workerData: options.workerData,
|
||||||
@ -289,18 +298,18 @@ class Worker extends EventEmitter {
|
|||||||
|
|
||||||
[kOnMessage](message) {
|
[kOnMessage](message) {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'upAndRunning':
|
case messageTypes.UP_AND_RUNNING:
|
||||||
return this.emit('online');
|
return this.emit('online');
|
||||||
case 'couldNotSerializeError':
|
case messageTypes.COULD_NOT_SERIALIZE_ERROR:
|
||||||
return this[kOnCouldNotSerializeErr]();
|
return this[kOnCouldNotSerializeErr]();
|
||||||
case 'errorMessage':
|
case messageTypes.ERROR_MESSAGE:
|
||||||
return this[kOnErrorMessage](message.error);
|
return this[kOnErrorMessage](message.error);
|
||||||
case 'stdioPayload':
|
case messageTypes.STDIO_PAYLOAD:
|
||||||
{
|
{
|
||||||
const { stream, chunk, encoding } = message;
|
const { stream, chunk, encoding } = message;
|
||||||
return this[kParentSideStdio][stream].push(chunk, encoding);
|
return this[kParentSideStdio][stream].push(chunk, encoding);
|
||||||
}
|
}
|
||||||
case 'stdioWantsMoreData':
|
case messageTypes.STDIO_WANTS_MORE_DATA:
|
||||||
{
|
{
|
||||||
const { stream } = message;
|
const { stream } = message;
|
||||||
return this[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
|
return this[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
|
||||||
@ -396,7 +405,7 @@ function setupChild(evalScript) {
|
|||||||
const publicWorker = require('worker_threads');
|
const publicWorker = require('worker_threads');
|
||||||
|
|
||||||
port.on('message', (message) => {
|
port.on('message', (message) => {
|
||||||
if (message.type === 'loadScript') {
|
if (message.type === messageTypes.LOAD_SCRIPT) {
|
||||||
const { filename, doEval, workerData, publicPort, hasStdin } = message;
|
const { filename, doEval, workerData, publicPort, hasStdin } = message;
|
||||||
publicWorker.parentPort = publicPort;
|
publicWorker.parentPort = publicPort;
|
||||||
setupPortReferencing(publicPort, publicPort, 'message');
|
setupPortReferencing(publicPort, publicPort, 'message');
|
||||||
@ -408,7 +417,7 @@ function setupChild(evalScript) {
|
|||||||
debug(`[${threadId}] starts worker script ${filename} ` +
|
debug(`[${threadId}] starts worker script ${filename} ` +
|
||||||
`(eval = ${eval}) at cwd = ${process.cwd()}`);
|
`(eval = ${eval}) at cwd = ${process.cwd()}`);
|
||||||
port.unref();
|
port.unref();
|
||||||
port.postMessage({ type: 'upAndRunning' });
|
port.postMessage({ type: messageTypes.UP_AND_RUNNING });
|
||||||
if (doEval) {
|
if (doEval) {
|
||||||
evalScript('[worker eval]', filename);
|
evalScript('[worker eval]', filename);
|
||||||
} else {
|
} else {
|
||||||
@ -416,11 +425,11 @@ function setupChild(evalScript) {
|
|||||||
require('module').runMain();
|
require('module').runMain();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if (message.type === 'stdioPayload') {
|
} else if (message.type === messageTypes.STDIO_PAYLOAD) {
|
||||||
const { stream, chunk, encoding } = message;
|
const { stream, chunk, encoding } = message;
|
||||||
workerStdio[stream].push(chunk, encoding);
|
workerStdio[stream].push(chunk, encoding);
|
||||||
return;
|
return;
|
||||||
} else if (message.type === 'stdioWantsMoreData') {
|
} else if (message.type === messageTypes.STDIO_WANTS_MORE_DATA) {
|
||||||
const { stream } = message;
|
const { stream } = message;
|
||||||
workerStdio[stream][kStdioWantsMoreDataCallback]();
|
workerStdio[stream][kStdioWantsMoreDataCallback]();
|
||||||
return;
|
return;
|
||||||
@ -451,9 +460,12 @@ function setupChild(evalScript) {
|
|||||||
} catch {}
|
} catch {}
|
||||||
debug(`[${threadId}] fatal exception serialized = ${!!serialized}`);
|
debug(`[${threadId}] fatal exception serialized = ${!!serialized}`);
|
||||||
if (serialized)
|
if (serialized)
|
||||||
port.postMessage({ type: 'errorMessage', error: serialized });
|
port.postMessage({
|
||||||
|
type: messageTypes.ERROR_MESSAGE,
|
||||||
|
error: serialized
|
||||||
|
});
|
||||||
else
|
else
|
||||||
port.postMessage({ type: 'couldNotSerializeError' });
|
port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
|
||||||
clearAsyncIdStack();
|
clearAsyncIdStack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user