diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 2b08c245..368c1480 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -203,51 +203,37 @@ class CodeGenerator { } generateStorageInitialization(useEmbarkJS) { - let self = this; + if (!useEmbarkJS || this.storageConfig === {}) return ""; + let result = "\n"; - - if (!useEmbarkJS || self.storageConfig === {}) return ""; - result += Templates.define_when_env_loaded(); - - let pluginsWithCode = this.plugins.getPluginsFor('initCode'); - if (pluginsWithCode.length > 0) { - for (let plugin of pluginsWithCode) { - let initCodes = plugin.embarkjs_init_code.storage || []; - for (let initCode of initCodes) { - let [block, shouldInit] = initCode; - if (shouldInit.call(plugin, self.storageConfig)) { - result += Templates.exec_when_env_loaded({block: block}); - } - } - } - } + result += this._getInitCode('storage', this.storageConfig); return result; } generateCommunicationInitialization(useEmbarkJS) { - let self = this; + if (!useEmbarkJS || this.communicationConfig === {}) return ""; + let result = "\n"; - - if (!useEmbarkJS || self.communicationConfig === {}) return ""; - - // TODO: don't repeat this twice; should have 'requirements' generator first result += Templates.define_when_env_loaded(); + result += this._getInitCode('communication', this.communicationConfig); + return result; + } + + _getInitCode(codeType, config) { + let result = ""; 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}); - } + for (let plugin of pluginsWithCode) { + let initCodes = plugin.embarkjs_init_code[codeType] || []; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, config)) { + result += Templates.exec_when_env_loaded({block: block}); } } } - return result; }