reducer
This commit is contained in:
parent
4926f74986
commit
2204cc177c
|
@ -12,6 +12,9 @@ import Platform from '@overflow/commons/platform';
|
||||||
import AppContext from '@overflow/commons/context';
|
import AppContext from '@overflow/commons/context';
|
||||||
import * as AppContextLifecycleActions from '@overflow/commons/context/redux/action/lifecycle';
|
import * as AppContextLifecycleActions from '@overflow/commons/context/redux/action/lifecycle';
|
||||||
import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC';
|
import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC';
|
||||||
|
import ReducerManager from '@overflow/commons/redux/ReducerManager';
|
||||||
|
|
||||||
|
|
||||||
import { store, sagaMiddleware, history } from './redux/store';
|
import { store, sagaMiddleware, history } from './redux/store';
|
||||||
|
|
||||||
import appConfig, { Config } from './config';
|
import appConfig, { Config } from './config';
|
||||||
|
@ -33,12 +36,14 @@ class Application {
|
||||||
private store: Store<any>;
|
private store: Store<any>;
|
||||||
private sagaMiddleware: SagaMiddleware<any>;
|
private sagaMiddleware: SagaMiddleware<any>;
|
||||||
private history: History;
|
private history: History;
|
||||||
|
private reducerManager: ReducerManager;
|
||||||
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
this.config = appConfig;
|
this.config = appConfig;
|
||||||
this.sagaMiddleware = sagaMiddleware;
|
this.sagaMiddleware = sagaMiddleware;
|
||||||
this.store = store;
|
this.store = store;
|
||||||
this.history = history;
|
this.history = history;
|
||||||
|
this.reducerManager = new ReducerManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Run(): void {
|
public static Run(): void {
|
||||||
|
@ -89,16 +94,15 @@ class Application {
|
||||||
}
|
}
|
||||||
|
|
||||||
private initRedux(): Promise<void> {
|
private initRedux(): Promise<void> {
|
||||||
|
const init = new Promise<void>(resolve => {
|
||||||
// state tree
|
// state tree
|
||||||
// reducer
|
// reducer
|
||||||
// middleware
|
// middleware
|
||||||
// saga
|
// saga
|
||||||
const rpcClient = new Promise<void>(resolve => {
|
|
||||||
this.sagaMiddleware.run(sagas);
|
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return rpcClient;
|
return init;
|
||||||
}
|
}
|
||||||
|
|
||||||
private initSagaEffect(): Promise<void> {
|
private initSagaEffect(): Promise<void> {
|
||||||
|
@ -158,9 +162,7 @@ class Application {
|
||||||
}
|
}
|
||||||
private displayDebugApp(): void {
|
private displayDebugApp(): void {
|
||||||
if (module.hot) {
|
if (module.hot) {
|
||||||
// app
|
|
||||||
module.hot.accept('./views/App', async () => {
|
module.hot.accept('./views/App', async () => {
|
||||||
// const NextApp = require('./app').App;
|
|
||||||
const NextApp = (await import('./views/App')).default;
|
const NextApp = (await import('./views/App')).default;
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<AppContainer>
|
<AppContainer>
|
||||||
|
|
|
@ -19,8 +19,6 @@ import {
|
||||||
composeWithDevTools,
|
composeWithDevTools,
|
||||||
} from 'redux-devtools-extension/logOnlyInProduction';
|
} from 'redux-devtools-extension/logOnlyInProduction';
|
||||||
|
|
||||||
import reduxLogger from 'redux-logger';
|
|
||||||
|
|
||||||
import reducer from './reducer';
|
import reducer from './reducer';
|
||||||
import State from './state';
|
import State from './state';
|
||||||
|
|
||||||
|
|
56
src/ts/@overflow/commons/redux/ReducerManager.ts
Normal file
56
src/ts/@overflow/commons/redux/ReducerManager.ts
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
import Action from './Action';
|
||||||
|
import * as Redux from 'redux';
|
||||||
|
|
||||||
|
interface ReducerItem<State> {
|
||||||
|
initialState: State;
|
||||||
|
reducer: Redux.Reducer<State>;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReducerManager {
|
||||||
|
private reducerMap: Map<string, ReducerItem<any>[]>;
|
||||||
|
|
||||||
|
public constructor() {
|
||||||
|
this.reducerMap = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
public putReducer<State>(type: string, initialState: State, reducer: Redux.Reducer<State>): void {
|
||||||
|
let reducerItems: ReducerItem<any>[];
|
||||||
|
if (!this.reducerMap.has(type)) {
|
||||||
|
reducerItems = new Array();
|
||||||
|
this.reducerMap.set(type, reducerItems);
|
||||||
|
} else {
|
||||||
|
reducerItems = this.reducerMap.get(type);
|
||||||
|
}
|
||||||
|
let reducerItem: ReducerItem<State> = {
|
||||||
|
initialState: initialState,
|
||||||
|
reducer: reducer,
|
||||||
|
};
|
||||||
|
|
||||||
|
reducerItems.push(reducerItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public putReducers<State>(initialState: State, handlers: Redux.ReducersMapObject): void {
|
||||||
|
for(let type in handlers) {
|
||||||
|
if (handlers.hasOwnProperty(type)) {
|
||||||
|
this.putReducer(type, initialState, handlers[type]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public reducer<State, A extends Action>(state: State, action: A): State {
|
||||||
|
const actionType = action.type;
|
||||||
|
if (this.reducerMap.has(actionType)) {
|
||||||
|
let reducerItems = this.reducerMap.get(actionType);
|
||||||
|
for (let reducerItem of reducerItems) {
|
||||||
|
let initialState = reducerItem.initialState;
|
||||||
|
let reducer = reducerItem.reducer;
|
||||||
|
state = reducer(state, action);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`Reducer for [${actionType}] is not loaded.`);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ReducerManager;
|
0
src/ts/@overflow/commons/redux/StateTreeManager.ts
Normal file
0
src/ts/@overflow/commons/redux/StateTreeManager.ts
Normal file
19
src/ts/@overflow/commons/redux/createReducer.ts
Normal file
19
src/ts/@overflow/commons/redux/createReducer.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import Action from './Action';
|
||||||
|
import * as Redux from 'redux';
|
||||||
|
|
||||||
|
// export type Reducer<S> = <A extends Action>(state: S, action: A) => S;
|
||||||
|
|
||||||
|
// export interface ReducersMapObject {
|
||||||
|
// [key: string]: Reducer<any>;
|
||||||
|
// }
|
||||||
|
|
||||||
|
const createReducer = <State>(initialState: State, handlers: Redux.ReducersMapObject): Redux.Reducer<State> => {
|
||||||
|
return <A extends Action>(state: State = initialState, action: A): State => {
|
||||||
|
if (handlers[action.type]) {
|
||||||
|
return handlers[action.type](state, action);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default createReducer;
|
|
@ -1,4 +1,5 @@
|
||||||
import Action from '@overflow/commons/redux/Action';
|
import Action from '@overflow/commons/redux/Action';
|
||||||
|
import createReducer from '@overflow/commons/redux/createReducer';
|
||||||
import Member from '@overflow/member/api/model/Member';
|
import Member from '@overflow/member/api/model/Member';
|
||||||
|
|
||||||
import * as SigninActionTypes from '../action/signIn';
|
import * as SigninActionTypes from '../action/signIn';
|
||||||
|
@ -6,24 +7,34 @@ import SigninState, { defaultState as signinDefaultState } from '../state/SignIn
|
||||||
|
|
||||||
export type reducer = (state: SigninState, action: Action<Member | Error>) => SigninState;
|
export type reducer = (state: SigninState, action: Action<Member | Error>) => SigninState;
|
||||||
|
|
||||||
export const reducer: reducer = (state: SigninState = signinDefaultState, action: Action<Member | Error>): SigninState => {
|
export const reducer: reducer = createReducer(signinDefaultState, {
|
||||||
switch (action.type) {
|
[SigninActionTypes.REQUEST_SUCCESS]: (state: SigninState, action: Action<Member>): SigninState => {
|
||||||
case SigninActionTypes.REQUEST_SUCCESS:
|
|
||||||
{
|
|
||||||
let member = (<Action<Member>>action).payload;
|
|
||||||
|
|
||||||
const aaa: SigninState = {
|
|
||||||
...state,
|
|
||||||
isAuthenticated: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
return aaa;
|
|
||||||
}
|
|
||||||
case SigninActionTypes.REQUEST_FAILURE:
|
|
||||||
{
|
|
||||||
return state;
|
return state;
|
||||||
}
|
},
|
||||||
default:
|
[SigninActionTypes.REQUEST_FAILURE]: (state: SigninState, action: Action<Error>): SigninState => {
|
||||||
return state;
|
return state;
|
||||||
}
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// export const reducer: reducer = (state: SigninState = signinDefaultState, action: Action<Member | Error>): SigninState => {
|
||||||
|
// switch (action.type) {
|
||||||
|
// case SigninActionTypes.REQUEST_SUCCESS:
|
||||||
|
// {
|
||||||
|
// let member = (<Action<Member>>action).payload;
|
||||||
|
|
||||||
|
// const aaa: SigninState = {
|
||||||
|
// ...state,
|
||||||
|
// isAuthenticated: true,
|
||||||
|
// };
|
||||||
|
|
||||||
|
// return aaa;
|
||||||
|
// }
|
||||||
|
// case SigninActionTypes.REQUEST_FAILURE:
|
||||||
|
// {
|
||||||
|
// return state;
|
||||||
|
// }
|
||||||
|
// default:
|
||||||
|
// return state;
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
Loading…
Reference in New Issue
Block a user