easy-redux
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
'use strict';exports.__esModule = true;var _extends2 = require('babel-runtime/helpers/extends');var _extends3 = _interopRequireDefault(_extends2);var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);exports. | ||
'use strict';exports.__esModule = true;var _entries = require('babel-runtime/core-js/object/entries');var _entries2 = _interopRequireDefault(_entries);var _keys = require('babel-runtime/core-js/object/keys');var _keys2 = _interopRequireDefault(_keys);var _assign = require('babel-runtime/core-js/object/assign');var _assign2 = _interopRequireDefault(_assign);var _extends2 = require('babel-runtime/helpers/extends');var _extends3 = _interopRequireDefault(_extends2);var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);exports. | ||
@@ -25,54 +25,83 @@ | ||
createAction = createAction;var _index = require('./index');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function checkAsyncHandlers(handlers) {var onWait = handlers.onWait;var onSuccess = handlers.onSuccess;var onFail = handlers.onFail;var errMessage = function errMessage(fnName) {return 'Expected that the ' + fnName + ' will be a function, and will returns new state';};if (!(0, _index.isFunction)(onWait)) {throw new Error(errMessage('onWait'));}if (!(0, _index.isFunction)(onSuccess)) {throw new Error(errMessage('onSuccess'));}if (!(0, _index.isFunction)(onFail)) {throw new Error(errMessage('onFail'));}} /** | ||
* | ||
* @param {String} name | ||
* @param {Object} options | ||
* @returns {Function} | ||
*/function createAction(name) {var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];var WAIT = 'WAIT@' + name;var SUCCESS = 'SUCCESS@' + name;var FAIL = 'FAIL@' + name;var type = name;var action = options.action;var async = options.async;var storeKey = options.storeKey;var handlers = options.handlers;var handler = options.handler;var initialState = options.initialState; | ||
var reducer = void 0; | ||
createAction = createAction;exports. | ||
if (!initialState) { | ||
throw new Error('Initial state should not be null or undefined'); | ||
} | ||
if (!storeKey) { | ||
throw new Error('Store key at action ' + name + ' not defined'); | ||
} | ||
if (async) { | ||
checkAsyncHandlers(handlers); | ||
reducer = function reducer() {var state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0];var action = arguments[1]; | ||
switch (action.type) { | ||
case WAIT:return handlers.onWait(state, action); | ||
case SUCCESS:return handlers.onSuccess(state, action); | ||
case FAIL:return handlers.onFail(state, action); | ||
default:return state;} | ||
}; | ||
} else | ||
{ | ||
if (!(0, _index.isFunction)(handler)) { | ||
throw new Error('Expected that actions ' + name + ' handler will be a function, and it will returns new state'); | ||
} | ||
reducer = function reducer() {var state = arguments.length <= 0 || arguments[0] === undefined ? initialState : arguments[0];var action = arguments[1];return action.type === name ? handler(state, action) : state;}; | ||
} | ||
(0, _index.mergeReducer)(storeKey, reducer); | ||
return function () {for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];} | ||
return function () {var _action$apply = | ||
action.apply(undefined, args);var promise = _action$apply.promise;var excludeTypeFromActionIfExists = _action$apply.type;var actionPayload = (0, _objectWithoutProperties3.default)(_action$apply, ['promise', 'type']); | ||
if (async) { | ||
if (!(0, _index.isFunction)(promise)) { | ||
throw new Error('Async action ' + name + ' should return promise property of Function type'); | ||
} else | ||
{ | ||
return (0, _extends3.default)({ types: [WAIT, SUCCESS, FAIL], promise: promise }, actionPayload); | ||
} | ||
} else | ||
{ | ||
return (0, _extends3.default)({ type: type }, actionPayload); | ||
} | ||
}(); | ||
}; | ||
createActions = createActions;var _index = require('./index');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function checkAsyncHandlers(handlers) {var onWait = handlers.onWait,onSuccess = handlers.onSuccess,onFail = handlers.onFail;var errMessage = function errMessage(fnName) {return 'Expected that the ' + fnName + ' will be a function, and will returns new state';};if (!(0, _index.isFunction)(onWait)) {throw new Error(errMessage('onWait'));}if (!(0, _index.isFunction)(onSuccess)) {throw new Error(errMessage('onSuccess'));}if (!(0, _index.isFunction)(onFail)) {throw new Error(errMessage('onFail'));}} /** | ||
* | ||
* @param {String} name | ||
* @param {Object} options | ||
* @returns {Function} | ||
*/function createAction(name) {var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};var WAIT = 'WAIT@' + name;var SUCCESS = 'SUCCESS@' + name;var FAIL = 'FAIL@' + name;var type = name;var action = options.action,async = options.async,storeKey = options.storeKey,handlers = options.handlers,handler = options.handler,initialState = options.initialState;var reducer = void 0;if (!initialState) {throw new Error('Initial state should not be null or undefined');}if (!storeKey) {throw new Error('Store key at action ' + name + ' not defined');}if (async) {checkAsyncHandlers(handlers);reducer = function reducer() {var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;var action = arguments[1];switch (action.type) {case WAIT:return handlers.onWait(state, action);case SUCCESS:return handlers.onSuccess(state, action);case FAIL:return handlers.onFail(state, action);default:return state;}};} else {if (!(0, _index.isFunction)(handler)) {throw new Error('Expected that synchronous action ' + name + ' handler will be a function, and it will returns new state');}reducer = function reducer() {var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;var action = arguments[1];return action.type === name ? handler(state, action) : state;};}(0, _index.mergeReducer)(storeKey, reducer);return function () {for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}return function () {var _action$apply = action.apply(undefined, args),promise = _action$apply.promise,excludeTypeFromActionIfExists = _action$apply.type,actionPayload = (0, _objectWithoutProperties3.default)(_action$apply, ['promise', 'type']);if (async) {if (!(0, _index.isFunction)(promise)) {throw new Error('Async action ' + name + ' should return promise property of Function type');} else {return (0, _extends3.default)({ types: [WAIT, SUCCESS, FAIL], promise: promise }, actionPayload);}} else {return (0, _extends3.default)({ type: type }, actionPayload);}}();};}function defaultAction() {return {};}function createActions() {var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _assign2.default)({}, defaults);var actions = options.actions,globalInitialState = options.initialState,globalStoreKey = options.storeKey;if (!(0, _keys2.default)(actions)) {throw new Error('No any action passed');}return (0, _entries2.default)(actions).reduce(function (res, _ref) {var actionName = _ref[0],actionOptions = _ref[1];var | ||
optionAsync = | ||
actionOptions.async,initialState = actionOptions.initialState,storeKey = actionOptions.storeKey,action = actionOptions.action,handler = actionOptions.handler,handlers = actionOptions.handlers; | ||
var async = optionAsync || !!handlers; | ||
res[actionName] = createAction(actionName, { | ||
async: async, | ||
action: action || defaultAction, | ||
handler: handler, | ||
handlers: handlers, | ||
initialState: initialState || globalInitialState, | ||
storeKey: storeKey || globalStoreKey }); | ||
return res; | ||
}, {}); | ||
} |
@@ -50,10 +50,10 @@ 'use strict';exports.__esModule = true;var _extends2 = require('babel-runtime/helpers/extends');var _extends3 = _interopRequireDefault(_extends2);exports. | ||
* | ||
*/function applyReducer(key, fn) {var replace = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];if (!!cache[key] && !replace) {return console.error('Reducer with name ' + key + ' exists');}cache[key] = fn;}function mergeReducer(key, fn) {if (!cache[key]) {//blank reducer if not exist | ||
*/function applyReducer(key, fn) {var replace = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;if (!!cache[key] && !replace) {return console.error('Reducer with name ' + key + ' exists');}cache[key] = fn;}function mergeReducer(key, fn) {if (!cache[key]) {//blank reducer if not exist | ||
cache[key] = function (state) {return state;};}applyReducer(key, localCompose(cache[key], fn), true);} /** | ||
* | ||
* @param {Object} reducers - reducers that must be merged with cached | ||
*/function combine() {var reducers = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];return (0, _redux.combineReducers)((0, _extends3.default)({}, reducers, cache));} /** | ||
* combine reducers to pass them to the store under single key | ||
* @param {Array} reducers | ||
* @returns {Function} | ||
*/function localCompose() {for (var _len = arguments.length, reducers = Array(_len), _key = 0; _key < _len; _key++) {reducers[_key] = arguments[_key];}return function (state, action) {return reducers.reduce(function (prevState, currentReducer) {return currentReducer(prevState, action);}, state);};} | ||
*/function combine() {var reducers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};return (0, _redux.combineReducers)((0, _extends3.default)({}, reducers, cache));} /** | ||
* combine reducers to pass them to the store under single key | ||
* @param {Array} reducers | ||
* @returns {Function} | ||
*/function localCompose() {for (var _len = arguments.length, reducers = Array(_len), _key = 0; _key < _len; _key++) {reducers[_key] = arguments[_key];}return function (state, action) {return reducers.reduce(function (prevState, currentReducer) {return currentReducer(prevState, action);}, state);};} |
@@ -12,2 +12,3 @@ 'use strict';exports.__esModule = true;var _middleware = require('./middleware');Object.defineProperty(exports, 'middleware', { enumerable: true, get: function get() {return _interopRequireDefault(_middleware). | ||
createAction;} });function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var isFunction = exports.isFunction = function isFunction(thing) {return typeof thing === 'function';}; | ||
createAction;} });Object.defineProperty(exports, 'createActions', { enumerable: true, get: function get() {return _actionCreator. | ||
createActions;} });function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}var isFunction = exports.isFunction = function isFunction(thing) {return typeof thing === 'function';}; |
'use strict';exports.__esModule = true;var _extends2 = require('babel-runtime/helpers/extends');var _extends3 = _interopRequireDefault(_extends2);var _assign = require('babel-runtime/core-js/object/assign');var _assign2 = _interopRequireDefault(_assign);var _promise2 = require('babel-runtime/core-js/promise');var _promise3 = _interopRequireDefault(_promise2);var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);exports.default = | ||
function (request) { | ||
return function (_ref) {var dispatch = _ref.dispatch;var getState = _ref.getState;return function (next) {return function (action) { | ||
return function (_ref) {var dispatch = _ref.dispatch,getState = _ref.getState;return function (next) {return function (action) { | ||
if ((0, _index.isFunction)(action)) { | ||
return action(dispatch, getState); | ||
}var | ||
promise = action.promise;var types = action.types;var rest = (0, _objectWithoutProperties3.default)(action, ['promise', 'types']); | ||
promise = action.promise,types = action.types,rest = (0, _objectWithoutProperties3.default)(action, ['promise', 'types']); | ||
if (!promise) { | ||
@@ -13,7 +13,7 @@ return next(action); | ||
REQUEST = types[0];var SUCCESS = types[1];var FAILURE = types[2]; | ||
REQUEST = types[0],SUCCESS = types[1],FAILURE = types[2]; | ||
var actionPromise = promise(request); | ||
if (!(actionPromise instanceof _promise3.default)) {var _actionPromise = | ||
actionPromise;var _promise = _actionPromise.promise;var actionRest = (0, _objectWithoutProperties3.default)(_actionPromise, ['promise']); | ||
actionPromise,_promise = _actionPromise.promise,actionRest = (0, _objectWithoutProperties3.default)(_actionPromise, ['promise']); | ||
if (!(0, _index.isFunction)(_promise)) { | ||
@@ -20,0 +20,0 @@ throw new Error('Bad promise'); |
{ | ||
"name": "easy-redux", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Helpers to facilitate the work with redux", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
122
README.md
@@ -7,1 +7,123 @@ # easy-redux | ||
See the [LICENSE](LICENSE.md) file for license rights and limitations (MIT). | ||
ΠΡΠ½ΠΎΠ²Π½Π°Ρ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΡ - ΡΡ ΠΎΠ΄ ΠΎΡ ΡΠ°Π·ΠΌΡΠ²Π°Π½ΠΈΡ ΠΊΠΎΠ΄Π° Π½Π° actions, constants, types. ΠΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° ΡΡΠ°Π½ΠΎΠ²ΠΈΡΡΡ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠΎΠΉ, ΠΈΠ½ΠΊΠ°ΠΏΡΡΠ»ΠΈΡΡΠ΅Ρ Π² ΡΠ΅Π±Π΅ Π²ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ ΠΈ ΠΌΠ΅ΡΠΎΠ΄Ρ ΠΊΠΎΡΠΎΡΡΠ΅ Π½ΡΠΆΠ½Ρ Π΄Π»Ρ ΡΠ°Π±ΠΎΡΡ. | ||
Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°: | ||
``` | ||
npm i easy-redux -S | ||
``` | ||
Π Π΄Π°Π½Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΡΡΡΡΠΊΡΡΡΠ° ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡ Π²Π½ΡΡΡΠΈ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ ΠΏΡΠΈΠ½ΠΈΠΌΠ΅Ρ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΉ Π²ΠΈΠ΄: | ||
``` | ||
/components | ||
/MyComponent | ||
actions.js | ||
component.js | ||
index.js | ||
``` | ||
```javascript | ||
//index.js | ||
import {PropTypes} from 'react'; | ||
const {bool, string, array} = PropTypes; | ||
export const dataShape = { | ||
isWaiting: bool, | ||
isFailed: bool, | ||
status: string.isRequired, | ||
likes: array.isRequired, | ||
}; | ||
export const STORE_KEY = 'likes'; | ||
export MyComponent from './component'; | ||
``` | ||
```javascript | ||
//actions.js | ||
import {STORE_KEY} from './index'; | ||
export const ACTION_RESET = 'increment'; | ||
export const ACTION_REMOTE_LIKES = 'remote_like'; | ||
const initialState = { | ||
isWaiting: false, | ||
isFailed: false, | ||
status: 'expired', | ||
likes: [], | ||
}; | ||
export default { | ||
[ACTION_REMOTE_LIKES]: createAction(ACTION_REMOTE_LIKES, { | ||
async: true, | ||
storeKey: STORE_KEY, | ||
initialState, | ||
action: userId => ({promise: request => request('/api/likes').get({userId})}), | ||
handlers: { | ||
onWait: (state, action) => ({...state, isWaiting: true, isFailed: false}), | ||
onFail: (state, action) => ({...state, isWaiting: false, isFailed: true}), | ||
onSuccess: (state, action) => ({...state, isWaiting: false, likes: action.result, status:'updated'}) | ||
} | ||
}), | ||
[ACTION_RESET]: createAction (ACTION_RESET, { | ||
initialState, | ||
storeKey: STORE_KEY, | ||
action: () =>({}), | ||
handler: (state, action) => ({...state, ...Object.assign({}, initialState)}) | ||
}) | ||
}; | ||
``` | ||
```javascript | ||
//component.js | ||
import {Component, PropTypes} from 'react'; | ||
import {STORE_KEY, dataShape} from './index'; | ||
import {connect} from 'react-redux'; | ||
import {ACTION_REMOTE_LIKES, ACTION_RESET} from './actions'; | ||
@connect(state => ({ | ||
[STORE_KEY]: state[STORE_KEY] | ||
}), {ACTION_REMOTE_LIKES, ACTION_RESET}) | ||
export default class MyComponent extends Component { | ||
static propTypes = { | ||
[STORE_KEY]: PropTypes.shape(dataShape), | ||
//actions | ||
[ACTION_REMOTE_LIKES]: PropTypes.func.isRequired, | ||
[ACTION_RESET]: PropTypes.func.isRequired | ||
}; | ||
onUpdateClick = (e) => { | ||
this.props[ACTION_REMOTE_LIKES](); | ||
}; | ||
onResetClick = (e) => { | ||
this.props[ACTION_RESET](); | ||
}; | ||
render () { | ||
const {[STORE_KEY]: {status, isWaiting}} = this.props; | ||
return ( | ||
<div> | ||
<p>{status}</p> | ||
<button onClick={this.onUpdateClick} className={isWaiting ? 'waiting' : ''}>Update</button> | ||
<button onClick={this.onResetClick}>Reset</button> | ||
</div> | ||
); | ||
} | ||
} | ||
``` | ||
Π‘ΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ ΠΏΡΠΈ ΠΈΠΌΠΏΠΎΡΡΠ΅ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡ Π² Π³Π»ΠΎΠ±Π°Π»ΡΠ½ΠΎΠΌ ΡΡΠΎΡΠ΅ ΠΏΠΎΡΠ²ΠΈΡΡΡ ΠΏΠΎΠ»Π΅ `likes` (`STORE_KEY`), ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠΈΡΡΡ `reducer` ΠΈ `actions` |
@@ -54,3 +54,3 @@ import {mergeReducer, isFunction} from './index'; | ||
if (!isFunction(handler)) { | ||
throw new Error(`Expected that actions ${name} handler will be a function, and it will returns new state`) | ||
throw new Error(`Expected that synchronous action ${name} handler will be a function, and it will returns new state`) | ||
} | ||
@@ -78,2 +78,31 @@ reducer = (state = initialState, action) => action.type === name ? handler(state, action) : state; | ||
} | ||
} | ||
function defaultAction() { return {}; } | ||
export function createActions(options = Object.assign({},defaults)) { | ||
const {actions, initialState: globalInitialState, storeKey: globalStoreKey} = options; | ||
if (!Object.keys(actions)) { | ||
throw new Error('No any action passed'); | ||
} | ||
return Object.entries(actions).reduce((res, [actionName, actionOptions]) => { | ||
const { | ||
async: optionAsync, | ||
initialState, | ||
storeKey, | ||
action, | ||
handler, | ||
handlers, | ||
} = actionOptions; | ||
const async = optionAsync || !!handlers; | ||
res[actionName] = createAction(actionName, { | ||
async, | ||
action: action || defaultAction, | ||
handler, | ||
handlers, | ||
initialState: initialState || globalInitialState, | ||
storeKey: storeKey || globalStoreKey, | ||
}); | ||
return res; | ||
}, {}); | ||
} |
@@ -12,2 +12,3 @@ export const isFunction = thing => typeof thing === 'function'; | ||
export {createAction} from './actionCreator'; | ||
export {createAction} from './actionCreator'; | ||
export {createActions} from './actionCreator'; |
Sorry, the diff of this file is not supported yet
29647
23.28%280
3.7%129
1742.86%