feat(@embark/embarkjs): introduce Blockchain.isAvailable() API

This is in order to be consistent with existing `isAvailable()` APIs on
the `EmbarkJS` module.

Another reason this API is needed, is because we need a way to fire
`EmbarkJS.onReady()` even if no blockchain is available. Prior to this
commit, `EmbarkJS.onReady()` was purely relying on an API on `EmbarkJS.Blockchain`
that would break if the blockchain feature was disabled, causing
`EmbarkJS.onReady()` to never fire.
This commit is contained in:
Pascal Precht 2019-11-27 15:34:23 +01:00
parent 70115a20e0
commit fdf0ef4f5f
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 18 additions and 2 deletions

View File

@ -27,13 +27,16 @@ class App extends React.Component {
componentDidMount() { componentDidMount() {
EmbarkJS.onReady((err) => { EmbarkJS.onReady((err) => {
this.setState({blockchainEnabled: true});
if (err) { if (err) {
// If err is not null then it means something went wrong connecting to ethereum // If err is not null then it means something went wrong connecting to ethereum
// you can use this to ask the user to enable metamask for e.g // you can use this to ask the user to enable metamask for e.g
return this.setState({error: err.message || err}); return this.setState({error: err.message || err});
} }
EmbarkJS.Blockchain.isAvailable().then(result => {
this.setState({blockchainEnabled: result});
});
EmbarkJS.Messages.isAvailable().then(result => { EmbarkJS.Messages.isAvailable().then(result => {
this.setState({whisperEnabled: result}); this.setState({whisperEnabled: result});
}); });

View File

@ -89,6 +89,13 @@ Blockchain.setProvider = function(providerName, options) {
provider.init(options); provider.init(options);
}; };
Blockchain.isAvailable = function () {
if (!this.blockchainConnector) {
return Promise.resolve(false);
}
return this.blockchainConnector.getNetworkId().then(_ => true);
};
Blockchain.doConnect = function(connectionList, opts, doneCb) { Blockchain.doConnect = function(connectionList, opts, doneCb) {
const self = this; const self = this;

View File

@ -6,7 +6,13 @@ import Utils from './utils';
var EmbarkJS = { var EmbarkJS = {
onReady: function (cb) { onReady: function (cb) {
Blockchain.execWhenReady(cb); Blockchain.isAvailable()
.then(available => {
if (available) {
return Blockchain.execWhenReady(cb);
}
cb();
});
}, },
enableEthereum: function () { enableEthereum: function () {
return Blockchain.enableEthereum(); return Blockchain.enableEthereum();