This commit is contained in:
crusader 2018-09-06 00:12:32 +09:00
parent d27b8b249f
commit 77387a5e64
5 changed files with 84 additions and 3 deletions

View File

@ -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",

View File

@ -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<ClientNotificationCodec> {
protected notification(): Observable<ClientNotificationCodec> {
if (undefined === this.notiSubject) {
this.notiSubject = new Subject<ClientNotificationCodec>();
}
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;
}
}

13
src/core/error.ts Normal file
View File

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

View File

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

View File

@ -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"