Skip to content

Commit

Permalink
fix(formula): fix dispose register function bug (#4783)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored Mar 6, 2025
1 parent e1ba4a0 commit be00bdd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/engine-formula/src/basics/cache-lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export class FormulaAstLRU<T> {
this._cache.clear();
}

delete(formulaString: string) {
this._cache.delete(this._hash(formulaString));
}

forEach(callbackfn: (value: T, key: string, map: LRUMap<string, T>) => void, thisArg?: any) {
this._cache.forEach(callbackfn, thisArg);
}

private _hash(formulaString: string) {
if (formulaString.length <= 64) {
return formulaString;
Expand Down
14 changes: 13 additions & 1 deletion packages/engine-formula/src/services/function.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import type { IDisposable, Nullable } from '@univerjs/core';
import type { IFunctionInfo, IFunctionNames } from '../basics/function';

import type { BaseFunction } from '../functions/base-function';
import { createIdentifier, Disposable, toDisposable } from '@univerjs/core';
import { FORMULA_AST_CACHE } from '../engine/utils/generate-ast-node';

export interface IFunctionService {
/**
Expand Down Expand Up @@ -53,6 +53,8 @@ export interface IFunctionService {
hasDescription(functionToken: IFunctionNames): boolean;

unregisterDescriptions(...functionTokens: IFunctionNames[]): void;

deleteFormulaAstCacheKey(...functionToken: IFunctionNames[]): void;
}
export const IFunctionService = createIdentifier<FunctionService>('univer.formula-function.service');

Expand Down Expand Up @@ -125,4 +127,14 @@ export class FunctionService extends Disposable implements IFunctionService {
this._functionDescriptions.delete(functionToken);
}
}

deleteFormulaAstCacheKey(...functionToken: IFunctionNames[]) {
FORMULA_AST_CACHE.forEach((_, key) => {
functionToken.forEach((token) => {
if (key.includes(token as string)) {
FORMULA_AST_CACHE.delete(key);
}
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ export class RegisterFunctionService extends Disposable implements IRegisterFunc
instance.calculateCustom = func;
this._functionService.registerExecutors(instance);
disposables.add(toDisposable(() => this._functionService.unregisterExecutors(name)));
disposables.add(toDisposable(() => this._functionService.unregisterDescriptions(name)));
disposables.add(toDisposable(() => this._functionService.deleteFormulaAstCacheKey(name)));

// Handle remote registration if available
if (this._remoteRegisterFunctionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export class RemoteRegisterFunctionService implements IRemoteRegisterFunctionSer
}

async unregisterFunctions(names: string[]): Promise<void> {
this._functionService.unregisterExecutors(...names);
this._functionService.unregisterDescriptions(...names);
this._functionService.deleteFormulaAstCacheKey(...names);
}
}

Expand Down

0 comments on commit be00bdd

Please sign in to comment.