Skip to content

Commit

Permalink
refactor: optimize formula code
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Oct 25, 2024
1 parent caed6de commit a3dd11c
Show file tree
Hide file tree
Showing 94 changed files with 640 additions and 1,023 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { IAccessor, IMutation } from '@univerjs/core';
import { CommandType } from '@univerjs/core';
import { IDefinedNamesService } from '../../services/defined-names.service';
import { DefinedNamesService } from '../../services/defined-names.service';

export interface ISetDefinedNameMutationSearchParam {
unitId: string;
Expand All @@ -42,7 +42,7 @@ export const SetDefinedNameMutationFactory = (
params: ISetDefinedNameMutationParam
): ISetDefinedNameMutationParam => {
const { unitId, id } = params;
const definedNamesService = accessor.get(IDefinedNamesService);
const definedNamesService = accessor.get(DefinedNamesService);

const definedName = definedNamesService.getValueById(unitId, id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
*/

import type { IMutation } from '@univerjs/core';
import { CommandType } from '@univerjs/core';

import type { IFeatureCalculationManagerParam } from '../../services/feature-calculation-manager.service';

import { CommandType } from '@univerjs/core';

export interface ISetFeatureCalculationMutation {
featureId: string;
calculationParam: IFeatureCalculationManagerParam;
}
/**
* In the formula engine, the mutation is solely responsible for communication between the worker and the main thread.
* It requires setting local to true during execution.
*
* @deprecated anti pattern
*/
export const SetFeatureCalculationMutation: IMutation<ISetFeatureCalculationMutation> = {
id: 'formula.mutation.set-feature-calculation',
Expand All @@ -39,6 +41,9 @@ export interface IRemoveFeatureCalculationMutationParam {
subUnitId: string;
}

/**
* @deprecated anti pattern
*/
export const RemoveFeatureCalculationMutation: IMutation<IRemoveFeatureCalculationMutationParam> = {
id: 'formula.mutation.remove-feature-calculation',
type: CommandType.MUTATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import type { IMutation } from '@univerjs/core';
import { CommandType } from '@univerjs/core';

import type { IFormulaDataItem } from '../../basics/common';

import { CommandType } from '@univerjs/core';

export interface ISetOtherFormulaMutationParams {
unitId: string;
subUnitId: string;
Expand All @@ -35,13 +35,18 @@ export interface IRemoveOtherFormulaMutationParams {
/**
* In the formula engine, the mutation is solely responsible for communication between the worker and the main thread.
* It requires setting local to true during execution.
*
* @deprecated anti-pattern
*/
export const SetOtherFormulaMutation: IMutation<ISetOtherFormulaMutationParams> = {
id: 'formula.mutation.set-other-formula',
type: CommandType.MUTATION,
handler: () => true,
};

/**
* @deprecated anti-pattern
*/
export const RemoveOtherFormulaMutation: IMutation<IRemoveOtherFormulaMutationParams> = {
id: 'formula.mutation.remove-other-formula',
type: CommandType.MUTATION,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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.
*/

/**
* This controller dispose necessary API to upper layer plugins to call the formula engine, without knowing if the
* formula engine is running in the same process or in a different process.
*/
export class FormulaSchedulerController {
constructor() {

}
}
29 changes: 14 additions & 15 deletions packages/engine-formula/src/controller/formula.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { IFunctionNames } from '../basics/function';
import type { BaseFunction } from '../functions/base-function';
import type { IUniverEngineFormulaConfig } from './config.schema';

import { Disposable, ICommandService, IConfigService } from '@univerjs/core';
import { Disposable, ICommandService, IConfigService, Inject } from '@univerjs/core';
import { type Ctor, Optional } from '@univerjs/core';
import { DataSyncPrimaryController } from '@univerjs/rpc';
import { RegisterFunctionMutation } from '../commands/mutations/register-function.mutation';
Expand Down Expand Up @@ -57,26 +57,28 @@ import { functionStatistical } from '../functions/statistical/function-map';
import { functionText } from '../functions/text/function-map';
import { functionUniver } from '../functions/univer/function-map';
import { functionWeb } from '../functions/web/function-map';
import { IFunctionService } from '../services/function.service';
import { FunctionService } from '../services/function.service';
import { PLUGIN_CONFIG_KEY } from './config.schema';

/**
* This controller register commands for running formula and built-in functions.
*/
export class FormulaController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IFunctionService private readonly _functionService: IFunctionService,
@Inject(FunctionService) private readonly _functionService: FunctionService,
@IConfigService private readonly _configService: IConfigService,
@Optional(DataSyncPrimaryController) private readonly _dataSyncPrimaryController?: DataSyncPrimaryController
) {
super();

this._initialize();
}

private _initialize(): void {
this._registerCommands();
this._registerFunctions();
}

// DEBT@wzhudev: some mutations are mutations just be be synced into the web worker
// they **should not** be mutations in the first place.

private _registerCommands(): void {
[
SetFormulaDataMutation,
Expand Down Expand Up @@ -125,14 +127,11 @@ export class FormulaController extends Disposable {
...functionUniver,
...functionWeb,
] as Array<[Ctor<BaseFunction>, IFunctionNames]>
)
.concat(config?.function ?? [])
.map((registerObject) => {
const Func = registerObject[0] as Ctor<BaseFunction>;
const name = registerObject[1] as IFunctionNames;

return new Func(name);
});
).concat(config?.function ?? []).map((registerObject) => {
const Func = registerObject[0] as Ctor<BaseFunction>;
const name = registerObject[1] as IFunctionNames;
return new Func(name);
});

this._functionService.registerExecutors(...functions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,45 @@ import type {
ISetDefinedNameMutationSearchParam,
} from '../commands/mutations/set-defined-name.mutation';

import { Disposable, ICommandService } from '@univerjs/core';
import { Disposable, ICommandService, Inject } from '@univerjs/core';
import { RemoveDefinedNameMutation, SetDefinedNameMutation } from '../commands/mutations/set-defined-name.mutation';
import { IDefinedNamesService } from '../services/defined-names.service';
import { DefinedNamesService } from '../services/defined-names.service';

/**
* This controller is for syncing defined names between the host thread and the worker thread.
*
* @deprecated Should be under formula interface.
*/
export class SetDefinedNameController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IDefinedNamesService private readonly _definedNamesService: IDefinedNamesService
@Inject(DefinedNamesService) private readonly _definedNamesService: DefinedNamesService
) {
super();

this._initialize();
}

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

private _commandExecutedListener() {
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (command.id === SetDefinedNameMutation.id) {
const params = command.params as ISetDefinedNameMutationParam;
if (params == null) {
return;
}
const { id, unitId, name, formulaOrRefString, comment, hidden, localSheetId } = params;
this._definedNamesService.registerDefinedName(unitId, {
id,
name: name.trim(),
formulaOrRefString: formulaOrRefString.trim(),
comment: comment?.trim(),
hidden,
localSheetId,
});
} else if (command.id === RemoveDefinedNameMutation.id) {
const params = command.params as ISetDefinedNameMutationSearchParam;
if (params == null) {
return;
}
const { unitId, id } = params;
this._definedNamesService.removeDefinedName(unitId, id);
this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (command.id === SetDefinedNameMutation.id) {
const params = command.params as ISetDefinedNameMutationParam;
if (params == null) {
return;
}
const { id, unitId, name, formulaOrRefString, comment, hidden, localSheetId } = params;
this._definedNamesService.registerDefinedName(unitId, {
id,
name: name.trim(),
formulaOrRefString: formulaOrRefString.trim(),
comment: comment?.trim(),
hidden,
localSheetId,
});
} else if (command.id === RemoveDefinedNameMutation.id) {
const params = command.params as ISetDefinedNameMutationSearchParam;
if (params == null) {
return;
}
})
);
const { unitId, id } = params;
this._definedNamesService.removeDefinedName(unitId, id);
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,25 @@ import type {
ISetFeatureCalculationMutation } from '../commands/mutations/set-feature-calculation.mutation';
import type { ISetFormulaDataMutationParams } from '../commands/mutations/set-formula-data.mutation';
import type { IRemoveOtherFormulaMutationParams, ISetOtherFormulaMutationParams } from '../commands/mutations/set-other-formula.mutation';
import { Disposable, ICommandService, ObjectMatrix } from '@univerjs/core';
import { Disposable, ICommandService, Inject, ObjectMatrix } from '@univerjs/core';
import { SetDefinedNameMutation } from '../commands/mutations/set-defined-name.mutation';
import {
RemoveFeatureCalculationMutation,
SetFeatureCalculationMutation,
} from '../commands/mutations/set-feature-calculation.mutation';
import { SetFormulaDataMutation } from '../commands/mutations/set-formula-data.mutation';
import { RemoveOtherFormulaMutation, SetOtherFormulaMutation } from '../commands/mutations/set-other-formula.mutation';
import { IDependencyManagerService } from '../services/dependency-manager.service';
import { IFeatureCalculationManagerService } from '../services/feature-calculation-manager.service';
import { DependencyManagerService } from '../services/dependency-manager.service';
import { FeatureCalculationManagerService } from '../services/feature-calculation-manager.service';

export class SetDependencyController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IFeatureCalculationManagerService
@IDependencyManagerService private readonly _dependencyManagerService: IDependencyManagerService,
@IFeatureCalculationManagerService private readonly _featureCalculationManagerService: IFeatureCalculationManagerService) {
@Inject(DependencyManagerService) private readonly _dependencyManagerService: DependencyManagerService,
@Inject(FeatureCalculationManagerService) private readonly _featureCalculationManagerService: FeatureCalculationManagerService) {
super();

this._initialize();
}

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

this._featureCalculationManagerServiceListener();
}

Expand Down Expand Up @@ -135,9 +129,7 @@ export class SetDependencyController extends Disposable {
);
}

private _handleSetDefinedName(
command: ICommandInfo
): void {
private _handleSetDefinedName(command: ICommandInfo): void {
const params = command.params as ISetDefinedNameMutationParam;
if (params == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ import type {
IRemoveFeatureCalculationMutationParam,
ISetFeatureCalculationMutation,
} from '../commands/mutations/set-feature-calculation.mutation';
import { Disposable, ICommandService } from '@univerjs/core';
import { Disposable, ICommandService, Inject } from '@univerjs/core';
import {
RemoveFeatureCalculationMutation,
SetFeatureCalculationMutation,
} from '../commands/mutations/set-feature-calculation.mutation';
import { IFeatureCalculationManagerService } from '../services/feature-calculation-manager.service';
import { FeatureCalculationManagerService } from '../services/feature-calculation-manager.service';

export class SetFeatureCalculationController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IFeatureCalculationManagerService
private readonly _featureCalculationManagerService: IFeatureCalculationManagerService
@Inject(FeatureCalculationManagerService)
private readonly _featureCalculationManagerService: FeatureCalculationManagerService
) {
super();

this._initialize();
}

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

Expand Down
Loading

0 comments on commit a3dd11c

Please sign in to comment.