Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
fix: wait for api websocket to open before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed May 11, 2019
1 parent 7872825 commit a2d2b57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import BxDialog from './BxDialog';
import BxDialogTransactions from './BxDialogTransactions';
import BxDialogWorldMap from './BxDialogWorldMap';
import BxAppBar from './BxAppBar';
import {sleep} from './sleep';

const history = createBrowserHistory();

Expand Down Expand Up @@ -571,7 +572,7 @@ class App extends Component {

let url = mkUrl(value, type);

let updateState = newVal => {
let updateState = async newVal => {
if (type === 'txns-by-prgid') {
let msg = JSON.stringify({
action: 'subscribe',
Expand All @@ -580,6 +581,10 @@ class App extends Component {
});

console.log('subscribe', msg);
while (self.ws.readyState !== 1 /*OPEN*/) {
console.log('Waiting for ws.readyState to be 1: ', self.ws.readyState);
await sleep(250);
}
self.ws.send(msg);

let txns = _(newVal)
Expand Down Expand Up @@ -608,12 +613,9 @@ class App extends Component {

axios
.get(url)
.then(response => {
updateState(response.data);
})
.then(response => updateState(response.data))
.catch((resp, err) => {
console.log('oops', resp, err);
history.goBack();
console.error('oops', resp, err);
});
};

Expand Down
6 changes: 6 additions & 0 deletions src/sleep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow

// zzz
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

0 comments on commit a2d2b57

Please sign in to comment.