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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(sheets-ui): stop auto-height idle loop and skip stale tasks on dispose (#7993)
wpxp1234563f2fd5a6f831
diff --git a/packages/sheets-ui/src/services/__tests__/auto-height.service.spec.ts b/packages/sheets-ui/src/services/__tests__/auto-height.service.spec.tsindex af576056b7..dd8d350f55 100644--- a/packages/sheets-ui/src/services/__tests__/auto-height.service.spec.ts+++ b/packages/sheets-ui/src/services/__tests__/auto-height.service.spec.ts@@ -339,4 +339,95 @@ describe('AutoHeightService', () => {requestIdleCallbackSpy.mockRestore();dateNowSpy.mockRestore();});++ describe('idle loop lifecycle', () => {+ const deadline = { timeRemaining: () => 100, didTimeout: false } as IdleDeadline;+ const range = { startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 };++ function mockIdleCallbacks() {+ const pending = new Map<number, IdleRequestCallback>();+ let nextId = 1;+ const idleGlobals = globalThis as unknown as IIdleGlobals & { cancelIdleCallback: (id: number) => void };+ const requestSpy = vi.spyOn(idleGlobals, 'requestIdleCallback').mockImplementation((cb) => {+ const id = nextId++;+ pending.set(id, cb);+ return id;+ });+ const cancelSpy = vi.spyOn(idleGlobals, 'cancelIdleCallback').mockImplementation((id) => {+ pending.delete(id);+ });+ const flush = () => {+ const [id, cb] = pending.entries().next().value!;+ pending.delete(id);+ cb(deadline);+ };++ return { pending, requestSpy, cancelSpy, flush };+ }++ it('stops scheduling idle callbacks once the queue is drained', () => {+ const { pending, requestSpy, flush } = mockIdleCallbacks();+ const service = createAutoHeightService({+ calculateAutoHeightInRange: () => [],+ getRowHeight: () => 10,+ });++ service.startAutoHeightTask({ id: 't', sheetId: 's-1', ranges: [range] });+ expect(pending.size).toBe(1);++ flush();+ expect(pending.size).toBe(0);+ expect(requestSpy).toHaveBeenCalledTimes(1);++ // A later task restarts the loop.+ service.startAutoHeightTask({ id: 't2', sheetId: 's-1', ranges: [range] });+ expect(pending.size).toBe(1);++ service.dispose();+ vi.restoreAllMocks();+ });++ it('cancels the pending idle callback and drops queued tasks on dispose', () => {+ const { pending, cancelSpy } = mockIdleCallbacks();+ const calculateAutoHeightInRange = vi.fn(() => []);+ const service = createAutoHeightService({ calculateAutoHeightInRange, getRowHeight: () => 10 });++ service.startAutoHeightTask({ id: 't', sheetId: 's-1', ranges: [range] });+ service.dispose();++ expect(cancelSpy).toHaveBeenCalledTimes(1);+ expect(pending.size).toBe(0);+ expect(calculateAutoHeightInRange).not.toHaveBeenCalled();++ vi.restoreAllMocks();+ });++ it('ignores new tasks after dispose', () => {+ const { requestSpy } = mockIdleCallbacks();+ const service = createAutoHeightService({ calculateAutoHeightInRange: () => [], getRowHeight: () => 10 });++ service.dispose();+ service.startAutoHeightTask({ id: 't', sheetId: 's-1', ranges: [range] });++ expect(requestSpy).not.toHaveBeenCalled();++ vi.restoreAllMocks();+ });++ it('skips queued tasks whose skeleton is no longer the current one', () => {+ const { flush } = mockIdleCallbacks();+ const staleCalculate = vi.fn(() => [{ row: 0, autoHeight: 30 }]);+ const service = createAutoHeightService({ calculateAutoHeightInRange: staleCalculate, getRowHeight: () => 10 });++ service.startAutoHeightTask({ id: 't', sheetId: 's-1', ranges: [range] });+ TestSheetSkeletonManagerService.skeleton = { calculateAutoHeightInRange: () => [], getRowHeight: () => 10 };+ flush();++ expect(staleCalculate).not.toHaveBeenCalled();+ expect(TestCommandService.executed).toEqual([]);++ service.dispose();+ vi.restoreAllMocks();+ });+ });});diff --git a/packages/sheets-ui/src/services/auto-height.service.ts b/packages/sheets-ui/src/services/auto-height.service.tsindex 0681677da2..e9539138cc 100644--- a/packages/sheets-ui/src/services/auto-height.service.ts+++ b/packages/sheets-ui/src/services/auto-height.service.ts@@ -121,8 +121,23 @@ export class AutoHeightService extends Disposable implements IRenderModule {);}+ override dispose(): void {+ if (this._calculateLoopId !== null) {+ cancelIdleCallback(this._calculateLoopId);+ this._calculateLoopId = null;+ }+ this._autoHeightTasks = [];++ super.dispose();+ }+private _loopTask(task: IAutoHeightTaskInfo) {const skeleton = task.skeleton;+ // The skeleton may have been replaced or disposed while the task was waiting for an idle period.+ if (this._disposed || this._sheetSkeletonManagerService.getSkeleton(task.sheetId) !== skeleton) {+ return;+ }+let result: IRange[] = [];let lasts: IRange[] = [];if (task.maxTime && task.maxTime + task.startTime <= Date.now()) {@@ -162,24 +177,27 @@ export class AutoHeightService extends Disposable implements IRenderModule {}private _calculateLoop(options: IdleDeadline) {+ this._calculateLoopId = null;+ if (this._disposed) {+ return;+ }+while (this._autoHeightTasks.length) {if (options.timeRemaining() <= 0) {break;}- const task = this._autoHeightTasks.shift();- if (!task) {- this._calculateLoopId = null;- return;- }+ const task = this._autoHeightTasks.shift()!;this._loopTask(task);}- this._calculateLoopId = requestIdleCallback((options) => {- this._calculateLoop(options);- });+ this._initialCalculateLoop();}private _initialCalculateLoop() {+ if (this._disposed || !this._autoHeightTasks.length) {+ return;+ }+if (this._calculateLoopId === null) {this._calculateLoopId = requestIdleCallback((options) => {this._calculateLoop(options);@@ -188,6 +206,10 @@ export class AutoHeightService extends Disposable implements IRenderModule {}startAutoHeightTask(task: IAutoHeightTask): void {+ if (this._disposed) {+ return;+ }+const skeleton = this._sheetSkeletonManagerService.getSkeleton(task.sheetId);if (!skeleton) {return;