feat(core): allow passing rpc call options by proxy

This commit is contained in:
alina 🌸 2023-12-13 18:30:28 +03:00
parent 1ce52d66ff
commit 38358622e7
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 47 additions and 1 deletions

View file

@ -455,6 +455,44 @@ export class BaseTelegramClient extends EventEmitter {
return res
}
/**
* Create a Proxy that will call all methods with given call parameters
* (see {@link RpcCallOptions}})
*
* This is useful when you don't call `call()` directly, but rather
* use high-level API provided by `@mtcute/client`, for example:
*
* ```ts
* const client = new TelegramClient(...)
*
* const someone = await client
* .withCallParams({ timeout: 500 })
* .getUsers(...)
* ```
*/
withCallParams(params: RpcCallOptions): this {
return new Proxy(this, {
get(target, prop, receiver) {
if (prop === 'call') {
return (message: tl.RpcMethod, paramsCustom?: RpcCallOptions) =>
target.call(message, {
...params,
...paramsCustom,
})
}
return Reflect.get(target, prop, receiver)
},
})
}
/**
* Shorthand for `withCallParams({ abortSignal })`
*/
withAbortSignal(signal: AbortSignal): this {
return this.withCallParams({ abortSignal: signal })
}
/**
* Change transport for the client.
*

View file

@ -127,6 +127,8 @@ export interface RpcCallOptions {
timeout?: number
/**
* **ADVANCED**
*
* Kind of connection to use for this call.
*
* @default 'main'
@ -134,11 +136,15 @@ export interface RpcCallOptions {
kind?: ConnectionKind
/**
* **ADVANCED**
*
* ID of the DC to use for this call
*/
dcId?: number
/**
* **ADVANCED**
*
* DC connection manager to use for this call.
* Overrides `dcId` if set.
*/

View file

@ -713,7 +713,7 @@ export class SessionConnection extends PersistentConnection {
return
}
this.log.debug('received %s for cancelled request %l: %j', result._, reqMsgId)
this.log.debug('received %s for cancelled request %l: %j', result._, reqMsgId, result)
this._onMessageAcked(reqMsgId)
return
@ -928,6 +928,7 @@ export class SessionConnection extends PersistentConnection {
break
}
case 'bind':
case 'cancel':
break // do nothing, wait for the result
default:
@ -1460,6 +1461,7 @@ export class SessionConnection extends PersistentConnection {
if (rpc.msgId) {
this._session.queuedCancelReq.push(rpc.msgId)
this._session.getStateSchedule.remove(rpc)
this._flushTimer.emitWhenIdle()
} else {
// in case rpc wasn't sent yet (or had some error),