-
-
Notifications
You must be signed in to change notification settings - Fork 550
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(facade): add hooks for facade
- Loading branch information
commit e35d308a5cd6227aabfc1672ad6c9356a5daf763
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 of-hook
. (And what are other possible books?)There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.