add connect to blockchain and make dapp call it

This commit is contained in:
Jonathan Rainville 2019-01-24 16:51:57 -05:00
parent d5ef560e3c
commit 519763453d
1 changed files with 44 additions and 2 deletions

View File

@ -1,9 +1,51 @@
/*global ethereum*/
import {reduce} from './async';
let Blockchain = {};
let Blockchain = {
list: [],
done: false,
err: null
};
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.registerProvider = function(providerName, obj) {
@ -24,7 +66,7 @@ Blockchain.setProvider = function(providerName, options) {
provider.init(options);
};
Blockchain.connect = function(connectionList, opts, doneCb) {
Blockchain.doConnect = function(connectionList, opts, doneCb) {
const self = this;
const checkConnect = (next) => {