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(use-x-bus): unsubscribe from bus events when component is unmounted #1472

Merged
merged 1 commit into from
May 21, 2024

Conversation

joseacabaneros
Copy link
Member

useXBus.on function registers the subscriptions itself to be able to unsubscribe once the component is unmounted.
useXBus.emit now returns the emission promise.

Highlights:

  • Open issue. If applicable, link: EMP-4083

Type of change

  • Vue 3 migration

What is the destination branch of this PR?

  • Main

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review on my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

@joseacabaneros joseacabaneros requested a review from a team as a code owner May 14, 2024 15:17
const xAliasAPI = useAliasApi();
const xBusAPI = useXBus();
return Object.assign(xAliasAPI, xBusAPI);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

How nice! 👏

subscription.unsubscribe();
Object.entries(listeners as XEventListeners).forEach(([eventName, callback]) => {
xBus.on(eventName as XEvent, true).subscribe(({ eventPayload, metadata }) => {
callback(eventPayload as never, metadata);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if I understand this cast🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

It is because callback payload is inferred as never due to TS can't know listeners type in build time. It is generic for any event.
I'm sure we could improve the type inference, but further on.

Screenshot 2024-05-20 at 3 01 29 PM

@diegopf diegopf merged commit 36f542a into main May 21, 2024
4 checks passed
@diegopf diegopf deleted the EMP-4083-usexbus-unmounted branch May 21, 2024 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants