fix: erc20 balance tracking and react unsubscribe

This commit is contained in:
Richard Ramos 2019-11-16 18:58:56 -04:00
parent 423c6860e7
commit ef3bb14c8d
2 changed files with 77 additions and 77 deletions

View File

@ -1,81 +1,81 @@
import React, {Component} from 'react';
import React, { Component } from "react";
import { isObservable } from "rxjs";
export function observe(WrappedComponent) {
return class extends Component {
state = {
observedValues: {},
subscriptions: {}
}
unsubscribe = prop => {
const subscriptions = {...this.state.subscriptions};
if(subscriptions[prop]) subscriptions[prop].unsubscribe();
delete subscriptions[prop];
this.setState({subscriptions});
}
subscribeToProp = prop => {
if(!isObservable(this.props[prop])) return;
const subscription = this.props[prop].subscribe(
value => {
this.setState(state => ({
observedValues: {
...state.observedValues,
[prop]: value
}
}));
},
err => {
// TODO: pass the error to the wrapped component
console.error(err);
}
);
this.setState({
subscriptions: {
...this.state.subscriptions,
[prop]: subscription
}
});
}
componentDidMount() {
Object.keys(this.props).forEach(this.subscribeToProp);
}
componentWillUnmount() {
this.state.subscriptions.forEach(subscription => {
subscription.unsubscribe();
});
}
componentDidUpdate(prevProps) {
Object.keys(prevProps).forEach(prop => {
if (!prevProps[prop] && this.props[prop]){
this.subscribeToProp(prop);
} else if(prevProps[prop] !== this.props[prop]){
this.unsubscribe(prop);
this.subscribeToProp(prop);
}
});
// TODO: check if prevProps and currProps are different, and unsubscribe from prevProp
}
render() {
const props = Object.keys(this.props).reduce((accum, curr) => {
if (!isObservable(this.props[curr])){
accum[curr] = this.props[curr];
return accum;
}
return accum;
}, {});
return React.createElement(WrappedComponent, {...props, ...this.state.observedValues});
}
return class extends Component {
state = {
observedValues: {},
subscriptions: {}
};
}
unsubscribe = prop => {
const subscriptions = { ...this.state.subscriptions };
if (subscriptions[prop]) subscriptions[prop].unsubscribe();
delete subscriptions[prop];
this.setState({ subscriptions });
};
subscribeToProp = prop => {
if (!isObservable(this.props[prop])) return;
const subscription = this.props[prop].subscribe(
value => {
this.setState(state => ({
observedValues: {
...state.observedValues,
[prop]: value
}
}));
},
err => {
// TODO: pass the error to the wrapped component
console.error(err);
}
);
this.setState({
subscriptions: {
...this.state.subscriptions,
[prop]: subscription
}
});
};
componentDidMount() {
Object.keys(this.props).forEach(this.subscribeToProp);
}
componentWillUnmount() {
Object.keys(this.state.subscriptions).forEach(subscription => {
this.unsubscribe(subscription);
});
}
componentDidUpdate(prevProps) {
Object.keys(prevProps).forEach(prop => {
if (!prevProps[prop] && this.props[prop]) {
this.subscribeToProp(prop);
} else if (prevProps[prop] !== this.props[prop]) {
this.unsubscribe(prop);
this.subscribeToProp(prop);
}
});
}
render() {
const props = Object.keys(this.props).reduce((accum, curr) => {
if (!isObservable(this.props[curr])) {
accum[curr] = this.props[curr];
return accum;
}
return accum;
}, {});
return React.createElement(WrappedComponent, {
...props,
...this.state.observedValues
});
}
};
}

View File

@ -237,7 +237,7 @@ export default class Subspace {
callFn = () => {
const fn = this.web3.call;
// balanceOf
const data = "0x70a08231" + "000000000000000000000000" + stripHexPrefix(erc20Address);
const data = "0x70a08231" + "000000000000000000000000" + stripHexPrefix(address);
fn.apply(fn, [{to: erc20Address, data}, (err, result) => {
if (err) {
sub.error(err);