UNPKG

@zedux/core

Version:

A high-level, declarative, composable form of Redux

34 lines (31 loc) 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.actionFactory = void 0; const detailedTypeof_1 = require("./detailedTypeof"); /** 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. */ const actionFactory = (actionType) => { if (true /* DEV */ && typeof actionType !== 'string') { throw new TypeError(`Zedux: actionFactory() - actionType must be a string. Received ${(0, detailedTypeof_1.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; }; exports.actionFactory = actionFactory;