overflow_app/src/ts/@overflow/app/index.tsx

197 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-06-27 13:00:55 +00:00
import * as React from 'react';
import * as ReactDOM from 'react-dom';
2017-07-21 07:08:04 +00:00
import { Provider, Store } from 'react-redux';
2017-06-27 13:00:55 +00:00
import { ConnectedRouter } from 'react-router-redux';
2017-07-20 09:46:42 +00:00
import createSagaMiddleware, { SagaMiddleware } from 'redux-saga';
2017-07-21 07:08:04 +00:00
import { History } from 'history';
2017-07-23 14:40:48 +00:00
import { AppContainer } from 'react-hot-loader';
2017-07-21 07:08:04 +00:00
import * as injectTapEventPlugin from 'react-tap-event-plugin';
2017-07-03 10:47:54 +00:00
2017-07-03 10:21:42 +00:00
import Platform from '@overflow/commons/platform';
2017-07-20 09:46:42 +00:00
import AppContext from '@overflow/commons/context';
import * as AppContextLifecycleActions from '@overflow/commons/context/redux/action/lifecycle';
import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC';
2017-07-24 10:58:45 +00:00
import ReducerManager from '@overflow/commons/redux/ReducerManager';
2017-07-03 10:21:42 +00:00
import { store, sagaMiddleware, history } from './redux/store';
2017-06-27 13:00:55 +00:00
2017-07-20 09:46:42 +00:00
import appConfig, { Config } from './config';
2017-07-03 10:21:42 +00:00
import sagas from './redux/saga';
2017-07-19 10:47:36 +00:00
// import routes from './router';
2017-07-21 05:57:12 +00:00
import App from './views/App';
2017-06-28 07:50:19 +00:00
2017-06-27 13:00:55 +00:00
injectTapEventPlugin();
2017-07-23 14:40:48 +00:00
const isProduction:boolean = process.env.NODE_ENV === 'production' ? true : false;
2017-07-20 09:46:42 +00:00
2017-07-21 05:57:12 +00:00
class Application {
2017-07-20 09:46:42 +00:00
private config: Config;
private container: HTMLElement;
private context: AppContext;
private rpcClient: WebSocketRPC;
private store: Store<any>;
private sagaMiddleware: SagaMiddleware<any>;
private history: History;
2017-07-24 10:58:45 +00:00
private reducerManager: ReducerManager;
2017-07-20 09:46:42 +00:00
public constructor() {
this.config = appConfig;
this.sagaMiddleware = sagaMiddleware;
this.store = store;
2017-07-21 05:57:12 +00:00
this.history = history;
2017-07-24 10:58:45 +00:00
this.reducerManager = new ReducerManager();
2017-07-20 09:46:42 +00:00
}
2017-07-23 14:40:48 +00:00
public static Run(): void {
2017-07-21 08:10:06 +00:00
let application = new Application();
2017-07-23 14:40:48 +00:00
application.start();
2017-07-21 08:10:06 +00:00
}
private async start(): Promise<void> {
2017-07-20 09:46:42 +00:00
try {
this.container = await Platform.getAppContainer(this.config.container.placeholderID);
this.displayLoading();
this.context = await this.initContext();
// this.rpcClient = await this.initRpcClient();
await this.initSagaEffect();
this.store.dispatch(AppContextLifecycleActions.initialized());
this.displayApp();
} catch (e) {
2017-07-21 05:57:12 +00:00
console.error(e);
2017-07-20 09:46:42 +00:00
this.displayError(e);
}
}
2017-07-21 08:10:06 +00:00
private initContext(): Promise<AppContext> {
2017-07-20 09:46:42 +00:00
const appContext = new Promise<AppContext>(resolve => {
const context = AppContext.getContext();
resolve(context);
});
return appContext;
}
2017-07-21 08:10:06 +00:00
private initRpcClient(): Promise<WebSocketRPC> {
2017-07-20 09:46:42 +00:00
const rpcClient = new Promise<WebSocketRPC>((resolve, reject) => {
let client = new WebSocketRPC(this.config.rpc.url);
client.initialize()
.then(() => {
resolve(client);
})
.catch((err: any) => {
reject(err);
});
});
return rpcClient;
}
2017-07-23 14:40:48 +00:00
private initRedux(): Promise<void> {
2017-07-24 10:58:45 +00:00
const init = new Promise<void>(resolve => {
// state tree
// reducer
// middleware
// saga
2017-07-20 09:46:42 +00:00
});
2017-07-24 10:58:45 +00:00
return init;
2017-07-20 09:46:42 +00:00
}
2017-07-23 14:40:48 +00:00
private initSagaEffect(): Promise<void> {
const rpcClient = new Promise<void>(resolve => {
this.sagaMiddleware.run(sagas);
resolve();
});
return rpcClient;
}
2017-07-20 09:46:42 +00:00
private displayLoading(): void {
ReactDOM.render(
<div style={{
width: '100vw',
height: '100vh',
backgroundColor: 'white',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}>
<h1>Loading...</h1>
</div>,
this.container,
);
}
private displayError(e: Error): void {
ReactDOM.render(
<div style={{
width: '100vw',
height: '100vh',
backgroundColor: 'white',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}>
<h1>{e.message}</h1>
</div>,
this.container,
);
}
private displayApp(): void {
2017-07-23 14:40:48 +00:00
isProduction ? this.displayProductionApp() : this.displayDebugApp();
}
private displayProductionApp(): void {
2017-07-20 09:46:42 +00:00
ReactDOM.render(
2017-07-21 07:08:04 +00:00
<Provider store={this.store}>
<ConnectedRouter history={this.history}>
<App />
</ConnectedRouter>
</Provider>,
2017-07-20 09:46:42 +00:00
this.container,
);
}
2017-07-23 14:40:48 +00:00
private displayDebugApp(): void {
if (module.hot) {
module.hot.accept('./views/App', async () => {
const NextApp = (await import('./views/App')).default;
ReactDOM.render(
<AppContainer>
<Provider store={this.store}>
<ConnectedRouter history={this.history}>
<NextApp />
</ConnectedRouter>
</Provider>
</AppContainer>
,
this.container,
);
});
}
ReactDOM.render(
<AppContainer>
<Provider store={this.store}>
<ConnectedRouter history={this.history}>
<App />
</ConnectedRouter>
</Provider>
</AppContainer>
,
this.container,
);
}
2017-07-20 09:46:42 +00:00
2017-06-21 11:24:44 +00:00
}
2017-07-21 08:10:06 +00:00
Application.Run();