feat: add useEventListener composable

This commit is contained in:
Jesse Mazzella 2023-12-21 16:45:07 -08:00
parent 715a44864e
commit f5d57374fb

View File

@ -0,0 +1,18 @@
import { onBeforeMount, onBeforeUnmount } from 'vue';
/**
* @param {*} api the specific openmct API to use i.e. openmct.editor
* @param {string} eventName
* @param {() => void} handler
*/
export function useEventListener(api, eventName, handler) {
onBeforeMount(() => {
// Add the event listener before the component is mounted
api.on(eventName, handler);
});
onBeforeUnmount(() => {
// Remove the event listener before the component is unmounted
api.off(eventName, handler);
});
}