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
feat(facade): add lifecycle hooks for facade (#2357)
Dushusirebb1d59734b9
diff --git a/packages/facade/src/apis/__tests__/f-hooks.spec.ts b/packages/facade/src/apis/__tests__/f-hooks.spec.tsnew file mode 100644index 0000000000..1a97f016d7--- /dev/null+++ b/packages/facade/src/apis/__tests__/f-hooks.spec.ts@@ -0,0 +1,52 @@+/**+ * 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.+ */++import { beforeEach, describe, expect, it, vi } from 'vitest';+import { LifecycleService, LifecycleStages } from '@univerjs/core';+import type { Injector } from '@wendellhu/redi';++import type { FUniver } from '../facade';+import { createFacadeTestBed } from './create-test-bed';++describe('Test FUniver', () => {+ let get: Injector['get'];+ let univerAPI: FUniver;++ beforeEach(() => {+ const testBed = createFacadeTestBed();+ get = testBed.get;+ univerAPI = testBed.univerAPI;+ });++ it('Hooks lifecycle stages', () => {+ const lifecycleService = get(LifecycleService);++ const onRendered = vi.fn();+ const onSteady = vi.fn();++ const onRenderedDisposable = univerAPI.getHooks().onRendered(onRendered);+ const onSteadyDisposable = univerAPI.getHooks().onSteady(onSteady);++ lifecycleService.stage = LifecycleStages.Rendered;+ lifecycleService.stage = LifecycleStages.Steady;++ expect(onRendered).toBeCalledTimes(1);+ expect(onSteady).toBeCalledTimes(1);++ onRenderedDisposable.dispose();+ onSteadyDisposable.dispose();+ });+});diff --git a/packages/facade/src/apis/f-hooks.ts b/packages/facade/src/apis/f-hooks.tsnew file mode 100644index 0000000000..4792af242b--- /dev/null+++ b/packages/facade/src/apis/f-hooks.ts@@ -0,0 +1,64 @@+/**+ * 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.+ */++import { LifecycleService, LifecycleStages, toDisposable } from '@univerjs/core';+import type { IDisposable } from '@wendellhu/redi';+import { Inject } from '@wendellhu/redi';+import { filter } from 'rxjs';++export class FHooks {+ constructor(+ @Inject(LifecycleService) private readonly _lifecycleService: LifecycleService+ ) {+ // empty+ }++ /**+ * The onStarting event is fired when lifecycle stage is Starting.+ * @param callback Callback function that will be called when the event is fired+ * @returns A disposable object that can be used to unsubscribe from the event+ */+ onStarting(callback: () => void): IDisposable {+ return toDisposable(this._lifecycleService.lifecycle$.pipe(filter((lifecycle) => lifecycle === LifecycleStages.Starting)).subscribe(callback));+ }++ /**+ * The onReady event is fired when lifecycle stage is Ready.+ * @param callback Callback function that will be called when the event is fired+ * @returns A disposable object that can be used to unsubscribe from the event+ */+ onReady(callback: () => void): IDisposable {+ return toDisposable(this._lifecycleService.lifecycle$.pipe(filter((lifecycle) => lifecycle === LifecycleStages.Ready)).subscribe(callback));+ }++ /**+ * The onRendered event is fired when lifecycle stage is Rendered.+ * @param callback Callback function that will be called when the event is fired+ * @returns A disposable object that can be used to unsubscribe from the event+ */+ onRendered(callback: () => void): IDisposable {+ return toDisposable(this._lifecycleService.lifecycle$.pipe(filter((lifecycle) => lifecycle === LifecycleStages.Rendered)).subscribe(callback));+ }++ /**+ * The onSteady event is fired when lifecycle stage is Steady.+ * @param callback Callback function that will be called when the event is fired+ * @returns A disposable object that can be used to unsubscribe from the event+ */+ onSteady(callback: () => void): IDisposable {+ return toDisposable(this._lifecycleService.lifecycle$.pipe(filter((lifecycle) => lifecycle === LifecycleStages.Steady)).subscribe(callback));+ }+}diff --git a/packages/facade/src/apis/facade.ts b/packages/facade/src/apis/facade.tsindex a54c1ce1ff..74b9e41265 100644--- a/packages/facade/src/apis/facade.ts+++ b/packages/facade/src/apis/facade.ts@@ -41,6 +41,7 @@ import { SetFormulaCalculationStartMutation } from '@univerjs/engine-formula';import { FDocument } from './docs/f-document';import { FWorkbook } from './sheets/f-workbook';import { FSheetHooks } from './sheets/f-sheet-hooks';+import { FHooks } from './f-hooks';export class FUniver {/**@@ -325,6 +326,13 @@ export class FUniver {return this._injector.createInstance(FSheetHooks);}+ /**+ * Get hooks+ */+ getHooks() {+ return this._injector.createInstance(FHooks);+ }+/*** Get sheet render component from render by unitId and view key.* @param unitId