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(conditional-formatting): render cache error (#3345)

Gpound.liue812441c616e
diff --git a/packages/core/src/common/interceptor.ts b/packages/core/src/common/interceptor.ts
index a2467a5d40..1e11f22fdd 100644
--- a/packages/core/src/common/interceptor.ts
+++ b/packages/core/src/common/interceptor.ts
@@ -74,13 +74,13 @@ export class InterceptorManager<P extends Record<string, IInterceptor<any, any>>
this._interceptorPoints = interceptorPoints;
}
- fetchThroughInterceptors<T, C>(name: IInterceptor<T, C>) {
+ public fetchThroughInterceptors<T, C>(name: IInterceptor<T, C>) {
const key = name as unknown as string;
const interceptors = this._interceptorsByName.get(key) as unknown as Array<typeof name>;
return composeInterceptors(interceptors || []);
}
- intercept<T extends IInterceptor<any, any>>(name: T, interceptor: T) {
+ public intercept<T extends IInterceptor<any, any>>(name: T, interceptor: T) {
const key = name as unknown as string;
if (!this._interceptorsByName.has(key)) {
this._interceptorsByName.set(key, []);
@@ -95,7 +95,11 @@ export class InterceptorManager<P extends Record<string, IInterceptor<any, any>>
return () => remove(this._interceptorsByName.get(key)!, interceptor);
}
- getInterceptPoints() {
+ public getInterceptPoints() {
return this._interceptorPoints;
}
+
+ public dispose() {
+ this._interceptorsByName.clear();
+ }
}
diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts
index 5dfbafbb7f..3dd1860041 100644
--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts
+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.render.controller.ts
@@ -22,10 +22,15 @@ import { bufferTime, filter } from 'rxjs/operators';
import type { ISheetFontRenderExtension } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { ConditionalFormattingRuleModel, ConditionalFormattingService, ConditionalFormattingViewModel, DEFAULT_PADDING, DEFAULT_WIDTH } from '@univerjs/sheets-conditional-formatting';
-import type { IConditionalFormattingCellData } from '@univerjs/sheets-conditional-formatting';
+import type { IConditionalFormattingCellData, IConditionFormattingRule } from '@univerjs/sheets-conditional-formatting';
@OnLifecycle(LifecycleStages.Starting, SheetsCfRenderController)
export class SheetsCfRenderController extends Disposable {
+ /**
+ * When a set operation is triggered multiple times over a short period of time, it may result in some callbacks not being disposed,and caused a render cache exception.
+ * The solution here is to store all the asynchronous tasks and focus on processing after the last callback
+ */
+ private _ruleChangeCacheMap: Map<string, Array<{ oldRule: IConditionFormattingRule; rule: IConditionFormattingRule; dispose: () => boolean }>> = new Map();
constructor(@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@Inject(ConditionalFormattingService) private _conditionalFormattingService: ConditionalFormattingService,
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@@ -38,6 +43,9 @@ export class SheetsCfRenderController extends Disposable {
this._initViewModelInterceptor();
this._initSkeleton();
this._initVmEffectByRule();
+ this.disposeWithMe(() => {
+ this._ruleChangeCacheMap.clear();
+ });
}
private _initSkeleton() {
@@ -84,6 +92,7 @@ export class SheetsCfRenderController extends Disposable {
private _initVmEffectByRule() {
this.disposeWithMe(
+
this._conditionalFormattingRuleModel.$ruleChange.subscribe((config) => {
const { rule, unitId, subUnitId } = config;
switch (config.type) {
@@ -113,44 +122,7 @@ export class SheetsCfRenderController extends Disposable {
return;
}
case 'set': {
- const oldRule = config.oldRule!;
- const list = this._conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId)!;
- const cfPriorityMap = list.map((item) => item.cfId).reduce((map, cur, index) => {
- map.set(cur, index);
- return map;
- }, new Map<string, number>());
-
- // After each setting, the cache needs to be cleared,
- // and this cleanup is deferred until the end of the calculation.
- // Otherwise the render will flash once
- const dispose = this._conditionalFormattingService.interceptorManager.intercept(this._conditionalFormattingService.interceptorManager.getInterceptPoints().beforeUpdateRuleResult, {
- handler: (config, _, next) => {
- if (unitId === config?.unitId && subUnitId === config.subUnitId && oldRule.cfId === config.cfId) {
- oldRule.ranges.forEach((range) => {
- Range.foreach(range, (row, col) => {
- this._conditionalFormattingViewModel.deleteCellCf(unitId, subUnitId, row, col, oldRule.cfId);
- });
- });
- rule.ranges.forEach((range) => {
- Range.foreach(range, (row, col) => {
- this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, oldRule.cfId);
- this._conditionalFormattingViewModel.sortCellCf(unitId, subUnitId, row, col, cfPriorityMap);
- });
- });
- dispose();
- return;
- }
- next(config);
- },
- });
-
- rule.ranges.forEach((range) => {
- Range.foreach(range, (row, col) => {
- this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, oldRule.cfId);
- });
- });
-
- this._conditionalFormattingViewModel.markRuleDirty(unitId, subUnitId, rule);
+ this._handleRuleChange(unitId, subUnitId, rule, config.oldRule!);
return;
}
case 'sort': {
@@ -169,6 +141,67 @@ export class SheetsCfRenderController extends Disposable {
}));
}
+ private _handleRuleChange(
+ unitId: string,
+ subUnitId: string,
+ rule: IConditionFormattingRule,
+ oldRule: IConditionFormattingRule
+ ) {
+ const list = this._conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId)!;
+ const cfPriorityMap = list.map((item) => item.cfId).reduce((map, cur, index) => {
+ map.set(cur, index);
+ return map;
+ }, new Map<string, number>());
+
+ // After each setting, the cache needs to be cleared,
+ // and this cleanup is deferred until the end of the calculation.
+ // Otherwise the render will flash once
+ const handleChange = (oldRule: IConditionFormattingRule, newRule: IConditionFormattingRule) => {
+ oldRule.ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ this._conditionalFormattingViewModel.deleteCellCf(unitId, subUnitId, row, col, oldRule.cfId);
+ });
+ });
+ newRule.ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, oldRule.cfId);
+ this._conditionalFormattingViewModel.sortCellCf(unitId, subUnitId, row, col, cfPriorityMap);
+ });
+ });
+ };
+ const dispose = this._conditionalFormattingService.interceptorManager.intercept(this._conditionalFormattingService.interceptorManager.getInterceptPoints().beforeUpdateRuleResult, {
+ handler: (config, _, next) => {
+ if (unitId === config?.unitId && subUnitId === config.subUnitId && oldRule.cfId === config.cfId) {
+ const list = this._ruleChangeCacheMap.get(config.cfId) || [];
+ list.forEach((config) => {
+ handleChange(config.oldRule, config.rule);
+ config.dispose();
+ });
+ this._ruleChangeCacheMap.delete(config.cfId);
+ return;
+ }
+ next(config);
+ },
+ });
+
+ if (oldRule.cfId === rule.cfId) {
+ let list = this._ruleChangeCacheMap.get(oldRule.cfId);
+ if (!list) {
+ list = [];
+ this._ruleChangeCacheMap.set(oldRule.cfId, list);
+ }
+ list.push({ oldRule, rule, dispose });
+ }
+
+ rule.ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, oldRule.cfId);
+ });
+ });
+
+ this._conditionalFormattingViewModel.markRuleDirty(unitId, subUnitId, rule);
+ }
+
private _initViewModelInterceptor() {
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
handler: (cell, context, next) => {
diff --git a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
index 068935de99..0ef8aec645 100644
--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
@@ -91,6 +91,10 @@ export class ConditionalFormattingService extends Disposable {
this._handleCalculateUnit(config.unitId, config.subUnitId, config.rule);
});
});
+
+ this.disposeWithMe(() => {
+ this.interceptorManager.dispose();
+ });
}
public composeStyle(unitId: string, subUnitId: string, row: number, col: number) {