extract communication provider

This commit is contained in:
Iuri Matias 2017-12-28 18:10:43 -05:00
parent 9ca337eeb1
commit 8956992950
2 changed files with 32 additions and 13 deletions

View File

@ -213,7 +213,7 @@ class CodeGenerator {
let pluginsWithCode = this.plugins.getPluginsFor('initCode');
if (pluginsWithCode.length > 0) {
for (let plugin of pluginsWithCode) {
let initCodes = plugin.embarkjs_init_code.storage;
let initCodes = plugin.embarkjs_init_code.storage || [];
for (let initCode of initCodes) {
let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, self.storageConfig)) {
@ -235,20 +235,19 @@ class CodeGenerator {
// TODO: don't repeat this twice; should have 'requirements' generator first
result += Templates.define_when_env_loaded();
let block;
//if (self.communicationConfig.enabled === true && ['whisper', 'orbit'].indexOf(self.communicationConfig.provider) < 0) {
// //TODO: add logger; also make sure it would still work with a plugin provider
// self.logger.warn("unknown provider " + self.communicationConfig.provider);
//}
// TODO: refactor this
if (self.communicationConfig.enabled === true) {
if (self.communicationConfig.connection === undefined) {
block = "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
} else {
block = "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.connection.host + "', port: '" + self.communicationConfig.connection.port + "', type: '" + self.communicationConfig.connection.type + "'});";
let pluginsWithCode = this.plugins.getPluginsFor('initCode');
if (pluginsWithCode.length > 0) {
for (let plugin of pluginsWithCode) {
let initCodes = plugin.embarkjs_init_code.communication || [];
for (let initCode of initCodes) {
let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, self.communicationConfig)) {
result += Templates.exec_when_env_loaded({block: block});
}
}
}
result += Templates.exec_when_env_loaded({block: block});
}
return result;
}

View File

@ -13,6 +13,7 @@ class Whisper {
this.setServiceCheck();
this.addWhisperToEmbarkJS();
this.addSetProvider();
}
setServiceCheck() {
@ -29,6 +30,7 @@ class Whisper {
}
addWhisperToEmbarkJS() {
// TODO: make this a shouldAdd condition
if (this.communicationConfig === {}) {
return;
}
@ -42,6 +44,24 @@ class Whisper {
this.embark.addCodeToEmbarkJS(code);
}
addSetProvider() {
let connection = this.communicationConfig.connection || {};
// todo: make the add code a function as well
let config = JSON.stringify({
server: connection.host || 'localhost',
port: connection.port || '8546',
type: connection.type || 'ws'
});
let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");";
let shouldInit = (communicationConfig) => {
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
};
this.embark.addProviderInit('communication', code, shouldInit);
}
}
module.exports = Whisper;