register api calls when web3 ready

This commit is contained in:
Jonathan Rainville 2018-08-08 15:56:08 -04:00 committed by Pascal Precht
parent 289ed2bda8
commit 2d1e081c15
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
1 changed files with 15 additions and 0 deletions

View File

@ -15,6 +15,7 @@ class Whisper {
this.communicationConfig = embark.config.communicationConfig; this.communicationConfig = embark.config.communicationConfig;
this.web3 = new Web3(); this.web3 = new Web3();
this.embark = embark; this.embark = embark;
this.web3Ready = false;
if (!this.communicationConfig.enabled) { if (!this.communicationConfig.enabled) {
return; return;
@ -24,6 +25,9 @@ class Whisper {
this.setServiceCheck(); this.setServiceCheck();
this.addWhisperToEmbarkJS(); this.addWhisperToEmbarkJS();
this.addSetProvider(); this.addSetProvider();
this.waitForWeb3Ready(() => {
this.registerAPICalls();
});
} }
connectToProvider() { connectToProvider() {
@ -37,6 +41,17 @@ class Whisper {
this.web3.setProvider(new Web3.providers.WebsocketProvider(web3Endpoint, {headers: {Origin: "http://embark"}})); 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() { setServiceCheck() {
const self = this; const self = this;
self.events.request("services:register", 'Whisper', function(cb) { self.events.request("services:register", 'Whisper', function(cb) {