File tree Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change 12
12
13
13
# Change Log
14
14
15
+ ## 0.3.1
16
+
17
+ ### 🐞 Fix
18
+
19
+ - Fix default timeout of RPC requests.
20
+
15
21
## 0.3.0
16
22
17
23
### ✨ New
Original file line number Diff line number Diff line change @@ -157,14 +157,55 @@ describe("RpcClient", () => {
157
157
}
158
158
} ) ;
159
159
160
+ /**
161
+ * Asserts the timeout monitor is correctly configured.
162
+ */
163
+ describe ( "timeout" , ( ) => {
164
+ it ( "has a default" , async ( ) => {
165
+ // Arrange.
166
+ vi . spyOn ( globalThis , "setTimeout" ) ;
167
+
168
+ // Act.
169
+ await client . request ( "test" ) ;
170
+ await client . request ( {
171
+ method : "test" ,
172
+ params : {
173
+ name : "Elgato" ,
174
+ } ,
175
+ } ) ;
176
+
177
+ // Assert.
178
+ expect ( setTimeout ) . toHaveBeenCalledTimes ( 2 ) ;
179
+ expect ( setTimeout ) . toHaveBeenNthCalledWith < Parameters < typeof setTimeout > > ( 1 , expect . any ( Function ) , 30000 ) ;
180
+ expect ( setTimeout ) . toHaveBeenNthCalledWith < Parameters < typeof setTimeout > > ( 2 , expect . any ( Function ) , 30000 ) ;
181
+ } ) ;
182
+
183
+ it ( "uses specified timeout" , async ( ) => {
184
+ // Arrange.
185
+ vi . spyOn ( globalThis , "setTimeout" ) ;
186
+
187
+ // Act.
188
+ await client . request ( {
189
+ method : "test" ,
190
+ params : {
191
+ name : "Elgato" ,
192
+ } ,
193
+ timeout : 1 ,
194
+ } ) ;
195
+
196
+ // Assert.
197
+ expect ( setTimeout ) . toHaveBeenCalledExactlyOnceWith < Parameters < typeof setTimeout > > ( expect . any ( Function ) , 1 ) ;
198
+ } ) ;
199
+ } ) ;
200
+
160
201
/**
161
202
* Asserts errors are appropriately mapped from a response.
162
203
*/
163
204
it ( "maps errors" , async ( ) => {
164
205
// Arrange, act.
165
206
const res = await client . request ( "err" ) ;
166
207
167
- // Asserts .
208
+ // Assert .
168
209
expect ( res . ok ) . toBe ( false ) ;
169
210
if ( ! res . ok ) {
170
211
expect ( res . error . code ) . toBe ( 1 ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ import type { RpcSender } from "./sender.js";
10
10
* Client capable of sending requests to, and receiving responses from, a server.
11
11
*/
12
12
export class RpcClient {
13
+ /**
14
+ * The timeout, in milliseconds, used when the request does not have a timeout specified; default 30 seconds.
15
+ */
16
+ static readonly #DEFAULT_TIMEOUT = 30000 ;
17
+
13
18
/**
14
19
* The client's options.
15
20
*/
@@ -163,7 +168,7 @@ export class RpcClient {
163
168
request : RpcRequestOptions ,
164
169
) : Promise < RpcResponse < TResult > > {
165
170
const id = crypto . randomUUID ( ) ;
166
- const { method, params, timeout } = request ;
171
+ const { method, params, timeout = RpcClient . #DEFAULT_TIMEOUT } = request ;
167
172
168
173
// Initialize the response handler.
169
174
const response = new Promise < RpcResponse < TResult > > ( ( resolve ) => {
You can’t perform that action at this time.
0 commit comments