Skip to content

Commit

Permalink
fix(sheets-formula): other formula register (#4073)
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 authored Nov 15, 2024
1 parent e94cad2 commit 403381b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -34,6 +34,8 @@ export class RegisterOtherFormulaService extends Disposable {
private _formulaResult$ = new Subject<Record<string, Record<string, IOtherFormulaResult[]>>>();
public formulaResult$ = this._formulaResult$.asObservable();

public calculateStarted$ = new BehaviorSubject(false);

constructor(
@ICommandService private readonly _commandService: ICommandService,
@IActiveDirtyManagerService private _activeDirtyManagerService: IActiveDirtyManagerService,
Expand All @@ -49,6 +51,7 @@ export class RegisterOtherFormulaService extends Disposable {

this._formulaChangeWithRange$.complete();
this._formulaResult$.complete();
this.calculateStarted$.complete();
}

private _ensureCacheMap(unitId: string, subUnitId: string) {
Expand Down Expand Up @@ -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)
);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 403381b

Please sign in to comment.