Skip to content

Commit

Permalink
refactor: 使用flow检查代码
Browse files Browse the repository at this point in the history
  • Loading branch information
duan602728596 committed Feb 19, 2019
1 parent 3bed919 commit 939720a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
4 changes: 3 additions & 1 deletion app/src/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
// @flow
import * as React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
Expand All @@ -17,5 +18,6 @@ ReactDOM.render(
</HashRouter>
</LocaleProvider>
</Provider>,
// $FlowFixMe
document.getElementById('app')
);
16 changes: 9 additions & 7 deletions app/src/router/Routers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React, { Component } from 'react';
// @flow
import * as React from 'react';
import { Component } from 'react';
import { Route, Switch } from 'react-router-dom';
import asyncModule from './asyncModule';
/* 加载模块 */
import Index from '../modules/Index/Layout';

const LoginBundle: Function = asyncModule((): Promise => import('../modules/Login/Layout'));
const OptionBundle: Function = asyncModule((): Promise => import('../modules/Option/Layout'));
const KouDai48Bundle: Function = asyncModule((): Promise => import('../modules/KouDai48/Layout'));
const HelpBundle: Function = asyncModule((): Promise => import('../modules/Help/Layout'));
const LoginBundle: Function = asyncModule((): Promise<Function> => import('../modules/Login/Layout'));
const OptionBundle: Function = asyncModule((): Promise<Function> => import('../modules/Option/Layout'));
const KouDai48Bundle: Function = asyncModule((): Promise<Function> => import('../modules/KouDai48/Layout'));
const HelpBundle: Function = asyncModule((): Promise<Function> => import('../modules/Help/Layout'));

/* 路由模块 */
class Routers extends Component {
render(): React.Element {
class Routers extends Component<{}> {
render(): React.Node {
return (
<Switch>
{/* 首页 */}
Expand Down
15 changes: 10 additions & 5 deletions app/src/router/asyncModule.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* 异步加载模块 */
import React, { lazy, Suspense } from 'react';
/**
* 异步加载模块
*
* @flow
*/
import * as React from 'react';
import { lazy, Suspense } from 'react';
import { injectReducers } from '../store/store';
import SwitchLoading from '../assembly/SwitchLoading/index';

const Fallback: React.Element = <SwitchLoading />;
const Fallback: React.Node = <SwitchLoading />;

/**
* 异步加载、注入模块和reducer
Expand All @@ -12,11 +17,11 @@ const Fallback: React.Element = <SwitchLoading />;
function asyncModule(loader: Function): Function {
const Module: Function = lazy(loader);

return (): React.Element => (
return (): React.Node => (
<Suspense fallback={ Fallback }>
<Module injectReducers={ injectReducers } />
</Suspense>
);
}

export default asyncModule;
export default asyncModule;
17 changes: 11 additions & 6 deletions app/src/store/loadReducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* 异步注入reducer的修饰器 */
import React, { Component } from 'react';
/**
* 异步注入reducer的修饰器
*
* @flow
*/
import * as React from 'react';
import { Component } from 'react';
import PropTypes from 'prop-types';

/**
Expand All @@ -9,8 +14,8 @@ function loadReducer(reducer: Object): Function {
/**
* @param { Function } Module: 需要修饰的模块
*/
return function(Module: Function): void {
return class extends Component {
return function(Module: Function): Function {
return class extends Component<{ injectReducers: Function }> {
static propTypes: Object = {
injectReducers: PropTypes.func
};
Expand All @@ -19,13 +24,13 @@ function loadReducer(reducer: Object): Function {
super(...arguments);

// 异步注入reducer
const injectReducers: Function = this?.props?.injectReducers || null;
const injectReducers: ?Function = this?.props?.injectReducers || null;

if (injectReducers) {
injectReducers(reducer);
}
}
render(): React.Element {
render(): React.Node {
return <Module />;
}
};
Expand Down
3 changes: 2 additions & 1 deletion app/src/store/reducers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* reducers */
// @flow
import { combineReducers } from 'redux-immutable';

/* reducers */
const reducers: Object = {};

/* 创建reducer */
Expand Down
12 changes: 9 additions & 3 deletions app/src/store/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* 全局的store */
/**
* 全局的store
*
* @flow
*/
import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import * as Immutable from 'immutable';
import { fromJS, Map } from 'immutable';
import { createReducer } from './reducers';

Expand All @@ -15,9 +20,10 @@ const store: Object = {
asyncReducers: {}
};

export function storeFactory(initialState: ?Object): Object {
export function storeFactory(initialState: Object = {}): Object {
/* initialState */
const $$initialState: Immutable.Map = Map(fromJS(initialState));
const state: any = fromJS(initialState);
const $$initialState: Immutable.Map<string, any> = Map(state);

/* store */
Object.assign(store, createStore(reducer, $$initialState, compose(middlewares)));
Expand Down

0 comments on commit 939720a

Please sign in to comment.