Websocket ing

This commit is contained in:
crusader 2017-07-26 19:03:15 +09:00
parent 38ad6c583a
commit d3c2449694
12 changed files with 77 additions and 67 deletions

View File

@ -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<T>(methodName: string, ...params: any[]): Promise<T> {
return this.webSocketRPC.Call(methodName, params)
.then(body => {
const o: T = JSON.parse(body);
return o;
protected send<T>(methodName: string, args: any): Promise<T> {
return this.webSocketRPC.Call(`${this.name}.${methodName}`, args)
.then(result => {
return result;
})
.catch(e => {
throw e;

View File

@ -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<string | symbol, Config>;
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,
});
}
}
};
};

View File

@ -1,30 +1,55 @@
import * as METADATA from './constants';
import { Config as InjectConfig } from './decorator/inject';
export type ClassConstructor<T> = {new(...args: any[]): T};
class AppContext {
private static context: AppContext = null;
private instanceMap: Map<string, Function>;
private instanceMap: Map<symbol, Function>;
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 = (<Function>instance).name;
if (instance.constructor !== undefined) {
name = instance.constructor.name;
}
}
this.instanceMap.set(name, instance);
this.instanceMap.set(aaa, instance);
}
public static getService<T>(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<string | symbol, InjectConfig> = 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<T>(type: ClassConstructor<T>, ...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>(): T {

View File

@ -58,13 +58,13 @@ export default class WebSocketRPC {
this.requestQueue = new Map();
}
public Call(method: string, params: any[]): Promise<string> {
return new Promise<string>((resolve, reject) => {
public Call(method: string, args: any): Promise<any> {
return new Promise<any>((resolve, reject) => {
const requestID = this.getRequestID();
let request = new ProtocolRequest<ID_TYPE>(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<ID_TYPE> = JSON.parse(ev.data);
let res: RPCResponse = JSON.parse(response.getBody());
let response = new ProtocolResponse<ID_TYPE>(JSON.parse(ev.data));
let res = new RPCResponse(response.getBody());
const requestID = response.getID();
const error = response.getError();
const result = res.getResult();

View File

@ -1,11 +1,12 @@
export class ProtocolHeader<ID> {
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<ID> {
/**
* 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);
}
}

View File

@ -26,10 +26,4 @@ export class ProtocolNotification {
return this.body;
}
/**
* toJSON
*/
public toJSON(): string {
return JSON.stringify(this);
}
}

View File

@ -2,10 +2,11 @@ import { ProtocolHeader } from './ProtocolHeader';
import { ProtocolError } from './ProtocolError';
export class ProtocolResponse<ID> extends ProtocolHeader<ID> {
private error: ProtocolError;
protected error: ProtocolError;
public constructor(protocol: string, id: ID) {
super(protocol, id);
public constructor(response: ProtocolResponse<ID>) {
super(response.protocol, response.id, response.body);
this.error = response.error;
}
/**

View File

@ -1,6 +1,2 @@
export abstract class ProtocolSub {
/**
* toJSON
*/
public abstract toJSON(): string;
}

View File

@ -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);
}
}

View File

@ -14,11 +14,4 @@ export class RPCResponse extends ProtocolSub {
public getResult(): any {
return this.result;
}
/**
* toJSON
*/
public toJSON(): string {
return JSON.stringify(this);
}
}

View File

@ -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';

View File

@ -18,8 +18,9 @@ function* signin(action: Action<SigninPayload>): 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);