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';
|
|
|
|
|
|
|
|
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-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-20 09:46:42 +00:00
|
|
|
const rpcURL = 'ws://127.0.0.1:18081/rpc';
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.config = appConfig;
|
|
|
|
this.sagaMiddleware = sagaMiddleware;
|
|
|
|
this.store = store;
|
2017-07-21 05:57:12 +00:00
|
|
|
this.history = history;
|
2017-07-20 09:46:42 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 08:10:06 +00:00
|
|
|
public static async Run(): Promise<void> {
|
|
|
|
let application = new Application();
|
|
|
|
return application.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 08:10:06 +00:00
|
|
|
private initSagaEffect(): Promise<void> {
|
2017-07-20 09:46:42 +00:00
|
|
|
const rpcClient = new Promise<void>(resolve => {
|
|
|
|
this.sagaMiddleware.run(sagas);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
return rpcClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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-06-21 11:24:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 08:10:06 +00:00
|
|
|
Application.Run();
|