Skip to content

Commit dd712d8

Browse files
committed
feat: add call noReply option. closes #10
1 parent 39bd4d0 commit dd712d8

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ If the invocation of the `handler` rejects or throws, an error will be passed to
532532
* `options` {Object}
533533
* `callTimeoutMs` {Number} - Milliseconds before unanswered call is rejected. Defaults to the same value as the option passed to the client/server constructor.
534534
* `signal` {AbortSignal} - `AbortSignal` to abort the call.
535+
* `noReply` {Boolean} - Send call without expecting a response, resolving immediately with `undefined`. If a response is received, a `badMessage` event will be emitted instead. Defaults to `false`.
535536

536537
Calls a remote method. Returns a `Promise` which either:
537538
* resolves to the value returned by the remote handler.

lib/client.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ class RPCClient extends EventEmitter {
262262
* @param {Object} options - Call options
263263
* @param {number} options.callTimeoutMs - Call timeout (in milliseconds)
264264
* @param {AbortSignal} options.signal - AbortSignal to cancel the call.
265+
* @param {boolean} options.noReply - If set to true, the call will return immediately.
265266
* @returns Promise<*> - Response value from the remote handler.
266267
*/
267268
async call(method, params, options = {}) {
@@ -289,50 +290,57 @@ class RPCClient extends EventEmitter {
289290
}
290291

291292
const pendingCall = {msgId, method, params};
292-
const timeoutAc = new AbortController();
293293

294-
const cleanup = () => {
295-
if (pendingCall.timeout) {
296-
timeoutAc.abort();
294+
if (!options.noReply) {
295+
const timeoutAc = new AbortController();
296+
297+
const cleanup = () => {
298+
if (pendingCall.timeout) {
299+
timeoutAc.abort();
300+
}
301+
this._pendingCalls.delete(msgId);
302+
};
303+
304+
pendingCall.abort = (reason) => {
305+
const err = Error(reason);
306+
err.name = "AbortError";
307+
pendingCall.reject(err);
308+
};
309+
310+
if (options.signal) {
311+
once(options.signal, 'abort').then(() => {
312+
pendingCall.abort(options.signal.reason);
313+
});
297314
}
298-
this._pendingCalls.delete(msgId);
299-
};
300-
301-
pendingCall.abort = (reason) => {
302-
const err = Error(reason);
303-
err.name = "AbortError";
304-
pendingCall.reject(err);
305-
};
306315

307-
if (options.signal) {
308-
once(options.signal, 'abort').then(() => {
309-
pendingCall.abort(options.signal.reason);
316+
pendingCall.promise = new Promise((resolve, reject) => {
317+
pendingCall.resolve = (...args) => {
318+
cleanup();
319+
resolve(...args);
320+
};
321+
pendingCall.reject = (...args) => {
322+
cleanup();
323+
reject(...args);
324+
};
310325
});
311-
}
312326

313-
pendingCall.promise = new Promise((resolve, reject) => {
314-
pendingCall.resolve = (...args) => {
315-
cleanup();
316-
resolve(...args);
317-
};
318-
pendingCall.reject = (...args) => {
319-
cleanup();
320-
reject(...args);
321-
};
322-
});
327+
if (timeoutMs && timeoutMs > 0 && timeoutMs < Infinity) {
328+
const timeoutError = new TimeoutError("Call timeout");
329+
pendingCall.timeout = setTimeout(timeoutMs, null, {signal: timeoutAc.signal}).then(() => {
330+
pendingCall.reject(timeoutError);
331+
}).catch(err=>{});
332+
}
323333

324-
if (timeoutMs && timeoutMs > 0 && timeoutMs < Infinity) {
325-
const timeoutError = new TimeoutError("Call timeout");
326-
pendingCall.timeout = setTimeout(timeoutMs, null, {signal: timeoutAc.signal}).then(() => {
327-
pendingCall.reject(timeoutError);
328-
}).catch(err=>{});
334+
this._pendingCalls.set(msgId, pendingCall);
329335
}
330336

331-
this._pendingCalls.set(msgId, pendingCall);
332-
333337
this.emit('call', {outbound: true, payload});
334338
this.sendRaw(JSON.stringify(payload));
335339

340+
if (options.noReply) {
341+
return;
342+
}
343+
336344
return await pendingCall.promise;
337345
}
338346

0 commit comments

Comments
 (0)