fix loading order

This commit is contained in:
Iuri Matias 2017-10-07 16:51:56 -04:00
parent a9e63069fb
commit 3d88138804
1 changed files with 21 additions and 14 deletions

View File

@ -88,6 +88,12 @@ result += "\n";
result += "\n })(0, memo);";
result += "\n};";
result += "\nvar __LoadManager = function() { this.list = []; this.done = false; }";
result += "\n__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }";
result += "\n__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }";
result += "\nthis.__loadManagerInstance = new __LoadManager();";
result += "\nthis.__mainContext = this;";
result += "\nvar whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);";
@ -105,7 +111,7 @@ result += "\n";
result += plugin.generateProvider(self) + "\n";
});
} else {
result += "\nwhenEnvIsLoaded(function() {\n";
result += "\nwhenEnvIsLoaded(__loadManagerInstance.doFirst(function(done) {\n";
result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }";
@ -138,13 +144,14 @@ result += "\n";
connectionCode += "\nweb3.eth.getAccounts(function(err, account) { if(err) { next(null, true) } else { next(null, false) }})";
connectionCode += "\n}, function(err, _result) {";
connectionCode += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
connectionCode += '\ndone();';
connectionCode += "\n});";
result += connectionCode;
}
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
result += '\n})';
result += '\n}))';
}
return result;
@ -175,21 +182,21 @@ result += "\n";
let gasEstimates = JSON.stringify(contract.gasEstimates);
// TODO: refactor this
result += "\nif (whenEnvIsLoaded === undefined) {";
result += "\n var whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);";
result += "\n } else {";
result += "\n cb();";
result += "\n }";
result += "\n }";
result += "\n}";
//result += "\nif (whenEnvIsLoaded === undefined) {";
//result += "\n var whenEnvIsLoaded = function(cb) {";
//result += "\n if (typeof document !== 'undefined' && document !== null) {";
//result += "\n document.addEventListener('DOMContentLoaded', cb);";
//result += "\n } else {";
//result += "\n cb();";
//result += "\n }";
//result += "\n }";
//result += "\n}";
result += "\nwhenEnvIsLoaded(function() {";
result += "\n__loadManagerInstance.execWhenReady(function() {";
result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }";
if (useEmbarkJS) {
let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined";
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
result += "\n__mainContext." + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
} else {
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";