|
|
@@ -4,6 +4,7 @@ import { PromiseIOClient, defaultClientConfig } from './PromiseIO/Client'
|
|
4
|
4
|
import * as T from './Types';
|
|
5
|
5
|
import * as I from './Interfaces';
|
|
6
|
6
|
import { stripAfterEquals, appendComma } from './Utils';
|
|
|
7
|
+import { SOCKET_NOT_CONNECTED, UNKNOWN_RPC_IDENTIFIER, USER_DEFINED_TIMEOUT } from './Strings';
|
|
7
|
8
|
|
|
8
|
9
|
|
|
9
|
10
|
/**
|
|
|
@@ -33,7 +34,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
|
|
33
|
34
|
*/
|
|
34
|
35
|
constructor(public port: number, public address: string, private conf: T.ClientConfig = defaultClientConfig) {
|
|
35
|
36
|
Object.defineProperty(this, 'socket', { value: undefined, writable: true })
|
|
36
|
|
- this.hook("$UNKNOWNRPC$", (err) => this.handlers['error'].forEach(handler => handler(err)))
|
|
|
37
|
+ this.hook(UNKNOWN_RPC_IDENTIFIER, (err) => this.handlers['error'].forEach(handler => handler(err)))
|
|
37
|
38
|
}
|
|
38
|
39
|
|
|
39
|
40
|
/**
|
|
|
@@ -114,11 +115,19 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
|
|
114
|
115
|
* @param args other arguments
|
|
115
|
116
|
*/
|
|
116
|
117
|
public async call(rpcname: string, ...args: any[]): Promise<any> {
|
|
117
|
|
- if (!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
|
|
118
|
+ if (!this.socket) throw new Error(SOCKET_NOT_CONNECTED)
|
|
|
119
|
+
|
|
118
|
120
|
|
|
119
|
121
|
try {
|
|
120
|
|
- const val = await this.socket.call.apply(this.socket, [rpcname, ...args])
|
|
121
|
|
- return val
|
|
|
122
|
+ if(!this.conf.callTimeoutMs || this.conf.callTimeoutMs <= 0)
|
|
|
123
|
+ return await this.socket.call.apply(this.socket, [rpcname, ...args])
|
|
|
124
|
+ else
|
|
|
125
|
+ return await Promise.race([
|
|
|
126
|
+ this.socket.call.apply(this.socket, [rpcname, ...args]),
|
|
|
127
|
+ new Promise((_, rej) => {
|
|
|
128
|
+ setTimeout(_ => rej(USER_DEFINED_TIMEOUT(this.conf.callTimeoutMs)), this.conf.callTimeoutMs)
|
|
|
129
|
+ })
|
|
|
130
|
+ ])
|
|
122
|
131
|
} catch (e) {
|
|
123
|
132
|
this.emit('error', e)
|
|
124
|
133
|
throw e
|
|
|
@@ -131,7 +140,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
|
|
131
|
140
|
* @param args other arguments
|
|
132
|
141
|
*/
|
|
133
|
142
|
public async fire(rpcname: string, ...args: any[]): Promise<void> {
|
|
134
|
|
- if (!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
|
|
143
|
+ if (!this.socket) throw new Error(SOCKET_NOT_CONNECTED)
|
|
135
|
144
|
await this.socket.fire.apply(this.socket, [rpcname, ...args])
|
|
136
|
145
|
}
|
|
137
|
146
|
|
|
|
@@ -181,7 +190,7 @@ export class RPCSocket<Ifc extends T.RPCInterface = T.RPCInterface> implements I
|
|
181
|
190
|
* Get a list of available RPCs from the server
|
|
182
|
191
|
*/
|
|
183
|
192
|
public async info(sesame?: string) {
|
|
184
|
|
- if (!this.socket) throw new Error("The socket is not connected! Use socket.connect() first")
|
|
|
193
|
+ if (!this.socket) throw new Error(SOCKET_NOT_CONNECTED)
|
|
185
|
194
|
return await this.socket.call('info', sesame)
|
|
186
|
195
|
}
|
|
187
|
196
|
|