Skip to content

Commit

Permalink
fix(conditional-formatting): viewmodel interception is not required a…
Browse files Browse the repository at this point in the history
…t initialization time
  • Loading branch information
Gggpound committed Apr 29, 2024
1 parent fa4ebea commit ef9a4dc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
35 changes: 35 additions & 0 deletions packages/core/src/shared/after-init-apply.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
};
1 change: 1 addition & 0 deletions packages/core/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import type { IConditionalFormattingCellData } from '@univerjs/sheets-conditiona
*/
@OnLifecycle(LifecycleStages.Rendered, RenderController)
export class RenderController extends Disposable {
/**
* ViewModel interception is not required at initialization time
*/
constructor(@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@Inject(ConditionalFormattingService) private _conditionalFormattingService: ConditionalFormattingService,
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.
*/

export const afterInitApply = (fn: () => void) => {
setTimeout(() => {
fn();
}, 0);
};

0 comments on commit ef9a4dc

Please sign in to comment.