conflict in solcW

This commit is contained in:
Jonathan Rainville 2018-05-18 14:11:29 -04:00
parent 566bcc71ae
commit c5c00fc3b4
4 changed files with 74 additions and 16 deletions

View File

@ -24,7 +24,7 @@ class SolcW {
}); });
this.solcProcess.send({action: "init", options: {}}); this.solcProcess.send({action: "init", options: {}});
this.solcProcess.subscribeTo('result', 'loadedCompiler', () => { this.solcProcess.on('result', 'loadedCompiler', () => {
self.compilerLoaded = true; self.compilerLoaded = true;
done(); done();
}); });
@ -50,7 +50,7 @@ class SolcW {
compile(jsonObj, done) { compile(jsonObj, done) {
const self = this; const self = this;
this.solcProcess.subscribeTo('result', 'compilation', (msg) => { this.solcProcess.on('result', 'compilation', (msg) => {
self.solcProcess.unsubscribeTo('result', 'compilation'); self.solcProcess.unsubscribeTo('result', 'compilation');
done(JSON.parse(msg.output)); done(JSON.parse(msg.output));
}); });

View File

@ -80,7 +80,7 @@ class Pipeline {
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});
webpackProcess.subscribeTo('result', constants.pipeline.built, (msg) => { webpackProcess.on('result', constants.pipeline.built, (msg) => {
webpackProcess.disconnect(); webpackProcess.disconnect();
return next(msg.error); return next(msg.error);
}); });

View File

@ -70,13 +70,13 @@ class ProcessLauncher {
_checkSubscriptions(msg) { _checkSubscriptions(msg) {
const messageKeys = Object.keys(msg); const messageKeys = Object.keys(msg);
const subscriptionsKeys = Object.keys(this.subscriptions); const subscriptionsKeys = Object.keys(this.subscriptions);
let subscriptions; let subscriptionsForKey;
let messageKey; let messageKey;
// Find if the message contains a key that we are subscribed to // Find if the message contains a key that we are subscribed to
messageKeys.some(_messageKey => { messageKeys.some(_messageKey => {
return subscriptionsKeys.some(subscriptionKey => { return subscriptionsKeys.some(subscriptionKey => {
if (_messageKey === subscriptionKey) { if (_messageKey === subscriptionKey) {
subscriptions = this.subscriptions[subscriptionKey]; subscriptionsForKey = this.subscriptions[subscriptionKey];
messageKey = _messageKey; messageKey = _messageKey;
return true; return true;
} }
@ -84,20 +84,28 @@ class ProcessLauncher {
}); });
}); });
if (subscriptions) { if (subscriptionsForKey) {
let subscription;
// Find if we are subscribed to one of the values // Find if we are subscribed to one of the values
subscriptions.some(sub => { let subsIndex = [];
const subscriptionsForValue = subscriptionsForKey.filter((sub, index) => {
if (msg[messageKey] === sub.value) { if (msg[messageKey] === sub.value) {
subscription = sub; subsIndex.push(index);
return true; return true;
} }
return false; return false;
}); });
if (subscription) { if (subscriptionsForValue.length) {
// We are subscribed to that message, call the callback // We are subscribed to that message, call the callback
subscription.callback(msg); subscriptionsForValue.forEach((subscription, index) => {
subscription.callback(msg);
if (subscription.once) {
// Called only once, we can remove it
subscription = null;
this.subscriptions[messageKey].splice(subsIndex[index], 1);
}
});
} }
} }
} }
@ -109,7 +117,7 @@ class ProcessLauncher {
* @param {Function} callback callback(response) * @param {Function} callback callback(response)
* @return {void} * @return {void}
*/ */
subscribeTo(key, value, callback) { on(key, value, callback) {
if (this.subscriptions[key]) { if (this.subscriptions[key]) {
this.subscriptions[key].push({value, callback}); this.subscriptions[key].push({value, callback});
return; return;
@ -117,6 +125,22 @@ class ProcessLauncher {
this.subscriptions[key] = [{value, callback}]; this.subscriptions[key] = [{value, callback}];
} }
/**
* Same as .on, but only triggers once
* @param {String} key Message key to subscribe to
* @param {String} value Value that the above key must have for the callback to be called
* @param {Function} callback callback(response)
* @return {void}
*/
once(key, value, callback) {
const obj = {value, callback, once: true};
if (this.subscriptions[key]) {
this.subscriptions[key].push(obj);
return;
}
this.subscriptions[key] = [obj];
}
/** /**
* Unsubscribes from a previously subscribed key-value pair (or key if no value) * Unsubscribes from a previously subscribed key-value pair (or key if no value)
* @param {String} key Message key to unsubscribe * @param {String} key Message key to unsubscribe

View File

@ -14,14 +14,14 @@ describe('ProcessWrapper', () => {
}); });
}); });
describe('subscribeTo', () => { describe('on', () => {
beforeEach(() => { beforeEach(() => {
processLauncher.subscriptions = {}; processLauncher.subscriptions = {};
}); });
it('should create an array for the key value', function () { it('should create an array for the key value', function () {
processLauncher.subscribeTo('test', 'value', 'myCallback'); processLauncher.on('test', 'value', 'myCallback');
assert.deepEqual(processLauncher.subscriptions, { assert.deepEqual(processLauncher.subscriptions, {
"test": [ "test": [
{ {
@ -33,8 +33,8 @@ describe('ProcessWrapper', () => {
}); });
it('should add another value to the key', () => { it('should add another value to the key', () => {
processLauncher.subscribeTo('test', 'value', 'myCallback'); processLauncher.on('test', 'value', 'myCallback');
processLauncher.subscribeTo('test', 'value2', 'myCallback2'); processLauncher.on('test', 'value2', 'myCallback2');
assert.deepEqual(processLauncher.subscriptions, { assert.deepEqual(processLauncher.subscriptions, {
"test": [ "test": [
{ {
@ -135,5 +135,39 @@ describe('ProcessWrapper', () => {
processLauncher._checkSubscriptions({test: 'value'}); processLauncher._checkSubscriptions({test: 'value'});
assert.strictEqual(callback.callCount, 1); assert.strictEqual(callback.callCount, 1);
}); });
it('should call the callback and remove the sub', function () {
const callback = sinon.stub();
processLauncher.subscriptions = {
"test": [
{
"callback": callback,
"value": "value",
"once": true
}
]
};
processLauncher._checkSubscriptions({test: 'value'});
assert.strictEqual(callback.callCount, 1);
assert.deepEqual(processLauncher.subscriptions, {test: []});
});
it('should call the callback twice', function () {
const callback = sinon.stub();
processLauncher.subscriptions = {
"test": [
{
"callback": callback,
"value": "value"
},
{
"callback": callback,
"value": "value"
}
]
};
processLauncher._checkSubscriptions({test: 'value'});
assert.strictEqual(callback.callCount, 2);
});
}); });
}); });