diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 383513b16..d8744afdc 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -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; } diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 788e99821..979fe3c88 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -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;