@empathyco/x-components
Version:
Empathy X Components
73 lines (70 loc) • 3 kB
JavaScript
import { wireCommit, wireCommitWithoutPayload, wireDispatch, wireDispatchWithoutPayload } from './wires.factory.js';
import { getStateAndGettersFromModule } from './wiring.utils.js';
/**
* Creates a namespaced {@link (wireCommit:1)} for the module name passed.
*
* @param moduleName - The module name for scoping the {@link (wireCommit:1)}.
* @returns A function which creates a namespaced wire for the {@link (wireCommit:1)}.
*
* @public
*/
function namespacedWireCommit(moduleName) {
return (mutation, payload) => wireCommit(`x/${moduleName}/${mutation}`, getPayload(moduleName, payload));
}
/**
* Creates a namespaced {@link wireCommitWithoutPayload} for the module name passed.
*
* @param moduleName - The module name for scoping the {@link wireCommitWithoutPayload}.
* @returns A function which creates a namespaced wire for the {@link wireCommitWithoutPayload}.
*
* @public
*/
function namespacedWireCommitWithoutPayload(moduleName) {
return mutation => wireCommitWithoutPayload(`x/${moduleName}/${mutation}`);
}
/**
* Creates a namespaced {@link (wireDispatch:1)} for the module name passed.
*
* @param moduleName - The module name for scoping the {@link (wireDispatch:1)}.
* @returns A function which creates a namespaced wire for the {@link (wireDispatch:1)}.
*
* @public
*/
function namespacedWireDispatch(moduleName) {
return (action, payload) => wireDispatch(`x/${moduleName}/${action}`, getPayload(moduleName, payload));
}
/**
* Creates a namespaced {@link wireDispatchWithoutPayload} for the module name passed.
*
* @param moduleName - The module name for scoping the {@link wireDispatchWithoutPayload}.
* @returns A function which creates a namespaced wire for the {@link wireDispatchWithoutPayload}.
*
* @public
*/
function namespacedWireDispatchWithoutPayload(moduleName) {
return action => wireDispatchWithoutPayload(`x/${moduleName}/${action}`);
}
/**
* Decision maker of if the payload is a function which receives the
* {@link StoreModuleStateAndGetters | module state and getters}, the payload and the metadata,
* returns the payload for or a static value.
*
* @param moduleName - The {@link XModuleName | module name} for scoping the state and getters.
* @param payload - The payload for the wire which can be a retrieving function or a static value.
* @returns The function which will retrieve data from the store or the static value.
*
* @internal
*/
function getPayload(moduleName, payload) {
return typeof payload === 'function'
? ({ state, getters, eventPayload, metadata }) =>
// eslint-disable-next-line ts/no-unsafe-call,ts/no-unsafe-return
payload({
...getStateAndGettersFromModule(state, getters, moduleName),
eventPayload,
metadata,
})
: payload;
}
export { namespacedWireCommit, namespacedWireCommitWithoutPayload, namespacedWireDispatch, namespacedWireDispatchWithoutPayload };
//# sourceMappingURL=namespaced-wires.factory.js.map