register api calls when web3 ready

This commit is contained in:
Jonathan Rainville 2018-08-08 15:56:08 -04:00 committed by Iuri Matias
parent a1759a4ad7
commit 8271d49db5
1 changed files with 15 additions and 1 deletions

View File

@ -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() {
@ -32,6 +36,17 @@ class Whisper {
this.web3.setProvider(new Web3.providers.WebsocketProvider(web3Endpoint, {headers: {Origin: "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) {
@ -43,7 +58,6 @@ class Whisper {
if (err || version === "2") {
return cb({name: 'Whisper', status: 'off'});
} else {
self.registerAPICalls();
return cb({name: 'Whisper (version ' + version + ')', status: 'on'});
}
});