diff --git a/src/ui/composables/event.js b/src/ui/composables/event.js new file mode 100644 index 0000000000..fcfc6ffb79 --- /dev/null +++ b/src/ui/composables/event.js @@ -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); + }); +}