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
test: ensure no error on disposing tests (#5329)
Evan Hu18691461414b
diff --git a/e2e/memory/memory.spec.ts b/e2e/memory/memory.spec.tsindex 25ea087a81..0d57f772a8 100644--- a/e2e/memory/memory.spec.ts+++ b/e2e/memory/memory.spec.ts@@ -123,7 +123,7 @@ test('memory', async ({ page }) => {let errored = false;page.on('pageerror', (error) => {- console.error('Page error:', error);+ console.error('Page error:', error.name, error.message, error.stack);errored = true;});@@ -160,8 +160,7 @@ test('memory', async ({ page }) => {expect(instanceMemoryOverflow).toBeLessThanOrEqual(MAX_SECOND_INSTANCE_OVERFLOW);expect(univerMemoryOverflow).toBeLessThanOrEqual(MAX_UNIVER_MEMORY_OVERFLOW);- // TODO: enable this when the memory leak is fixed- // expect(errored).toBeFalsy();+ expect(errored).toBeFalsy();});declare global {diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex d58f3dac1d..7876695271 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -101,7 +101,7 @@ export { ContextService, IContextService } from './services/context/context.servexport { ErrorService, type IError } from './services/error/error.service';export { type ICreateUnitOptions, IUniverInstanceService, UniverInstanceService } from './services/instance/instance.service';export { LifecycleStages } from './services/lifecycle/lifecycle';-export { LifecycleService } from './services/lifecycle/lifecycle.service';+export { LifecycleService, LifecycleUnreachableError } from './services/lifecycle/lifecycle.service';export { ILocalStorageService } from './services/local-storage/local-storage.service';export { LocaleService } from './services/locale/locale.service';export { DesktopLogService, ILogService, LogLevel } from './services/log/log.service';diff --git a/packages/core/src/services/lifecycle/__tests__/lifecycle.service.spec.ts b/packages/core/src/services/lifecycle/__tests__/lifecycle.service.spec.tsindex 349fb19e4e..31f8ec6355 100644--- a/packages/core/src/services/lifecycle/__tests__/lifecycle.service.spec.ts+++ b/packages/core/src/services/lifecycle/__tests__/lifecycle.service.spec.ts@@ -116,4 +116,17 @@ describe('Test LifecycleService', () => {expect(lifecycleStages4).toEqual(steadyStages);expect(lifecycleStages5).toEqual(steadyStages);});++ it('Should "onStage" reject with LifecycleUnreachableError if stage is unreachable', async () => {+ injector = createLifecycleTestBed().injector;+ lifecycleService = injector.get(LifecycleService);++ lifecycleService.stage = LifecycleStages.Starting;+ lifecycleService.stage = LifecycleStages.Ready;++ injector.dispose();+ await expect(lifecycleService.onStage(LifecycleStages.Rendered)).rejects.toThrowError(+ '[LifecycleService]: lifecycle stage "Rendered" will never be reached!'+ );+ });});diff --git a/packages/core/src/services/lifecycle/lifecycle.service.ts b/packages/core/src/services/lifecycle/lifecycle.service.tsindex f385d8be40..de9bdfd124 100644--- a/packages/core/src/services/lifecycle/lifecycle.service.ts+++ b/packages/core/src/services/lifecycle/lifecycle.service.ts@@ -22,6 +22,17 @@ import { takeAfter } from '../../shared/rxjs';import { ILogService } from '../log/log.service';import { LifecycleNameMap, LifecycleStages } from './lifecycle';+/**+ * An error that indicates a lifecycle stage will never be reached, mostly due to the Univer instance is+ * disposed.+ */+export class LifecycleUnreachableError extends Error {+ constructor(stage: LifecycleStages) {+ super(`[LifecycleService]: lifecycle stage "${LifecycleNameMap[stage]}" will never be reached!`);+ this.name = 'LifecycleUnreachableError';+ }+}+/*** This service controls the lifecycle of a Univer instance. Other modules can* inject this service to read the current lifecycle stage or subscribe to@@ -59,18 +70,33 @@ export class LifecycleService extends Disposable {super.dispose();}+ /**+ * Wait for a specific lifecycle stage to be reached.+ * @param stage The lifecycle stage to wait for.+ * If the current stage is already at or beyond the specified stage, it will+ * resolve immediately.+ * If the specified stage is unreachable, it will reject with a+ * `LifecycleUnreachableError`.+ * @returns A promise that resolves when the specified stage is reached.+ */onStage(stage: LifecycleStages): Promise<void> {return firstValueFrom(this.lifecycle$.pipe(filter((s) => s >= stage),takeAfter((s) => s === stage),map(() => void 0)- ));+ )).catch((err) => {+ if (err.name === 'EmptyError') {+ return Promise.reject(new LifecycleUnreachableError(stage));+ }++ return Promise.reject(err);+ });}/*** Subscribe to lifecycle changes and all previous stages and the current* stage will be emitted immediately.- * @returns+ * @returns An observable that emits the lifecycle stages, including the current*/subscribeWithPrevious(): Observable<LifecycleStages> {return merge(getLifecycleStagesAndBefore(this.stage), this._lifecycle$.pipe(skip(1)))diff --git a/packages/ui/src/controllers/ui/ui-shared.controller.ts b/packages/ui/src/controllers/ui/ui-shared.controller.tsindex 2abfd92e51..bec5de8c45 100644--- a/packages/ui/src/controllers/ui/ui-shared.controller.ts+++ b/packages/ui/src/controllers/ui/ui-shared.controller.ts@@ -17,7 +17,7 @@import type { IDisposable, Injector, IUniverInstanceService, LifecycleService } from '@univerjs/core';import type { IRenderManagerService } from '@univerjs/engine-render';import type { ILayoutService } from '../../services/layout/layout.service';-import { Disposable, isInternalEditorID, LifecycleStages } from '@univerjs/core';+import { Disposable, isInternalEditorID, LifecycleStages, LifecycleUnreachableError } from '@univerjs/core';const STEADY_TIMEOUT = 3000;@@ -52,33 +52,41 @@ export abstract class SingleUnitUIController extends Disposable {this.disposeWithMe(this._layoutService.registerContentElement(contentElement));}- await this._lifecycleService.onStage(LifecycleStages.Ready);- this._renderTimeout = window.setTimeout(() => {- // First render- const allRenders = this._renderManagerService.getRenderAll();- for (const [key, render] of allRenders) {- if (this._changeRenderUnit(key, contentElement)) break;+ try {+ await this._lifecycleService.onStage(LifecycleStages.Ready);+ this._renderTimeout = window.setTimeout(() => {+ // First render.+ const allRenders = this._renderManagerService.getRenderAll();+ for (const [key] of allRenders) {+ if (this._changeRenderUnit(key, contentElement)) break;+ }++ // Render as focus shifts.+ this.disposeWithMe(this._instanceService.focused$.subscribe((unit) => {+ if (unit) this._changeRenderUnit(unit, contentElement);+ }));++ // When renderer created, check if matches the focused.+ this.disposeWithMe(this._renderManagerService.created$.subscribe((renderer) => {+ if (renderer.unitId === this._instanceService.getFocusedUnit()?.getUnitId()) this._changeRenderUnit(renderer.unitId, contentElement);+ }));++ this.disposeWithMe(this._renderManagerService.disposed$.subscribe((renderer) => {+ if (this._currentRenderId === renderer) this._currentRenderId = null;+ }));++ this._lifecycleService.stage = LifecycleStages.Rendered;+ this._steadyTimeout = window.setTimeout(() => {+ this._lifecycleService.stage = LifecycleStages.Steady;+ }, STEADY_TIMEOUT);+ }, 300);+ } catch (error) {+ if (error instanceof LifecycleUnreachableError) {+ return;}- // Render as focus shifts- this.disposeWithMe(this._instanceService.focused$.subscribe((unit) => {- if (unit) this._changeRenderUnit(unit, contentElement);- }));-- // When renderer created, check if matches the focused.- this.disposeWithMe(this._renderManagerService.created$.subscribe((renderer) => {- if (renderer.unitId === this._instanceService.getFocusedUnit()?.getUnitId()) this._changeRenderUnit(renderer.unitId, contentElement);- }));-- this.disposeWithMe(this._renderManagerService.disposed$.subscribe((renderer) => {- if (this._currentRenderId === renderer) this._currentRenderId = null;- }));-- this._lifecycleService.stage = LifecycleStages.Rendered;- this._steadyTimeout = window.setTimeout(() => {- this._lifecycleService.stage = LifecycleStages.Steady;- }, STEADY_TIMEOUT);- }, 300);+ throw error;+ }}));}