@shopgate/engage
Version:
Shopgate's ENGAGE library.
8 lines • 2.11 kB
JavaScript
function _extends(){_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;};return _extends.apply(this,arguments);}function _toArray(arr){return _arrayWithHoles(arr)||_iterableToArray(arr)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter);}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import{Observable}from'rxjs/Observable';import{ACTION_RESET}from'@virtuous/conductor';import{main$}from'@shopgate/pwa-common/streams/main';import{routeDidEnter$}from'@shopgate/pwa-common/streams';import{routeDidEnter}from'@shopgate/pwa-common/action-creators';import{getRouterStack}from'@shopgate/engage/core/selectors';import{RELOAD_APP}from"../constants";import{historyReset}from"../actions";/**
* Gets triggered when the app is supposed to be reloaded
* @type {Observable}
*/export var reloadApp$=main$.filter(function(_ref){var action=_ref.action;return action.type===RELOAD_APP;}).switchMap(function(params){var dispatch=params.dispatch,getState=params.getState;var _getRouterStack=getRouterStack(getState()),_getRouterStack2=_toArray(_getRouterStack),initialRoute=_getRouterStack2[0],otherRoutes=_getRouterStack2.slice(1);if(!otherRoutes.length){// When the initial route is the only route inside the router stack, routeDidEnter$ will
// not always be triggered, so just simulate it here.
return Observable.of(_extends({},params,{action:routeDidEnter(initialRoute,ACTION_RESET)}));}// The PWA can't be reloaded on any route, so we first need to reset to the first history entry
dispatch(historyReset());return routeDidEnter$// Emit when initial route is reached
.filter(function(_ref2){var action=_ref2.action;return action.route.id===(initialRoute===null||initialRoute===void 0?void 0:initialRoute.id);});});