From 403381bd5a54085aa59f6c419a9f49362c08e532 Mon Sep 17 00:00:00 2001 From: zhangw Date: Fri, 15 Nov 2024 14:37:05 +0800 Subject: [PATCH] fix(sheets-formula): other formula register (#4073) --- .../trigger-calculation.controller.ts | 6 +++++- .../services/register-other-formula.service.ts | 16 +++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts index 1c445f4939d..120f91fd3d3 100644 --- a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts +++ b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts @@ -41,6 +41,7 @@ import { SetStyleCommand, } from '@univerjs/sheets'; import { BehaviorSubject } from 'rxjs'; +import { RegisterOtherFormulaService } from '../services/register-other-formula.service'; import { CalculationMode, PLUGIN_CONFIG_KEY_BASE } from './config.schema'; /** @@ -140,7 +141,8 @@ export class TriggerCalculationController extends Disposable { @ILogService private readonly _logService: ILogService, @IConfigService private readonly _configService: IConfigService, @Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel, - @Inject(LocaleService) private readonly _localeService: LocaleService + @Inject(LocaleService) private readonly _localeService: LocaleService, + @Inject(RegisterOtherFormulaService) private readonly _registerOtherFormulaService: RegisterOtherFormulaService ) { super(); @@ -510,6 +512,8 @@ export class TriggerCalculationController extends Disposable { private _initialExecuteFormula() { const params = this._getDiryDataByCalculationMode(this._calculationMode); this._commandService.executeCommand(SetFormulaCalculationStartMutation.id, params, lo); + + this._registerOtherFormulaService.calculateStarted$.next(true); } private _getDiryDataByCalculationMode(calculationMode: CalculationMode): IFormulaDirtyData { diff --git a/packages/sheets-formula/src/services/register-other-formula.service.ts b/packages/sheets-formula/src/services/register-other-formula.service.ts index 32d399db4c9..d6def09aab7 100644 --- a/packages/sheets-formula/src/services/register-other-formula.service.ts +++ b/packages/sheets-formula/src/services/register-other-formula.service.ts @@ -17,9 +17,9 @@ import type { IRange, Nullable } from '@univerjs/core'; import type { IRemoveOtherFormulaMutationParams, ISetFormulaCalculationResultMutation, ISetOtherFormulaMutationParams } from '@univerjs/engine-formula'; import type { IOtherFormulaMarkDirtyParams } from '../commands/mutations/formula.mutation'; -import { Disposable, ICommandService, Inject, LifecycleService, LifecycleStages, ObjectMatrix, Tools } from '@univerjs/core'; +import { Disposable, ICommandService, Inject, LifecycleService, ObjectMatrix, Tools } from '@univerjs/core'; import { IActiveDirtyManagerService, RemoveOtherFormulaMutation, SetFormulaCalculationResultMutation, SetOtherFormulaMutation } from '@univerjs/engine-formula'; -import { bufferWhen, filter, Subject } from 'rxjs'; +import { BehaviorSubject, bufferWhen, filter, Subject } from 'rxjs'; import { OtherFormulaMarkDirty } from '../commands/mutations/formula.mutation'; import { FormulaResultStatus, type IOtherFormulaResult } from './formula-common'; @@ -34,6 +34,8 @@ export class RegisterOtherFormulaService extends Disposable { private _formulaResult$ = new Subject>>(); public formulaResult$ = this._formulaResult$.asObservable(); + public calculateStarted$ = new BehaviorSubject(false); + constructor( @ICommandService private readonly _commandService: ICommandService, @IActiveDirtyManagerService private _activeDirtyManagerService: IActiveDirtyManagerService, @@ -49,6 +51,7 @@ export class RegisterOtherFormulaService extends Disposable { this._formulaChangeWithRange$.complete(); this._formulaResult$.complete(); + this.calculateStarted$.complete(); } private _ensureCacheMap(unitId: string, subUnitId: string) { @@ -113,16 +116,15 @@ export class RegisterOtherFormulaService extends Disposable { }); }; - // Wait until the stage is steady, then register the formula that needs to be marked dirty with formula and range list this.disposeWithMe( this._formulaChangeWithRange$ - .pipe(bufferWhen(() => this._lifecycleService.lifecycle$.pipe(filter((stage) => stage === LifecycleStages.Steady)))) + .pipe(bufferWhen(() => this.calculateStarted$.pipe(filter((calculateStarted) => calculateStarted)))) .subscribe((options) => options.forEach(handleRegister)) ); this.disposeWithMe( this._formulaChangeWithRange$ - .pipe(filter(() => this._lifecycleService.stage >= LifecycleStages.Steady)) + .pipe(filter(() => this.calculateStarted$.getValue())) .subscribe(handleRegister) ); } @@ -153,12 +155,12 @@ export class RegisterOtherFormulaService extends Disposable { continue; } - if (!item?.result) { + if (!item.result) { item.result = {}; } const resultMatrix = new ObjectMatrix(current); - const resultObject = new ObjectMatrix(item?.result); + const resultObject = new ObjectMatrix(item.result); resultMatrix.forValue((row, col, value) => { resultObject.setValue(row, col, value);