Skip to content

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.

refactor: syncing mutation should be registered explicitly (#2401)

Wenzhao Huc5ae9609e88c
diff --git a/examples/src/sheets/worker.ts b/examples/src/sheets/worker.ts
index 98f821ce65..179c9d99cb 100644
--- a/examples/src/sheets/worker.ts
+++ b/examples/src/sheets/worker.ts
@@ -14,21 +14,20 @@
* limitations under the License.
*/
-import { LocaleType, Univer } from '@univerjs/core';
+import { LocaleType, LogLevel, Univer } from '@univerjs/core';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRPCWorkerThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsPlugin } from '@univerjs/sheets';
-import { UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
// Univer web worker is also a univer application.
const univer = new Univer({
locale: LocaleType.ZH_CN,
+ logLevel: LogLevel.VERBOSE,
});
univer.registerPlugin(UniverSheetsPlugin, { onlyRegisterFormulaRelatedMutations: true });
univer.registerPlugin(UniverFormulaEnginePlugin);
univer.registerPlugin(UniverRPCWorkerThreadPlugin);
-univer.registerPlugin(UniverSheetsFilterPlugin);
declare let self: WorkerGlobalScope & typeof globalThis & { univer: Univer };
self.univer = univer;
diff --git a/packages/engine-formula/package.json b/packages/engine-formula/package.json
index 3694ee2215..d40cd9489e 100644
--- a/packages/engine-formula/package.json
+++ b/packages/engine-formula/package.json
@@ -64,6 +64,7 @@
},
"peerDependencies": {
"@univerjs/core": "workspace:*",
+ "@univerjs/rpc": "workspace:*",
"@wendellhu/redi": "0.15.2",
"rxjs": ">=7.0.0"
},
@@ -74,6 +75,7 @@
"devDependencies": {
"@types/big.js": "^6.2.2",
"@univerjs/core": "workspace:*",
+ "@univerjs/rpc": "workspace:*",
"@univerjs/shared": "workspace:*",
"@wendellhu/redi": "0.15.2",
"rxjs": "^7.8.1",
diff --git a/packages/engine-formula/src/controller/formula.controller.ts b/packages/engine-formula/src/controller/formula.controller.ts
index 94acf29f34..1971290e0f 100644
--- a/packages/engine-formula/src/controller/formula.controller.ts
+++ b/packages/engine-formula/src/controller/formula.controller.ts
@@ -15,8 +15,8 @@
*/
import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';
-import type { Ctor } from '@wendellhu/redi';
-import { Inject, Injector } from '@wendellhu/redi';
+import { type Ctor, Optional } from '@wendellhu/redi';
+import { DataSyncPrimaryController } from '@univerjs/rpc';
import type { IFunctionNames } from '../basics/function';
import { RegisterFunctionMutation } from '../commands/mutations/register-function.mutation';
@@ -64,8 +64,8 @@ export class FormulaController extends Disposable {
constructor(
private _function: Array<[Ctor<BaseFunction>, IFunctionNames]> = [],
@ICommandService private readonly _commandService: ICommandService,
- @Inject(Injector) private readonly _injector: Injector,
- @IFunctionService private readonly _functionService: IFunctionService
+ @IFunctionService private readonly _functionService: IFunctionService,
+ @Optional(DataSyncPrimaryController) private readonly _dataSyncPrimaryController?: DataSyncPrimaryController
) {
super();
@@ -98,7 +98,10 @@ export class FormulaController extends Disposable {
SetSuperTableOptionMutation,
RegisterFunctionMutation,
UnregisterFunctionMutation,
- ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
+ ].forEach((mutation) => {
+ this._commandService.registerCommand(mutation);
+ this._dataSyncPrimaryController?.registerSyncingMutations(mutation);
+ });
}
private _registerFunctions() {
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.ts
index f225da697b..ae87da74bf 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
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IMutationInfo, Workbook } from '@univerjs/core';
+import type { IMutation, IMutationInfo, Workbook } from '@univerjs/core';
import {
CommandType,
ICommandService,
@@ -44,8 +44,11 @@ import { fromModule, toModule } from '../../services/rpc/rpc.service';
@OnLifecycle(LifecycleStages.Starting, DataSyncPrimaryController)
export class DataSyncPrimaryController extends RxDisposable {
private _remoteInstanceService!: IRemoteInstanceService;
+
private readonly _syncingUnits = new Set<string>();
+ private readonly _syncingMutations = new Set<string>();
+
constructor(
@Inject(Injector) private readonly _injector: Injector,
@ICommandService private readonly _commandService: ICommandService,
@@ -59,17 +62,19 @@ export class DataSyncPrimaryController extends RxDisposable {
this._init();
}
+ registerSyncingMutations(mutation: IMutation<object>): void {
+ this._syncingMutations.add(mutation.id);
+ }
+
private _initRPCChannels(): void {
+ // for the worker to call
this._rpcChannelService.registerChannel(RemoteSyncServiceName, fromModule(this._remoteSyncService));
+ // to call the worker
this._injector.add([
IRemoteInstanceService,
- {
- useFactory: () =>
- toModule<IRemoteInstanceService>(this._rpcChannelService.requestChannel(RemoteInstanceServiceName)),
- },
+ { useFactory: () => toModule<IRemoteInstanceService>(this._rpcChannelService.requestChannel(RemoteInstanceServiceName)) },
]);
-
this._remoteInstanceService = this._injector.get(IRemoteInstanceService);
}
@@ -93,21 +98,20 @@ export class DataSyncPrimaryController extends RxDisposable {
});
});
- this.disposeWithMe(
- // Mutations executed on the main thread should be synced to the worker thread.
- this._commandService.onCommandExecuted((commandInfo, options) => {
- const { type, params } = commandInfo;
- const unitID = (params as any)?.unitId || '';
- if (
- // only sync mutations to the worker thread
- type === CommandType.MUTATION &&
- (!unitID || this._syncingUnits.has(unitID)) &&
- // do not sync mutations from the web worker back to the web worker
- !(options as IRemoteSyncMutationOptions)?.fromSync
- ) {
- this._remoteInstanceService.syncMutation({ mutationInfo: commandInfo as IMutationInfo });
- }
- })
- );
+ // Mutations executed on the main thread should be synced to the worker thread.
+ this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo, options) => {
+ const { type, params, id } = commandInfo;
+ const unitId = (params as { unitId: string })?.unitId || '';
+ if (type === CommandType.MUTATION &&
+ // only sync mutations to the worker thread
+ (!unitId || this._syncingUnits.has(unitId)) &&
+ // do not sync mutations from the web worker back to the web worker
+ !(options as IRemoteSyncMutationOptions)?.fromSync &&
+ // do not sync mutations those are not meant to be synced
+ this._syncingMutations.has(id)
+ ) {
+ this._remoteInstanceService.syncMutation({ mutationInfo: commandInfo as IMutationInfo });
+ }
+ }));
}
}
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.ts
index 43310a178e..4d17da17aa 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
@@ -53,10 +53,7 @@ export class DataSyncReplicaController extends Disposable {
this._injector.add([
IRemoteSyncService,
- {
- useFactory: () =>
- toModule<IRemoteSyncService>(this._rpcChannelService.requestChannel(RemoteSyncServiceName)),
- },
+ { useFactory: () => toModule<IRemoteSyncService>(this._rpcChannelService.requestChannel(RemoteSyncServiceName)) },
]);
this._remoteSyncService = this._injector.get(IRemoteSyncService);
}
diff --git a/packages/rpc/src/services/remote-instance/remote-instance.service.ts b/packages/rpc/src/services/remote-instance/remote-instance.service.ts
index c1bbc614a1..6d92652ebb 100644
--- a/packages/rpc/src/services/remote-instance/remote-instance.service.ts
+++ b/packages/rpc/src/services/remote-instance/remote-instance.service.ts
@@ -16,7 +16,7 @@
import type { IExecutionOptions, IMutationInfo, IWorkbookData } from '@univerjs/core';
import { ICommandService, ILogService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { createIdentifier, Inject, Injector } from '@wendellhu/redi';
+import { createIdentifier } from '@wendellhu/redi';
export interface IRemoteSyncMutationOptions extends IExecutionOptions {
/** If this mutation is executed after it was sent from the peer univer instance (e.g. in a web worker). */
@@ -66,7 +66,6 @@ export interface IRemoteInstanceService {
export class WebWorkerRemoteInstanceService implements IRemoteInstanceService {
constructor(
- @Inject(Injector) private readonly _injector: Injector,
@IUniverInstanceService protected readonly _univerInstanceService: IUniverInstanceService,
@ICommandService protected readonly _commandService: ICommandService,
@ILogService protected readonly _logService: ILogService
@@ -113,11 +112,6 @@ export class WebWorkerRemoteInstanceService implements IRemoteInstanceService {
protected _applyMutation(mutationInfo: IMutationInfo): boolean {
const { id, params: mutationParams } = mutationInfo;
- if (!this._commandService.hasCommand(id)) {
- this._logService.debug('[RemoteInstanceReplicaService]', `command "${id}" not found. Skip sync mutation.`);
- return true;
- }
-
return this._commandService.syncExecuteCommand(id, mutationParams, {
onlyLocal: true,
fromSync: true,
diff --git a/packages/sheets/package.json b/packages/sheets/package.json
index 1561c05827..35666c5e3c 100644
--- a/packages/sheets/package.json
+++ b/packages/sheets/package.json
@@ -68,6 +68,7 @@
"@univerjs/core": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
+ "@univerjs/rpc": "workspace:*",
"@wendellhu/redi": "0.15.2",
"rxjs": ">=7.0.0"
},
@@ -79,6 +80,7 @@
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-numfmt": "workspace:*",
"@univerjs/engine-render": "workspace:*",
+ "@univerjs/rpc": "workspace:*",
"@univerjs/shared": "workspace:*",
"@wendellhu/redi": "0.15.2",
"rxjs": "^7.8.1",
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index 8e56d73175..1f7d989171 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -14,9 +14,10 @@
* limitations under the License.
*/
-import type { IStyleData } from '@univerjs/core';
+import type { IMutation, IStyleData } from '@univerjs/core';
import { Disposable, ICommandService, IConfigService, LifecycleStages, OnLifecycle } from '@univerjs/core';
-import type { IDisposable } from '@wendellhu/redi';
+import { type IDisposable, Optional } from '@wendellhu/redi';
+import { DataSyncPrimaryController } from '@univerjs/rpc';
import { ClearSelectionAllCommand } from '../commands/commands/clear-selection-all.command';
import { ClearSelectionContentCommand } from '../commands/commands/clear-selection-content.command';
@@ -144,123 +145,126 @@ export class BasicWorksheetController extends Disposable implements IDisposable
// eslint-disable-next-line max-lines-per-function
constructor(
@ICommandService private readonly _commandService: ICommandService,
- @IConfigService private readonly _configService: IConfigService
+ @IConfigService private readonly _configService: IConfigService,
+ @Optional(DataSyncPrimaryController) private readonly _dataSyncPrimaryController?: DataSyncPrimaryController
) {
super();
- [
- AddWorksheetMergeMutation,
- ClearSelectionAllCommand,
- ClearSelectionContentCommand,
- ClearSelectionFormatCommand,
- CopySheetCommand,
- DeleteRangeMoveLeftCommand,
- DeleteRangeMoveUpCommand,
- DeltaColumnWidthCommand,
- DeltaRowHeightCommand,
- InsertColAfterCommand,
- InsertColBeforeCommand,
- InsertColCommand,
+ ([
+ SetRangeValuesMutation,
InsertColMutation,
- InsertRangeMoveDownCommand,
- InsertRangeMoveRightCommand,
- InsertRowAfterCommand,
- InsertRowBeforeCommand,
- InsertRowCommand,
InsertRowMutation,
- InsertSheetCommand,
InsertSheetMutation,
- MoveColsCommand,
- MoveColsMutation,
- MoveRangeCommand,
MoveRangeMutation,
- MoveRowsCommand,
MoveRowsMutation,
- RemoveColCommand,
+ MoveColsMutation,
RemoveColMutation,
- RemoveRowCommand,
RemoveRowMutation,
- RemoveSheetCommand,
RemoveSheetMutation,
- RemoveWorksheetMergeCommand,
RemoveWorksheetMergeMutation,
- ResetBackgroundColorCommand,
- ResetTextColorCommand,
- SetBackgroundColorCommand,
- SetBorderBasicCommand,
- SetBorderColorCommand,
- SetBorderCommand,
- SetBorderPositionCommand,
- SetBorderStyleCommand,
- SetColHiddenCommand,
- SetColHiddenMutation,
- SetColVisibleMutation,
- SetColWidthCommand,
- SetFrozenCancelCommand,
- SetFrozenCommand,
- SetFrozenMutation,
- SetHorizontalTextAlignCommand,
- SetRangeValuesCommand,
- SetRangeValuesMutation,
- SetRowHeightCommand,
- SetRowHiddenCommand,
- SetRowHiddenMutation,
- SetRowVisibleMutation,
- SetSelectedColsVisibleCommand,
- SetSelectedRowsVisibleCommand,
- SetSpecificColsVisibleCommand,
- SetSpecificRowsVisibleCommand,
- SetStyleCommand,
- SetTabColorCommand,
- SetTabColorMutation,
- SetTextColorCommand,
- SetTextRotationCommand,
- SetTextWrapCommand,
- SetVerticalTextAlignCommand,
- SetWorkbookNameCommand,
+ RemoveNumfmtMutation,
+ AddWorksheetMergeMutation,
SetWorkbookNameMutation,
- SetWorksheetActivateCommand,
- SetWorksheetActiveOperation,
- SetWorksheetColWidthMutation,
- SetWorksheetHideCommand,
- SetWorksheetHideMutation,
- SetWorksheetNameCommand,
SetWorksheetNameMutation,
- SetWorksheetOrderCommand,
- SetWorksheetOrderMutation,
- SetWorksheetRowAutoHeightMutation,
- SetWorksheetRowHeightMutation,
- SetWorksheetRowIsAutoHeightCommand,
- SetWorksheetRowIsAutoHeightMutation,
SetNumfmtMutation,
- SetSelectionsOperation,
- RemoveNumfmtMutation,
EmptyMutation,
- ScrollToCellOperation,
- InsertDefinedNameCommand,
- RemoveDefinedNameCommand,
- SetDefinedNameCommand,
-
- SetWorksheetPermissionPointsCommand,
- AddWorksheetProtectionMutation,
- SetWorksheetProtectionMutation,
- DeleteWorksheetProtectionMutation,
- SetWorksheetPermissionPointsMutation,
-
- // range protection
- AddRangeProtectionCommand,
- DeleteRangeProtectionCommand,
- SetRangeProtectionCommand,
- AddRangeProtectionMutation,
- DeleteRangeProtectionMutation,
- SetRangeProtectionMutation,
- ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
+ ] as IMutation<object>[]).forEach((mutation) => {
+ this._commandService.registerCommand(mutation);
+ this._dataSyncPrimaryController?.registerSyncingMutations(mutation);
+ });
const onlyRegisterFormulaRelatedMutations = this._configService.getConfig(ONLY_REGISTER_FORMULA_RELATED_MUTATIONS_KEY) ?? false;
if (!onlyRegisterFormulaRelatedMutations) {
- // TODO: more commands should be moved into this array
[
+ ClearSelectionAllCommand,
+ ClearSelectionContentCommand,
+ ClearSelectionFormatCommand,
+ CopySheetCommand,
+ DeleteRangeMoveLeftCommand,
+ DeleteRangeMoveUpCommand,
+ DeltaColumnWidthCommand,
+ DeltaRowHeightCommand,
+ InsertColAfterCommand,
+ InsertColBeforeCommand,
+ InsertColCommand,
+ InsertRangeMoveDownCommand,
+ InsertRangeMoveRightCommand,
+ InsertRowAfterCommand,
+ InsertRowBeforeCommand,
+ InsertRowCommand,
+ InsertSheetCommand,
+ MoveColsCommand,
+ MoveRangeCommand,
+ MoveRowsCommand,
+ RemoveColCommand,
+ RemoveRowCommand,
+ RemoveSheetCommand,
+
+ RemoveWorksheetMergeCommand,
+ ResetBackgroundColorCommand,
+ ResetTextColorCommand,
+ SetBackgroundColorCommand,
+ SetBorderBasicCommand,
+ SetBorderColorCommand,
+ SetBorderCommand,
+ SetBorderPositionCommand,
+ SetBorderStyleCommand,
+ SetColHiddenCommand,
+ SetColHiddenMutation,
+ SetColVisibleMutation,
+ SetColWidthCommand,
+ SetFrozenCancelCommand,
+ SetFrozenCommand,
+ SetFrozenMutation,
+ SetHorizontalTextAlignCommand,
+ SetRangeValuesCommand,
+ SetRowHeightCommand,
+ SetRowHiddenCommand,
+ SetRowHiddenMutation,
+ SetRowVisibleMutation,
+ SetSelectedColsVisibleCommand,
+ SetSelectedRowsVisibleCommand,
+ SetSpecificColsVisibleCommand,
+ SetSpecificRowsVisibleCommand,
+ SetStyleCommand,
+ SetTabColorCommand,
+ SetTabColorMutation,
+ SetTextColorCommand,
+ SetTextRotationCommand,
+ SetTextWrapCommand,
+ SetVerticalTextAlignCommand,
+ SetWorkbookNameCommand,
+ SetWorksheetActivateCommand,
+ SetWorksheetActiveOperation,
+ SetWorksheetColWidthMutation,
+ SetWorksheetHideCommand,
+ SetWorksheetHideMutation,
+ SetWorksheetNameCommand,
+ SetWorksheetOrderCommand,
+ SetWorksheetOrderMutation,
+ SetWorksheetRowAutoHeightMutation,
+ SetWorksheetRowHeightMutation,
+ SetWorksheetRowIsAutoHeightCommand,
+ SetWorksheetRowIsAutoHeightMutation,
+ SetSelectionsOperation,
+ ScrollToCellOperation,
+ InsertDefinedNameCommand,
+ RemoveDefinedNameCommand,
+ SetDefinedNameCommand,
SetWorksheetShowCommand,
+
+ // permissions range protection
+ SetWorksheetPermissionPointsCommand,
+ AddWorksheetProtectionMutation,
+ SetWorksheetProtectionMutation,
+ DeleteWorksheetProtectionMutation,
+ SetWorksheetPermissionPointsMutation,
+ AddRangeProtectionCommand,
+ DeleteRangeProtectionCommand,
+ SetRangeProtectionCommand,
+ AddRangeProtectionMutation,
+ DeleteRangeProtectionMutation,
+ SetRangeProtectionMutation,
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1133be3033..6106679ba4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -865,6 +865,9 @@ importers:
'@univerjs/core':
specifier: workspace:*
version: link:../core
+ '@univerjs/rpc':
+ specifier: workspace:*
+ version: link:../rpc
'@univerjs/shared':
specifier: workspace:*
version: link:../../common/shared
@@ -1115,6 +1118,9 @@ importers:
'@univerjs/engine-render':
specifier: workspace:*
version: link:../engine-render
+ '@univerjs/rpc':
+ specifier: workspace:*
+ version: link:../rpc
'@univerjs/shared':
specifier: workspace:*
version: link:../../common/shared