UNPKG

@empathyco/x-components

Version:
54 lines (51 loc) 1.79 kB
import { map } from '@empathyco/x-utils'; import { defineComponent, inject, computed } from 'vue'; import { useXBus } from '../composables/use-x-bus.js'; import _sfc_main$1 from './global-x-bus.vue.js'; /** * This component subscribes to any {@link XEvent} with a custom callbacks provided by the snippet * configuration. * * @public */ var _sfc_main = defineComponent({ name: 'SnippetCallbacks', components: { GlobalXBus: _sfc_main$1 }, setup() { const xBus = useXBus(); /** * Injects {@link SnippetConfig} provided by an ancestor as snippetConfig. * * @internal */ const snippetConfig = inject('snippetConfig'); /** * It maps all the callbacks provided by the snippetConfig and adds an emit to each one. * * @returns The event listeners with the {@link XEventsTypes.SnippetCallbackExecuted} emit in * the callback. * * @internal * */ const eventListeners = computed(() => { const callbacks = snippetConfig?.callbacks; return callbacks ? map(callbacks, (eventName, callback) => { return (payload, metadata) => { const callbackReturn = callback(payload, metadata); xBus.emit('SnippetCallbackExecuted', { event: eventName, callbackReturn, payload: payload, metadata, }); }; }) : {}; }); return { eventListeners }; }, }); export { _sfc_main as default }; //# sourceMappingURL=snippet-callbacks.vue2.js.map