UNPKG

@dapplets/dapplet-overlay-bridge

Version:
33 lines (32 loc) 1.48 kB
import React from 'react'; import { Bridge } from './Bridge'; function dappletState(WrappedComponent) { return class extends React.Component { constructor(props) { super(props); this.bridge = new Bridge(); this.changeSharedState = (newStateData, id) => this.bridge.changeSharedState(newStateData, id); this.state = { sharedState: {}, loading: true, }; } componentDidMount() { this.bridge.on('onOpen', (id) => this.setState({ id, loading: false })); this.bridge.on('getDefaultState', (defaultState) => this.setState({ defaultState, loading: false })); this.bridge.on('changeSharedState', (newState) => { this.setState({ sharedState: { ...this.state.sharedState, ...newState }, loading: false }); }); } componentWillUnmount() { this.bridge.off('onOpen'); this.bridge.off('getDefaultState'); this.bridge.off('changeSharedState'); } render() { return this.state.loading ? React.createElement(React.Fragment, null) : React.createElement(WrappedComponent, Object.assign({ sharedState: this.state.sharedState, id: this.state.id, defaultState: this.state.defaultState, changeSharedState: this.changeSharedState }, this.props)); } }; } export default dappletState;