Skip to content

Commit

Permalink
Merge pull request #494 from ahslr/web-2.0
Browse files Browse the repository at this point in the history
Web 2.0
  • Loading branch information
ahslr authored Jan 25, 2021
2 parents 166547e + 464d526 commit db95a05
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion web/src/containers/Trade/components/OrderEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ const mapStateToProps = (state) => {
min_price,
increment_size,
increment_price,
} = state.app.pairs[pair];
} = state.app.pairs[pair] || { pair_base: '', pair_2: '' };
const marketPrice = marketPriceSelector(state);

return {
Expand Down
2 changes: 1 addition & 1 deletion web/src/containers/Trade/components/Orderbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ const mapStateToProps = (store) => {
app: { pair, pairs },
orderbook: { depth },
} = store;
const { increment_price } = pairs[pair];
const { increment_price } = pairs[pair] || { pair_base: '', pair_2: '' };

return {
pair,
Expand Down
47 changes: 26 additions & 21 deletions web/src/containers/Trade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ class Trade extends PureComponent {

componentWillMount() {
this.setSymbol(this.props.routeParams.pair);
this.initializeOrderbookWs(this.props.routeParams.pair, getToken());
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.routeParams.pair !== this.props.routeParams.pair) {
this.closeOrderbookSocket();
this.setSymbol(nextProps.routeParams.pair);
this.subscribe(nextProps.routeParams.pair);
this.unsubscribe(this.props.routeParams.pair);
}
}

Expand All @@ -73,9 +75,6 @@ class Trade extends PureComponent {
if (isLoggedIn()) {
this.props.getUserTrades(symbol);
}
if (!this.props.fetchingAuth) {
this.initializeOrderbookWs(symbol, getToken());
}
this.props.changePair(symbol);
this.setState({ symbol: '' }, () => {
setTimeout(() => {
Expand Down Expand Up @@ -203,12 +202,7 @@ class Trade extends PureComponent {
this.setState({ orderbookWs });

orderbookWs.onopen = (evt) => {
orderbookWs.send(
JSON.stringify({
op: 'subscribe',
args: [`orderbook:${symbol}`],
})
);
this.subscribe(symbol);

setWsHeartbeat(orderbookWs, JSON.stringify({ op: 'ping' }), {
pingTimeout: 60000,
Expand All @@ -217,7 +211,6 @@ class Trade extends PureComponent {
};

orderbookWs.onmessage = (evt) => {
this.setState({ orderbookSocketInitialized: true });
const data = JSON.parse(evt.data);
if (data.topic === 'orderbook')
switch (data.action) {
Expand All @@ -241,20 +234,32 @@ class Trade extends PureComponent {
};
};

subscribe = (pair) => {
const { orderbookWs } = this.state;
if (orderbookWs) {
orderbookWs.send(
JSON.stringify({
op: 'subscribe',
args: [`orderbook:${pair}`],
})
);
}
};

unsubscribe = (pair) => {
const { orderbookWs } = this.state;
if (orderbookWs) {
orderbookWs.send(
JSON.stringify({ op: 'unsubscribe', args: [`orderbook:${pair}`] })
);
}
};

closeOrderbookSocket = () => {
const {
routeParams: { pair },
} = this.props;
const { orderbookWs, orderbookSocketInitialized } = this.state;
const { orderbookWs } = this.state;
if (orderbookWs) {
if (orderbookSocketInitialized) {
orderbookWs.send(
JSON.stringify({ op: 'unsubscribe', args: [`orderbook:${pair}`] })
);
}
orderbookWs.close();
}
this.setState({ orderbookSocketInitialized: false });
};

render() {
Expand Down

0 comments on commit db95a05

Please sign in to comment.