diff --git a/src/react/index.js b/src/react/index.js index d1fddb0..6860f2f 100644 --- a/src/react/index.js +++ b/src/react/index.js @@ -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 + }); + } + }; +} diff --git a/src/subspace.js b/src/subspace.js index d1afd78..9c43a38 100644 --- a/src/subspace.js +++ b/src/subspace.js @@ -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);