/
App.tsx
123 lines (110 loc) · 5.08 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {ConnectedRouter, connectRouter, routerMiddleware} from "connected-react-router";
import {createBrowserHistory} from "history";
import React, {ComponentType} from "react";
import ReactDOM from "react-dom";
import {Provider} from "react-redux";
import {withRouter} from "react-router-dom";
import {applyMiddleware, createStore, Reducer, Store} from "redux";
import createSagaMiddleware from "redux-saga";
import ErrorBoundary from "../component/ErrorBoundary";
import {devtools} from "../util/devtools";
import {Model, ActionStore, setStateAction, saga, storeListener, rootReducer, setErrorAction} from "./redux";
import {getPrototypeOfExceptConstructor} from "../util/object";
import {AppView, StateView, ActionTypeView} from "./type";
console.time("[framework] initialized");
function createApp(): AppView {
const history = createBrowserHistory();
const actionStore = new ActionStore();
const sagaMiddleware = createSagaMiddleware();
const reducer: Reducer<StateView> = rootReducer(connectRouter(history));
const store: Store<StateView> = createStore(reducer, devtools(applyMiddleware(routerMiddleware(history), sagaMiddleware)));
store.subscribe(storeListener(store));
sagaMiddleware.run(saga, actionStore);
window.onerror = (message: string | Event, source?: string, line?: number, column?: number, error?: Error): void => {
console.error("Window Global Error");
console.error(`Message: ${message.toString()}`);
if (error) {
console.error(error);
}
if (source && line && column) {
console.error(`Source: ${source} (${line}, ${column})`);
}
if (!error) {
error = new Error(message.toString());
}
store.dispatch(setErrorAction(error));
};
return {history, store, sagaMiddleware, actionStore, modules: {}};
}
const app = createApp();
export function render(Component: ComponentType<any>, onInitialized: null | (() => void) = null): void {
const rootElement: HTMLDivElement = document.createElement("div");
rootElement.id = "framework-app-root";
document.body.appendChild(rootElement);
const WithRouterComponent = withRouter(Component);
ReactDOM.render(
<Provider store={app.store}>
<ErrorBoundary>
<ConnectedRouter history={app.history}>
<WithRouterComponent />
</ConnectedRouter>
</ErrorBoundary>
</Provider>,
rootElement,
() => {
console.timeEnd("[framework] initialized");
if (typeof onInitialized === "function") {
onInitialized();
}
}
);
}
export function register<H extends Model<any>>(handler: H, Component: ComponentType<any>): any {
// Each module needs to load.
if (app.modules.hasOwnProperty(handler.module)) {
throw new Error(`module is already registered, module=${handler.module}`);
}
app.modules[handler.module] = 1;
// 存储初始化 State 到 redux
app.store.dispatch(setStateAction(handler.module, (handler as any) /* "as any" to get private-readonly initialState */.initialState, `@@${handler.module}/initState`));
// 1.存储方法名与参数、2.存储方法对应逻辑
const moduleName = handler.module;
const keys = getPrototypeOfExceptConstructor(handler);
keys.forEach(actionType => {
const method = handler[actionType];
const qualifiedActionType = `${moduleName}/${actionType}`;
app.actionStore.actionType[actionType] = (...payload: any[]): ActionTypeView<any[]> => ({type: qualifiedActionType, payload});
app.actionStore.actionHandler[qualifiedActionType] = method.bind(handler);
});
class ProxyComponent<P extends {} = {}> extends React.PureComponent<P> {
constructor(props: P) {
super(props);
if (app.actionStore.actionType.onReady) {
app.store.dispatch(app.actionStore.actionType.onReady());
}
}
componentDidMount() {
if (app.actionStore.actionType.onLoad) {
app.store.dispatch(app.actionStore.actionType.onLoad());
}
}
componentDidUpdate(prevProps: Readonly<P>) {
const prevLocation = (prevProps as any).location;
const currentLocation = (this.props as any).location;
const currentRouteParams = (this.props as any).match ? (this.props as any).match.params : null;
if (currentLocation && currentRouteParams && prevLocation !== currentLocation && app.actionStore.actionType.onLoad) {
// Only trigger if current component is connected to <Route> and call Handler's setHistory
app.store.dispatch(app.actionStore.actionType.onLoad());
}
}
componentWillUnmount() {
if (app.actionStore.actionType.onUnload) {
app.store.dispatch(app.actionStore.actionType.onUnload());
}
}
render() {
return <Component {...this.props} />;
}
}
return {View: ProxyComponent, Controller: app.actionStore.actionType};
}