diff options
author | Weijia Wang <381152119@qq.com> | 2018-06-26 08:14:41 +0800 |
---|---|---|
committer | Weijia Wang <381152119@qq.com> | 2018-06-29 19:40:21 +0800 |
commit | ebf5b58bec6954ebeed1c302cf31ff55494a0c93 (patch) | |
tree | c4eb2b9b83cffa0597fe4bddc1699c4720aa0c68 /lib/internal/worker.js | |
parent | d6397afbac475316d50b1b147c89879f5f4dbd72 (diff) | |
download | android-node-v8-ebf5b58bec6954ebeed1c302cf31ff55494a0c93.tar.gz android-node-v8-ebf5b58bec6954ebeed1c302cf31ff55494a0c93.tar.bz2 android-node-v8-ebf5b58bec6954ebeed1c302cf31ff55494a0c93.zip |
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>
Diffstat (limited to 'lib/internal/worker.js')
-rw-r--r-- | lib/internal/worker.js | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/lib/internal/worker.js b/lib/internal/worker.js index df4f28cf74..bcc864b5b8 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -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 @@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable { } this[kPort].postMessage({ - type: 'stdioWantsMoreData', + type: messageTypes.STDIO_WANTS_MORE_DATA, stream: this[kName] }); } @@ -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 @@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable { _final(cb) { this[kPort].postMessage({ - type: 'stdioPayload', + type: messageTypes.STDIO_PAYLOAD, stream: this[kName], chunk: null }); @@ -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, @@ -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](); @@ -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'); @@ -408,7 +417,7 @@ 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 { @@ -416,11 +425,11 @@ function setupChild(evalScript) { 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; @@ -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(); } } |