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.subscribeTo('result', 'loadedCompiler', () => {
this.solcProcess.on('result', 'loadedCompiler', () => {
self.compilerLoaded = true;
done();
});
@ -50,7 +50,7 @@ class SolcW {
compile(jsonObj, done) {
const self = this;
this.solcProcess.subscribeTo('result', 'compilation', (msg) => {
this.solcProcess.on('result', 'compilation', (msg) => {
self.solcProcess.unsubscribeTo('result', 'compilation');
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.build, file, importsList});
webpackProcess.subscribeTo('result', constants.pipeline.built, (msg) => {
webpackProcess.on('result', constants.pipeline.built, (msg) => {
webpackProcess.disconnect();
return next(msg.error);
});

View File

@ -70,13 +70,13 @@ class ProcessLauncher {
_checkSubscriptions(msg) {
const messageKeys = Object.keys(msg);
const subscriptionsKeys = Object.keys(this.subscriptions);
let subscriptions;
let subscriptionsForKey;
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];
subscriptionsForKey = this.subscriptions[subscriptionKey];
messageKey = _messageKey;
return true;
}
@ -84,20 +84,28 @@ class ProcessLauncher {
});
});
if (subscriptions) {
let subscription;
if (subscriptionsForKey) {
// 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) {
subscription = sub;
subsIndex.push(index);
return true;
}
return false;
});
if (subscription) {
if (subscriptionsForValue.length) {
// We are subscribed to that message, call the callback
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)
* @return {void}
*/
subscribeTo(key, value, callback) {
on(key, value, callback) {
if (this.subscriptions[key]) {
this.subscriptions[key].push({value, callback});
return;
@ -117,6 +125,22 @@ class ProcessLauncher {
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)
* @param {String} key Message key to unsubscribe

View File

@ -14,14 +14,14 @@ describe('ProcessWrapper', () => {
});
});
describe('subscribeTo', () => {
describe('on', () => {
beforeEach(() => {
processLauncher.subscriptions = {};
});
it('should create an array for the key value', function () {
processLauncher.subscribeTo('test', 'value', 'myCallback');
processLauncher.on('test', 'value', 'myCallback');
assert.deepEqual(processLauncher.subscriptions, {
"test": [
{
@ -33,8 +33,8 @@ describe('ProcessWrapper', () => {
});
it('should add another value to the key', () => {
processLauncher.subscribeTo('test', 'value', 'myCallback');
processLauncher.subscribeTo('test', 'value2', 'myCallback2');
processLauncher.on('test', 'value', 'myCallback');
processLauncher.on('test', 'value2', 'myCallback2');
assert.deepEqual(processLauncher.subscriptions, {
"test": [
{
@ -135,5 +135,39 @@ describe('ProcessWrapper', () => {
processLauncher._checkSubscriptions({test: 'value'});
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);
});
});
});