redoc/src/hmr-playground.tsx

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import * as React from 'react';
import { render } from 'react-dom';
import { AppContainer } from 'react-hot-loader';
2017-10-12 00:01:37 +03:00
// import DevTools from 'mobx-react-devtools';
import { Redoc } from './components/Redoc/Redoc';
2017-10-12 00:01:37 +03:00
import { AppStore } from './services/AppStore';
import { loadSpec } from './utils/loadSpec';
2017-10-12 00:01:37 +03:00
const renderRoot = (Component: typeof Redoc, props: { store: AppStore }) =>
2017-10-12 00:01:37 +03:00
render(
<div>
<AppContainer>
<Component {...props} />
</AppContainer>
</div>,
document.getElementById('example'),
);
const big = window.location.search.indexOf('big') > -1;
const specUrl = big ? 'big-swagger.json' : 'swagger.yaml';
let store;
async function init() {
const spec = await loadSpec(specUrl);
store = new AppStore(spec, specUrl);
renderRoot(Redoc, { store: store });
}
init();
2017-10-12 00:01:37 +03:00
if (module.hot) {
const reload = (reloadStore = false) => () => {
if (reloadStore) {
// create a new Store
store.dispose();
2017-10-12 00:01:37 +03:00
const state = store.toJS();
store = AppStore.fromJS(state);
2017-10-12 00:01:37 +03:00
}
renderRoot(Redoc, { store: store });
2017-10-12 00:01:37 +03:00
};
module.hot.accept(['./components/Redoc/Redoc'], reload());
2017-10-12 00:01:37 +03:00
module.hot.accept(['./services/AppStore'], reload(true));
}