Skip to content

Commit

Permalink
fix: fix args parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Dec 10, 2024
1 parent 9159bc5 commit 5e2758b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions packages/rpc/src/services/rpc/__tests__/rpc.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

import type { Observable } from 'rxjs';
import type { IMessageProtocol } from '../rpc.service';
import { awaitTime } from '@univerjs/core';
import { of, Subject } from 'rxjs';
import { beforeEach, describe, expect, it } from 'vitest';

import type { Observable } from 'rxjs';
import { ChannelClient, ChannelServer, fromModule, toModule } from '../rpc.service';
import type { IMessageProtocol } from '../rpc.service';

describe('Test ChannelClient & ChannelServer', () => {
let clientProtocol: TestMessageProtocolForClient;
Expand Down
20 changes: 10 additions & 10 deletions packages/rpc/src/services/rpc/rpc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export interface IMessageProtocol {
* event sources are usually provided by the same service or controller.
*/
export interface IChannel {
call<T>(method: string, ...args: any[]): Promise<T>;
subscribe<T>(event: string, ...args: any[]): Observable<T>;
call<T>(method: string, args?: any): Promise<T>;
subscribe<T>(event: string, args?: any): Observable<T>;
}

/**
Expand All @@ -55,7 +55,7 @@ export function fromModule(module: unknown): IChannel {
// const observables = new Map<string, Observable<any>>();

return new (class implements IChannel {
call<T>(method: string, args: any[]): Promise<T> {
call<T>(method: string, args?: any): Promise<T> {
const target = handler[method];
if (typeof target === 'function') {
let res = args ? target.apply(handler, args) : target.call(handler);
Expand All @@ -68,10 +68,10 @@ export function fromModule(module: unknown): IChannel {
throw new Error(`[RPC]: method not found for ${method}!`);
}

subscribe<T>(eventMethod: string, ...args: any[]): Observable<T> {
subscribe<T>(eventMethod: string, args?: any): Observable<T> {
const target = handler[eventMethod];
if (typeof target === 'function') {
const res = args ? target.call(handler, ...args) : target.call(handler);
const res = args ? target.apply(handler, args) : target.call(handler);
if (!isObservable(res)) {
return of(res);
}
Expand Down Expand Up @@ -102,11 +102,11 @@ export function toModule<T extends object>(channel: IChannel): T {
return function (...args: any[]) {
const isObservable = propertyIsEventSource(propKey);
if (isObservable) {
const observable = channel.subscribe(propKey, ...args);
const observable = channel.subscribe(propKey, args);
return observable;
}

return channel.call(propKey, ...args);
return channel.call(propKey, args);
};
},
});
Expand Down Expand Up @@ -206,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();
}
Expand All @@ -232,7 +232,7 @@ export class ChannelClient extends RxDisposable implements IChannelClient {
);
}

private async _remoteCall(channelName: string, method: string, args: any[]): Promise<any> {
private async _remoteCall(channelName: string, method: string, args?: any): Promise<any> {
await this._whenReady();

const sequence = ++this._lastRequestCounter;
Expand Down Expand Up @@ -265,7 +265,7 @@ export class ChannelClient extends RxDisposable implements IChannelClient {
});
}

private _remoteSubscribe(channelName: string, method: string, args: any[]): Observable<any> {
private _remoteSubscribe(channelName: string, method: string, args?: any): Observable<any> {
return new Observable((subscriber) => {
let sequence: number = -1;
this._whenReady().then(() => {
Expand Down

0 comments on commit 5e2758b

Please sign in to comment.