diff --git a/packages/rpc/src/services/rpc/rpc.service.ts b/packages/rpc/src/services/rpc/rpc.service.ts index b53f3c4f673..43aa4066475 100644 --- a/packages/rpc/src/services/rpc/rpc.service.ts +++ b/packages/rpc/src/services/rpc/rpc.service.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +/* eslint-disable ts/no-explicit-any */ + import type { Subscription } from 'rxjs'; import { RxDisposable } from '@univerjs/core'; import { BehaviorSubject, firstValueFrom, isObservable, Observable, of } from 'rxjs'; @@ -23,9 +25,7 @@ import { filter, take, takeUntil } from 'rxjs/operators'; /** This protocol is for transferring data from the two peer univer instance running in different locations. */ export interface IMessageProtocol { - // eslint-disable-next-line ts/no-explicit-any send(message: any): void; - // eslint-disable-next-line ts/no-explicit-any onMessage: Observable; } @@ -36,10 +36,8 @@ export interface IMessageProtocol { * event sources are usually provided by the same service or controller. */ export interface IChannel { - // eslint-disable-next-line ts/no-explicit-any - call(method: string, args?: any): Promise; - // eslint-disable-next-line ts/no-explicit-any - subscribe(event: string, args?: any): Observable; + call(method: string, ...args: any[]): Promise; + subscribe(event: string, ...args: any[]): Observable; } /** @@ -57,10 +55,10 @@ export function fromModule(module: unknown): IChannel { // const observables = new Map>(); return new (class implements IChannel { - call(method: string, args?: any): Promise { + call(method: string, args: any[]): Promise { const target = handler[method]; if (typeof target === 'function') { - let res = target.apply(handler, [args]); + let res = args ? target.apply(handler, args) : target.call(handler); if (!(res instanceof Promise)) { res = Promise.resolve(res); } @@ -70,10 +68,10 @@ export function fromModule(module: unknown): IChannel { throw new Error(`[RPC]: method not found for ${method}!`); } - subscribe(eventMethod: string, args: any): Observable { + subscribe(eventMethod: string, ...args: any[]): Observable { const target = handler[eventMethod]; if (typeof target === 'function') { - const res = target.apply(handler, args); + const res = args ? target.call(handler, ...args) : target.call(handler); if (!isObservable(res)) { return of(res); } @@ -104,11 +102,11 @@ export function toModule(channel: IChannel): T { return function (...args: any[]) { const isObservable = propertyIsEventSource(propKey); if (isObservable) { - const observable = channel.subscribe(propKey, args[0]); + const observable = channel.subscribe(propKey, ...args); return observable; } - return channel.call(propKey, args[0]); + return channel.call(propKey, ...args); }; }, }); @@ -153,7 +151,7 @@ interface IRPCRequest { type: RequestType; channelName: string; method: string; - args?: any; + args?: any[]; } enum ResponseType { @@ -208,7 +206,7 @@ export class ChannelClient extends RxDisposable implements IChannelClient { const self = this; return { - call(method: string, args?: any) { + call(method: string, ...args: any[]) { if (self._disposed) { return Promise.reject(); } @@ -234,7 +232,7 @@ export class ChannelClient extends RxDisposable implements IChannelClient { ); } - private async _remoteCall(channelName: string, method: string, args?: any): Promise { + private async _remoteCall(channelName: string, method: string, args: any[]): Promise { await this._whenReady(); const sequence = ++this._lastRequestCounter; @@ -267,7 +265,7 @@ export class ChannelClient extends RxDisposable implements IChannelClient { }); } - private _remoteSubscribe(channelName: string, method: string, args?: any): Observable { + private _remoteSubscribe(channelName: string, method: string, args: any[]): Observable { return new Observable((subscriber) => { let sequence: number = -1; this._whenReady().then(() => { @@ -388,7 +386,7 @@ export class ChannelServer extends RxDisposable implements IChannelServer { if (!channel) { throw new Error(`[ChannelServer]: Channel ${channelName} not found!`); } - promise = channel.call(method, args); + promise = args ? channel.call(method, args) : channel.call(method); } catch (err: unknown) { promise = Promise.reject(err); }