UNPKG

redux-fluorine

Version:

A Redux enhancer to manage groups of actions as observables (agendas)

460 lines (343 loc) 13.3 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["ReduxFluorine"] = factory(); else root["ReduxFluorine"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.l = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // identity function for calling harmory imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ // define getter function for harmory exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ }; /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 10); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony export (binding) */ __webpack_require__.d(exports, "b", function() { return RECOVER_AFTER_ERROR; }); /* harmony export (immutable) */ exports["a"] = filterActions; var RECOVER_AFTER_ERROR = '@@redux-fluorine/RECOVER_AFTER_ERROR'; function filterActions(anchor, actions) { return { type: RECOVER_AFTER_ERROR, anchor: anchor, actions: actions }; } /***/ }, /* 1 */ /***/ function(module, exports, __webpack_require__) { module.exports = __webpack_require__(7); /***/ }, /* 2 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_symbol_observable__ = __webpack_require__(1); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_symbol_observable___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_symbol_observable__); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__util_isObservable__ = __webpack_require__(4); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__util_executeAgenda__ = __webpack_require__(3); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__util_wrapStateReducer__ = __webpack_require__(6); /* harmony export (immutable) */ exports["a"] = createAgendaEnhancer; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _ref2() { return this; } function wrapStore(store) { var _extends2; function getState() { var _store$getState = store.getState(); var state = _store$getState.state; return state; } function dispatch(action) { if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1__util_isObservable__["a" /* default */])(action)) { return __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_2__util_executeAgenda__["a" /* default */])(action, store); } return store.dispatch(action); } function replaceReducer(reducer) { store.replaceReducer(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_3__util_wrapStateReducer__["a" /* default */])(reducer)); } function _ref(observer) { if (typeof observer !== 'object') { throw new TypeError('Expected the observer to be an object.'); } function observeState() { observer.next && observer.next(getState()); } observeState(); var unsubscribe = store.subscribe(observeState); return { unsubscribe: unsubscribe }; } function observable() { var _ref3; return _ref3 = { subscribe: _ref }, _ref3[__WEBPACK_IMPORTED_MODULE_0_symbol_observable___default.a] = _ref2, _ref3; } return _extends({}, store, (_extends2 = { preAgendaStore: store, dispatch: dispatch, getState: getState, replaceReducer: replaceReducer }, _extends2[__WEBPACK_IMPORTED_MODULE_0_symbol_observable___default.a] = observable, _extends2)); } function _ref4(createStore) { return function (reducer, preloadedState, enhancer) { // Pass wrapped reducer to `createStore` instead var wrappedReducer = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_3__util_wrapStateReducer__["a" /* default */])(reducer, preloadedState); var store = createStore(wrappedReducer, undefined, enhancer); if (store.preAgendaStore) { // Check whether this method was applied twice throw new Error('createAgendaEnhancer: The Redux Fluorine enhancer should only be applied once.'); } return wrapStore(store); }; } function createAgendaEnhancer() { return _ref4; } /***/ }, /* 3 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__actions_filterActions__ = __webpack_require__(0); /* harmony export (immutable) */ exports["a"] = executeAgenda; function _ref2() { // NOTE: We don't need to do anything here } function executeAgenda(agenda, store) { var dispatch = store.dispatch; var getState = store.getState; var anchor = getState(); var actions = []; // Subscribe to the agenda and return the subscription function _ref() { dispatch(__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__actions_filterActions__["a" /* default */])(anchor, actions)); } var sub = agenda.subscribe({ next: function next(action) { // Dispatch action normally and push it to our bucket dispatch(action); actions.push(action); }, error: function error(err) { console.error && console.error(err); // Instruct the reducer wrapper to recompute the state // without any actions inside the `actions` array if (actions.length) { setTimeout(_ref); } }, complete: _ref2 }); return { unsubscribe: sub.unsubscribe.bind(sub) }; } /***/ }, /* 4 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_symbol_observable__ = __webpack_require__(1); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_symbol_observable___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_symbol_observable__); /* harmony export (immutable) */ exports["a"] = isObservable; function isObservable(obj) { return obj && typeof obj.subscribe === 'function' && typeof obj[__WEBPACK_IMPORTED_MODULE_0_symbol_observable___default.a] === 'function'; } /***/ }, /* 5 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony export (immutable) */ exports["a"] = reapplyActionsWithout; function reapplyActionsWithout(reducer, base, subtract) { var diverged = false; var anchor = base.lastValidState || base; var lastValid = anchor; while (anchor.next) { // Iterate to next state anchor = anchor.next; var _anchor = anchor; var action = _anchor.action; if (subtract.indexOf(action) > -1) { // Prevent recomputation of any states until it's actually // necessary diverged = true; // Link last valid state for any agenda that might have stored // this state as its anchor anchor.lastValidState = lastValid; } else { if (diverged) { // Recompute state anchor.state = reducer(lastValid.state, action); } // Link the last valid state to this equally valid one and // replace the lastValid reference with this state lastValid.next = anchor; lastValid = anchor; } } // Make sure that the lastValid state is not pointing anywhere and // return it lastValid.next = null; return lastValid; } /***/ }, /* 6 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__actions_filterActions__ = __webpack_require__(0); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__reapplyActionsWithout__ = __webpack_require__(5); /* harmony export (immutable) */ exports["a"] = wrapStateReducer; function wrapStateReducer(reducer, initialState) { if (typeof reducer !== 'function') { throw new Error('wrapStateReducer: Expected `reducer` to be a function.'); } var _defaultState = { action: null, state: initialState, next: null }; return function () { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _defaultState; var action = arguments[1]; if (action.type === __WEBPACK_IMPORTED_MODULE_0__actions_filterActions__["b" /* RECOVER_AFTER_ERROR */]) { // Reapply all past actions subtracting the errored ones var anchor = action.anchor; var actions = action.actions; return __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_1__reapplyActionsWithout__["a" /* default */])(reducer, anchor, actions); } var lastState = state.state; var nextState = reducer(lastState, action); // Create next wrapped state and link it state.next = { action: action, state: nextState, next: null }; return state.next; }; } /***/ }, /* 7 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* WEBPACK VAR INJECTION */(function(global) {'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _ponyfill = __webpack_require__(8); var _ponyfill2 = _interopRequireDefault(_ponyfill); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var root = undefined; /* global window */ if (typeof global !== 'undefined') { root = global; } else if (typeof window !== 'undefined') { root = window; } var result = (0, _ponyfill2['default'])(root); exports['default'] = result; /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(9))) /***/ }, /* 8 */ /***/ function(module, exports) { "use strict"; 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = symbolObservablePonyfill; function symbolObservablePonyfill(root) { var result; var _Symbol = root.Symbol; if (typeof _Symbol === 'function') { if (_Symbol.observable) { result = _Symbol.observable; } else { result = _Symbol('observable'); _Symbol.observable = result; } } else { result = '@@observable'; } return result; }; /***/ }, /* 9 */ /***/ function(module, exports) { var g; // This works in non-strict mode g = (function() { return this; })(); try { // This works if eval is allowed (see CSP) g = g || Function("return this")() || (1,eval)("this"); } catch(e) { // This works if the window reference is available if(typeof window === "object") g = window; } // g can still be undefined, but nothing to do about it... // We return undefined, instead of nothing here, so it's // easier to handle this case. if(!global) { ...} module.exports = g; /***/ }, /* 10 */ /***/ function(module, exports, __webpack_require__) { "use strict"; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__createAgendaEnhancer__ = __webpack_require__(2); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__actions_filterActions__ = __webpack_require__(0); /* harmony reexport (binding) */ __webpack_require__.d(exports, "createAgendaEnhancer", function() { return __WEBPACK_IMPORTED_MODULE_0__createAgendaEnhancer__["a"]; }); /* harmony reexport (binding) */ __webpack_require__.d(exports, "RECOVER_AFTER_ERROR", function() { return __WEBPACK_IMPORTED_MODULE_1__actions_filterActions__["b"]; }); /***/ } /******/ ]) }); ;