UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

42 lines 3.97 kB
import{Observable}from'rxjs/Observable';import'rxjs/add/observable/interval';import'rxjs/add/operator/takeUntil';import{REQUEST_LOGIN,SUCCESS_LOGIN,ERROR_LOGIN,ERROR_LEGACY_CONNECT_REGISTER,REQUEST_LOGOUT,SUCCESS_LOGOUT,RECEIVE_USER,ERROR_USER}from"../constants/ActionTypes";import{appDidStart$}from"./app";import{main$}from"./main";import{SESSION_EXPIRY_CHECK_INTERVAL}from"../constants/user";import{getSessionExpiry,getIsSessionExpired}from"../selectors/user";/** * Gets triggered when user is requesting to login. * @type {Observable} */export var userWillLogin$=main$.filter(function(_ref){var action=_ref.action;return action.type===REQUEST_LOGIN;});/** * Gets triggered when user login request has gained a response. * @type {Observable} */export var userLoginResponse$=main$.filter(function(_ref2){var action=_ref2.action;return action.type===SUCCESS_LOGIN||action.type===ERROR_LOGIN;});/** * Gets triggered when user has logged in. * @type {Observable} */export var userDidLogin$=main$.filter(function(_ref3){var action=_ref3.action;return action.type===SUCCESS_LOGIN;});/** * Gets triggered when user is requesting to logout. * @type {Observable} */export var userWillLogout$=main$.filter(function(_ref4){var action=_ref4.action;return action.type===REQUEST_LOGOUT;});/** * Gets triggered when user has logged out. * @type {Observable} */export var userDidLogout$=main$.filter(function(_ref5){var action=_ref5.action;return action.type===SUCCESS_LOGOUT;});/** * Gets triggered when the user data was updated. * @type {Observable} */export var userDidUpdate$=main$.filter(function(_ref6){var action=_ref6.action;return action.type===RECEIVE_USER||action.type===SUCCESS_LOGOUT||action.type===ERROR_USER;});/** * Gets triggered when we have a stable login state of the user. Since user data is persisted, * some parts of the code might not rely on the redux states. * @type {Observable} */export var userDidInitialize$=appDidStart$.switchMap(function(){return main$.filter(function(_ref7){var action=_ref7.action;return action.type===RECEIVE_USER||action.type===ERROR_USER;});}).first();/** * Gets triggered when we received the user data. * @type {Observable} */export var userDataReceived$=userDidUpdate$.filter(function(_ref8){var action=_ref8.action;return action.type===RECEIVE_USER;});/** * Gets triggered when the login failed. * @type {Observable} */export var loginDidFail$=main$.filter(function(_ref9){var action=_ref9.action;return action.type===ERROR_LOGIN;});/** * Gets triggered when the legacy sgconnect registration failed * @type {Observable} */export var legacyConnectRegisterDidFail$=main$.filter(function(_ref10){var action=_ref10.action;return action.type===ERROR_LEGACY_CONNECT_REGISTER;});/** * Emits when user session expiry needs to be checked. That's the case when the login state in * Redux contains a numeric value for the "expires" property. * Checks are performed at appStart since there might be a persisted value in Redux from the last * sessions, or it happens after login when the login response contained a session lease. */export var sessionExpiryNeedsToBeChecked$=appDidStart$.merge(userDidLogin$).filter(function(_ref11){var getState=_ref11.getState;var expiry=getSessionExpiry(getState());return expiry!==null;});/** * Emits when a user session with an expiry expires. Therefore it performs an interval check to * check if the session is still active. */export var userSessionExpired$=sessionExpiryNeedsToBeChecked$.switchMap(function(data){// At first check if the session already expired - necessary for appDidStart$ if(getIsSessionExpired(data.getState())){return Observable.of(data);}// Setup an interval that periodically checks if the session expired - runs till user logged out return Observable.interval(SESSION_EXPIRY_CHECK_INTERVAL).takeUntil(userDidLogout$).filter(function(){return getIsSessionExpired(data.getState());}).switchMap(function(){return Observable.of(data);});});