UNPKG

react-sweet-state

Version:

Global + local state combining the best of Redux and Context API

59 lines (46 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bindActions = exports.bindAction = void 0; var _defaults = _interopRequireDefault(require("../defaults")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var namedMutator = function namedMutator(storeState, actionName) { return function () { storeState.mutator.actionName = actionName; return storeState.mutator.apply(storeState, arguments); }; }; var warnings = new WeakMap(); var bindAction = function bindAction(storeState, actionFn, actionKey, config, actions) { var callThunk = function callThunk(instance, thunkFn, actionName) { return thunkFn({ setState: _defaults["default"].devtools ? namedMutator(instance.storeState, actionName) : instance.storeState.mutator, getState: instance.storeState.getState, get actions() { if (!warnings.has(actionFn)) { warnings.set(actionFn, console.warn("react-sweet-state 'actions' property has been deprecated and will be removed in the next mayor. " + "Please check action '".concat(actionName, "' of Store '").concat(instance.storeState.key, "' and use 'dispatch' instead"))); } return actions; }, dispatch: function dispatch(tFn) { return callThunk(instance, tFn, "".concat(actionName, ".dispatch")); } }, config.props()); }; return function () { return callThunk({ storeState: storeState, actions: actions }, actionFn.apply(void 0, arguments), actionKey); }; }; exports.bindAction = bindAction; var bindActions = function bindActions(actions, storeState, config) { var boundActions = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; return Object.keys(actions).reduce(function (acc, k) { acc[k] = bindAction(storeState, actions[k], k, config, boundActions || acc); return acc; }, {}); }; exports.bindActions = bindActions;