fix bug in subscribe with param

This commit is contained in:
2020-02-09 01:01:36 +01:00
parent a2268526af
commit 96976974c7
4 changed files with 71 additions and 23 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "rpclibrary", "name": "rpclibrary",
"version": "1.7.0", "version": "1.7.1",
"description": "rpclibrary is a websocket on steroids!", "description": "rpclibrary is a websocket on steroids!",
"main": "./js/Index.js", "main": "./js/Index.js",
"repository": { "repository": {
+8 -3
View File
@@ -96,6 +96,7 @@ export class RPCSocket implements I.Socket{
const info:T.ExtendedRpcInfo[] = await this.info() const info:T.ExtendedRpcInfo[] = await this.info()
info.forEach(i => { info.forEach(i => {
let f: any let f: any
switch (i.type) { switch (i.type) {
case 'Call': case 'Call':
f = this.callGenerator(i.uniqueName, i.argNames, sesame) f = this.callGenerator(i.uniqueName, i.argNames, sesame)
@@ -138,14 +139,18 @@ export class RPCSocket implements I.Socket{
* @param fnArgs A string-list of parameters * @param fnArgs A string-list of parameters
*/ */
private frontEndHookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{ private frontEndHookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
fnArgs.pop()
if(sesame)
fnArgs.shift()
let headerArgs = fnArgs.join(",") let headerArgs = fnArgs.join(",")
const argParams = fnArgs.map(stripAfterEquals).join(",") const argParams = fnArgs.map(stripAfterEquals).join(",")
sesame = appendComma(sesame) sesame = appendComma(sesame, true)
headerArgs = appendComma(headerArgs) headerArgs = fnArgs.length>0?headerArgs+",":headerArgs
return eval( ` return eval( `
async (${headerArgs} callback) => { async (${headerArgs} callback) => {
const r = await this.socket.call("${fnName}", ${sesame} ${argParams}) const r = await this.socket.call("${fnName}", ${sesame} ${argParams})
if(r && r.result === 'Success'){ if(r && r.result === 'Success'){
this.socket.hook(r.uuid, callback) this.socket.hook(r.uuid, callback)
+6 -3
View File
@@ -115,7 +115,7 @@ const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandle
const args = sesameFn?(['sesame', ...argsArr].join(',')) const args = sesameFn?(['sesame', ...argsArr].join(','))
:callArgs :callArgs
callArgs = appendComma(callArgs) callArgs = appendComma(callArgs, false)
//note rpc.hook is the associated RPC, not a socket.hook //note rpc.hook is the associated RPC, not a socket.hook
return eval(` return eval(`
@@ -143,7 +143,8 @@ const makeError = (callName: string) => {
*/ */
const extractArgs = (f:Function):string[] => { const extractArgs = (f:Function):string[] => {
let fn:string let fn:string
return (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1).split(",") fn = (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1)
return fn!==""?fn.split(',') : []
} }
/** /**
@@ -170,8 +171,10 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
} }
export function appendComma(s?:string):string{ export function appendComma(s?:string, turnToString = true):string{
if(turnToString)
return s?`'${s}',`:"" return s?`'${s}',`:""
return s?`${s},`:""
} }
+53 -13
View File
@@ -175,6 +175,7 @@ describe('RPCSocket', () => {
describe('It should do unhook', () => { describe('It should do unhook', () => {
let candy = "OK" let candy = "OK"
let cb: Function let cb: Function
let cb2: Function
let client: RPCSocket let client: RPCSocket
let server: RPCServer<{topic: string}> let server: RPCServer<{topic: string}>
@@ -192,6 +193,26 @@ describe('It should do unhook', () => {
} }
} }
}, },
{
name: 'subscribeWithParam',
hook: async(param, callback):Promise<SubscriptionResponse<{topic:string}>> => {
if(param != "OK"){
console.log("param was"+ param);
return {
result: "Success",
uuid: "no",
topic: "test"
}
}
cb2 = <Function> callback
return {
result: "Success",
uuid: "OK",
topic: "test"
}
}
},
function checkCandy():string { cb(candy); return candy }, function checkCandy():string { cb(candy); return candy },
function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy } function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
] ]
@@ -209,6 +230,15 @@ describe('It should do unhook', () => {
server.destroy() server.destroy()
}) })
it('Subscribe with param', (done) => {
client['test'].subscribeWithParam("OK", c => {}).then( async (res: SubscriptionResponse) => {
if(res.uuid === "OK"){
done()
}else
done(new Error("Results did not match "+res.uuid))
})
})
it('Unhook+unsubscribe should stop callbacks', (done) => { it('Unhook+unsubscribe should stop callbacks', (done) => {
client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => { client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
const r1 = await client['test'].checkCandy() const r1 = await client['test'].checkCandy()
@@ -502,32 +532,42 @@ describe("Errorhandler functionality", ()=>{
}) })
}) })
type myExporterIfc = {
MyExporter: {
myRPC: ()=>Promise<string>
}
}
describe("Class binding", ()=>{ describe("Class binding", ()=>{
class MyExporter implements RPCExporter{ class MyExporter implements RPCExporter<myExporterIfc>{
name = "MyExporter"; name = "MyExporter" as "MyExporter";
exportRPCs = () => [ exportRPCs = () => [
this.myRPC this.myRPC
] ]
myRPC = () => "Hello World" myRPC = async () => "Hello World"
} }
let serv: RPCServer,
sock: RPCSocket,
exporter: MyExporter
it("binds correctly", (done)=>{ before((done)=>{
const exporter = new MyExporter() exporter = new MyExporter()
const serv = new RPCServer(21004, [exporter]) serv = new RPCServer(21004, [exporter])
const sock = new RPCSocket(21004, 'localhost') sock = new RPCSocket(21004, 'localhost')
sock.connect().then(sock => { sock.connect<myExporterIfc>().then(_ => done())
if(sock.MyExporter && sock.MyExporter.myRPC){
done()
}
}) })
.catch(done) after(() => {
.finally(() => {
sock.destroy() sock.destroy()
serv.destroy() serv.destroy()
}) })
it("binds correctly", (done)=>{
sock['MyExporter'].myRPC().then(() => {
done()
})
}) })
}) })