embark/lib/process/processLauncher.js

112 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-05-16 20:41:15 +00:00
const child_process = require('child_process');
const constants = require('../constants');
class ProcessLauncher {
constructor(options) {
this.process = child_process.fork(options.modulePath);
this.logger = options.logger;
this.normalizeInput = options.normalizeInput;
this.subscriptions = {};
this._subscribeToMessages();
}
_subscribeToMessages() {
const self = this;
this.process.on('message', (msg) => {
if (msg.result === constants.process.log) {
2018-05-16 14:19:46 +00:00
return self._handleLog(msg);
}
if (msg.event) {
return this._handleEvent(msg);
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
this._checkSubscriptions(msg);
});
}
2018-05-16 20:41:15 +00:00
2018-05-16 14:19:46 +00:00
_handleLog(msg) {
if (this.logger[msg.type]) {
return this.logger[msg.type](this.normalizeInput(msg.message));
}
this.logger.debug(this.normalizeInput(msg.message));
}
_handleEvent(msg) {
console.log(msg);
}
2018-05-16 20:41:15 +00:00
2018-05-16 14:19:46 +00:00
_checkSubscriptions(msg) {
const messageKeys = Object.keys(msg);
const subscriptionsKeys = Object.keys(this.subscriptions);
let subscriptions;
let messageKey;
// Find if the message contains a key that we are subscribed to
messageKeys.some(_messageKey => {
return subscriptionsKeys.some(subscriptionKey => {
if (_messageKey === subscriptionKey) {
subscriptions = this.subscriptions[subscriptionKey];
messageKey = _messageKey;
return true;
}
return false;
});
});
2018-05-16 20:41:15 +00:00
2018-05-16 14:19:46 +00:00
if (subscriptions) {
let subscription;
// Find if we are subscribed to one of the values
subscriptions.some(sub => {
if (msg[messageKey] === sub.value) {
subscription = sub;
return true;
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
return false;
});
if (subscription) {
// We are subscribed to that message, call the callback
subscription.callback(msg);
2018-05-16 20:41:15 +00:00
}
2018-05-16 14:19:46 +00:00
}
2018-05-16 20:41:15 +00:00
}
subscribeTo(key, value, callback) {
if (this.subscriptions[key]) {
this.subscriptions[key].push(value);
return;
}
this.subscriptions[key] = [{value, callback}];
}
unsubscribeTo(key, value) {
if (!value) {
this.subscriptions[key] = [];
}
if (this.subscriptions[key]) {
this.subscriptions[key].filter((val, index) => {
if (val.value === value) {
this.subscriptions[key] = this.subscriptions[key].splice(index, 1);
}
});
}
}
unsubsribeToAll() {
this.subscriptions = {};
}
send() {
this.process.send(...arguments);
}
disconnect() {
this.process.disconnect();
}
kill() {
this.process.kill(...arguments);
}
}
module.exports = ProcessLauncher;