Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(facade): add lifecycle hooks for facade #2357

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/facade/src/apis/__tests__/f-hooks.spec.ts
Original file line number Diff line number Diff line change
@@ -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
*
* https://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();
});
});
64 changes: 64 additions & 0 deletions packages/facade/src/apis/f-hooks.ts
Original file line number Diff line number Diff line change
@@ -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
*
* https://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));
}
}
8 changes: 8 additions & 0 deletions packages/facade/src/apis/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -325,6 +326,13 @@ export class FUniver {
return this._injector.createInstance(FSheetHooks);
}

/**
* Get hooks
*/
getHooks() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name should be more specific, like getLifecycleHooks. Unless you're going to bind other types of hooks o f-hook. (And what are other possible books?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have considered this, and it is indeed worth discussing.

The current design is divided into 4 types of hooks, FHooks general hooks, FSheetHooks/FDocHooks/FSlideHooks hooks for specific document types

Although the role of hooks can be replaced by listening to Command in most scenarios, hooks are more semantic and more popular. In the future, we may refer to hansontable to add some hooks https://handsontable.com/docs/javascript-data-grid/api/hooks/
Add afterDispose, afterCopy, afterPaste... to FHooks

This brings up a management issue. Should the internal distinction of the 4 types of hooks be refined?
If it is necessary to refine, it can be divided into
FLifecycleHooks: including life cycle, dispose event
FClipboardHooks: clipboard hook
FRowHooks
FColumnHooks
FCellHooks
etc.

Which solution is better?

Copy link
Member

@wzhudev wzhudev Jun 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Let's merge all hooks in this class. Maybe it's better for new users to get what they want.

return this._injector.createInstance(FHooks);
}

/**
* Get sheet render component from render by unitId and view key.
* @param unitId
Expand Down
Loading