From f5d57374fb8291620256f8c742052cd7d5069f9d Mon Sep 17 00:00:00 2001 From: Jesse Mazzella Date: Thu, 21 Dec 2023 16:45:07 -0800 Subject: [PATCH] feat: add `useEventListener` composable --- src/ui/composables/event.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/ui/composables/event.js 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); + }); +}