This commit is contained in:
crusader 2017-12-20 23:54:26 +09:00
parent fec88d5f8e
commit 6b9ff2db2d
10 changed files with 85 additions and 43 deletions

View File

@ -14,6 +14,7 @@ export class Decorator {
return (...handlerArgs: any[]) => { return (...handlerArgs: any[]) => {
let annotation: Annotation = new AnnotationClass(...handlerArgs); let annotation: Annotation = new AnnotationClass(...handlerArgs);
let name: string = AnnotationClass.name;
return (...decoratorArgs: any[]) => { return (...decoratorArgs: any[]) => {
let decoratorType: Constants.DecoratorType = Decorator._detectDecoratorType(name, annotation, decoratorArgs); let decoratorType: Constants.DecoratorType = Decorator._detectDecoratorType(name, annotation, decoratorArgs);
@ -46,23 +47,23 @@ export class Decorator {
let decoratorType: Constants.DecoratorType; let decoratorType: Constants.DecoratorType;
if (1 === paramCount) { if (1 === paramCount) {
if (Constants.ANNOTATION_HANDLER_CLASS in annotation) { if (typeof(annotation.classDecorator) === 'undefined') {
throw new Error(`Cannot apply @${name} decorator on Class.`); throw new Error(`Cannot apply @${name} decorator on Class.`);
} }
decoratorType = Constants.DecoratorType.CLASS; decoratorType = Constants.DecoratorType.CLASS;
} else if (2 === paramCount) { } else if (2 === paramCount) {
if (Constants.ANNOTATION_HANDLER_PROPERTY in annotation) { if (typeof(annotation.propertyDecorator) === 'undefined') {
throw new Error(`Cannot apply @${name} decorator on Property.`); throw new Error(`Cannot apply @${name} decorator on Property.`);
} }
decoratorType = Constants.DecoratorType.PROPERTY; decoratorType = Constants.DecoratorType.PROPERTY;
} else if (3 === paramCount) { } else if (3 === paramCount) {
if(typeof args[2] === 'number') { if(typeof args[2] === 'number') {
if (Constants.ANNOTATION_HANDLER_PARAMETER in annotation) { if (typeof(annotation.parameterDecorator) === 'undefined') {
throw new Error(`Cannot apply @${name} decorator on Parameter.`); throw new Error(`Cannot apply @${name} decorator on Parameter.`);
} }
decoratorType = Constants.DecoratorType.PARAMETER; decoratorType = Constants.DecoratorType.PARAMETER;
} else { } else {
if (Constants.ANNOTATION_HANDLER_METHOD in annotation) { if (typeof(annotation.methodDecorator) === 'undefined') {
throw new Error(`Cannot apply @${name} decorator on Method.`); throw new Error(`Cannot apply @${name} decorator on Method.`);
} }
decoratorType = Constants.DecoratorType.METHOD; decoratorType = Constants.DecoratorType.METHOD;

View File

@ -0,0 +1,3 @@
export * from './reducer';
export * from './rest_api';
export * from './rpc_api';

View File

@ -30,5 +30,3 @@ export class ReducerAnnotation extends Annotation {
} }
export const Reducer = Decorator.create(ReducerAnnotation); export const Reducer = Decorator.create(ReducerAnnotation);
export default Reducer;

View File

@ -30,5 +30,3 @@ export class RestAPIAnnotation extends Annotation {
} }
export const RestAPI = Decorator.create(RestAPIAnnotation); export const RestAPI = Decorator.create(RestAPIAnnotation);
export default RestAPI;

View File

@ -30,5 +30,3 @@ export class RpcAPIAnnotation extends Annotation {
} }
export const RpcAPI = Decorator.create(RpcAPIAnnotation); export const RpcAPI = Decorator.create(RpcAPIAnnotation);
export default RpcAPI;

View File

@ -2,33 +2,33 @@ import {
Action, Action,
} from './action'; } from './action';
class LPCReducer { export default class LPCReducer {
private reducerMap: Map<string, any[]>; private reducerMap: Map<string, any[]>;
private constructor() { protected constructor() {
this.reducerMap = new Map(); this.reducerMap = new Map();
} }
public reducer<State>(state: State, action: Action): State { public reducer<State>(state: State, action: Action): State {
let rm: string[] = action.type.split('.'); // let rm: string[] = action.type.split('.');
if (null == rm || 2 !== rm.length) { // if (null == rm || 2 !== rm.length) {
console.log(`Method[${action.type}] is not valid.`); // console.log(`Method[${action.type}] is not valid.`);
return null; // return null;
} // }
const reducerName: string = rm[0]; // const reducerName: string = rm[0];
const methodName: string = rm[1]; // const methodName: string = rm[1];
if (this.reducerMap.has(reducerName)) { // if (this.reducerMap.has(reducerName)) {
let reducers = this.reducerMap.get(reducerName); // let reducers = this.reducerMap.get(reducerName);
for (let reducer of reducers) { // for (let reducer of reducers) {
state = reducer(state, action); // state = reducer(state, action);
} // }
} // }
return null; return state;
} }
/** /**

View File

@ -1,8 +1,14 @@
import {
Reducer,
RestAPI,
} from '@overflow/commons/redux/decorators';
@Reducer('@overflow/modules/member/MemberReducer')
export default class MemberReducer { export default class MemberReducer {
/** /**
* signin * signin
*/ */
@RestAPI('/account/signin')
public signin(state: any, result: any, error: any): any { public signin(state: any, result: any, error: any): any {
return state; return state;

View File

@ -0,0 +1,31 @@
import MemberReducer from '@overflow/modules/member/reducer/member_reducer';
const reducers: any[] = [
MemberReducer,
];
const state: any = {
};
export interface ReduxConfig {
state: any;
reducers: any[];
}
const reduxConfig: ReduxConfig = {
state: state,
reducers: reducers,
};
// Configuration
export interface WebAppConfig {
redux: ReduxConfig;
}
const config: WebAppConfig = {
redux: reduxConfig,
};
export default config;

View File

@ -28,12 +28,17 @@ import {
AppContainer, AppContainer,
} from 'react-hot-loader'; } from 'react-hot-loader';
declare let module: { hot: any }; import config from './config';
import WebAppReducer from '@overflow/webapp/redux/webapp_reducer';
// declare let module: { hot: any };
declare global { declare global {
interface Window { interface Window {
devToolsExtension: () => any; devToolsExtension: () => any;
} }
const process: any;
const module: { hot: any };
} }
class WebAppApplication { class WebAppApplication {
@ -52,6 +57,9 @@ class WebAppApplication {
public async run(): Promise<void> { public async run(): Promise<void> {
try { try {
this.renderLoading(); this.renderLoading();
let reducer: WebAppReducer = WebAppReducer.getInstance();
this.store = createStore(reducer.reducer, config.redux.state);
this.renderApp(); this.renderApp();
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -107,7 +115,7 @@ class WebAppApplication {
private rederDevelopment(): void { private rederDevelopment(): void {
if (module.hot) { if (module.hot) {
module.hot.accept('./pages/webapp', async () => { module.hot.accept('./pages/webapp', async () => {
const NextApp = (await require('./pages/webapp')).default; const NextApp = (await import('./pages/webapp')).default;
ReactDOM.render( ReactDOM.render(
<AppContainer> <AppContainer>
<Provider store={this.store}> <Provider store={this.store}>
@ -120,8 +128,7 @@ class WebAppApplication {
this.container, this.container,
); );
}); });
} } else {
ReactDOM.render( ReactDOM.render(
<AppContainer> <AppContainer>
<Provider store={this.store}> <Provider store={this.store}>
@ -135,6 +142,8 @@ class WebAppApplication {
); );
} }
}
public static main(): void { public static main(): void {

View File

@ -1,8 +1,6 @@
import { import LPCReducer from '@overflow/commons/redux/lpc_reducer';
LPCReducer,
} from '@overflow/commons/redux/lpc_reducer';
class WebAppReducer extends LPCReducer { export default class WebAppReducer extends LPCReducer {
private static readonly instance: WebAppReducer = new WebAppReducer(); private static readonly instance: WebAppReducer = new WebAppReducer();
public static getInstance(): WebAppReducer { public static getInstance(): WebAppReducer {
return WebAppReducer.instance; return WebAppReducer.instance;