Skip to content

Commit

Permalink
fix(@embark/embarkjs-whisper): Messages.isAvailable() should always r…
Browse files Browse the repository at this point in the history
…eturn a promise

`EmbarkJS.Messages.isAvailable()` in some cases return synchronously (when whisper isn't
set up), in other cases asynchronously. This actually breaks our demo application for
the following reason:

We check for Whisper's availability via:

```
EmbarkJS.Messages.Providers.whisper.getWhisperVersion((err, _version) => {
  if (err) {
    return console.log(err);
  }
  this.setState({whisperEnabled: true});
});
```

There's a couple of problems here:

- This code will break right away when whisper isn't available, resulting in an error:
  ```
  Cannot read property _requestManager of undefined
  ```

- The reason this error happens is because there's no `web3` object available inside
  our EmbarkJS.Messages code. Even though there **is** a web3 object, EmbarkJS.Messages
  doesn't know about this because it only sets it when its `setProvider()` API is called,
  which effectively doesn't happen at all when Whisper isn't enabled on the connected
  node

- While this could be fixed with a simple check on whether EmbarkJS.Messages' internal
  `web3` references is a thing, really what should be used in the demo is the `isAvailable()`
  API.

`isAvailable()` should always return a promise (similar to `EmbarkJS.Storage.isAvailable()`.

This commit ensures that `isAvailable()` always returns a promise and changes the demo
template to use `isAvailable()` over `getWhisperVersion()`.
  • Loading branch information
0x-r4bbit committed Jul 1, 2019
1 parent fc7eb0c commit 93ca3ad
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
7 changes: 2 additions & 5 deletions dapps/templates/demo/app/dapp.js
Expand Up @@ -35,11 +35,8 @@ class App extends React.Component {
return this.setState({error: err.message || err});
}

EmbarkJS.Messages.Providers.whisper.getWhisperVersion((err, _version) => {
if (err) {
return console.log(err);
}
this.setState({whisperEnabled: true});
EmbarkJS.Messages.isAvailable().then(result => {
this.setState({whisperEnabled: result});
});

EmbarkJS.Storage.isAvailable().then((result) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/embarkjs/src/messages.js
Expand Up @@ -21,7 +21,7 @@ Messages.setProvider = function (providerName, options) {

Messages.isAvailable = function () {
if (!this.currentMessages) {
return false;
return Promise.resolve(false);
}
return this.currentMessages.isAvailable();
};
Expand Down

0 comments on commit 93ca3ad

Please sign in to comment.