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",
"version": "1.7.0",
"version": "1.7.1",
"description": "rpclibrary is a websocket on steroids!",
"main": "./js/Index.js",
"repository": {
+8 -3
View File
@@ -96,6 +96,7 @@ export class RPCSocket implements I.Socket{
const info:T.ExtendedRpcInfo[] = await this.info()
info.forEach(i => {
let f: any
switch (i.type) {
case 'Call':
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
*/
private frontEndHookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
fnArgs.pop()
if(sesame)
fnArgs.shift()
let headerArgs = fnArgs.join(",")
const argParams = fnArgs.map(stripAfterEquals).join(",")
sesame = appendComma(sesame)
headerArgs = appendComma(headerArgs)
sesame = appendComma(sesame, true)
headerArgs = fnArgs.length>0?headerArgs+",":headerArgs
return eval( `
async (${headerArgs} callback) => {
const r = await this.socket.call("${fnName}", ${sesame} ${argParams})
if(r && r.result === 'Success'){
this.socket.hook(r.uuid, callback)
+7 -4
View File
@@ -115,7 +115,7 @@ const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandle
const args = sesameFn?(['sesame', ...argsArr].join(','))
:callArgs
callArgs = appendComma(callArgs)
callArgs = appendComma(callArgs, false)
//note rpc.hook is the associated RPC, not a socket.hook
return eval(`
@@ -143,7 +143,8 @@ const makeError = (callName: string) => {
*/
const extractArgs = (f:Function):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{
return s?`'${s}',`:""
export function appendComma(s?:string, turnToString = true):string{
if(turnToString)
return s?`'${s}',`:""
return s?`${s},`:""
}
+55 -15
View File
@@ -175,6 +175,7 @@ describe('RPCSocket', () => {
describe('It should do unhook', () => {
let candy = "OK"
let cb: Function
let cb2: Function
let client: RPCSocket
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 stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
]
@@ -209,6 +230,15 @@ describe('It should do unhook', () => {
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) => {
client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
const r1 = await client['test'].checkCandy()
@@ -502,32 +532,42 @@ describe("Errorhandler functionality", ()=>{
})
})
type myExporterIfc = {
MyExporter: {
myRPC: ()=>Promise<string>
}
}
describe("Class binding", ()=>{
class MyExporter implements RPCExporter{
name = "MyExporter";
class MyExporter implements RPCExporter<myExporterIfc>{
name = "MyExporter" as "MyExporter";
exportRPCs = () => [
this.myRPC
]
myRPC = () => "Hello World"
myRPC = async () => "Hello World"
}
let serv: RPCServer,
sock: RPCSocket,
exporter: MyExporter
before((done)=>{
exporter = new MyExporter()
serv = new RPCServer(21004, [exporter])
sock = new RPCSocket(21004, 'localhost')
sock.connect<myExporterIfc>().then(_ => done())
})
after(() => {
sock.destroy()
serv.destroy()
})
it("binds correctly", (done)=>{
const exporter = new MyExporter()
const serv = new RPCServer(21004, [exporter])
const sock = new RPCSocket(21004, 'localhost')
sock.connect().then(sock => {
if(sock.MyExporter && sock.MyExporter.myRPC){
done()
}
})
.catch(done)
.finally(() => {
sock.destroy()
serv.destroy()
sock['MyExporter'].myRPC().then(() => {
done()
})
})
})