diff --git a/package.json b/package.json index a1e1186..89b6562 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/Frontend.ts b/src/Frontend.ts index 72a9150..4ec7cf7 100644 --- a/src/Frontend.ts +++ b/src/Frontend.ts @@ -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) diff --git a/src/Utils.ts b/src/Utils.ts index 9535b36..1631406 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -115,7 +115,7 @@ const hookGenerator = (rpc:T.HookRPC, 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},`:"" } diff --git a/test/Test.ts b/test/Test.ts index f976206..80a44b2 100644 --- a/test/Test.ts +++ b/test/Test.ts @@ -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> => { + + if(param != "OK"){ + console.log("param was"+ param); + return { + result: "Success", + uuid: "no", + topic: "test" + } + } + cb2 = 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 + } +} + describe("Class binding", ()=>{ - class MyExporter implements RPCExporter{ - name = "MyExporter"; + class MyExporter implements RPCExporter{ + 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().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() }) }) }) \ No newline at end of file