feat(core): allow passing rpc call options by proxy
This commit is contained in:
parent
1ce52d66ff
commit
38358622e7
3 changed files with 47 additions and 1 deletions
|
@ -455,6 +455,44 @@ export class BaseTelegramClient extends EventEmitter {
|
||||||
return res
|
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.
|
* Change transport for the client.
|
||||||
*
|
*
|
||||||
|
|
|
@ -127,6 +127,8 @@ export interface RpcCallOptions {
|
||||||
timeout?: number
|
timeout?: number
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* **ADVANCED**
|
||||||
|
*
|
||||||
* Kind of connection to use for this call.
|
* Kind of connection to use for this call.
|
||||||
*
|
*
|
||||||
* @default 'main'
|
* @default 'main'
|
||||||
|
@ -134,11 +136,15 @@ export interface RpcCallOptions {
|
||||||
kind?: ConnectionKind
|
kind?: ConnectionKind
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* **ADVANCED**
|
||||||
|
*
|
||||||
* ID of the DC to use for this call
|
* ID of the DC to use for this call
|
||||||
*/
|
*/
|
||||||
dcId?: number
|
dcId?: number
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* **ADVANCED**
|
||||||
|
*
|
||||||
* DC connection manager to use for this call.
|
* DC connection manager to use for this call.
|
||||||
* Overrides `dcId` if set.
|
* Overrides `dcId` if set.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -713,7 +713,7 @@ export class SessionConnection extends PersistentConnection {
|
||||||
return
|
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)
|
this._onMessageAcked(reqMsgId)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -928,6 +928,7 @@ export class SessionConnection extends PersistentConnection {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'bind':
|
case 'bind':
|
||||||
|
case 'cancel':
|
||||||
break // do nothing, wait for the result
|
break // do nothing, wait for the result
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -1460,6 +1461,7 @@ export class SessionConnection extends PersistentConnection {
|
||||||
|
|
||||||
if (rpc.msgId) {
|
if (rpc.msgId) {
|
||||||
this._session.queuedCancelReq.push(rpc.msgId)
|
this._session.queuedCancelReq.push(rpc.msgId)
|
||||||
|
this._session.getStateSchedule.remove(rpc)
|
||||||
this._flushTimer.emitWhenIdle()
|
this._flushTimer.emitWhenIdle()
|
||||||
} else {
|
} else {
|
||||||
// in case rpc wasn't sent yet (or had some error),
|
// in case rpc wasn't sent yet (or had some error),
|
||||||
|
|
Loading…
Reference in a new issue