|
@@ -18,6 +18,9 @@ export class RPCSocket implements I.Socket{
|
18
|
18
|
}
|
19
|
19
|
|
20
|
20
|
private socket: I.Socket
|
|
21
|
+ private closeHandlers: T.CloseHandler[] = []
|
|
22
|
+ private errorHandlers: T.ErrorHandler[] = []
|
|
23
|
+ private hooks : {[name in string]: T.AnyFunction} = {}
|
21
|
24
|
|
22
|
25
|
/**
|
23
|
26
|
*
|
|
@@ -35,7 +38,11 @@ export class RPCSocket implements I.Socket{
|
35
|
38
|
* @param handler The handler to attach
|
36
|
39
|
*/
|
37
|
40
|
public hook(name: string, handler: (...args:any[]) => any | Promise<any>){
|
38
|
|
- return this.socket.hook(name, handler)
|
|
41
|
+ if(!this.socket){
|
|
42
|
+ this.hooks[name] = handler
|
|
43
|
+ }else{
|
|
44
|
+ this.socket.hook(name, handler)
|
|
45
|
+ }
|
39
|
46
|
}
|
40
|
47
|
|
41
|
48
|
/**
|
|
@@ -43,7 +50,11 @@ export class RPCSocket implements I.Socket{
|
43
|
50
|
* @param name The function name
|
44
|
51
|
*/
|
45
|
52
|
public unhook(name: string){
|
46
|
|
- return this.socket.unhook(name)
|
|
53
|
+ if(!this.socket){
|
|
54
|
+ delete this.hooks[name]
|
|
55
|
+ }else{
|
|
56
|
+ this.socket.unhook(name)
|
|
57
|
+ }
|
47
|
58
|
}
|
48
|
59
|
|
49
|
60
|
/**
|
|
@@ -51,22 +62,32 @@ export class RPCSocket implements I.Socket{
|
51
|
62
|
* @param type 'error' or 'close'
|
52
|
63
|
* @param f The listener to attach
|
53
|
64
|
*/
|
54
|
|
- public on(type: "error" | "close", f: (e?: any) => void){
|
55
|
|
- return this.socket.on(type, f)
|
|
65
|
+ public on<T extends "error" | "close">(type: T, f: T.HandlerType[T]){
|
|
66
|
+ if(!this.socket){
|
|
67
|
+ switch(type){
|
|
68
|
+ case "error": this.errorHandlers.push(<T.HandlerType['error']> f); break;
|
|
69
|
+ case "close": this.closeHandlers.push(<T.HandlerType['close']> f); break;
|
|
70
|
+ default: throw new Error('socket.on only supports ´error´ and ´close´ as first parameter. Got: ´'+type+'´')
|
|
71
|
+ }
|
|
72
|
+ }else{
|
|
73
|
+ this.socket.on(type, f)
|
|
74
|
+ }
|
56
|
75
|
}
|
57
|
76
|
|
58
|
77
|
/**
|
59
|
78
|
* Destroys the socket
|
60
|
79
|
*/
|
61
|
80
|
public destroy(){
|
62
|
|
- return this.socket.destroy()
|
|
81
|
+ if(!this.socket) return;
|
|
82
|
+ this.socket.destroy()
|
63
|
83
|
}
|
64
|
84
|
|
65
|
85
|
/**
|
66
|
86
|
* Closes the socket. It may attempt to reconnect.
|
67
|
87
|
*/
|
68
|
88
|
public close(){
|
69
|
|
- return this.socket.close()
|
|
89
|
+ if(!this.socket) return;
|
|
90
|
+ this.socket.close()
|
70
|
91
|
}
|
71
|
92
|
|
72
|
93
|
/**
|
|
@@ -75,6 +96,7 @@ export class RPCSocket implements I.Socket{
|
75
|
96
|
* @param args other arguments
|
76
|
97
|
*/
|
77
|
98
|
public async call (rpcname: string, ...args: any[]) : Promise<any>{
|
|
99
|
+ if(!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
78
|
100
|
return await this.socket.call.apply(this.socket, [rpcname, ...args])
|
79
|
101
|
}
|
80
|
102
|
|
|
@@ -84,6 +106,7 @@ export class RPCSocket implements I.Socket{
|
84
|
106
|
* @param args other arguments
|
85
|
107
|
*/
|
86
|
108
|
public async fire(rpcname: string, ...args: any[]) : Promise<void>{
|
|
109
|
+ if(!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
87
|
110
|
await this.socket.fire.apply(this.socket, [rpcname, ...args])
|
88
|
111
|
}
|
89
|
112
|
|
|
@@ -92,6 +115,11 @@ export class RPCSocket implements I.Socket{
|
92
|
115
|
*/
|
93
|
116
|
public async connect<T extends T.RPCInterface= T.RPCInterface>( sesame?: string ) : Promise<RPCSocket & T>{
|
94
|
117
|
this.socket = await bsock.connect(this.port, this.server, this.conf.tls?this.conf.tls:false)
|
|
118
|
+ this.errorHandlers.forEach(h => this.socket.on('error', h))
|
|
119
|
+ this.closeHandlers.forEach(h => this.socket.on('close', h))
|
|
120
|
+ Object.entries(this.hooks).forEach((kv: [string, T.AnyFunction]) => {
|
|
121
|
+ this.socket.hook(kv[0], kv[1])
|
|
122
|
+ })
|
95
|
123
|
|
96
|
124
|
const info:T.ExtendedRpcInfo[] = await this.info()
|
97
|
125
|
info.forEach(i => {
|
|
@@ -117,6 +145,7 @@ export class RPCSocket implements I.Socket{
|
117
|
145
|
* Get a list of available RPCs from the server
|
118
|
146
|
*/
|
119
|
147
|
public async info(){
|
|
148
|
+ if(!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
120
|
149
|
return await this.socket.call('info')
|
121
|
150
|
}
|
122
|
151
|
|
|
@@ -154,7 +183,6 @@ export class RPCSocket implements I.Socket{
|
154
|
183
|
const r = await this.socket.call("${fnName}", ${sesame} ${argParams})
|
155
|
184
|
if(r && r.result === 'Success'){
|
156
|
185
|
this.socket.hook(r.uuid, callback)
|
157
|
|
- this.socket.on('error', e => this.socket.unhook(r.uuid))
|
158
|
186
|
}
|
159
|
187
|
return r
|
160
|
188
|
}`)
|