Browse Source

setExporters

master
peter 4 years ago
parent
commit
0057625800
3 changed files with 62 additions and 12 deletions
  1. 1
    1
      package.json
  2. 17
    2
      src/Backend.ts
  3. 44
    9
      test/Test.ts

+ 1
- 1
package.json View File

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

+ 17
- 2
src/Backend.ts View File

@@ -6,6 +6,9 @@ import bsock = require('bsock');
6 6
 import * as T from './Types'; 
7 7
 import * as U from './Utils'; 
8 8
 import * as I from './Interfaces';
9
+import { Socket } from 'dgram';
10
+
11
+type Exporters<InterfaceT extends T.RPCInterface = T.RPCInterface, SubResType = {}> = I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[]
9 12
 
10 13
 /**
11 14
  * A Websocket-server-on-steroids with built-in RPC capabilities
@@ -31,10 +34,9 @@ export class RPCServer<
31 34
      */
32 35
     constructor(
33 36
         private port:number,
34
-        private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[] = [],
37
+        private exporters: Exporters<InterfaceT, SubResType> = [],
35 38
         conf: T.ServerConf =  {}
36 39
     ){
37
-        
38 40
         if(!conf.visibility) this.visibility = "127.0.0.1"        
39 41
         
40 42
         if(conf.sesame){
@@ -91,6 +93,19 @@ export class RPCServer<
91 93
         ]
92 94
     }
93 95
 
96
+    /**
97
+     * Publishes a new list of Exporters. This destroys and restarts the socket
98
+     * @param exporters the exporters to publish
99
+     */
100
+    public setExporters(exporters: Exporters<InterfaceT, SubResType>):any{
101
+        exporters.forEach(U.fixNames)
102
+        this.destroy()
103
+        this.ws = http.createServer()
104
+        this.io = bsock.createServer()
105
+        this.exporters = exporters
106
+        this.startWebsocket()
107
+    }
108
+
94 109
     destroy(): void {
95 110
         this.io.close()
96 111
         this.ws.close()

+ 44
- 9
test/Test.ts View File

@@ -541,38 +541,73 @@ type myExporterIfc = {
541 541
 
542 542
 describe("Class binding", ()=>{
543 543
 
544
+    let exporter1 : MyExporter
545
+    let serv : RPCServer
546
+    let sock: RPCSocket & myExporterIfc
547
+
544 548
     class MyExporter implements RPCExporter<myExporterIfc>{
545 549
         name = "MyExporter" as "MyExporter";
546 550
         exportRPCs = () => [
547 551
             this.myRPC
548 552
         ]
549 553
 
550
-        myRPC = async () => "Hello World"
554
+        myRPC = async () => {
555
+            serv.setExporters([new MyOtherExporter])
556
+            return "Hello World"
557
+        }
558
+    }
559
+
560
+    class MyOtherExporter implements RPCExporter<myExporterIfc>{
561
+        name = "MyExporter" as "MyExporter";
562
+        exportRPCs = () => [
563
+            this.myRPC
564
+        ]
565
+
566
+        myRPC = async () => "Hello Borld"
551 567
 
552 568
     }
553
-    let serv: RPCServer,
554
-        sock: RPCSocket & myExporterIfc,
555
-        exporter: MyExporter
569
+
570
+    before(done => {
571
+        exporter1 = new MyExporter()
572
+        serv = new RPCServer(21004, [exporter1])
573
+        done()
574
+    })
575
+        
576
+    beforeEach((done)=>{
556 577
         
557
-    before((done)=>{
558
-        exporter = new MyExporter()
559
-        serv = new RPCServer(21004, [exporter])
560 578
         const s = new RPCSocket(21004, 'localhost')
561 579
         s.connect<myExporterIfc>().then(conn => {
562 580
             sock = conn
563 581
             done()
564 582
         })
565 583
     })
566
-    after(() => {
584
+
585
+    afterEach(done => {
567 586
         sock.destroy()
587
+        done()
588
+    })
589
+
590
+    after(() => {
568 591
         serv.destroy()
569 592
     })
570 593
 
571 594
     it("binds correctly", (done)=>{
572
-        sock['MyExporter'].myRPC().then(() => {
595
+        sock['MyExporter'].myRPC().then((res) => {
596
+            done(new Error(res))
597
+        }).catch(e => {
598
+            //job will time out because of setExporters
573 599
             done()
574 600
         })
575 601
     })
602
+
603
+    it("changes exporters", (done) => {
604
+        sock['MyExporter'].myRPC().then((res) => {
605
+            if(res === "Hello Borld")
606
+                done()
607
+            else
608
+                done(new Error(res))
609
+        })
610
+    })
576 611
 })
577 612
 
578 613
 

Loading…
Cancel
Save