diff --git a/package.json b/package.json index 0eafb5e..047d391 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@overflow/rpc-js", - "version": "0.0.5", + "version": "0.0.7", "description": "TypeScript library setup for multiple compilation targets using tsc and webpack", "main": "./bundles/index.umd.js", "module": "./esm5/index.js", @@ -58,7 +58,9 @@ "peerDependencies": { "tslib": ">=1.9.0" }, - "dependencies": {}, + "dependencies": { + "@overflow/core-js": "^0.0.7" + }, "devDependencies": { "@types/chokidar": "^1.7.5", "@types/jest": "^23.3.1", diff --git a/src/client/client.ts b/src/client/client.ts index beb24a4..91ebd2d 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,4 +1,5 @@ import { Observable, Subject } from 'rxjs'; +import { SubscriberParameterError } from '../core/error'; import { Message } from '../core/type'; import { ClientCodec, @@ -177,11 +178,55 @@ export abstract class Client { } } - public notification(): Observable { + protected notification(): Observable { if (undefined === this.notiSubject) { this.notiSubject = new Subject(); } return this.notiSubject.asObservable(); } + + protected converNotificationParams(params: string[], paramTypes: string[]): any[] { + const results: any[] = []; + + if (0 === params.length) { + return results; + } + if (0 === paramTypes.length) { + return results; + } + if (params.length !== paramTypes.length) { + throw new SubscriberParameterError(`Count is not same from server[${params.length}] and method[${paramTypes.length}]`); + } + for (let indexI = 0; indexI < params.length; indexI++) { + const param = params[indexI]; + const type = paramTypes[indexI]; + switch (type) { + case 'Object': + case 'Array': + case 'Map': + results.push(JSON.parse(param)); + break; + case 'String': + results.push(param); + break; + case 'Number': + results.push(Number(param)); + break; + case 'Boolean': + results.push(Boolean(param)); + break; + case 'Date': + results.push(new Date(Number(param))); + break; + case 'Function': + throw new SubscriberParameterError(`Function type [${indexI}] is not allowed`); + default: + throw new SubscriberParameterError(`${type} type parameter[${indexI}] is not allowed`); + } + } + + return results; + } + } diff --git a/src/core/error.ts b/src/core/error.ts new file mode 100644 index 0000000..52c0e71 --- /dev/null +++ b/src/core/error.ts @@ -0,0 +1,13 @@ +export class SubscriberParameterError extends Error { + public constructor(message?: string) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + } +} + +export class SubscriberExistError extends Error { + public constructor(message?: string) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + } +} diff --git a/src/core/type.ts b/src/core/type.ts index 1a57aac..ff313d7 100644 --- a/src/core/type.ts +++ b/src/core/type.ts @@ -1 +1,12 @@ +import { Method, PropertyKeyType } from '@overflow/core-js'; + export type Message = string | ArrayBuffer; + +export interface SubscriberMethod { + className: PropertyKeyType; + methodName: PropertyKeyType; + parameterTypes: string[]; + + method: Method; + instance: any; +} diff --git a/yarn.lock b/yarn.lock index 55c14c2..90e81d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16,6 +16,12 @@ esutils "^2.0.2" js-tokens "^4.0.0" +"@overflow/core-js@^0.0.7": + version "0.0.7" + resolved "https://nexus.loafle.net/repository/npm-all/@overflow/core-js/-/core-js-0.0.7.tgz#07463557b77d967a011d2f7f6a6f7ab5b2df9252" + dependencies: + reflect-metadata "^0.1.12" + "@samverschueren/stream-to-observable@^0.3.0": version "0.3.0" resolved "https://nexus.loafle.net/repository/npm-all/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz#ecdf48d532c58ea477acfcab80348424f8d0662f" @@ -4334,6 +4340,10 @@ redent@^2.0.0: indent-string "^3.0.0" strip-indent "^2.0.0" +reflect-metadata@^0.1.12: + version "0.1.12" + resolved "https://nexus.loafle.net/repository/npm-all/reflect-metadata/-/reflect-metadata-0.1.12.tgz#311bf0c6b63cd782f228a81abe146a2bfa9c56f2" + regenerate@^1.2.1: version "1.4.0" resolved "https://nexus.loafle.net/repository/npm-all/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"