UNPKG

a1-redux

Version:

Angular >=1.5 and redux

92 lines (70 loc) 2.46 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _redux = require('redux'); var _directiveProptypes = require('./directive-proptypes'); var _directiveProptypes2 = _interopRequireDefault(_directiveProptypes); var _reduxThunk = require('redux-thunk'); var _reduxThunk2 = _interopRequireDefault(_reduxThunk); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var ActionTypes = { INIT: '@@redux/INIT' }; /** * * @param app {{angular}} */ function angularSetup(app) { /** * understanding angular provider: https://www.evernote.com/shard/s16/sh/93652e36-4492-438d-94e3-86714b8ed49e/d885f65c9a74675c3d22cb4ff1d9df56 */ app.provider('ngStore', function () { // because we need the context `this` var state = {}; var currentUpdater = function currentUpdater(currentState) { return currentState; }; var dispatchLayers = [_reduxThunk2.default]; this.setInitialState = function (initialState) { state = initialState; }; this.setReducers = this.putUpdaters = function (updater) { currentUpdater = updater; }; this.putLayers = function (layers) { dispatchLayers = layers; }; this.$get = ['$timeout', function ($timeout) { function digestAngularUI() { return function (nextLayer) { return function (action) { var result = void 0; try { result = nextLayer(action); } catch (e) { console.error('DISPATCH ERROR >>> ', e); } // update the UI on angular, make sure changes in state will be in the $digest circle $timeout(function () { return result; }); return result; }; }; } dispatchLayers.unshift(digestAngularUI); var finalStore = (0, _redux.createStore)(currentUpdater, state, _redux.applyMiddleware.apply(undefined, _toConsumableArray(dispatchLayers))); return finalStore; }]; }); app.run(['ngStore', function (store) { store.dispatch({ type: ActionTypes.INIT }); }]); (0, _directiveProptypes2.default)(app); return app; } exports.default = angularSetup;