UNPKG

redux-polling

Version:

Convenient way to support polling in your Redux app so you can focus on the business logic

116 lines (82 loc) 3.7 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.reduxPollingNamespace = undefined; var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); var _extends3 = require('babel-runtime/helpers/extends'); var _extends4 = _interopRequireDefault(_extends3); exports.getPollingState = getPollingState; var _actions = require('./actions'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var initialPollingState = { isActive: false, requestPayload: undefined, history: [], lastEntry: undefined }; var reduxPollingNamespace = exports.reduxPollingNamespace = 'REDUX_POLLING'; function getPollingState(state, pollingName) { return (state[reduxPollingNamespace] || {})[pollingName] || initialPollingState; } function createPollingReducer() { var initialState = {}; return function () { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; var action = arguments[1]; if (!(0, _actions.isPollingAction)(action)) { return state; } var _action$meta = action.meta, pollingName = _action$meta.pollingName, historyLimit = _action$meta.historyLimit; var prevPollingState = state[pollingName]; var nextPollingState = void 0; var nextHistory = void 0; var lastEntry = void 0; switch (action.type) { case _actions.actionTypes.start: nextPollingState = (0, _extends4.default)({}, initialPollingState, prevPollingState, { isActive: true, requestPayload: action.payload }); break; case _actions.actionTypes.stop: nextPollingState = (0, _extends4.default)({}, prevPollingState, { isActive: false }); break; case _actions.actionTypes.reset: nextPollingState = initialPollingState; break; case _actions.actionTypes.addEntries: if (historyLimit === 0) { nextHistory = prevPollingState.history; } else { nextHistory = [].concat((0, _toConsumableArray3.default)(prevPollingState.history), (0, _toConsumableArray3.default)(action.payload)); if (historyLimit > 0) { nextHistory = nextHistory.slice(-historyLimit); } } var _action$payload$slice = action.payload.slice(-1); var _action$payload$slice2 = (0, _slicedToArray3.default)(_action$payload$slice, 1); lastEntry = _action$payload$slice2[0]; nextPollingState = (0, _extends4.default)({}, prevPollingState, { history: nextHistory, lastEntry: lastEntry }); break; default: nextPollingState = undefined; break; } var nextState = !nextPollingState ? state : (0, _extends4.default)({}, state, (0, _defineProperty3.default)({}, pollingName, nextPollingState)); return nextState; }; } exports.default = createPollingReducer();