|
@@ -18,8 +18,8 @@ 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[] = []
|
|
21
|
+ private closeHandlers: T.FrontEndHandlerType['close'][] = []
|
|
22
|
+ private errorHandlers: T.FrontEndHandlerType['error'][] = []
|
23
|
23
|
private hooks : {[name in string]: T.AnyFunction} = {}
|
24
|
24
|
|
25
|
25
|
/**
|
|
@@ -62,11 +62,11 @@ export class RPCSocket implements I.Socket{
|
62
|
62
|
* @param type 'error' or 'close'
|
63
|
63
|
* @param f The listener to attach
|
64
|
64
|
*/
|
65
|
|
- public on<T extends "error" | "close">(type: T, f: T.HandlerType[T]){
|
|
65
|
+ public on<T extends "error" | "close">(type: T, f: T.FrontEndHandlerType[T]){
|
66
|
66
|
if(!this.socket){
|
67
|
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;
|
|
68
|
+ case "error": this.errorHandlers.push(<T.FrontEndHandlerType['error']> f); break;
|
|
69
|
+ case "close": this.closeHandlers.push(<T.FrontEndHandlerType['close']> f); break;
|
70
|
70
|
default: throw new Error('socket.on only supports ´error´ and ´close´ as first parameter. Got: ´'+type+'´')
|
71
|
71
|
}
|
72
|
72
|
}else{
|
|
@@ -74,6 +74,16 @@ export class RPCSocket implements I.Socket{
|
74
|
74
|
}
|
75
|
75
|
}
|
76
|
76
|
|
|
77
|
+ /**
|
|
78
|
+ * Emit a LOCAL event
|
|
79
|
+ * @param eventName The event name to emit under
|
|
80
|
+ * @param data The data the event carries
|
|
81
|
+ */
|
|
82
|
+ public emit(eventName:string, data:any){
|
|
83
|
+ if(!this.socket) return
|
|
84
|
+ this.socket.emit(eventName, data)
|
|
85
|
+ }
|
|
86
|
+
|
77
|
87
|
/**
|
78
|
88
|
* Destroys the socket
|
79
|
89
|
*/
|
|
@@ -97,7 +107,11 @@ export class RPCSocket implements I.Socket{
|
97
|
107
|
*/
|
98
|
108
|
public async call (rpcname: string, ...args: any[]) : Promise<any>{
|
99
|
109
|
if(!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
100
|
|
- return await this.socket.call.apply(this.socket, [rpcname, ...args])
|
|
110
|
+ try{
|
|
111
|
+ return await this.socket.call.apply(this.socket, [rpcname, ...args])
|
|
112
|
+ }catch(e){
|
|
113
|
+ this.emit('error', e)
|
|
114
|
+ }
|
101
|
115
|
}
|
102
|
116
|
|
103
|
117
|
/**
|
|
@@ -159,7 +173,9 @@ export class RPCSocket implements I.Socket{
|
159
|
173
|
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
160
|
174
|
sesame = appendComma(sesame)
|
161
|
175
|
|
162
|
|
- return eval(`async (${headerArgs}) => { return await this.socket.call("${fnName}", ${sesame} ${argParams})}`)
|
|
176
|
+ return eval(`async (${headerArgs}) => {
|
|
177
|
+ return await this.socket.call("${fnName}", ${sesame} ${argParams})
|
|
178
|
+ }`)
|
163
|
179
|
}
|
164
|
180
|
|
165
|
181
|
/**
|
|
@@ -179,7 +195,6 @@ export class RPCSocket implements I.Socket{
|
179
|
195
|
|
180
|
196
|
return eval( `
|
181
|
197
|
async (${headerArgs} callback) => {
|
182
|
|
-
|
183
|
198
|
const r = await this.socket.call("${fnName}", ${sesame} ${argParams})
|
184
|
199
|
if(r && r.result === 'Success'){
|
185
|
200
|
this.socket.hook(r.uuid, callback)
|