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(sheets-data-validation): data-validation perf issues (#2810)

zhangw3a4de22ac978
diff --git a/packages/engine-render/src/components/sheets/sheet-skeleton.ts b/packages/engine-render/src/components/sheets/sheet-skeleton.ts
index da92b3cc10..0f37c7b894 100644
--- a/packages/engine-render/src/components/sheets/sheet-skeleton.ts
+++ b/packages/engine-render/src/components/sheets/sheet-skeleton.ts
@@ -431,13 +431,14 @@ export class SpreadsheetSkeleton extends Skeleton {
const results: IRowAutoHeightInfo[] = [];
const { mergeData, rowData } = this._worksheetData;
const rowObjectArray = rowData;
+ const calculatedRows = new Set<number>();
for (const range of ranges) {
const { startRow, endRow, startColumn, endColumn } = range;
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
// If the row has already been calculated, it does not need to be calculated
- if (results.some(({ row }) => row === rowIndex)) {
+ if (calculatedRows.has(rowIndex)) {
continue;
}
@@ -450,7 +451,7 @@ export class SpreadsheetSkeleton extends Skeleton {
if (hasUnMergedCell) {
const autoHeight = this._calculateRowAutoHeight(rowIndex);
-
+ calculatedRows.add(rowIndex);
results.push({
row: rowIndex,
autoHeight,
diff --git a/packages/sheets-data-validation/src/controllers/dv-render.controller.ts b/packages/sheets-data-validation/src/controllers/dv-render.controller.ts
index fa72dac76b..6dfa6c7c75 100644
--- a/packages/sheets-data-validation/src/controllers/dv-render.controller.ts
+++ b/packages/sheets-data-validation/src/controllers/dv-render.controller.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICellDataForSheetInterceptor, ICellRenderContext, Workbook } from '@univerjs/core';
+import type { ICellDataForSheetInterceptor, ICellRenderContext, IRange, Workbook } from '@univerjs/core';
import { DataValidationRenderMode, DataValidationStatus, DataValidationType, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable, sequenceExecute, UniverInstanceType, WrapStrategy } from '@univerjs/core';
import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';
import type { MenuConfig } from '@univerjs/ui';
@@ -24,6 +24,7 @@ import { AutoHeightController, IEditorBridgeService, SheetSkeletonManagerService
import type { Spreadsheet } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
+import { bufferTime, debounceTime } from 'rxjs';
import { getCellValueOrigin } from '../utils/get-cell-data-origin';
import type { ListValidator } from '../validators';
import type { SheetDataValidationManager } from '../models/sheet-data-validation-manager';
@@ -170,8 +171,8 @@ export class SheetsDataValidationRenderController extends RxDisposable {
}
};
- this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(() => markSkeletonDirty()));
- this.disposeWithMe(this._dataValidationModel.validStatusChange$.subscribe(() => markSkeletonDirty()));
+ this.disposeWithMe(this._dataValidationModel.ruleChange$.pipe(debounceTime(16)).subscribe(() => markSkeletonDirty()));
+ this.disposeWithMe(this._dataValidationModel.validStatusChange$.pipe(debounceTime(16)).subscribe(() => markSkeletonDirty()));
}
// eslint-disable-next-line max-lines-per-function
@@ -311,9 +312,16 @@ export class SheetsDataValidationRenderController extends RxDisposable {
}
private _initAutoHeight() {
- this._dataValidationModel.ruleChange$.subscribe((info) => {
- if (info.rule?.ranges) {
- const mutations = this._autoHeightController.getUndoRedoParamsOfAutoHeight(info.rule.ranges);
+ this._dataValidationModel.ruleChange$.pipe(bufferTime(16)).subscribe((infos) => {
+ const ranges: IRange[] = [];
+ infos.forEach((info) => {
+ if (info.rule?.ranges) {
+ ranges.push(...info.rule.ranges);
+ }
+ });
+
+ if (ranges.length) {
+ const mutations = this._autoHeightController.getUndoRedoParamsOfAutoHeight(ranges);
sequenceExecute(mutations.redos, this._commandService);
}
});
@@ -362,8 +370,8 @@ export class SheetsDataValidationMobileRenderController extends RxDisposable {
}
};
- this.disposeWithMe(this._dataValidationModel.ruleChange$.subscribe(() => markSkeletonDirty()));
- this.disposeWithMe(this._dataValidationModel.validStatusChange$.subscribe(() => markSkeletonDirty()));
+ this.disposeWithMe(this._dataValidationModel.ruleChange$.pipe(debounceTime(16)).subscribe(() => markSkeletonDirty()));
+ this.disposeWithMe(this._dataValidationModel.validStatusChange$.pipe(debounceTime(16)).subscribe(() => markSkeletonDirty()));
}
// eslint-disable-next-line max-lines-per-function
@@ -503,9 +511,16 @@ export class SheetsDataValidationMobileRenderController extends RxDisposable {
}
private _initAutoHeight() {
- this._dataValidationModel.ruleChange$.subscribe((info) => {
- if (info.rule?.ranges) {
- const mutations = this._autoHeightController.getUndoRedoParamsOfAutoHeight(info.rule.ranges);
+ this._dataValidationModel.ruleChange$.pipe(bufferTime(16)).subscribe((infos) => {
+ const ranges: IRange[] = [];
+ infos.forEach((info) => {
+ if (info.rule?.ranges) {
+ ranges.push(...info.rule.ranges);
+ }
+ });
+
+ if (ranges.length) {
+ const mutations = this._autoHeightController.getUndoRedoParamsOfAutoHeight(ranges);
sequenceExecute(mutations.redos, this._commandService);
}
});
diff --git a/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx b/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx
index 7c3e490567..00fdd04597 100644
--- a/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx
+++ b/packages/sheets-data-validation/src/views/formula-input/list-formula-input.tsx
@@ -22,6 +22,7 @@ import { createInternalEditorID, DataValidationType, isFormulaString, LocaleServ
import { DataValidationModel, DataValidatorRegistryService, type IFormulaInputProps } from '@univerjs/data-validation';
import { DeleteSingle, IncreaseSingle, SequenceSingle } from '@univerjs/icons';
import cs from 'clsx';
+import { debounceTime } from 'rxjs';
import { deserializeListOptions, serializeListOptions } from '../../validators/util';
import { DROP_DOWN_DEFAULT_COLOR } from '../../common/const';
import type { ListValidator } from '../../validators';
@@ -163,7 +164,8 @@ export function ListFormulaInput(props: IFormulaInputProps) {
const [refOptions, setRefOptions] = useState<string[]>([]);
const [localError, setLocalError] = useState('');
const formula1Res = showError ? validResult?.formula1 : '';
- const ruleChange = useObservable(dataValidationModel.ruleChange$);
+ const ruleChange$ = useMemo(() => dataValidationModel.ruleChange$.pipe(debounceTime(16)), []);
+ const ruleChange = useObservable(ruleChange$);
const onChange = useEvent(_onChange);
diff --git a/packages/sheets-data-validation/src/views/list-dropdown/index.tsx b/packages/sheets-data-validation/src/views/list-dropdown/index.tsx
index 61c9587d9d..907efe02fd 100644
--- a/packages/sheets-data-validation/src/views/list-dropdown/index.tsx
+++ b/packages/sheets-data-validation/src/views/list-dropdown/index.tsx
@@ -18,13 +18,14 @@ import { DataValidationRenderMode, DataValidationType, ICommandService, LocaleSe
import type { ISetRangeValuesCommandParams } from '@univerjs/sheets';
import { SetRangeValuesCommand } from '@univerjs/sheets';
import { useDependency } from '@wendellhu/redi/react-bindings';
-import React, { useState } from 'react';
+import React, { useMemo, useState } from 'react';
import { CheckMarkSingle } from '@univerjs/icons';
import { IEditorBridgeService } from '@univerjs/sheets-ui';
import { KeyCode, useObservable } from '@univerjs/ui';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { RectPopup, Scrollbar } from '@univerjs/design';
import { DataValidationModel } from '@univerjs/data-validation';
+import { debounceTime } from 'rxjs';
import type { ListMultipleValidator } from '../../validators/list-multiple-validator';
import { deserializeListOptions, getDataValidationCellValue, serializeListOptions } from '../../validators/util';
import type { IDropdownComponentProps } from '../../services/dropdown-manager.service';
@@ -98,7 +99,8 @@ export function ListDropDown(props: IDropdownComponentProps) {
const localeService = useDependency(LocaleService);
const [localValue, setLocalValue] = useState('');
const editorBridgeService = useDependency(IEditorBridgeService);
- useObservable(dataValidationModel.ruleChange$);
+ const ruleChange$ = useMemo(() => dataValidationModel.ruleChange$.pipe(debounceTime(16)), []);
+ useObservable(ruleChange$);
const anchorRect = RectPopup.useContext();
const cellWidth = anchorRect.right - anchorRect.left;
if (!worksheet) {