mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-06 08:25:19 +01:00
38 lines
939 B
JavaScript
38 lines
939 B
JavaScript
import React from 'react';
|
|
import context from 'cad/context';
|
|
|
|
export default function connect(streamProvider) {
|
|
return function (Component) {
|
|
return class Connected extends React.Component {
|
|
|
|
state = {hasError: false, streamProps: {}};
|
|
|
|
UNSAFE_componentWillMount() {
|
|
const stream = streamProvider(context.streams, this.props);
|
|
this.detacher = stream.attach(data => {
|
|
this.setState({
|
|
hasError: false,
|
|
streamProps: this.state.streamProps === data ? {...data} : data,
|
|
});
|
|
});
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
this.detacher();
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
return null;
|
|
}
|
|
return <Component {...this.state.streamProps}
|
|
{...this.props} />;
|
|
|
|
}
|
|
|
|
componentDidCatch() {
|
|
this.setState({hasError: true});
|
|
}
|
|
};
|
|
};
|
|
}
|