@zedux/core
Version:
A high-level, declarative, composable form of Redux
30 lines (27 loc) • 1.1 kB
JavaScript
import { detailedTypeof } from './detailedTypeof.js';
/**
Factory for creating ActionFactory objects.
ActionFactories are just action creators with an extra `.type` property set to
the string passed to `actionFactory()`.
ActionFactories can be passed directly to a ReducerBuilder's `reduce()`
method, thus removing the necessity of string constants.
*/
export const actionFactory = (actionType) => {
if (true /* DEV */ && typeof actionType !== 'string') {
throw new TypeError(`Zedux: actionFactory() - actionType must be a string. Received ${detailedTypeof(actionType)}`);
}
// The factory itself just returns a normal action object with the `type` and
// optional `payload` set.
const factory = ((payload) => {
const action = {
type: factory.type,
};
if (typeof payload !== 'undefined')
action.payload = payload;
return action;
});
// Expose the action `type` for this factory. Read only! There should never be
// any reason to modify this.
factory.type = actionType;
return factory;
};