This commit is contained in:
crusader 2017-07-25 19:28:27 +09:00
parent 96f20d5784
commit f62e9216bd
15 changed files with 94 additions and 62 deletions

View File

@ -1,5 +1,8 @@
import { ReducersMapObject } from 'redux';
import { SagaWatcher } from '@overflow/commons/redux-saga';
import signInReducer from '@overflow/member/redux/reducer/signIn';
import signInSagaWatchers from '@overflow/member/redux/saga/signIn';
// Container Configuration
export interface ContainerConfig {
@ -21,17 +24,23 @@ const rpcConfig: RPCConfig = {
export interface ReduxConfig {
state: ReduxState;
reducerMaps: ReducersMapObject[];
sagaWarchers: (SagaWatcher[])[];
}
export interface ReduxState {
}
const reduxState: ReduxState = {};
const reduxConfig: ReduxConfig = {
state: reduxState,
reducerMaps: [
signInReducer,
],
sagaWarchers: [
signInSagaWatchers,
],
};

View File

@ -20,8 +20,15 @@ import {
import createSagaMiddleware, {
SagaMiddleware,
SagaIterator,
} from 'redux-saga';
import {
fork,
} from 'redux-saga/effects';
import {
createHashHistory,
History,
@ -39,12 +46,10 @@ import AppContext from '@overflow/commons/context';
import * as AppContextLifecycleActions from '@overflow/commons/context/redux/action/lifecycle';
import WebSocketRPC from '@overflow/commons/websocket/WebSocketRPC';
import ReducerContext from '@overflow/commons/redux/ReducerContext';
import { SagaWatcher } from '@overflow/commons/redux-saga';
import appConfig, { Config, ReduxState } from './config';
import sagas from './redux/saga';
// import routes from './router';
import App from './views/App';
injectTapEventPlugin();
@ -81,7 +86,8 @@ class Application {
this.displayLoading();
this.context = await this.initContext();
// this.rpcClient = await this.initRpcClient();
this.rpcClient = await this.initRpcClient();
this.context.put(this.rpcClient);
await this.initRedux();
@ -141,13 +147,21 @@ class Application {
Application.useReduxDevTools ? compose(middleware, window.devToolsExtension()) : middleware,
);
// saga
this.sagaMiddleware.run(sagas);
this.sagaMiddleware.run(this.initReduxSagaWarchers, this.config.redux.sagaWarchers);
resolve();
});
return init;
}
private * initReduxSagaWarchers(sagaWarchers: (SagaWatcher[])[]): SagaIterator {
for (let sagaWarcher of sagaWarchers) {
for (let warcher of sagaWarcher) {
yield fork(warcher);
}
}
}
private displayLoading(): void {
ReactDOM.render(
<div style={{

View File

@ -1,8 +0,0 @@
import { SagaIterator } from 'redux-saga';
import { fork } from 'redux-saga/effects';
import { watchSignin as memberWatchSignin } from '@overflow/member/redux/saga/signIn';
export default function* sagas(): SagaIterator {
yield fork(memberWatchSignin);
}

View File

@ -10,10 +10,15 @@ abstract class Service {
}
protected send(methodName: string, ...params: any[]): Promise<string> {
return new Promise<string>(resolve => {
resolve('');
});
protected send<T>(methodName: string, ...params: any[]): Promise<T> {
return this.webSocketRPC.Call(methodName, params)
.then(body => {
const o: T = JSON.parse(body);
return o;
})
.catch(e => {
throw e;
});
}
}

View File

@ -5,4 +5,4 @@ export const PARAM_TYPES = 'overflow:paramtypes';
export const DESIGN_PARAM_TYPES = 'design:paramtypes';
// The type of the binding at design time
export const INJECT_TAG = 'inject';
export const INJECT_TAG = '__inject__';

View File

@ -5,13 +5,24 @@ export interface Config {
required?: boolean;
}
const inject = (config?: Config) => {
return (target: Object, propertyKey: string | symbol, parameterIndex?: number): void => {
if (typeof parameterIndex === 'number') {
// tagParameter(target, targetKey, index, metadata);
} else {
let types = Reflect.getMetadata('design:type', target, propertyKey);
if (Reflect.hasOwnMetadata(METADATA.INJECT_TAG, target)) {
let meta = Reflect.getMetadata(METADATA.INJECT_TAG, target);
} else {
let meta: string[];
}
let types = Reflect.getMetadata(METADATA.INJECT_TAG, target);
// Reflect.defineMetadata(METADATA.PARAM_TYPES, types, target);
console.log(types);
// tagProperty(target, targetKey, metadata);
}
};

View File

@ -1,12 +1,28 @@
import * as METADATA from './constants';
class AppContext {
private static context: AppContext = null;
private instanceMap: Map<string, Function>;
private constructor() {
this.instanceMap = new Map();
}
public static getService<T>(): T {
return null;
public put(instance: any, name?: string): void {
if (typeof name !== 'string') {
if (instance instanceof Function) {
name = (<Function>instance).name;
}
}
this.instanceMap.set(name, instance);
}
public static getService<T>(clazz: {new(...args: any[]): T}): T {
let types = Reflect.getMetadata(METADATA.PARAM_TYPES, clazz);
let i = new clazz();
return i;
}
public static get<T>(): T {
@ -15,10 +31,8 @@ class AppContext {
return null;
}
public static put<T>(instance: T): void {
return null;
public static put(instance: any, name?: string): void {
AppContext.getContext().put(instance, name);
}
public static getContext(): AppContext {

View File

@ -0,0 +1,3 @@
import { SagaIterator } from 'redux-saga';
export type SagaWatcher = () => SagaIterator;

View File

@ -6,7 +6,8 @@ interface ReducerItem<State> {
}
class ReducerContext {
private static instance: ReducerContext = new ReducerContext();
private static readonly instance: ReducerContext = new ReducerContext();
private static readonly sagaActionKey: string = '@@redux-saga/SAGA_ACTION';
private reducerMap: Map<string, ReducerItem<any>[]>;
private constructor() {
@ -26,7 +27,9 @@ class ReducerContext {
}
public static reducer<State, A extends Action>(state: State, action: A): State {
const actionType = action.type;
const actionType: string = action.type;
const isSagaAction = action.hasOwnProperty(ReducerContext.sagaActionKey) ? action[ReducerContext.sagaActionKey] : false;
if (ReducerContext.getInstance().reducerMap.has(actionType)) {
let reducerItems = ReducerContext.getInstance().reducerMap.get(actionType);
for (let reducerItem of reducerItems) {
@ -34,7 +37,9 @@ class ReducerContext {
state = reducer(state, action);
}
} else {
console.log(`Reducer for [${actionType}] is not exist in the context.`);
if (-1 === actionType.indexOf('@@')) {
console.log(`Reducer for [${actionType}] is not exist in the context.`);
}
}
return state;
}

View File

@ -1,19 +0,0 @@
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;

View File

@ -58,10 +58,8 @@ export default class WebSocketRPC {
this.requestQueue = new Map();
}
public Call(method: string, params: any[]): Promise<any> {
return new Promise<any>((resolve, reject) => {
public Call(method: string, params: any[]): Promise<string> {
return new Promise<string>((resolve, reject) => {
const requestID = this.getRequestID();
let request = new ProtocolRequest<ID_TYPE>(RPCProtocol, requestID);
let req = new RPCRequest(method, params);

View File

@ -12,12 +12,7 @@ export class MemberService extends Service {
}
public signin(signinId: string, signinPw: string): Promise<Member> {
return new Promise<Member>(resolve => {
const json = this.send('signin', [signinId, signinPw]);
let member: Member;
// member = ........(json);
resolve(member);
});
return this.send<Member>('signin', [signinId, signinPw]);
}
public signout(member: Member): Member {

View File

@ -9,13 +9,13 @@ import State from '../redux/state/SignIn';
import * as signinActions from '../redux/action/signIn';
export function mapStateToProps(state: any): SignInStateProps {
export function mapStateToProps(state: any, ownProps?: any): SignInStateProps {
return {
};
}
export function mapDispatchToProps(dispatch: Dispatch<any>): SigninDispatchProps {
export function mapDispatchToProps(dispatch: Dispatch<any>, ownProps?: any): SigninDispatchProps {
return {
onSignIn: (signinId: string, signinPw: string) => {
dispatch(signinActions.request(signinId, signinPw));

View File

@ -1,6 +1,7 @@
import { SagaIterator } from 'redux-saga';
import { call, Effect, fork, put, takeLatest } from 'redux-saga/effects';
import { SagaWatcher } from '@overflow/commons/redux-saga';
import AppContext from '@overflow/commons/context';
import Action from '@overflow/commons/redux/Action';
@ -18,7 +19,7 @@ function* signin(action: Action<SigninPayload>): SagaIterator {
// payload: {sendingRequest: true},
// });
const member = yield call(AppContext.getService<MemberService>().signin, signinId, signinPw);
const member = yield call(AppContext.getService(MemberService).signin, signinId, signinPw);
// if (responseBody.token === undefined) {
// throw new Error(MESSAGES.UNABLE_TO_FIND_TOKEN_IN_LOGIN_RESPONSE);
@ -34,6 +35,10 @@ function* signin(action: Action<SigninPayload>): SagaIterator {
}
}
export function* watchSignin(): SagaIterator {
function* watchSignin(): SagaIterator {
yield takeLatest(SigninActions.REQUEST, signin);
}
const sagaWatchers: SagaWatcher[] = [watchSignin];
export default sagaWatchers;