UNPKG

@empathyco/x-components

Version:
70 lines (67 loc) 2.62 kB
import { forEach } from '@empathyco/x-utils'; import { getGettersProxyFromModule } from '../store/utils/getters-proxy.utils.js'; /** * Registers the store emitters, making them emit the event when the part of the state selected * changes. * * @param xModule - The {@link XModule} to register its Store Emitters. * @param bus - The XBus to emit the events by the Emitters. * @param store - The Vuex store to access to state and getters to watch them. * @internal */ function registerStoreEmitters({ name, storeEmitters, storeModule }, bus, store) { const safeGettersProxy = getGettersProxyFromModule(store.getters, name, storeModule); forEach(storeEmitters, (event, stateSelector) => { const { selector, immediate, filter, metadata, ...options } = normalizeStateSelector(stateSelector); const watcherSelector = () => // eslint-disable-next-line ts/no-unsafe-return,ts/no-unsafe-member-access selector(store.state.x[name], safeGettersProxy); const emit = (newValue, oldValue) => { // eslint-disable-next-line ts/no-unsafe-member-access if (filter(newValue, oldValue, store.state.x[name])) { void bus.emit(event, newValue, { ...metadata, moduleName: name, oldValue }); } }; store.watch(watcherSelector, emit, options); if (immediate) { emit(watcherSelector()); } }); } /** * Transforms a {@link AnySimpleStateSelector} into a {@link AnyStateSelector}, and sets * default values for its properties. * * @param stateSelector - The state selector to normalize. * @returns A {@link AnyStateSelector} with all the properties set. * * @internal */ function normalizeStateSelector(stateSelector) { const selectorOptions = isSimpleSelector(stateSelector) ? { selector: stateSelector } : stateSelector; return { deep: false, immediate: false, filter: () => true, metadata: { replaceable: true, }, ...selectorOptions, }; } /** * Checks if the type of the store emitter selector is simple or complex. This selector can be * a function if it is simple or an object with the selector and other options if it is complex. * * @param stateSelector - The store emitter selector. * @returns A boolean which flags if the stateSelector is simple (function) or complex (object). * * @internal */ function isSimpleSelector(stateSelector) { return typeof stateSelector === 'function'; } export { isSimpleSelector, registerStoreEmitters }; //# sourceMappingURL=x-emitters.js.map