@@ -27,7 +27,7 @@ class CodeGenerator {
2727 } ) ;
2828
2929 this . events . setCommandHandler ( 'abi-contracts-vanila' , function ( cb ) {
30- let vanillaContractsABI = self . generateContracts ( false ) ;
30+ let vanillaContractsABI = self . generateContracts ( false , true ) ;
3131 let contractsJSON = self . generateContractsJSON ( ) ;
3232
3333 cb ( vanillaContractsABI , contractsJSON ) ;
@@ -88,11 +88,19 @@ result += "\n";
8888 result += "\n })(0, memo);" ;
8989 result += "\n};" ;
9090
91- result += "\nvar __LoadManager = function() { this.list = []; this.done = false; }" ;
92- result += "\n__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }" ;
93- result += "\n__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }" ;
94- result += "\nthis.__loadManagerInstance = new __LoadManager();" ;
95- result += "\nthis.__mainContext = this;" ;
91+ let mainContext = "" ;
92+ if ( isDeployment ) {
93+ mainContext = "__mainContext." ;
94+ result += "\nvar __mainContext = __mainContext || this;" ;
95+ } else {
96+ result += "\nvar __mainContext = __mainContext || this;" ;
97+ mainContext = "__mainContext." ;
98+ }
99+
100+ result += "\n" + mainContext + "__LoadManager = function() { this.list = []; this.done = false; }" ;
101+ result += "\n" + mainContext + "__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }" ;
102+ result += "\n" + mainContext + "__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }" ;
103+ result += "\n" + mainContext + "__loadManagerInstance = new " + mainContext + "__LoadManager();" ;
96104
97105 result += "\nvar whenEnvIsLoaded = function(cb) {" ;
98106 result += "\n if (typeof document !== 'undefined' && document !== null) {" ;
@@ -111,13 +119,14 @@ result += "\n";
111119 result += plugin . generateProvider ( self ) + "\n" ;
112120 } ) ;
113121 } else {
114- result += "\nwhenEnvIsLoaded(__loadManagerInstance.doFirst(function(done) {\n" ;
122+ result += "\nwhenEnvIsLoaded(function(){" + mainContext + " __loadManagerInstance.doFirst(function(done) {\n";
115123
116124 result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }" ;
117125
118126 if ( isDeployment ) {
119127 let connection = "http://" + this . contractsConfig . deployment . host + ":" + this . contractsConfig . deployment . port ;
120128 result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));' ;
129+ result += '\n\tdone();' ;
121130 } else {
122131
123132 let connectionCode = "" ;
@@ -151,17 +160,24 @@ result += "\n";
151160 result += connectionCode ;
152161 }
153162
154- result += '\n}))' ;
163+ result += '\n})} )' ;
155164 }
156165
157166 return result ;
158167 }
159168
160- generateContracts ( useEmbarkJS ) {
169+ generateContracts ( useEmbarkJS , isDeployment ) {
161170 let self = this ;
162171 let result = "\n" ;
163172 let contractsPlugins ;
164173
174+ let mainContext = "" ;
175+ if ( isDeployment ) {
176+ mainContext = "__mainContext." ;
177+ } else {
178+ mainContext = "__mainContext." ;
179+ }
180+
165181 if ( self . blockchainConfig === { } || self . blockchainConfig . enabled === false ) {
166182 return "" ;
167183 }
@@ -192,11 +208,11 @@ result += "\n";
192208 //result += "\n }";
193209 //result += "\n }";
194210
195- result += "\n__loadManagerInstance .execWhenReady(function() {" ;
211+ result += "\n" + mainContext + "__loadManagerInstance .execWhenReady(function() {";
196212 result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }" ;
197213 if ( useEmbarkJS ) {
198214 let contractAddress = contract . deployedAddress ? ( "'" + contract . deployedAddress + "'" ) : "undefined" ;
199- result += "\n__mainContext. " + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract . code + "', gasEstimates: " + gasEstimates + "});" ;
215+ result += "\n" + mainContext + " " + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract . code + "', gasEstimates: " + gasEstimates + "});" ;
200216 } else {
201217 result += "\n" + className + "Abi = " + abi + ";" ;
202218 result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);" ;
@@ -269,7 +285,7 @@ result += "\n";
269285 let result = "" ;
270286
271287 result += this . generateProvider ( options . deployment ) ;
272- result += this . generateContracts ( options . useEmbarkJS ) ;
288+ result += this . generateContracts ( options . useEmbarkJS , options . deployment ) ;
273289 //result += this.generateStorageInitialization(options.useEmbarkJS);
274290 //result += this.generateCommunicationInitialization(options.useEmbarkJS);
275291
0 commit comments