diff --git a/src/ts/@overflow/commons/api/Service.ts b/src/ts/@overflow/commons/api/Service.ts index d06bb16..984e514 100644 --- a/src/ts/@overflow/commons/api/Service.ts +++ b/src/ts/@overflow/commons/api/Service.ts @@ -2,7 +2,7 @@ import inject from '../context/decorator/inject'; import WebSocketRPC from '../websocket/WebSocketRPC'; abstract class Service { - @inject() + @inject({qualifier: '11111', required: false}) private webSocketRPC: WebSocketRPC; private name: string; protected constructor(name: string) { @@ -10,11 +10,10 @@ abstract class Service { } - protected send(methodName: string, ...params: any[]): Promise { - return this.webSocketRPC.Call(methodName, params) - .then(body => { - const o: T = JSON.parse(body); - return o; + protected send(methodName: string, args: any): Promise { + return this.webSocketRPC.Call(`${this.name}.${methodName}`, args) + .then(result => { + return result; }) .catch(e => { throw e; diff --git a/src/ts/@overflow/commons/context/decorator/inject.ts b/src/ts/@overflow/commons/context/decorator/inject.ts index a86515a..4bf8fbd 100644 --- a/src/ts/@overflow/commons/context/decorator/inject.ts +++ b/src/ts/@overflow/commons/context/decorator/inject.ts @@ -3,15 +3,18 @@ import * as METADATA from '../constants'; export interface Config { qualifier?: string; required?: boolean; + type?: any; } -const inject = (config?: Config) => { +const inject = (config: Config = {}) => { return (target: Object, propertyKey: string | symbol, parameterIndex?: number): void => { if (typeof parameterIndex === 'number') { // tagParameter(target, targetKey, index, metadata); } else { let meta: Map; + let propertyType = Reflect.getMetadata('design:type', target, propertyKey); + if (Reflect.hasOwnMetadata(METADATA.INJECT_TAG, target)) { meta = Reflect.getMetadata(METADATA.INJECT_TAG, target); } else { @@ -21,7 +24,16 @@ const inject = (config?: Config) => { if (meta.has(propertyKey)) { throw new Error('Cannot apply @injectable decorator multiple times.'); } + if (config.type === undefined) { + config.type = propertyType.prototype; + } meta.set(propertyKey, config); + if (delete target[propertyKey]) { + // Create new property with getter and setter + Object.defineProperty(target, propertyKey, { + writable: true, + }); + } } }; }; diff --git a/src/ts/@overflow/commons/context/index.ts b/src/ts/@overflow/commons/context/index.ts index 08c744f..c0b1958 100644 --- a/src/ts/@overflow/commons/context/index.ts +++ b/src/ts/@overflow/commons/context/index.ts @@ -1,30 +1,55 @@ import * as METADATA from './constants'; +import { Config as InjectConfig } from './decorator/inject'; + +export type ClassConstructor = {new(...args: any[]): T}; class AppContext { private static context: AppContext = null; - private instanceMap: Map; + private instanceMap: Map; private constructor() { this.instanceMap = new Map(); } public put(instance: any, name?: string): void { + let aaa = Object.getPrototypeOf(instance); if (typeof name !== 'string') { - if (instance instanceof Function) { - name = (instance).name; + if (instance.constructor !== undefined) { + name = instance.constructor.name; } } - this.instanceMap.set(name, instance); + this.instanceMap.set(aaa, instance); } - public static getService(clazz: {new(...args: any[]): T}): T { - let s = Object.getPrototypeOf(clazz.prototype); - let types1 = Reflect.getMetadata(METADATA.INJECT_TAG, s); - let types2 = Reflect.getMetadata(METADATA.INJECT_TAG, clazz); + private setValue(type: any, target: object): void { + if (type.constructor === Object) { + console.log('Object'); + return; + } - let i = new clazz(); + let meta: Map = Reflect.getMetadata(METADATA.INJECT_TAG, type); + if (undefined !== meta) { + let property: any; + meta.forEach((config, key, map) => { + property = Object.getOwnPropertyDescriptor(type, key); - return i; + let dddd = this.instanceMap.get(config.type); + target[key] = dddd; + console.log(dddd); + }); + } + + + this.setValue(Object.getPrototypeOf(type), target); + } + + public static getService(type: ClassConstructor, ...args: any[]): T { + let clazz = type.prototype; + let instance = Object.create(clazz); + instance.constructor.apply(instance, args); + + AppContext.getContext().setValue(clazz, instance); + return instance; } public static get(): T { diff --git a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts index c13ebe4..7ccaecd 100644 --- a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts +++ b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts @@ -58,13 +58,13 @@ export default class WebSocketRPC { this.requestQueue = new Map(); } - public Call(method: string, params: any[]): Promise { - return new Promise((resolve, reject) => { + public Call(method: string, args: any): Promise { + return new Promise((resolve, reject) => { const requestID = this.getRequestID(); let request = new ProtocolRequest(RPCProtocol, requestID); - let req = new RPCRequest(method, params); - request.setBody(req.toJSON()); - this.conn.send(request.toJSON()); + let req = new RPCRequest(method, args); + request.setBody(req); + this.conn.send(JSON.stringify(request)); this.requestQueue.set(requestID, {resolve: resolve, reject: reject}); }); } @@ -111,8 +111,8 @@ export default class WebSocketRPC { }; this.conn.onmessage = (ev: MessageEvent): any => { - let response: ProtocolResponse = JSON.parse(ev.data); - let res: RPCResponse = JSON.parse(response.getBody()); + let response = new ProtocolResponse(JSON.parse(ev.data)); + let res = new RPCResponse(response.getBody()); const requestID = response.getID(); const error = response.getError(); const result = res.getResult(); diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts index 059cd2b..1a4f7d8 100644 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts +++ b/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts @@ -1,11 +1,12 @@ export class ProtocolHeader { - private readonly protocol: string; - private readonly id: ID; - private body: string; + protected readonly protocol: string; + protected readonly id: ID; + protected body: any; - public constructor(protocol: string, id: ID) { + public constructor(protocol: string, id: ID, body?: any) { this.protocol = protocol; this.id = id; + this.body = body; } /** @@ -25,20 +26,14 @@ export class ProtocolHeader { /** * setID */ - public setBody(body: string): void { + public setBody(body: any): void { this.body = body; } /** * getID */ - public getBody(): string { + public getBody(): any { return this.body; } - /** - * toJSON - */ - public toJSON(): string { - return JSON.stringify(this); - } } diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts index 619025b..57e5787 100644 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts +++ b/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts @@ -26,10 +26,4 @@ export class ProtocolNotification { return this.body; } - /** - * toJSON - */ - public toJSON(): string { - return JSON.stringify(this); - } } diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts index 551ee5e..1a6253f 100644 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts +++ b/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts @@ -2,10 +2,11 @@ import { ProtocolHeader } from './ProtocolHeader'; import { ProtocolError } from './ProtocolError'; export class ProtocolResponse extends ProtocolHeader { - private error: ProtocolError; + protected error: ProtocolError; - public constructor(protocol: string, id: ID) { - super(protocol, id); + public constructor(response: ProtocolResponse) { + super(response.protocol, response.id, response.body); + this.error = response.error; } /** diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts index 9149392..7d6d925 100644 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts +++ b/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts @@ -1,6 +1,2 @@ export abstract class ProtocolSub { - /** - * toJSON - */ - public abstract toJSON(): string; } diff --git a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts b/src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts index 72866a2..08e01c6 100644 --- a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts +++ b/src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts @@ -2,9 +2,9 @@ import { ProtocolSub } from '../ProtocolSub'; export class RPCRequest extends ProtocolSub { private readonly method: string; - private readonly params: any[]; + private readonly params: any; - public constructor(method: string, params: any[]) { + public constructor(method: string, params: any) { super(); this.method = method; this.params = params; @@ -20,14 +20,7 @@ export class RPCRequest extends ProtocolSub { /** * getParams */ - public getParams(): any[] { + public getParams(): any { return this.params; } - - /** - * toJSON - */ - public toJSON(): string { - return JSON.stringify(this); - } } diff --git a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts b/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts index af7ebcc..4b1179b 100644 --- a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts +++ b/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts @@ -14,11 +14,4 @@ export class RPCResponse extends ProtocolSub { public getResult(): any { return this.result; } - - /** - * toJSON - */ - public toJSON(): string { - return JSON.stringify(this); - } } diff --git a/src/ts/@overflow/member/api/service/MemberService.ts b/src/ts/@overflow/member/api/service/MemberService.ts index b460da0..b1257ea 100644 --- a/src/ts/@overflow/member/api/service/MemberService.ts +++ b/src/ts/@overflow/member/api/service/MemberService.ts @@ -1,6 +1,7 @@ import Service from '@overflow/commons/api/Service'; import Member from '../model/Member'; import injectable from '@overflow/commons/context/decorator/injectable'; +import inject from '@overflow/commons/context/decorator/inject'; diff --git a/src/ts/@overflow/member/redux/saga/signIn.ts b/src/ts/@overflow/member/redux/saga/signIn.ts index b1fa1c2..30dd57b 100644 --- a/src/ts/@overflow/member/redux/saga/signIn.ts +++ b/src/ts/@overflow/member/redux/saga/signIn.ts @@ -18,8 +18,9 @@ function* signin(action: Action): SagaIterator { // type: types.SENDING_REQUEST, // payload: {sendingRequest: true}, // }); + let memberService = AppContext.getService(MemberService); - const member = yield call(AppContext.getService(MemberService).signin, signinId, signinPw); + const member = yield call({context: memberService, fn: memberService.signin}, signinId, signinPw); // if (responseBody.token === undefined) { // throw new Error(MESSAGES.UNABLE_TO_FIND_TOKEN_IN_LOGIN_RESPONSE);