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(sheets-ui): hover-manager-service memory leak (#4213)

zhangw3ce3b275c160
diff --git a/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts b/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts
index 5cfd626fd0..4101b57a08 100644
--- a/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts
+++ b/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts
@@ -45,13 +45,13 @@ export class DataValidationAlertController extends Disposable {
private _initCellAlertPopup() {
this.disposeWithMe(this._hoverManagerService.currentCell$.pipe(debounceTime(100)).subscribe((cellPos) => {
if (cellPos) {
- const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- const worksheet = workbook.getActiveSheet();
+ const workbook = this._univerInstanceService.getUnit<Workbook>(cellPos.location.unitId, UniverInstanceType.UNIVER_SHEET)!;
+ const worksheet = workbook.getSheetBySheetId(cellPos.location.subUnitId);
if (!worksheet) return;
const rule = this._dataValidationModel.getRuleByLocation(cellPos.location.unitId, cellPos.location.subUnitId, cellPos.location.row, cellPos.location.col);
if (!rule) return;
- const validStatus = this._dataValidationModel.validator(rule, cellPos.location);
+ const validStatus = this._dataValidationModel.validator(rule, { ...cellPos.location, workbook, worksheet });
if (validStatus === DataValidationStatus.INVALID) {
const currentAlert = this._cellAlertManagerService.currentAlert.get(ALERT_KEY);
const currentLoc = currentAlert?.alert?.location;
diff --git a/packages/sheets-ui/src/facade/__tests__/f-sheet-hooks.spec.ts b/packages/sheets-ui/src/facade/__tests__/f-sheet-hooks.spec.ts
index d442ae7d45..07258a2695 100644
--- a/packages/sheets-ui/src/facade/__tests__/f-sheet-hooks.spec.ts
+++ b/packages/sheets-ui/src/facade/__tests__/f-sheet-hooks.spec.ts
@@ -157,50 +157,50 @@ describe('Test FSheetHooks', () => {
it('Test onCellPointerMove', () => {
sheetHooks.onCellPointerMove((cellPos) => {
- expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
+ expect(cellPos).toEqual({ location: { unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});
// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet()!;
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
- hoverCurrentPosition$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
+ hoverCurrentPosition$.next({ location: { unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});
it('Test onCellPointerOver', () => {
sheetHooks.onCellPointerOver((cellPos) => {
- expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
+ expect(cellPos).toEqual({ location: { unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});
// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet()!;
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
- hoverCurrentCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
+ hoverCurrentCell$.next({ location: { unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});
it('Test onCellDragOver', () => {
sheetHooks.onCellDragOver((cellPos) => {
- expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
+ expect(cellPos).toEqual({ location: { unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});
// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet()!;
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
- dragCurrentCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
+ dragCurrentCell$.next({ location: { unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});
it('Test onCellDrop', () => {
sheetHooks.onCellDrop((cellPos) => {
- expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
+ expect(cellPos).toEqual({ location: { unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});
// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet()!;
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
- dragEndCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
+ dragEndCell$.next({ location: { unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});
it('Test onCellRender', () => {
diff --git a/packages/sheets-ui/src/services/cell-alert-manager.service.ts b/packages/sheets-ui/src/services/cell-alert-manager.service.ts
index 49cb3ae784..4ec1039734 100644
--- a/packages/sheets-ui/src/services/cell-alert-manager.service.ts
+++ b/packages/sheets-ui/src/services/cell-alert-manager.service.ts
@@ -14,11 +14,11 @@
* limitations under the License.
*/
+import type { IDisposable } from '@univerjs/core';
+import type { ISheetLocationBase } from '@univerjs/sheets';
import { Disposable, Inject } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { Subject } from 'rxjs';
-import type { IDisposable } from '@univerjs/core';
-import type { ISheetLocation } from '@univerjs/sheets';
import { CELL_ALERT_KEY } from '../views/cell-alert';
import { SheetCanvasPopManagerService } from './canvas-pop-manager.service';
@@ -32,7 +32,7 @@ export interface ICellAlert {
type: CellAlertType;
title: React.ReactNode;
message: React.ReactNode;
- location: ISheetLocation;
+ location: ISheetLocationBase;
width: number;
height: number;
key: string;
diff --git a/packages/sheets-ui/src/services/hover-manager.service.ts b/packages/sheets-ui/src/services/hover-manager.service.ts
index 7ab60f6060..330d7a2e49 100644
--- a/packages/sheets-ui/src/services/hover-manager.service.ts
+++ b/packages/sheets-ui/src/services/hover-manager.service.ts
@@ -30,7 +30,7 @@ export interface IHoverCellPosition {
/**
* location of cell
*/
- location: ISheetLocation;
+ location: ISheetLocationBase;
}
export interface IHoverRichTextInfo extends IHoverCellPosition {
@@ -67,6 +67,11 @@ export interface IHoverRichTextPosition extends ISheetLocationBase {
drawing?: Nullable<string>;
}
+export function getLocationBase(location: ISheetLocation) {
+ const { workbook, worksheet, ...locBase } = location;
+ return locBase;
+}
+
export class HoverManagerService extends Disposable {
private _currentCell$ = new BehaviorSubject<Nullable<IHoverCellPosition>>(null);
private _currentRichText$ = new BehaviorSubject<Nullable<IHoverRichTextInfo>>(null);
@@ -136,6 +141,16 @@ export class HoverManagerService extends Disposable {
this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
if (!workbook) this._currentCell$.next(null);
}));
+
+ this.disposeWithMe(this._univerInstanceService.unitDisposed$.subscribe((unit) => {
+ if (this._currentCell$.getValue()?.location.unitId === unit.getUnitId()) {
+ this._currentCell$.next(null);
+ }
+
+ if (this._currentRichText$.getValue()?.location.unitId === unit.getUnitId()) {
+ this._currentRichText$.next(null);
+ }
+ }));
}
// eslint-disable-next-line complexity
@@ -218,20 +233,23 @@ export class HoverManagerService extends Disposable {
triggerMouseMove(unitId: string, offsetX: number, offsetY: number) {
const activeCell = this._calcActiveCell(unitId, offsetX, offsetY);
this._currentCell$.next(activeCell && {
- location: activeCell.location,
+ location: getLocationBase(activeCell.location),
position: activeCell.position,
});
this._currentRichText$.next(activeCell && {
...activeCell,
- location: activeCell.overflowLocation,
+ location: getLocationBase(activeCell.overflowLocation),
});
}
triggerClick(unitId: string, offsetX: number, offsetY: number) {
const activeCell = this._calcActiveCell(unitId, offsetX, offsetY);
if (activeCell) {
- this._currentClickedCell$.next(activeCell);
+ this._currentClickedCell$.next({
+ ...activeCell,
+ location: getLocationBase(activeCell.location),
+ });
}
}