From 2d1e081c15c51fccd66ec9e9e975fa1187e4987b Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 8 Aug 2018 15:56:08 -0400 Subject: [PATCH] register api calls when web3 ready --- lib/modules/whisper/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index b1879807..394b6cee 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -15,6 +15,7 @@ class Whisper { this.communicationConfig = embark.config.communicationConfig; this.web3 = new Web3(); this.embark = embark; + this.web3Ready = false; if (!this.communicationConfig.enabled) { return; @@ -24,6 +25,9 @@ class Whisper { this.setServiceCheck(); this.addWhisperToEmbarkJS(); this.addSetProvider(); + this.waitForWeb3Ready(() => { + this.registerAPICalls(); + }); } connectToProvider() { @@ -37,6 +41,17 @@ class Whisper { this.web3.setProvider(new Web3.providers.WebsocketProvider(web3Endpoint, {headers: {Origin: "http://embark"}})); } + waitForWeb3Ready(cb) { + if (this.web3Ready) { + return cb(); + } + if (this.web3.currentProvider.connection.readyState !== 1) { + return setTimeout(this.waitForWeb3Ready.bind(this, cb), 50); + } + this.web3Ready = true; + cb(); + } + setServiceCheck() { const self = this; self.events.request("services:register", 'Whisper', function(cb) {