Websocket ing
This commit is contained in:
parent
38ad6c583a
commit
d3c2449694
|
@ -2,7 +2,7 @@ import inject from '../context/decorator/inject';
|
||||||
import WebSocketRPC from '../websocket/WebSocketRPC';
|
import WebSocketRPC from '../websocket/WebSocketRPC';
|
||||||
|
|
||||||
abstract class Service {
|
abstract class Service {
|
||||||
@inject()
|
@inject({qualifier: '11111', required: false})
|
||||||
private webSocketRPC: WebSocketRPC;
|
private webSocketRPC: WebSocketRPC;
|
||||||
private name: string;
|
private name: string;
|
||||||
protected constructor(name: string) {
|
protected constructor(name: string) {
|
||||||
|
@ -10,11 +10,10 @@ abstract class Service {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected send<T>(methodName: string, ...params: any[]): Promise<T> {
|
protected send<T>(methodName: string, args: any): Promise<T> {
|
||||||
return this.webSocketRPC.Call(methodName, params)
|
return this.webSocketRPC.Call(`${this.name}.${methodName}`, args)
|
||||||
.then(body => {
|
.then(result => {
|
||||||
const o: T = JSON.parse(body);
|
return result;
|
||||||
return o;
|
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -3,15 +3,18 @@ import * as METADATA from '../constants';
|
||||||
export interface Config {
|
export interface Config {
|
||||||
qualifier?: string;
|
qualifier?: string;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
|
type?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const inject = (config?: Config) => {
|
const inject = (config: Config = {}) => {
|
||||||
return (target: Object, propertyKey: string | symbol, parameterIndex?: number): void => {
|
return (target: Object, propertyKey: string | symbol, parameterIndex?: number): void => {
|
||||||
if (typeof parameterIndex === 'number') {
|
if (typeof parameterIndex === 'number') {
|
||||||
// tagParameter(target, targetKey, index, metadata);
|
// tagParameter(target, targetKey, index, metadata);
|
||||||
} else {
|
} else {
|
||||||
let meta: Map<string | symbol, Config>;
|
let meta: Map<string | symbol, Config>;
|
||||||
|
let propertyType = Reflect.getMetadata('design:type', target, propertyKey);
|
||||||
|
|
||||||
if (Reflect.hasOwnMetadata(METADATA.INJECT_TAG, target)) {
|
if (Reflect.hasOwnMetadata(METADATA.INJECT_TAG, target)) {
|
||||||
meta = Reflect.getMetadata(METADATA.INJECT_TAG, target);
|
meta = Reflect.getMetadata(METADATA.INJECT_TAG, target);
|
||||||
} else {
|
} else {
|
||||||
|
@ -21,7 +24,16 @@ const inject = (config?: Config) => {
|
||||||
if (meta.has(propertyKey)) {
|
if (meta.has(propertyKey)) {
|
||||||
throw new Error('Cannot apply @injectable decorator multiple times.');
|
throw new Error('Cannot apply @injectable decorator multiple times.');
|
||||||
}
|
}
|
||||||
|
if (config.type === undefined) {
|
||||||
|
config.type = propertyType.prototype;
|
||||||
|
}
|
||||||
meta.set(propertyKey, config);
|
meta.set(propertyKey, config);
|
||||||
|
if (delete target[propertyKey]) {
|
||||||
|
// Create new property with getter and setter
|
||||||
|
Object.defineProperty(target, propertyKey, {
|
||||||
|
writable: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,30 +1,55 @@
|
||||||
import * as METADATA from './constants';
|
import * as METADATA from './constants';
|
||||||
|
import { Config as InjectConfig } from './decorator/inject';
|
||||||
|
|
||||||
|
export type ClassConstructor<T> = {new(...args: any[]): T};
|
||||||
|
|
||||||
class AppContext {
|
class AppContext {
|
||||||
private static context: AppContext = null;
|
private static context: AppContext = null;
|
||||||
private instanceMap: Map<string, Function>;
|
private instanceMap: Map<symbol, Function>;
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
this.instanceMap = new Map();
|
this.instanceMap = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
public put(instance: any, name?: string): void {
|
public put(instance: any, name?: string): void {
|
||||||
|
let aaa = Object.getPrototypeOf(instance);
|
||||||
if (typeof name !== 'string') {
|
if (typeof name !== 'string') {
|
||||||
if (instance instanceof Function) {
|
if (instance.constructor !== undefined) {
|
||||||
name = (<Function>instance).name;
|
name = instance.constructor.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.instanceMap.set(name, instance);
|
this.instanceMap.set(aaa, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getService<T>(clazz: {new(...args: any[]): T}): T {
|
private setValue(type: any, target: object): void {
|
||||||
let s = Object.getPrototypeOf(clazz.prototype);
|
if (type.constructor === Object) {
|
||||||
let types1 = Reflect.getMetadata(METADATA.INJECT_TAG, s);
|
console.log('Object');
|
||||||
let types2 = Reflect.getMetadata(METADATA.INJECT_TAG, clazz);
|
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 {
|
public static get<T>(): T {
|
||||||
|
|
|
@ -58,13 +58,13 @@ export default class WebSocketRPC {
|
||||||
this.requestQueue = new Map();
|
this.requestQueue = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Call(method: string, params: any[]): Promise<string> {
|
public Call(method: string, args: any): Promise<any> {
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<any>((resolve, reject) => {
|
||||||
const requestID = this.getRequestID();
|
const requestID = this.getRequestID();
|
||||||
let request = new ProtocolRequest<ID_TYPE>(RPCProtocol, requestID);
|
let request = new ProtocolRequest<ID_TYPE>(RPCProtocol, requestID);
|
||||||
let req = new RPCRequest(method, params);
|
let req = new RPCRequest(method, args);
|
||||||
request.setBody(req.toJSON());
|
request.setBody(req);
|
||||||
this.conn.send(request.toJSON());
|
this.conn.send(JSON.stringify(request));
|
||||||
this.requestQueue.set(requestID, {resolve: resolve, reject: reject});
|
this.requestQueue.set(requestID, {resolve: resolve, reject: reject});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -111,8 +111,8 @@ export default class WebSocketRPC {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.conn.onmessage = (ev: MessageEvent): any => {
|
this.conn.onmessage = (ev: MessageEvent): any => {
|
||||||
let response: ProtocolResponse<ID_TYPE> = JSON.parse(ev.data);
|
let response = new ProtocolResponse<ID_TYPE>(JSON.parse(ev.data));
|
||||||
let res: RPCResponse = JSON.parse(response.getBody());
|
let res = new RPCResponse(response.getBody());
|
||||||
const requestID = response.getID();
|
const requestID = response.getID();
|
||||||
const error = response.getError();
|
const error = response.getError();
|
||||||
const result = res.getResult();
|
const result = res.getResult();
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
export class ProtocolHeader<ID> {
|
export class ProtocolHeader<ID> {
|
||||||
private readonly protocol: string;
|
protected readonly protocol: string;
|
||||||
private readonly id: ID;
|
protected readonly id: ID;
|
||||||
private body: string;
|
protected body: any;
|
||||||
|
|
||||||
public constructor(protocol: string, id: ID) {
|
public constructor(protocol: string, id: ID, body?: any) {
|
||||||
this.protocol = protocol;
|
this.protocol = protocol;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.body = body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,20 +26,14 @@ export class ProtocolHeader<ID> {
|
||||||
/**
|
/**
|
||||||
* setID
|
* setID
|
||||||
*/
|
*/
|
||||||
public setBody(body: string): void {
|
public setBody(body: any): void {
|
||||||
this.body = body;
|
this.body = body;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* getID
|
* getID
|
||||||
*/
|
*/
|
||||||
public getBody(): string {
|
public getBody(): any {
|
||||||
return this.body;
|
return this.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* toJSON
|
|
||||||
*/
|
|
||||||
public toJSON(): string {
|
|
||||||
return JSON.stringify(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,4 @@ export class ProtocolNotification {
|
||||||
return this.body;
|
return this.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* toJSON
|
|
||||||
*/
|
|
||||||
public toJSON(): string {
|
|
||||||
return JSON.stringify(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,11 @@ import { ProtocolHeader } from './ProtocolHeader';
|
||||||
import { ProtocolError } from './ProtocolError';
|
import { ProtocolError } from './ProtocolError';
|
||||||
|
|
||||||
export class ProtocolResponse<ID> extends ProtocolHeader<ID> {
|
export class ProtocolResponse<ID> extends ProtocolHeader<ID> {
|
||||||
private error: ProtocolError;
|
protected error: ProtocolError;
|
||||||
|
|
||||||
public constructor(protocol: string, id: ID) {
|
public constructor(response: ProtocolResponse<ID>) {
|
||||||
super(protocol, id);
|
super(response.protocol, response.id, response.body);
|
||||||
|
this.error = response.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,6 +1,2 @@
|
||||||
export abstract class ProtocolSub {
|
export abstract class ProtocolSub {
|
||||||
/**
|
|
||||||
* toJSON
|
|
||||||
*/
|
|
||||||
public abstract toJSON(): string;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@ import { ProtocolSub } from '../ProtocolSub';
|
||||||
|
|
||||||
export class RPCRequest extends ProtocolSub {
|
export class RPCRequest extends ProtocolSub {
|
||||||
private readonly method: string;
|
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();
|
super();
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.params = params;
|
this.params = params;
|
||||||
|
@ -20,14 +20,7 @@ export class RPCRequest extends ProtocolSub {
|
||||||
/**
|
/**
|
||||||
* getParams
|
* getParams
|
||||||
*/
|
*/
|
||||||
public getParams(): any[] {
|
public getParams(): any {
|
||||||
return this.params;
|
return this.params;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* toJSON
|
|
||||||
*/
|
|
||||||
public toJSON(): string {
|
|
||||||
return JSON.stringify(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,4 @@ export class RPCResponse extends ProtocolSub {
|
||||||
public getResult(): any {
|
public getResult(): any {
|
||||||
return this.result;
|
return this.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* toJSON
|
|
||||||
*/
|
|
||||||
public toJSON(): string {
|
|
||||||
return JSON.stringify(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Service from '@overflow/commons/api/Service';
|
import Service from '@overflow/commons/api/Service';
|
||||||
import Member from '../model/Member';
|
import Member from '../model/Member';
|
||||||
import injectable from '@overflow/commons/context/decorator/injectable';
|
import injectable from '@overflow/commons/context/decorator/injectable';
|
||||||
|
import inject from '@overflow/commons/context/decorator/inject';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,9 @@ function* signin(action: Action<SigninPayload>): SagaIterator {
|
||||||
// type: types.SENDING_REQUEST,
|
// type: types.SENDING_REQUEST,
|
||||||
// payload: {sendingRequest: true},
|
// 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) {
|
// if (responseBody.token === undefined) {
|
||||||
// throw new Error(MESSAGES.UNABLE_TO_FIND_TOKEN_IN_LOGIN_RESPONSE);
|
// throw new Error(MESSAGES.UNABLE_TO_FIND_TOKEN_IN_LOGIN_RESPONSE);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user