extract to method duplicated code

This commit is contained in:
Iuri Matias 2017-12-29 16:29:32 -05:00
parent d1a207941d
commit b29e865cd6
1 changed files with 17 additions and 31 deletions

View File

@ -203,51 +203,37 @@ class CodeGenerator {
} }
generateStorageInitialization(useEmbarkJS) { generateStorageInitialization(useEmbarkJS) {
let self = this; if (!useEmbarkJS || this.storageConfig === {}) return "";
let result = "\n"; let result = "\n";
if (!useEmbarkJS || self.storageConfig === {}) return "";
result += Templates.define_when_env_loaded(); result += Templates.define_when_env_loaded();
result += this._getInitCode('storage', this.storageConfig);
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});
}
}
}
}
return result; return result;
} }
generateCommunicationInitialization(useEmbarkJS) { generateCommunicationInitialization(useEmbarkJS) {
let self = this; if (!useEmbarkJS || this.communicationConfig === {}) return "";
let result = "\n"; 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 += Templates.define_when_env_loaded();
result += this._getInitCode('communication', this.communicationConfig);
return result;
}
_getInitCode(codeType, config) {
let result = "";
let pluginsWithCode = this.plugins.getPluginsFor('initCode'); let pluginsWithCode = this.plugins.getPluginsFor('initCode');
if (pluginsWithCode.length > 0) {
for (let plugin of pluginsWithCode) { for (let plugin of pluginsWithCode) {
let initCodes = plugin.embarkjs_init_code.communication || []; let initCodes = plugin.embarkjs_init_code[codeType] || [];
for (let initCode of initCodes) { for (let initCode of initCodes) {
let [block, shouldInit] = initCode; let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, self.communicationConfig)) { if (shouldInit.call(plugin, config)) {
result += Templates.exec_when_env_loaded({block: block}); result += Templates.exec_when_env_loaded({block: block});
} }
} }
} }
}
return result; return result;
} }