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
feat: run formula calculation in worker (#492)
Wenzhao Hu6b1148eb3bf1
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsxindex ef84bae7ec..6d30d51b6d 100644--- a/examples/univer-sheet-ts/src/main.tsx+++ b/examples/univer-sheet-ts/src/main.tsx@@ -1,4 +1,5 @@import { DocPlugin } from '@univerjs/base-docs';+import { BaseFormulaEnginePlugin } from '@univerjs/base-formula-engine';import { RenderEngine } from '@univerjs/base-render';import { SheetPlugin } from '@univerjs/base-sheets';import { UIPlugin } from '@univerjs/base-ui';@@ -43,7 +44,10 @@ univer.registerPlugin(SheetUIPlugin);univer.registerPlugin(DebuggerPlugin);// univer.registerPlugin(NumfmtPlugin);// univer.registerPlugin(FindPlugin);-univer.registerPlugin(FormulaPlugin);+univer.registerPlugin(BaseFormulaEnginePlugin);+univer.registerPlugin(FormulaPlugin, {+ notExecuteFormula: true,+});univer.registerPlugin(FormulaUIPlugin);univer.registerPlugin(UniverRPCMainThreadPlugin, {workerURL: './worker.js',diff --git a/examples/univer-sheet-ts/src/worker.ts b/examples/univer-sheet-ts/src/worker.tsindex b09111d61c..fdb292433c 100644--- a/examples/univer-sheet-ts/src/worker.ts+++ b/examples/univer-sheet-ts/src/worker.ts@@ -1,8 +1,10 @@// The entry of web worker scripts+import { BaseFormulaEnginePlugin } from '@univerjs/base-formula-engine';import { SheetPlugin } from '@univerjs/base-sheets';import { LocaleType, Univer } from '@univerjs/core';import { UniverRPCWorkerThreadPlugin } from '@univerjs/rpc';+import { FormulaPlugin } from '@univerjs/sheets-plugin-formula';// Univer web worker is also a univer application.const univer = new Univer({@@ -10,7 +12,9 @@ const univer = new Univer({});univer.registerPlugin(SheetPlugin);+univer.registerPlugin(BaseFormulaEnginePlugin);univer.registerPlugin(UniverRPCWorkerThreadPlugin);+univer.registerPlugin(FormulaPlugin);declare let self: WorkerGlobalScope & typeof globalThis & { univer: Univer };self.univer = univer;diff --git a/packages/base-formula-engine/package.json b/packages/base-formula-engine/package.jsonindex c2c7eea24e..641e2e7a08 100644--- a/packages/base-formula-engine/package.json+++ b/packages/base-formula-engine/package.json@@ -6,7 +6,7 @@"author": "DreamNum <[email protected]>","license": "MIT","main": "./src/index.ts",- "types": "./lib/index.d.ts",+ "types": "./lib/index.ts","publishConfig": {"access": "public","main": "./lib/index.cjs.js",diff --git a/packages/base-formula-engine/src/index.ts b/packages/base-formula-engine/src/index.tsindex 1440f6369e..1f947e00b3 100644--- a/packages/base-formula-engine/src/index.ts+++ b/packages/base-formula-engine/src/index.ts@@ -10,6 +10,7 @@ export type {} from './basics/common';export { FUNCTION_NAMES, FunctionType, type IFunctionInfo, type IFunctionParam } from './basics/function';export { type ISequenceNode, sequenceNodeType } from './basics/sequence';+export { BaseFormulaEnginePlugin } from './plugin';export { RangeReferenceObject } from './reference-object/range-reference-object';export { FormulaEngineService } from './services/formula-engine.service';export { IFunctionService } from './services/function.service';diff --git a/packages/base-formula-engine/src/plugin.ts b/packages/base-formula-engine/src/plugin.tsnew file mode 100644index 0000000000..47c4846fca--- /dev/null+++ b/packages/base-formula-engine/src/plugin.ts@@ -0,0 +1,19 @@+import { Plugin } from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';++import { FormulaEngineService } from './services/formula-engine.service';++const PLUGIN_NAME = 'base-formula-engine';++export class BaseFormulaEnginePlugin extends Plugin {+ constructor(+ _config: undefined,+ @Inject(Injector) protected override _injector: Injector+ ) {+ super(PLUGIN_NAME);+ }++ override onStarting(injector: Injector): void {+ injector.add([FormulaEngineService]);+ }+}diff --git a/packages/base-formula-engine/src/types/index.d.ts b/packages/base-formula-engine/src/types/index.d.tsdeleted file mode 100644index 7e50ad6146..0000000000--- a/packages/base-formula-engine/src/types/index.d.ts+++ /dev/null@@ -1,2 +0,0 @@-export * from '../index';-declare module '@univerjs/base-formula-engine' {}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 99882c22f2..5b04cb036e 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,6 +1,14 @@-import { Disposable, IUniverInstanceService, LifecycleStages, OnLifecycle } from '@univerjs/core';+import {+ CommandType,+ Disposable,+ ICommandService,+ IMutationInfo,+ IUniverInstanceService,+ LifecycleStages,+ OnLifecycle,+} from '@univerjs/core';-import { IRemoteInstanceService } from '../../services/remote-instance/remote-instance.service';+import { IRemoteSyncMutationOptions, IRemoteSyncService } from '../../services/remote-instance/remote-instance.service';/*** This controller is responsible for syncing data from the worker thread to@@ -9,9 +17,25 @@ import { IRemoteInstanceService } from '../../services/remote-instance/remote-in@OnLifecycle(LifecycleStages.Starting, DataSyncReplicaController)export class DataSyncReplicaController extends Disposable {constructor(- @IRemoteInstanceService private readonly: IRemoteInstanceService,+ @ICommandService private readonly _commandService: ICommandService,+ @IRemoteSyncService private readonly _remoteInstanceService: IRemoteSyncService,@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService) {super();++ this._init();+ }++ 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({+ mutationInfo: commandInfo as IMutationInfo,+ });+ }+ })+ );}}diff --git a/packages/rpc/src/services/remote-instance/remote-instance.service.ts b/packages/rpc/src/services/remote-instance/remote-instance.service.tsindex 9b82a923e0..070ae1097d 100644--- a/packages/rpc/src/services/remote-instance/remote-instance.service.ts+++ b/packages/rpc/src/services/remote-instance/remote-instance.service.ts@@ -7,7 +7,8 @@ import {IWorkbookData,} from '@univerjs/core';import { createIdentifier } from '@wendellhu/redi';-import { type } from 'os';++// TODO@wzhudev: add some notes here. Even I cannot understand the code in 10 seconds after days.export interface IRemoteSyncMutationOptions extends IExecutionOptions {/** If this mutation is executed after it was sent from the peer univer instance. */diff --git a/packages/sheets-plugin-formula/src/controllers/calculate.controller.ts b/packages/sheets-plugin-formula/src/controllers/calculate.controller.tsindex 2710f3d419..4389eec0a2 100644--- a/packages/sheets-plugin-formula/src/controllers/calculate.controller.ts+++ b/packages/sheets-plugin-formula/src/controllers/calculate.controller.ts@@ -4,6 +4,7 @@ import {Disposable,ICommandInfo,ICommandService,+ IConfigService,IRange,isFormulaId,isFormulaString,@@ -22,6 +23,7 @@ import { FormulaDataModel } from '../models/formula-data.model';@OnLifecycle(LifecycleStages.Starting, CalculateController)export class CalculateController extends Disposable {constructor(+ @IConfigService private readonly _configService: IConfigService,@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,@ICommandService private readonly _commandService: ICommandService,@Inject(FormulaEngineService) private readonly _formulaEngineService: FormulaEngineService,diff --git a/packages/sheets-plugin-formula/src/formula-plugin.ts b/packages/sheets-plugin-formula/src/formula-plugin.tsindex e8e5514a8e..38a9ae3a00 100644--- a/packages/sheets-plugin-formula/src/formula-plugin.ts+++ b/packages/sheets-plugin-formula/src/formula-plugin.ts@@ -1,4 +1,4 @@-import { IUniverInstanceService, Plugin, PluginType } from '@univerjs/core';+import { Plugin, PluginType } from '@univerjs/core';import { Dependency, Inject, Injector } from '@wendellhu/redi';import { FORMULA_PLUGIN_NAME } from './common/plugin-name';@@ -10,9 +10,8 @@ export class FormulaPlugin extends Plugin {static override type = PluginType.Sheet;constructor(- private _config: IFormulaConfig,- @Inject(Injector) override readonly _injector: Injector,- @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService+ private _config: IFormulaConfig | undefined,+ @Inject(Injector) override readonly _injector: Injector) {super(FORMULA_PLUGIN_NAME);}@@ -23,13 +22,16 @@ export class FormulaPlugin extends Plugin {[FormulaDataModel, { useFactory: () => this._injector.createInstance(FormulaDataModel, this._config) }],// controllers[FormulaController],- [CalculateController],];+ if (!this._config?.notExecuteFormula) {+ dependencies.push([CalculateController]);+ }+dependencies.forEach((dependency) => this._injector.add(dependency));}- override onReady(): void {+ override onStarting(): void {this.initialize();}}diff --git a/packages/sheets-plugin-formula/src/models/formula-data.model.ts b/packages/sheets-plugin-formula/src/models/formula-data.model.tsindex 6aa42dc996..fa9a8ea930 100644--- a/packages/sheets-plugin-formula/src/models/formula-data.model.ts+++ b/packages/sheets-plugin-formula/src/models/formula-data.model.ts@@ -2,6 +2,8 @@ import { ArrayFormulaDataType, IFormulaData } from '@univerjs/base-formula-enginimport { Disposable, ObjectMatrix } from '@univerjs/core';export interface IFormulaConfig {+ notExecuteFormula?: boolean;+formulaData: IFormulaData;}diff --git a/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts b/packages/ui-plugin-sheets/src/sheet-ui-plugin.tsindex be5d410778..89d03d98fd 100644--- a/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts+++ b/packages/ui-plugin-sheets/src/sheet-ui-plugin.ts@@ -1,4 +1,3 @@-import { FormulaEngineService } from '@univerjs/base-formula-engine';import { IUniverInstanceService, LocaleService, Plugin, PluginType } from '@univerjs/core';import { Dependency, Inject, Injector } from '@wendellhu/redi';import { filter } from 'rxjs/operators';@@ -77,7 +76,6 @@ export class SheetUIPlugin extends Plugin {useClass: SelectionRenderService,},],- [FormulaEngineService],[IStatusBarService, { useClass: StatusBarService }],// controllers