UNPKG

hele-hbus

Version:
53 lines (49 loc) 1.51 kB
import { Component, Context, createElement } from 'hele'; import { Bus } from 'hbus'; const defaultBusName = 'hele-hbus-bus'; class Bus$1 extends Component { constructor(props, context) { super(props, context); this.bus = new Bus(props.processor, props.defaultState); this.name = props.name; } render() { return (createElement(Context, { value: { [this.name]: this.bus } }, this.props.children)); } } Bus$1.defaultProps = { name: defaultBusName }; class Subscription extends Component { constructor(props, context) { super(props, context); const bus = this.bus = context[props.bus], state = bus.getState(); this.state = 'prop' in props ? state[props.prop] : state; this.subscriber = this.update.bind(this); } onWillMount() { const { props, bus, subscriber } = this; if ('prop' in props) { bus.subscribeProp(props.prop, subscriber); } else { bus.subscribe(subscriber); } } render() { return this.props.children[0](this.state); } onWillUnmount() { const { props, bus, subscriber } = this; if ('prop' in props) { bus.unsubscribeProp(props.prop, subscriber); } else { bus.unsubscribe(subscriber); } } } Subscription.defaultProps = { bus: defaultBusName }; export { defaultBusName, Bus$1 as Bus, Subscription };