From 519763453dd826e2c1540c87160d6064e0b0466d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 24 Jan 2019 16:51:57 -0500 Subject: [PATCH] add connect to blockchain and make dapp call it --- src/blockchain.js | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/blockchain.js b/src/blockchain.js index 6b9db70..83377d4 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -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) => {