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): viewmodel interception is not required at initialization time (#2107)

Gpound.liu305d235091a0
diff --git a/packages/core/src/shared/after-init-apply.ts b/packages/core/src/shared/after-init-apply.ts
new file mode 100644
index 0000000000..74f895ec8e
--- /dev/null
+++ b/packages/core/src/shared/after-init-apply.ts
@@ -0,0 +1,35 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { merge, timer } from 'rxjs';
+import { debounceTime, filter, first } from 'rxjs/operators';
+import type { ICommandService } from '../services/command/command.service';
+import { CommandType } from '../services/command/command.service';
+import { fromCallback } from './rxjs';
+
+
+export const afterInitApply = (commandService: ICommandService) => {
+ return new Promise<void>((res) => {
+ merge(
+ fromCallback(commandService.onCommandExecuted).pipe(filter(([info]) => {
+ return info.type === CommandType.MUTATION;
+ })),
+ timer(300)
+ ).pipe(debounceTime(16), first()).subscribe(() => {
+ res();
+ });
+ });
+};
diff --git a/packages/core/src/shared/index.ts b/packages/core/src/shared/index.ts
index 12e85c184c..3c6f5cf21d 100644
--- a/packages/core/src/shared/index.ts
+++ b/packages/core/src/shared/index.ts
@@ -41,3 +41,4 @@ export * from './debounce';
export * from './clipboard';
export { queryObjectMatrix } from './object-matrix-query';
export { moveRangeByOffset } from './range';
+export { afterInitApply } from './after-init-apply';
diff --git a/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts b/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts
index 08120cff83..5d52831991 100644
--- a/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts
+++ b/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts
@@ -26,6 +26,7 @@ describe('Test conditional formatting service', () => {
beforeEach(() => {
testBed && testBed.univer.dispose();
testBed = createTestBed();
+ (testBed.getConditionalFormattingService() as any)._afterInitApplyPromise = Promise.resolve();
});
describe('Test highlight', () => {
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 4ba46d8b03..63e7c0ff75 100644
--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
@@ -15,7 +15,7 @@
*/
import type { IMutationInfo, IRange, Workbook } from '@univerjs/core';
-import { createInterceptorKey, Disposable, ICommandService, InterceptorManager, IResourceManagerService, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, Rectangle, Tools, UniverInstanceType } from '@univerjs/core';
+import { afterInitApply, createInterceptorKey, Disposable, ICommandService, InterceptorManager, IResourceManagerService, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, Rectangle, Tools, UniverInstanceType } from '@univerjs/core';
import type { IInsertColMutationParams, IMoveColumnsMutationParams, IMoveRangeMutationParams, IMoveRowsMutationParams, IRemoveRowsMutationParams, IRemoveSheetCommandParams, ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { InsertColMutation, InsertRowMutation, MoveColsMutation, MoveRangeMutation, MoveRowsMutation, RemoveColMutation, RemoveRowMutation, RemoveSheetCommand, SetRangeValuesMutation, SheetInterceptorService } from '@univerjs/sheets';
import { Inject, Injector } from '@wendellhu/redi';
@@ -43,6 +43,8 @@ const beforeUpdateRuleResult = createInterceptorKey<{ subUnitId: string; unitId:
@OnLifecycle(LifecycleStages.Starting, ConditionalFormattingService)
export class ConditionalFormattingService extends Disposable {
// <unitId,<subUnitId,<cfId,IComputeCache>>>
+
+ private _afterInitApplyPromise: Promise<void>;
private _ruleCacheMap: Map<string, Map<string, Map<string, IComputeCache>>> = new Map();
private _ruleComputeStatus$: Subject<{ status: ComputeStatus; result?: ObjectMatrix<any>; unitId: string; subUnitId: string; cfId: string }> = new Subject();
@@ -71,6 +73,7 @@ export class ConditionalFormattingService extends Disposable {
this._registerCalculationUnit(colorScaleCellCalculateUnit);
this._registerCalculationUnit(highlightCellCalculateUnit);
this._registerCalculationUnit(iconSetCalculateUnit);
+ this._afterInitApplyPromise = afterInitApply(_commandService);
}
public composeStyle(unitId: string, subUnitId: string, row: number, col: number) {
@@ -375,12 +378,13 @@ export class ConditionalFormattingService extends Disposable {
}
private async _handleCalculateUnit(unitId: string, subUnitId: string, rule: IConditionFormattingRule) {
- const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);
+ const workbook = this._univerInstanceService.getUnit<Workbook>(unitId);
const worksheet = workbook?.getSheetBySheetId(subUnitId);
let cache = this._getComputedCache(unitId, subUnitId, rule.cfId);
if (cache && cache.status === 'computing') {
return;
}
+ await this._afterInitApplyPromise;
if (!cache) {
cache = { status: 'computing' };
this._setComputedCache(unitId, subUnitId, rule.cfId, cache);