Browse Source

fix bug in subscribe with param

master
peter 4 years ago
parent
commit
96976974c7
4 changed files with 71 additions and 23 deletions
  1. 1
    1
      package.json
  2. 8
    3
      src/Frontend.ts
  3. 7
    4
      src/Utils.ts
  4. 55
    15
      test/Test.ts

+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "rpclibrary",
3
-  "version": "1.7.0",
3
+  "version": "1.7.1",
4 4
   "description": "rpclibrary is a websocket on steroids!",
5 5
   "main": "./js/Index.js",
6 6
   "repository": {

+ 8
- 3
src/Frontend.ts View File

@@ -96,6 +96,7 @@ export class RPCSocket implements I.Socket{
96 96
         const info:T.ExtendedRpcInfo[] = await this.info()
97 97
         info.forEach(i => {
98 98
             let f: any
99
+            
99 100
             switch (i.type) {
100 101
                 case 'Call':
101 102
                     f = this.callGenerator(i.uniqueName, i.argNames, sesame)    
@@ -138,14 +139,18 @@ export class RPCSocket implements I.Socket{
138 139
      * @param fnArgs A string-list of parameters
139 140
      */
140 141
     private frontEndHookGenerator(fnName: string, fnArgs:string[], sesame?:string): T.HookFunction{
141
-        fnArgs.pop()
142
+        
143
+        if(sesame)
144
+            fnArgs.shift()
145
+
142 146
         let headerArgs = fnArgs.join(",")
143 147
         const argParams = fnArgs.map(stripAfterEquals).join(",")
144
-        sesame = appendComma(sesame)
145
-        headerArgs = appendComma(headerArgs) 
148
+        sesame = appendComma(sesame, true)
149
+        headerArgs = fnArgs.length>0?headerArgs+",":headerArgs 
146 150
 
147 151
         return eval( `
148 152
         async (${headerArgs} callback) => {
153
+
149 154
             const r = await this.socket.call("${fnName}", ${sesame} ${argParams})
150 155
             if(r && r.result === 'Success'){
151 156
                 this.socket.hook(r.uuid, callback)

+ 7
- 4
src/Utils.ts View File

@@ -115,7 +115,7 @@ const hookGenerator = (rpc:T.HookRPC<any, any, any>, errorHandler: T.ErrorHandle
115 115
     const args = sesameFn?(['sesame', ...argsArr].join(','))
116 116
                          :callArgs
117 117
 
118
-    callArgs = appendComma(callArgs)
118
+    callArgs = appendComma(callArgs, false)
119 119
 
120 120
     //note rpc.hook is the associated RPC, not a socket.hook
121 121
     return eval(`
@@ -143,7 +143,8 @@ const makeError = (callName: string) => {
143 143
  */
144 144
 const extractArgs = (f:Function):string[] => {
145 145
     let fn:string
146
-    return (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1).split(",")
146
+    fn = (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1)
147
+    return fn!==""?fn.split(',') : []
147 148
 }
148 149
 
149 150
 /**
@@ -170,8 +171,10 @@ export function makeSesameFunction (sesame : T.SesameFunction | string) : T.Sesa
170 171
 }
171 172
 
172 173
 
173
-export function appendComma(s?:string):string{
174
-    return s?`'${s}',`:"" 
174
+export function appendComma(s?:string, turnToString = true):string{
175
+    if(turnToString)   
176
+        return s?`'${s}',`:"" 
177
+        return s?`${s},`:"" 
175 178
 }
176 179
 
177 180
 

+ 55
- 15
test/Test.ts View File

@@ -175,6 +175,7 @@ describe('RPCSocket', () => {
175 175
 describe('It should do unhook', () => {
176 176
     let candy = "OK"
177 177
     let cb: Function
178
+    let cb2: Function
178 179
     let client: RPCSocket
179 180
     let server: RPCServer<{topic: string}> 
180 181
 
@@ -192,6 +193,26 @@ describe('It should do unhook', () => {
192 193
                     }
193 194
                 }
194 195
             }, 
196
+            {
197
+                name: 'subscribeWithParam',
198
+                hook: async(param, callback):Promise<SubscriptionResponse<{topic:string}>> => {
199
+                    
200
+                    if(param != "OK"){
201
+                        console.log("param was"+ param);
202
+                        return {
203
+                            result: "Success",
204
+                            uuid: "no",
205
+                            topic: "test"
206
+                        }
207
+                    }
208
+                    cb2 = <Function> callback
209
+                    return {
210
+                        result: "Success",
211
+                        uuid: "OK",
212
+                        topic: "test"
213
+                    }
214
+                }
215
+            },
195 216
             function checkCandy():string { cb(candy); return candy },
196 217
             function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
197 218
         ]
@@ -209,6 +230,15 @@ describe('It should do unhook', () => {
209 230
         server.destroy()
210 231
     })
211 232
 
233
+    it('Subscribe with param', (done) => {
234
+        client['test'].subscribeWithParam("OK", c => {}).then( async (res: SubscriptionResponse) => {
235
+            if(res.uuid === "OK"){
236
+                done()
237
+            }else
238
+                done(new Error("Results did not match "+res.uuid))
239
+        })
240
+    })
241
+
212 242
     it('Unhook+unsubscribe should stop callbacks', (done) => {
213 243
         client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
214 244
             const r1 = await client['test'].checkCandy()
@@ -502,32 +532,42 @@ describe("Errorhandler functionality", ()=>{
502 532
     })
503 533
 })
504 534
 
535
+type myExporterIfc = {
536
+    MyExporter: {
537
+        myRPC: ()=>Promise<string>
538
+    }
539
+}
540
+
505 541
 
506 542
 describe("Class binding", ()=>{
507 543
 
508
-    class MyExporter implements RPCExporter{
509
-        name = "MyExporter";
544
+    class MyExporter implements RPCExporter<myExporterIfc>{
545
+        name = "MyExporter" as "MyExporter";
510 546
         exportRPCs = () => [
511 547
             this.myRPC
512 548
         ]
513 549
 
514
-        myRPC = () => "Hello World"
550
+        myRPC = async () => "Hello World"
515 551
 
516 552
     }
553
+    let serv: RPCServer,
554
+        sock: RPCSocket,
555
+        exporter: MyExporter
556
+        
557
+    before((done)=>{
558
+        exporter = new MyExporter()
559
+        serv = new RPCServer(21004, [exporter])
560
+        sock = new RPCSocket(21004, 'localhost')
561
+        sock.connect<myExporterIfc>().then(_ => done())
562
+    })
563
+    after(() => {
564
+        sock.destroy()
565
+        serv.destroy()
566
+    })
517 567
 
518 568
     it("binds correctly", (done)=>{
519
-        const exporter = new MyExporter()
520
-        const serv = new RPCServer(21004, [exporter])
521
-        const sock = new RPCSocket(21004, 'localhost')
522
-        sock.connect().then(sock => {
523
-            if(sock.MyExporter && sock.MyExporter.myRPC){
524
-                done()
525
-            }
526
-        })
527
-        .catch(done)
528
-        .finally(() => {
529
-            sock.destroy()
530
-            serv.destroy()
569
+        sock['MyExporter'].myRPC().then(() => {
570
+            done()
531 571
         })
532 572
     })
533 573
 })

Loading…
Cancel
Save