reorg subscribe to message method

This commit is contained in:
Jonathan Rainville 2018-05-16 10:19:46 -04:00
parent 1c125a019c
commit 953ce6659e
2 changed files with 50 additions and 43 deletions

View File

@ -74,7 +74,6 @@ class Pipeline {
logger: self.logger, logger: self.logger,
normalizeInput: self.normalizeInput normalizeInput: self.normalizeInput
}); });
console.log('Starting process');
webpackProcess.send({action: constants.pipeline.init, options: {}}); webpackProcess.send({action: constants.pipeline.init, options: {}});
webpackProcess.send({action: constants.pipeline.build, file, importsList}); webpackProcess.send({action: constants.pipeline.build, file, importsList});

View File

@ -14,60 +14,68 @@ class ProcessLauncher {
_subscribeToMessages() { _subscribeToMessages() {
const self = this; const self = this;
this.process.on('message', (msg) => { this.process.on('message', (msg) => {
console.log('Received message', msg);
if (msg.result === constants.process.log) { if (msg.result === constants.process.log) {
if (self.logger[msg.type]) { return self._handleLog(msg);
return self.logger[msg.type](self.normalizeInput(msg.message));
}
self.logger.debug(self.normalizeInput(msg.message));
return;
} }
if (msg.event) {
const messageKeys = Object.keys(msg); return this._handleEvent(msg);
const subscriptionsKeys = Object.keys(self.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 = self.subscriptions[subscriptionKey];
messageKey = _messageKey;
return true;
}
return false;
});
});
if (subscriptions) {
console.log('Found the 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;
}
return false;
});
if (subscription) {
console.log('Found the sub and calling');
// We are subscribed to that message, call the callback
subscription.callback(msg);
}
} }
this._checkSubscriptions(msg);
}); });
} }
_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);
}
_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;
});
});
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;
}
return false;
});
if (subscription) {
// We are subscribed to that message, call the callback
subscription.callback(msg);
}
}
}
subscribeTo(key, value, callback) { subscribeTo(key, value, callback) {
console.log('Subscribe to ', key, value);
if (this.subscriptions[key]) { if (this.subscriptions[key]) {
this.subscriptions[key].push(value); this.subscriptions[key].push(value);
return; return;
} }
this.subscriptions[key] = [{value, callback}]; this.subscriptions[key] = [{value, callback}];
console.log('Subs', this.subscriptions);
} }
unsubscribeTo(key, value) { unsubscribeTo(key, value) {