Skip to content

Commit

Permalink
feat: migrating global-x-bus (apart from $listeners) and adding defau…
Browse files Browse the repository at this point in the history
…lt value to location in useXBus
  • Loading branch information
albertjcuac committed Feb 28, 2024
1 parent ae3bb29 commit 823f2b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
83 changes: 43 additions & 40 deletions packages/x-components/src/components/global-x-bus.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import { reduce } from '@empathyco/x-utils';
import { Component } from 'vue-property-decorator';
import { Observable, Subscription } from 'rxjs';
import { EventPayload, SubjectPayload } from '@empathyco/x-bus';
import { XEventListeners } from '../x-installer/api/api.types';
import { defineComponent, getCurrentInstance, onBeforeUnmount } from 'vue';
import { WireMetadata } from '../wiring/wiring.types';
import { XEventsTypes } from '../wiring/events.types';
import { useXBus } from '../composables/use-x-bus';
import { XEventListeners } from '../x-installer/api/api.types';
import { NoElement } from './no-element';
/**
Expand All @@ -14,48 +15,50 @@
*
* @public
*/
@Component
export default class GlobalXBus extends NoElement {
/**
* Object with the {@link XEvent} listeners.
*
* @internal
*/
public $listeners!: XEventListeners;
export default defineComponent({
name: 'GlobalXBus',
extends: NoElement,
created(): void {
this.handleXEventSubscription();
}
setup() {
/**
* Object with the {@link XEvent} listeners.
*
* @internal
*/
//TODO: When we get to vue 3 remove $listeners and use $attrs instead
const listeners: XEventListeners = getCurrentInstance()?.proxy?.$listeners ?? {};
/**
* Handles a subscription to all the events provided in the listeners with the function that
* will execute the callback. Also unsubscribes on beforeDestroy.
*
* @internal
*/
protected handleXEventSubscription(): void {
const subscription = reduce(
this.$listeners,
(subscription, eventName, callback) => {
subscription.add(
(
this.$x.on(eventName, true) as unknown as Observable<
SubjectPayload<EventPayload<XEventsTypes, typeof eventName>, WireMetadata>
>
).subscribe(({ eventPayload, metadata }) => {
callback(eventPayload as never, metadata);
})
);
return subscription;
},
new Subscription()
);
/**
* Handles a subscription to all the events provided in the listeners with the function that
* will execute the callback. Also unsubscribes on beforeDestroy.
*
* @internal
*/
const handleXEventSubscription = (): void => {
const subscription = reduce(
listeners,
(subscription, eventName, callback) => {
subscription.add(
(
useXBus().on(eventName, true) as unknown as Observable<
SubjectPayload<EventPayload<XEventsTypes, typeof eventName>, WireMetadata>
>
).subscribe(({ eventPayload, metadata }: any) => {
callback(eventPayload as never, metadata);
})
);
return subscription;
},
new Subscription()
);
this.$on('hook:beforeDestroy', () => {
subscription.unsubscribe();
});
onBeforeUnmount(() => {
subscription.unsubscribe();
});
};
handleXEventSubscription();
}
}
});
</script>

<docs lang="mdx">
Expand Down
2 changes: 1 addition & 1 deletion packages/x-components/src/composables/use-x-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { PropsWithType } from '../utils/index';
* @returns An object with the `on` and `emit` functions.
*/
export function useXBus(): UseXBusAPI {
const location = inject<FeatureLocation>('location');
const location = inject<FeatureLocation>('location', undefined as any);

const currentComponent: PrivateExtendedVueComponent | undefined | null =
getCurrentInstance()?.proxy;
Expand Down

0 comments on commit 823f2b1

Please sign in to comment.