Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sheets-formula): other formula register #4073

Merged
merged 7 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -113,16 +115,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 +154,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
Loading