Files
scratch-gui/src/index.jsx
T
Ray Schamp e0a9f0ea76 Throttle actions coming in from the VM
These aren't necessary to update in realtime, so throttle them to once every 30ms
2017-03-02 10:39:31 -05:00

78 lines
2.3 KiB
React

const React = require('react');
const ReactDOM = require('react-dom');
const {Provider} = require('react-redux');
const {createStore, applyMiddleware} = require('redux');
const throttle = require('redux-throttle').default;
const GUI = require('./containers/gui.jsx');
const log = require('./lib/log');
const ProjectLoader = require('./lib/project-loader');
const reducer = require('./reducers/gui');
const styles = require('./index.css');
class App extends React.Component {
constructor (props) {
super(props);
this.fetchProjectId = this.fetchProjectId.bind(this);
this.updateProject = this.updateProject.bind(this);
this.state = {
projectId: null,
projectData: JSON.stringify(ProjectLoader.DEFAULT_PROJECT_DATA)
};
}
componentDidMount () {
window.addEventListener('hashchange', this.updateProject);
this.updateProject();
}
componentWillUnmount () {
window.removeEventListener('hashchange', this.updateProject);
}
fetchProjectId () {
return window.location.hash.substring(1);
}
updateProject () {
const projectId = this.fetchProjectId();
if (projectId !== this.state.projectId) {
if (projectId.length < 1) {
return this.setState({
projectId: projectId,
projectData: JSON.stringify(ProjectLoader.DEFAULT_PROJECT_DATA)
});
}
ProjectLoader.load(projectId, (err, body) => {
if (err) return log.error(err);
this.setState({projectData: body});
});
this.setState({projectId: projectId});
}
}
render () {
return (
<GUI
basePath={this.props.basePath}
projectData={this.state.projectData}
/>
);
}
}
App.propTypes = {
basePath: React.PropTypes.string
};
const appTarget = document.createElement('div');
appTarget.className = styles.app;
document.body.appendChild(appTarget);
const store = applyMiddleware(
throttle(300, {leading: true, trailing: true})
)(createStore)(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
ReactDOM.render((
<Provider store={store}>
<App basePath={process.env.BASE_PATH} />
</Provider>
), appTarget);