Skip to content

Commit 49706b4

Browse files
starkwangtargos
authored andcommitted
workers: replace message types string by constants
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>
1 parent eb6dcf2 commit 49706b4

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

‎lib/internal/worker.js‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ const kIncrementsPortRef = Symbol('kIncrementsPortRef');
4747

4848
constdebug=util.debuglog('worker');
4949

50+
constmessageTypes={
51+
UP_AND_RUNNING: 'upAndRunning',
52+
COULD_NOT_SERIALIZE_ERROR: 'couldNotSerializeError',
53+
ERROR_MESSAGE: 'errorMessage',
54+
STDIO_PAYLOAD: 'stdioPayload',
55+
STDIO_WANTS_MORE_DATA: 'stdioWantsMoreData',
56+
LOAD_SCRIPT: 'loadScript'
57+
};
58+
5059
// A communication channel consisting of a handle (that wraps around an
5160
// uv_async_t) which can receive information from other threads and emits
5261
// .onmessage events, and a function used for sending data to a MessagePort
@@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable {
158167
}
159168

160169
this[kPort].postMessage({
161-
type: 'stdioWantsMoreData',
170+
type: messageTypes.STDIO_WANTS_MORE_DATA,
162171
stream: this[kName]
163172
});
164173
}
@@ -174,7 +183,7 @@ class WritableWorkerStdio extends Writable {
174183

175184
_write(chunk,encoding,cb){
176185
this[kPort].postMessage({
177-
type: 'stdioPayload',
186+
type: messageTypes.STDIO_PAYLOAD,
178187
stream: this[kName],
179188
chunk,
180189
encoding
@@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable {
186195

187196
_final(cb){
188197
this[kPort].postMessage({
189-
type: 'stdioPayload',
198+
type: messageTypes.STDIO_PAYLOAD,
190199
stream: this[kName],
191200
chunk: null
192201
});
@@ -258,7 +267,7 @@ class Worker extends EventEmitter {
258267
this[kPublicPort].on('message',(message)=>this.emit('message',message));
259268
setupPortReferencing(this[kPublicPort],this,'message');
260269
this[kPort].postMessage({
261-
type: 'loadScript',
270+
type: messageTypes.LOAD_SCRIPT,
262271
filename,
263272
doEval: !!options.eval,
264273
workerData: options.workerData,
@@ -289,18 +298,18 @@ class Worker extends EventEmitter {
289298

290299
[kOnMessage](message){
291300
switch(message.type){
292-
case'upAndRunning':
301+
casemessageTypes.UP_AND_RUNNING:
293302
returnthis.emit('online');
294-
case'couldNotSerializeError':
303+
casemessageTypes.COULD_NOT_SERIALIZE_ERROR:
295304
returnthis[kOnCouldNotSerializeErr]();
296-
case'errorMessage':
305+
casemessageTypes.ERROR_MESSAGE:
297306
returnthis[kOnErrorMessage](message.error);
298-
case'stdioPayload':
307+
casemessageTypes.STDIO_PAYLOAD:
299308
{
300309
const{ stream, chunk, encoding }=message;
301310
returnthis[kParentSideStdio][stream].push(chunk,encoding);
302311
}
303-
case'stdioWantsMoreData':
312+
casemessageTypes.STDIO_WANTS_MORE_DATA:
304313
{
305314
const{ stream }=message;
306315
returnthis[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
@@ -396,7 +405,7 @@ function setupChild(evalScript) {
396405
constpublicWorker=require('worker_threads');
397406

398407
port.on('message',(message)=>{
399-
if(message.type==='loadScript'){
408+
if(message.type===messageTypes.LOAD_SCRIPT){
400409
const{ filename, doEval, workerData, publicPort, hasStdin }=message;
401410
publicWorker.parentPort=publicPort;
402411
setupPortReferencing(publicPort,publicPort,'message');
@@ -408,19 +417,19 @@ function setupChild(evalScript) {
408417
debug(`[${threadId}] starts worker script ${filename} `+
409418
`(eval = ${eval}) at cwd = ${process.cwd()}`);
410419
port.unref();
411-
port.postMessage({type: 'upAndRunning'});
420+
port.postMessage({type: messageTypes.UP_AND_RUNNING});
412421
if(doEval){
413422
evalScript('[worker eval]',filename);
414423
}else{
415424
process.argv[1]=filename;// script filename
416425
require('module').runMain();
417426
}
418427
return;
419-
}elseif(message.type==='stdioPayload'){
428+
}elseif(message.type===messageTypes.STDIO_PAYLOAD){
420429
const{ stream, chunk, encoding }=message;
421430
workerStdio[stream].push(chunk,encoding);
422431
return;
423-
}elseif(message.type==='stdioWantsMoreData'){
432+
}elseif(message.type===messageTypes.STDIO_WANTS_MORE_DATA){
424433
const{ stream }=message;
425434
workerStdio[stream][kStdioWantsMoreDataCallback]();
426435
return;
@@ -451,9 +460,12 @@ function setupChild(evalScript) {
451460
}catch{}
452461
debug(`[${threadId}] fatal exception serialized = ${!!serialized}`);
453462
if(serialized)
454-
port.postMessage({type: 'errorMessage',error: serialized});
463+
port.postMessage({
464+
type: messageTypes.ERROR_MESSAGE,
465+
error: serialized
466+
});
455467
else
456-
port.postMessage({type: 'couldNotSerializeError'});
468+
port.postMessage({type: messageTypes.COULD_NOT_SERIALIZE_ERROR});
457469
clearAsyncIdStack();
458470
}
459471
}

0 commit comments

Comments
 (0)