UNPKG

standalone-store

Version:

Middleware for redux store in standalone mode for nodejs environment

77 lines (63 loc) 2.07 kB
import { isActionOf } from 'typesafe-actions'; var dispatchActionsAndWaitResponse = function dispatchActionsAndWaitResponse(_ref) { var actionsDispatch = _ref.actionsDispatch, actionCreatorsResolve = _ref.actionCreatorsResolve, configureStore = _ref.configureStore, selector = _ref.selector; var standaloneStore = new StandaloneStore({ configureStore: configureStore }); return !actionsDispatch.length ? Promise.reject('You should at least give one action.') : new Promise(function (resolve) { actionCreatorsResolve.forEach(function (actionResolve) { standaloneStore.subscribe(function (action, state) { if (isActionOf(actionResolve, action)) { resolve({ state: state }); } }); }); actionsDispatch.forEach(function (action) { standaloneStore.dispatchAction(action); }); }).then(function (_ref2) { var state = _ref2.state; return Promise.resolve(selector(state)); }); }; var StandaloneStore = function StandaloneStore(_ref) { var _this = this; var configureStore = _ref.configureStore; this.listeners = []; this.dispatchAction = function (action) { _this.store.dispatch(action); }; this.afterActionMiddleware = function (store) { return function (next) { return function (action) { var actionToDispatch = next(action); if (_this.listeners.length) { var state = store.getState(); _this.listeners.forEach(function (listener) { listener(action, state); }); } return actionToDispatch; }; }; }; this.getListeners = function () { return _this.listeners; }; this.subscribe = function (listener) { _this.listeners = _this.listeners.concat(listener); }; this.unsubscribe = function () { _this.listeners = []; }; this.store = configureStore({ middlewares: [this.afterActionMiddleware] }); }; export { StandaloneStore, dispatchActionsAndWaitResponse }; //# sourceMappingURL=standalone-store.esm.js.map