Skip to content

Commit

Permalink
fix(formula): add dependency controller
Browse files Browse the repository at this point in the history
  • Loading branch information
DR-Univer committed Apr 15, 2024
1 parent be1fc22 commit 072ac81
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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 type { ICommandInfo } from '@univerjs/core';
import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';

import {
RemoveFeatureCalculationMutation,
} from '../commands/mutations/set-feature-calculation.mutation';
import { IFeatureCalculationManagerService } from '../services/feature-calculation-manager.service';
import { IDependencyManagerService } from '../services/dependency-manager.service';
import { RemoveOtherFormulaMutation } from '../commands/mutations/set-other-formula.mutation';
import type { IOtherFormulaManagerSearchParam } from '../services/other-formula-manager.service';

@OnLifecycle(LifecycleStages.Ready, SetDependencyController)
export class SetDependencyController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IFeatureCalculationManagerService
@IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService
) {
super();

this._initialize();
}

private _initialize(): void {
this._commandExecutedListener();
}

private _commandExecutedListener() {
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (command.id === RemoveFeatureCalculationMutation.id) {
const params = command.params as { featureId: string };
if (params == null) {
return;
}
const { featureId } = params;
this._dependencyManagerService.removeFeatureFormulaDependency(featureId);
} else if (command.id === RemoveOtherFormulaMutation.id) {
const params = command.params as IOtherFormulaManagerSearchParam;
if (params == null) {
return;
}

this._dependencyManagerService.removeOtherFormulaDependency(params.unitId, params.subUnitId, params.formulaId);
}
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import {
SetFeatureCalculationMutation,
} from '../commands/mutations/set-feature-calculation.mutation';
import { IFeatureCalculationManagerService } from '../services/feature-calculation-manager.service';
import { IDependencyManagerService } from '../services/dependency-manager.service';

@OnLifecycle(LifecycleStages.Ready, SetFeatureCalculationController)
export class SetFeatureCalculationController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IFeatureCalculationManagerService
private readonly _featureCalculationManagerService: IFeatureCalculationManagerService,
@IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService
private readonly _featureCalculationManagerService: IFeatureCalculationManagerService
) {
super();

Expand All @@ -58,7 +56,6 @@ export class SetFeatureCalculationController extends Disposable {
return;
}
const { featureId } = params;
this._dependencyManagerService.removeFeatureFormulaDependency(featureId);
this._featureCalculationManagerService.remove(featureId);
}
})
Expand Down
2 changes: 2 additions & 0 deletions packages/engine-formula/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { FormulaRuntimeService, IFormulaRuntimeService } from './services/runtim
import { ISuperTableService, SuperTableService } from './services/super-table.service';
import { ActiveDirtyManagerService, IActiveDirtyManagerService } from './services/active-dirty-manager.service';
import { DependencyManagerService, IDependencyManagerService } from './services/dependency-manager.service';
import { SetDependencyController } from './controller/set-dependency.controller';

const PLUGIN_NAME = 'base-formula-engine';

Expand Down Expand Up @@ -120,6 +121,7 @@ export class UniverFormulaEnginePlugin extends Plugin {
[CalculateController],
[SetOtherFormulaController],
[RegisterFunctionController],
[SetDependencyController],

// Calculation engine
[FormulaDependencyGenerator],
Expand Down

0 comments on commit 072ac81

Please sign in to comment.