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
 {
1
 {
2
   "name": "rpclibrary",
2
   "name": "rpclibrary",
3
-  "version": "1.7.0",
3
+  "version": "1.7.1",
4
   "description": "rpclibrary is a websocket on steroids!",
4
   "description": "rpclibrary is a websocket on steroids!",
5
   "main": "./js/Index.js",
5
   "main": "./js/Index.js",
6
   "repository": {
6
   "repository": {

+ 8
- 3
src/Frontend.ts View File

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

+ 7
- 4
src/Utils.ts View File

115
     const args = sesameFn?(['sesame', ...argsArr].join(','))
115
     const args = sesameFn?(['sesame', ...argsArr].join(','))
116
                          :callArgs
116
                          :callArgs
117
 
117
 
118
-    callArgs = appendComma(callArgs)
118
+    callArgs = appendComma(callArgs, false)
119
 
119
 
120
     //note rpc.hook is the associated RPC, not a socket.hook
120
     //note rpc.hook is the associated RPC, not a socket.hook
121
     return eval(`
121
     return eval(`
143
  */
143
  */
144
 const extractArgs = (f:Function):string[] => {
144
 const extractArgs = (f:Function):string[] => {
145
     let fn:string
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
 }
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
 describe('It should do unhook', () => {
175
 describe('It should do unhook', () => {
176
     let candy = "OK"
176
     let candy = "OK"
177
     let cb: Function
177
     let cb: Function
178
+    let cb2: Function
178
     let client: RPCSocket
179
     let client: RPCSocket
179
     let server: RPCServer<{topic: string}> 
180
     let server: RPCServer<{topic: string}> 
180
 
181
 
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
             function checkCandy():string { cb(candy); return candy },
216
             function checkCandy():string { cb(candy); return candy },
196
             function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
217
             function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
197
         ]
218
         ]
209
         server.destroy()
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
     it('Unhook+unsubscribe should stop callbacks', (done) => {
242
     it('Unhook+unsubscribe should stop callbacks', (done) => {
213
         client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
243
         client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
214
             const r1 = await client['test'].checkCandy()
244
             const r1 = await client['test'].checkCandy()
502
     })
532
     })
503
 })
533
 })
504
 
534
 
535
+type myExporterIfc = {
536
+    MyExporter: {
537
+        myRPC: ()=>Promise<string>
538
+    }
539
+}
540
+
505
 
541
 
506
 describe("Class binding", ()=>{
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
         exportRPCs = () => [
546
         exportRPCs = () => [
511
             this.myRPC
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
     it("binds correctly", (done)=>{
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