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.

fix: avoid BehaviorSubject replay loop in bufferWhen (#6882)

wpxp123456dfc5de756b46
diff --git a/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts b/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts
index 0afa721b0f..18be3c89b5 100644
--- a/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts
+++ b/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts
@@ -133,6 +133,27 @@ describe('RegisterOtherFormulaService', () => {
);
});
+ it('should not recursively reopen buffer when calculation starts', () => {
+ const commandService = createCommandServiceMock();
+ const activeDirtyManagerService = { register: vi.fn() } as unknown as IActiveDirtyManagerService;
+ const service = new RegisterOtherFormulaService(
+ commandService as never,
+ activeDirtyManagerService,
+ {} as LifecycleService
+ );
+ let subscribeCount = 0;
+ const calculateStarted$ = service.calculateStarted$ as any;
+ const originalSubscribe = calculateStarted$._subscribe.bind(calculateStarted$);
+ calculateStarted$._subscribe = (...args: unknown[]) => {
+ subscribeCount++;
+ return originalSubscribe(...args);
+ };
+
+ service.calculateStarted$.next(true);
+
+ expect(subscribeCount).toBe(1);
+ });
+
it('should cache formula results and resolve pending getFormulaValue', async () => {
const commandService = createCommandServiceMock();
const activeDirtyManagerService = { register: vi.fn() } as unknown as IActiveDirtyManagerService;
diff --git a/packages/engine-formula/src/services/register-other-formula.service.ts b/packages/engine-formula/src/services/register-other-formula.service.ts
index 4b0cb4e477..43735578ee 100644
--- a/packages/engine-formula/src/services/register-other-formula.service.ts
+++ b/packages/engine-formula/src/services/register-other-formula.service.ts
@@ -21,7 +21,7 @@ import type { ISetFormulaCalculationResultMutation } from '../commands/mutations
import type { IRemoveOtherFormulaMutationParams, ISetOtherFormulaMutationParams } from '../commands/mutations/set-other-formula.mutation';
import type { IOtherFormulaResult } from './formula-common';
import { Disposable, generateRandomId, ICommandService, Inject, LifecycleService, ObjectMatrix } from '@univerjs/core';
-import { BehaviorSubject, bufferWhen, filter, Subject } from 'rxjs';
+import { BehaviorSubject, bufferWhen, filter, skip, Subject } from 'rxjs';
import { OtherFormulaMarkDirty } from '../commands/mutations/formula.mutation';
import { SetFormulaCalculationResultMutation } from '../commands/mutations/set-formula-calculation.mutation';
import { RemoveOtherFormulaMutation, SetOtherFormulaMutation } from '../commands/mutations/set-other-formula.mutation';
@@ -133,7 +133,7 @@ export class RegisterOtherFormulaService extends Disposable {
this.disposeWithMe(
this._formulaChangeWithRange$
- .pipe(bufferWhen(() => this.calculateStarted$.pipe(filter((calculateStarted) => calculateStarted))))
+ .pipe(bufferWhen(() => this.calculateStarted$.pipe(skip(1), filter((calculateStarted) => calculateStarted))))
.subscribe((options) => options.forEach(handleRegister))
);
diff --git a/packages/sheets-data-validation/src/services/__tests__/dv-validator-service.spec.ts b/packages/sheets-data-validation/src/services/__tests__/dv-validator-service.spec.ts
index 17f194d72f..da881a7325 100644
--- a/packages/sheets-data-validation/src/services/__tests__/dv-validator-service.spec.ts
+++ b/packages/sheets-data-validation/src/services/__tests__/dv-validator-service.spec.ts
@@ -30,6 +30,13 @@ function createRule(uid: string, ranges: IRange[]): IDataValidationRule {
function createService() {
const dirtyRanges$ = new Subject<{ unitId: string; subUnitId: string; ranges: IRange[] }>();
const lifecycle$ = new BehaviorSubject(LifecycleStages.Rendered);
+ let lifecycleSubscribeCount = 0;
+ const lifecycleSubject = lifecycle$ as any;
+ const originalSubscribe = lifecycleSubject._subscribe.bind(lifecycleSubject);
+ lifecycleSubject._subscribe = (...args: unknown[]) => {
+ lifecycleSubscribeCount++;
+ return originalSubscribe(...args);
+ };
const worksheet = {
getSheetId: () => 'sheet-1',
getMergedCell: vi.fn(() => null),
@@ -81,6 +88,7 @@ function createService() {
cacheService,
univerInstanceService,
lifecycleService,
+ getLifecycleSubscribeCount: () => lifecycleSubscribeCount,
service: new SheetsDataValidationValidatorService(univerInstanceService, model, cacheService, lifecycleService),
};
}
@@ -99,6 +107,14 @@ describe('SheetsDataValidationValidatorService', () => {
vi.unstubAllGlobals();
});
+ it('does not recursively reopen dirty range buffer when created after render', () => {
+ const { service, getLifecycleSubscribeCount } = createService();
+
+ expect(getLifecycleSubscribeCount()).toBe(1);
+
+ service.dispose();
+ });
+
it('validates single cells, merged cells, ranges, worksheets, and workbooks', async () => {
const { service, worksheet, model, cacheService } = createService();
diff --git a/packages/sheets-data-validation/src/services/dv-validator-service.ts b/packages/sheets-data-validation/src/services/dv-validator-service.ts
index 750968687c..85afc52a15 100644
--- a/packages/sheets-data-validation/src/services/dv-validator-service.ts
+++ b/packages/sheets-data-validation/src/services/dv-validator-service.ts
@@ -16,7 +16,7 @@
import type { IDataValidationRule, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';
import { bufferDebounceTime, DataValidationStatus, Disposable, getIntersectRange, Inject, IUniverInstanceService, LifecycleService, LifecycleStages, Range, Tools, UniverInstanceType } from '@univerjs/core';
-import { bufferWhen, filter } from 'rxjs';
+import { bufferWhen, filter, skip } from 'rxjs';
import { SheetDataValidationModel } from '../models/sheet-data-validation-model';
import { DataValidationCacheService } from './dv-cache.service';
@@ -70,7 +70,7 @@ export class SheetsDataValidationValidatorService extends Disposable {
});
};
- this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe(bufferWhen(() => this._lifecycleService.lifecycle$.pipe(filter((stage) => stage === LifecycleStages.Rendered)))).subscribe(handleDirtyRanges));
+ this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe(bufferWhen(() => this._lifecycleService.lifecycle$.pipe(skip(1), filter((stage) => stage === LifecycleStages.Rendered)))).subscribe(handleDirtyRanges));
this.disposeWithMe(this._dataValidationCacheService.dirtyRanges$.pipe(filter(() => this._lifecycleService.stage >= LifecycleStages.Rendered), bufferDebounceTime(20)).subscribe(handleDirtyRanges));
}