Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
refactor: add channel service in rpc module as public api (#647)
Wenzhao Hu0a165a88ecef
diff --git a/examples/sheets/src/worker.ts b/examples/sheets/src/worker.tsindex 54301806f9..6fef84c458 100644--- a/examples/sheets/src/worker.ts+++ b/examples/sheets/src/worker.ts@@ -1,10 +1,10 @@// The entry of web worker scripts-import { BaseFormulaEnginePlugin } from '@univerjs/engine-formula';-import { SheetsPlugin } from '@univerjs/sheets';import { LocaleType, Univer } from '@univerjs/core';-import { UniverRPCWorkerThreadPlugin } from '@univerjs/rpc';+import { BaseFormulaEnginePlugin } from '@univerjs/engine-formula';import { FormulaPlugin } from '@univerjs/formula';+import { UniverRPCWorkerThreadPlugin } from '@univerjs/rpc';+import { SheetsPlugin } from '@univerjs/sheets';// Univer web worker is also a univer application.const univer = new Univer({diff --git a/packages/rpc/src/controllers/data-sync/data-sync-primary.controller.ts b/packages/rpc/src/controllers/data-sync/data-sync-primary.controller.tsindex 5d2d473780..7a88366680 100644--- a/packages/rpc/src/controllers/data-sync/data-sync-primary.controller.ts+++ b/packages/rpc/src/controllers/data-sync/data-sync-primary.controller.ts@@ -8,10 +8,18 @@ import {OnLifecycle,RxDisposable,} from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';import { takeUntil } from 'rxjs/operators';import type { IRemoteSyncMutationOptions } from '../../services/remote-instance/remote-instance.service';-import { IRemoteInstanceService } from '../../services/remote-instance/remote-instance.service';+import {+ IRemoteInstanceService,+ IRemoteSyncService,+ RemoteInstanceServiceName,+ RemoteSyncServiceName,+} from '../../services/remote-instance/remote-instance.service';+import { IRPChannelService } from '../../services/rpc/channel.service';+import { fromModule, toModule } from '../../services/rpc/rpc.service';/*** This controller is responsible for syncing data from the primary thread to@@ -19,17 +27,36 @@ import { IRemoteInstanceService } from '../../services/remote-instance/remote-in*/@OnLifecycle(LifecycleStages.Starting, DataSyncPrimaryController)export class DataSyncPrimaryController extends RxDisposable {+ private _remoteInstanceService!: IRemoteInstanceService;+constructor(private readonly _unsyncMutations: Set<string>,+ @Inject(Injector) private readonly _injector: Injector,+ @ICommandService private readonly _commandService: ICommandService,@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,- @IRemoteInstanceService private readonly _remoteInstanceService: IRemoteInstanceService,- @ICommandService private readonly _commandService: ICommandService+ @IRPChannelService private readonly _rpcChannelService: IRPChannelService,+ @IRemoteSyncService private readonly _remoteSyncService: IRemoteSyncService) {super();+ this._initRPCChannels();this._init();}+ private _initRPCChannels(): void {+ this._rpcChannelService.registerChannel(RemoteSyncServiceName, fromModule(this._remoteSyncService));++ this._injector.add([+ IRemoteInstanceService,+ {+ useFactory: () =>+ toModule<IRemoteInstanceService>(this._rpcChannelService.requestChannel(RemoteInstanceServiceName)),+ },+ ]);++ this._remoteInstanceService = this._injector.get(IRemoteInstanceService);+ }+private _init(): void {this._univerInstanceService.sheetAdded$.pipe(takeUntil(this.dispose$)).subscribe((sheet) => {// If a sheet is created, it should sync the data to the worker thread.diff --git a/packages/rpc/src/controllers/data-sync/data-sync-replica.controller.ts b/packages/rpc/src/controllers/data-sync/data-sync-replica.controller.tsindex 6ee245940a..ba3ce3a23d 100644--- a/packages/rpc/src/controllers/data-sync/data-sync-replica.controller.ts+++ b/packages/rpc/src/controllers/data-sync/data-sync-replica.controller.ts@@ -1,15 +1,16 @@import type { IMutationInfo } from '@univerjs/core';-import {- CommandType,- Disposable,- ICommandService,- IUniverInstanceService,- LifecycleStages,- OnLifecycle,-} from '@univerjs/core';+import { CommandType, Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';import type { IRemoteSyncMutationOptions } from '../../services/remote-instance/remote-instance.service';-import { IRemoteSyncService } from '../../services/remote-instance/remote-instance.service';+import {+ IRemoteInstanceService,+ IRemoteSyncService,+ RemoteInstanceServiceName,+ RemoteSyncServiceName,+} from '../../services/remote-instance/remote-instance.service';+import { IRPChannelService } from '../../services/rpc/channel.service';+import { fromModule, toModule } from '../../services/rpc/rpc.service';/*** This controller is responsible for syncing data from the worker thread to@@ -17,22 +18,39 @@ import { IRemoteSyncService } from '../../services/remote-instance/remote-instan*/@OnLifecycle(LifecycleStages.Starting, DataSyncReplicaController)export class DataSyncReplicaController extends Disposable {+ private _remoteSyncService!: IRemoteSyncService;+constructor(+ @Inject(Injector) private readonly _injector: Injector,+ @IRemoteInstanceService private readonly _remoteInstanceService: IRemoteInstanceService,@ICommandService private readonly _commandService: ICommandService,- @IRemoteSyncService private readonly _remoteInstanceService: IRemoteSyncService,- @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService+ @IRPChannelService private readonly _rpcChannelService: IRPChannelService) {super();+ this._initRPCChannels();this._init();}+ private _initRPCChannels(): void {+ this._rpcChannelService.registerChannel(RemoteInstanceServiceName, fromModule(this._remoteInstanceService));++ this._injector.add([+ IRemoteSyncService,+ {+ useFactory: () =>+ toModule<IRemoteSyncService>(this._rpcChannelService.requestChannel(RemoteSyncServiceName)),+ },+ ]);+ this._remoteSyncService = this._injector.get(IRemoteSyncService);+ }+private _init(): void {this.disposeWithMe(// Mutations executed on the main thread should be synced to the worker thread.this._commandService.onCommandExecuted((commandInfo, options) => {if (commandInfo.type === CommandType.MUTATION && !(options as IRemoteSyncMutationOptions)?.fromSync) {- this._remoteInstanceService.syncMutation({+ this._remoteSyncService.syncMutation({mutationInfo: commandInfo as IMutationInfo,});}diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.tsindex e94ccae3b9..4fdf1b3b60 100644--- a/packages/rpc/src/index.ts+++ b/packages/rpc/src/index.ts@@ -4,3 +4,5 @@ export {UniverRPCMainThreadPlugin,UniverRPCWorkerThreadPlugin,} from './plugin';+export { IRPChannelService } from './services/rpc/channel.service';+export { ChannelClient, ChannelServer, type IMessageProtocol } from './services/rpc/rpc.service';diff --git a/packages/rpc/src/plugin.ts b/packages/rpc/src/plugin.tsindex 54a038ecb3..1a46ffb345 100644--- a/packages/rpc/src/plugin.ts+++ b/packages/rpc/src/plugin.ts@@ -1,33 +1,26 @@import { Plugin, PluginType } from '@univerjs/core';import type { Dependency } from '@wendellhu/redi';-import { createIdentifier, Inject, Injector } from '@wendellhu/redi';+import { Inject, Injector } from '@wendellhu/redi';import { DataSyncPrimaryController } from './controllers/data-sync/data-sync-primary.controller';import { DataSyncReplicaController } from './controllers/data-sync/data-sync-replica.controller';-import {- createWebWorkerMessagePortOnMain,- createWebWorkerMessagePortOnWorker,-} from './services/implementations/web-worker-rpc.service';import {IRemoteInstanceService,IRemoteSyncService,RemoteInstanceReplicaService,- RemoteInstanceServiceName,RemoteSyncPrimaryService,- RemoteSyncServiceName,} from './services/remote-instance/remote-instance.service';-import { ChannelClient, ChannelServer, fromModule, toModule } from './services/rpc/rpc.service';+import { ChannelService, IRPChannelService } from './services/rpc/channel.service';+import {+ createWebWorkerMessagePortOnMain,+ createWebWorkerMessagePortOnWorker,+} from './services/rpc/implementations/web-worker-rpc.service';export interface IUniverRPCMainThreadPluginConfig {workerURL: string | URL;unsyncMutations?: Set<string>;}-interface ITestService {- getName(): Promise<string>;-}-const ITestService = createIdentifier<ITestService>('ITestService');-/*** This plugin is used to register the RPC services on the main thread. It* is also responsible for booting up the Web Worker instance of Univer.@@ -45,10 +38,13 @@ export class UniverRPCMainThreadPlugin extends Plugin {override async onStarting(injector: Injector): Promise<void> {const worker = new Worker(this._config.workerURL);const messageProtocol = createWebWorkerMessagePortOnMain(worker);- const client = new ChannelClient(messageProtocol);- const server = new ChannelServer(messageProtocol);-const dependencies: Dependency[] = [+ [+ IRPChannelService,+ {+ useFactory: () => new ChannelService(messageProtocol),+ },+ ],[DataSyncPrimaryController,{@@ -56,15 +52,9 @@ export class UniverRPCMainThreadPlugin extends Plugin {injector.createInstance(DataSyncPrimaryController, this._config?.unsyncMutations ?? new Set()),},],- [- IRemoteInstanceService,- { useFactory: () => toModule<IRemoteInstanceService>(client.getChannel(RemoteInstanceServiceName)) },- ],[IRemoteSyncService, { useClass: RemoteSyncPrimaryService }],];dependencies.forEach((dependency) => injector.add(dependency));-- server.registerChannel(RemoteSyncServiceName, fromModule(injector.get(IRemoteSyncService)));}}@@ -84,20 +74,19 @@ export class UniverRPCWorkerThreadPlugin extends Plugin {}override onStarting(injector: Injector): void {- const messageProtocol = createWebWorkerMessagePortOnWorker();- const client = new ChannelClient(messageProtocol);- const server = new ChannelServer(messageProtocol);-- const dependencies: Dependency[] = [- [DataSyncReplicaController],+ ([- IRemoteSyncService,- { useFactory: () => toModule<IRemoteSyncService>(client.getChannel(RemoteSyncServiceName)) },- ],- [IRemoteInstanceService, { useClass: RemoteInstanceReplicaService }],- ];- dependencies.forEach((dependency) => injector.add(dependency));+ [DataSyncReplicaController],+ [+ IRPChannelService,+ {+ useFactory: () => new ChannelService(createWebWorkerMessagePortOnWorker()),+ },+ ],+ [IRemoteInstanceService, { useClass: RemoteInstanceReplicaService }],+ ] as Dependency[]+ ).forEach((dependency) => injector.add(dependency));- server.registerChannel(RemoteInstanceServiceName, fromModule(injector.get(IRemoteInstanceService)));+ injector.get(DataSyncReplicaController);}}diff --git a/packages/rpc/src/services/rpc/channel.service.ts b/packages/rpc/src/services/rpc/channel.service.tsnew file mode 100644index 0000000000..b4b3c8001a--- /dev/null+++ b/packages/rpc/src/services/rpc/channel.service.ts@@ -0,0 +1,32 @@+import { createIdentifier } from '@wendellhu/redi';++import type { IChannel, IMessageProtocol } from './rpc.service';+import { ChannelClient, ChannelServer } from './rpc.service';++export interface IRPChannelService {+ requestChannel(name: string): IChannel;+ registerChannel(name: string, channel: IChannel): void;+}++export const IRPChannelService = createIdentifier<IRPChannelService>('IRPChannelService');++/**+ * This service is responsible for managing the RPC channels.+ */+export class ChannelService {+ private readonly _client: ChannelClient;+ private readonly _server: ChannelServer;++ constructor(_messageProtocol: IMessageProtocol) {+ this._client = new ChannelClient(_messageProtocol);+ this._server = new ChannelServer(_messageProtocol);+ }++ requestChannel(name: string): IChannel {+ return this._client.getChannel(name);+ }++ registerChannel(name: string, channel: IChannel): void {+ this._server.registerChannel(name, channel);+ }+}diff --git a/packages/rpc/src/services/implementations/web-worker-rpc.service.ts b/packages/rpc/src/services/rpc/implementations/web-worker-rpc.service.tssimilarity index 95%rename from packages/rpc/src/services/implementations/web-worker-rpc.service.tsrename to packages/rpc/src/services/rpc/implementations/web-worker-rpc.service.tsindex 4d592578dc..05819d780c 100644--- a/packages/rpc/src/services/implementations/web-worker-rpc.service.ts+++ b/packages/rpc/src/services/rpc/implementations/web-worker-rpc.service.ts@@ -1,7 +1,7 @@/* eslint-disable @typescript-eslint/no-explicit-any */import { Observable, shareReplay } from 'rxjs';-import type { IMessageProtocol } from '../rpc/rpc.service';+import type { IMessageProtocol } from '../rpc.service';/*** Generate an `IMessageProtocol` on the web worker.diff --git a/packages/rpc/src/services/rpc/rpc.service.ts b/packages/rpc/src/services/rpc/rpc.service.tsindex 9aad224774..6801d31e70 100644--- a/packages/rpc/src/services/rpc/rpc.service.ts+++ b/packages/rpc/src/services/rpc/rpc.service.ts@@ -2,7 +2,6 @@/* eslint-disable @typescript-eslint/no-explicit-any */import { RxDisposable } from '@univerjs/core';-import { createIdentifier } from '@wendellhu/redi';import type { Subscriber, Subscription } from 'rxjs';import { BehaviorSubject, firstValueFrom, isObservable, Observable, of } from 'rxjs';import { filter, take, takeUntil } from 'rxjs/operators';@@ -402,9 +401,3 @@ export class ChannelServer extends RxDisposable implements IChannelServer {this._protocol.send(response);}}--/**- * This service is for other service to register channels to the RPC framework.- */-export interface IRPCService {}-export const IRPCService = createIdentifier<IRPCService>('IRPCService');