@equinor/fusion-observable
Version:
39 lines • 1.64 kB
JavaScript
/**
* Maps action definitions to callable dispatch functions bound to a subject.
*
* Given a record of named action creators, returns an object with the same keys
* where each value is a function that creates the action and dispatches it
* to the provided subject via `subject.next()`.
*
* Supports nested action definition objects (recursively mapped).
*
* @template T - The action definitions record type.
* @param actions - A record of named action creators (or nested action definition objects).
* @param subject - The target subject (e.g., {@link FlowSubject}) to dispatch actions to.
* @returns An object of callable dispatch functions mirroring the action definitions.
*
* @example
* ```ts
* import { actionMapper, createAction, FlowSubject } from '@equinor/fusion-observable';
*
* const actions = {
* increment: createAction<number>('increment'),
* decrement: createAction<number>('decrement'),
* };
*
* const subject = new FlowSubject(reducer);
* const dispatch = actionMapper(actions, subject);
* dispatch.increment(1); // dispatches { type: 'increment', payload: 1 }
* ```
*/
export const actionMapper = (actions,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
subject) => Object.entries(actions).reduce((cur, [prop, fnOrActions]) => Object.assign(cur, {
[prop]: typeof fnOrActions === 'function'
? /** if value is a function, call it 🤙🏻 */
(...args) => subject.next(fnOrActions(...args))
: /** extract child actions */
actionMapper(fnOrActions, subject),
}), {});
export default actionMapper;
//# sourceMappingURL=action-mapper.js.map