Merge pull request #38 from embark-framework/feat/move-web3-connection

add connect to blockchain and make dapp call it
This commit is contained in:
Iuri Matias 2019-01-25 17:39:11 -05:00 committed by GitHub
commit 90a22a1d25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 2 deletions

View File

@ -1,9 +1,51 @@
/*global ethereum*/ /*global ethereum*/
import {reduce} from './async'; import {reduce} from './async';
let Blockchain = {}; let Blockchain = {
list: [],
done: false,
err: null
};
let contracts = []; let contracts = [];
Blockchain.connect = function({dappConnection, dappAutoEnable = true, warnAboutMetamask, blockchainClient = ''}, cb = () => {}) {
return new Promise((resolve, reject) => {
this.whenEnvIsLoaded(() => {
this.doFirst((done) => {
this.autoEnable = dappAutoEnable;
this.doConnect(dappConnection, {
warnAboutMetamask: warnAboutMetamask,
blockchainClient: blockchainClient
}, (err) => {
cb(err);
done(err);
if (err) {
return reject(err);
}
resolve(err);
});
});
});
});
};
Blockchain.doFirst = function(todo) {
todo((err) => {
this.done = true;
this.err = err;
this.list.map((x) => x.apply(x, [self.err]));
});
};
Blockchain.whenEnvIsLoaded = function(cb) {
if (typeof document !== 'undefined' && document !== null && !(/comp|inter|loaded/).test(document.readyState)) {
document.addEventListener('DOMContentLoaded', cb);
} else {
cb();
}
};
Blockchain.Providers = {}; Blockchain.Providers = {};
Blockchain.registerProvider = function(providerName, obj) { Blockchain.registerProvider = function(providerName, obj) {
@ -24,7 +66,7 @@ Blockchain.setProvider = function(providerName, options) {
provider.init(options); provider.init(options);
}; };
Blockchain.connect = function(connectionList, opts, doneCb) { Blockchain.doConnect = function(connectionList, opts, doneCb) {
const self = this; const self = this;
const checkConnect = (next) => { const checkConnect = (next) => {