diff --git a/lib/constants.json b/lib/constants.json index 362dbbc0..b99d1387 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -16,11 +16,18 @@ "contractFilesChanged": "contractFilesChanged", "contractConfigChanged": "contractConfigChanged" }, + "process": { + "log": "log", + "events": { + "on": "on", + "request": "request", + "response": "response" + } + }, "pipeline": { "init": "init", "build": "build", "initiated": "initiated", - "built": "built", - "log": "log" + "built": "built" } } diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 6b20a890..579dd4cc 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -79,7 +79,7 @@ class Pipeline { return next(msg.error); } - if (msg.result === constants.pipeline.log) { + if (msg.result === constants.process.log) { if (self.logger[msg.type]) { return self.logger[msg.type](self.normalizeInput(msg.message)); } diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 26d557e2..ca917d3c 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -4,7 +4,7 @@ const utils = require('../utils/utils'); const fs = require('../core/fs'); const constants = require('../constants'); const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); -const ProcessWrapper = require('../core/processWrapper'); +const ProcessWrapper = require('../process/processWrapper'); let webpackProcess; diff --git a/lib/process/eventsWrapper.js b/lib/process/eventsWrapper.js new file mode 100644 index 00000000..c4181490 --- /dev/null +++ b/lib/process/eventsWrapper.js @@ -0,0 +1,48 @@ +const constants = require('../constants'); + +class Events { + constructor() { + this.eventId = 0; + this.subscribedEvents = {}; + this.listenToParentProcess(); + } + + listenToParentProcess() { + process.on('message', (msg) => { + if (!msg.event || msg.event !== constants.process.events.response) { + return; + } + if (!this.subscribedEvents[msg.eventId]) { + return; + } + this.subscribedEvents[msg.eventId](msg.result); + }); + } + + sendEvent() { + const eventType = arguments[0]; + const requestName = arguments[1]; + + let args = [].slice.call(arguments, 2); + this.eventId++; + this.subscribedEvents[this.eventId] = args[args.length - 1]; + args = args.splice(0, args.length - 2); + + process.send({ + event: eventType, + requestName, + args, + eventId: this.eventId + }); + } + + on() { + this.sendEvent(constants.process.events.on, ...arguments); + } + + request() { + this.sendEvent(constants.process.events.request, ...arguments); + } +} + +module.exports = Events; diff --git a/lib/core/processWrapper.js b/lib/process/processWrapper.js similarity index 92% rename from lib/core/processWrapper.js rename to lib/process/processWrapper.js index e8c115f9..27691547 100644 --- a/lib/core/processWrapper.js +++ b/lib/process/processWrapper.js @@ -30,7 +30,7 @@ class ProcessWrapper { if (messages[0].indexOf('hard-source')) { return; } - process.send({result: constants.pipeline.log, message: messages, type}); + process.send({result: constants.process.log, message: messages, type}); } }