UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

27 lines 6.18 kB
import{event,emitter as errorEmitter,registerEvents,closeInAppBrowser,onload}from'@shopgate/pwa-core';import{EVENT_KEYBOARD_WILL_CHANGE,APP_EVENT_VIEW_WILL_APPEAR,APP_EVENT_VIEW_DID_APPEAR,APP_EVENT_VIEW_WILL_DISAPPEAR,APP_EVENT_VIEW_DID_DISAPPEAR}from'@shopgate/pwa-core/constants/AppEvents';import{SOURCE_APP,SOURCE_PIPELINE}from'@shopgate/pwa-core/constants/ErrorManager';import pipelineManager from'@shopgate/pwa-core/classes/PipelineManager';import*as errorCodes from'@shopgate/pwa-core/constants/Pipeline';import{onWillPush,onDidPush,onWillPop,onDidPop,onWillReplace,onDidReplace,onWillReset,onDidReset,onUpdate}from'@virtuous/conductor';import{appConfig}from'@shopgate/engage';import{UI_VISIBILITY_CHANGE}from"../constants/ui";import{appError,pipelineError,pwaDidAppear,pwaDidDisappear}from"../action-creators";import{historyPush,routeWillPush,routeDidPush,routeWillPop,routeDidPop,routeWillReplace,routeDidReplace,routeWillReset,routeDidReset,routeDidUpdate}from"../actions/router";import{receiveClientConnectivity}from"../action-creators/client";import{appWillStart$,appDidStart$,clientInformationDidUpdate$,navigate$}from"../streams";import registerLinkEvents from"../actions/app/registerLinkEvents";import{APP_PLATFORM}from"../constants/Configuration";import{getPlatform,isAndroid}from"../selectors/client";import{prepareLegacyNavigation,showPreviousTab,pageContext}from"../helpers/legacy";import{embeddedMedia,configuration}from"../collections";import{Vimeo,YouTube}from"../collections/media-providers";import clearUpInAppBrowser from"./helpers/clearUpInAppBrowser";/** * App subscriptions. * @param {Function} subscribe The subscribe function. */export default function app(subscribe){// Gets triggered before the app starts. subscribe(appWillStart$,function(_ref){var _appConfig$responsify,_appConfig$responsify2;var dispatch=_ref.dispatch,action=_ref.action,getState=_ref.getState,events=_ref.events;embeddedMedia.addProvider(new Vimeo({responsify:appConfig===null||appConfig===void 0?void 0:(_appConfig$responsify=appConfig.responsifyVideos)===null||_appConfig$responsify===void 0?void 0:_appConfig$responsify.vimeo}));embeddedMedia.addProvider(new YouTube({responsify:appConfig===null||appConfig===void 0?void 0:(_appConfig$responsify2=appConfig.responsifyVideos)===null||_appConfig$responsify2===void 0?void 0:_appConfig$responsify2.youTube}));dispatch(registerLinkEvents(action.location));onWillPush(function(_ref2){var prev=_ref2.prev,next=_ref2.next;return dispatch(routeWillPush(prev,next));});onDidPush(function(_ref3){var prev=_ref3.prev,next=_ref3.next;return dispatch(routeDidPush(prev,next));});onWillPop(function(_ref4){var prev=_ref4.prev,next=_ref4.next;return dispatch(routeWillPop(prev,next));});onDidPop(function(_ref5){var prev=_ref5.prev,next=_ref5.next;return dispatch(routeDidPop(prev,next));});onWillReplace(function(_ref6){var prev=_ref6.prev,next=_ref6.next;return dispatch(routeWillReplace(prev,next));});onDidReplace(function(_ref7){var prev=_ref7.prev,next=_ref7.next;return dispatch(routeDidReplace(prev,next));});onWillReset(function(_ref8){var prev=_ref8.prev,next=_ref8.next;return dispatch(routeWillReset(prev,next));});onDidReset(function(_ref9){var prev=_ref9.prev,next=_ref9.next;return dispatch(routeDidReset(prev,next));});onUpdate(function(updated){return dispatch(routeDidUpdate(updated));});// Suppress errors globally pipelineManager.addSuppressedErrors([errorCodes.EACCESS,errorCodes.ENOTFOUND,errorCodes.EVALIDATION]);// Map the error events into the Observable streams. errorEmitter.addListener(SOURCE_APP,function(error){return dispatch(appError(error));});errorEmitter.addListener(SOURCE_PIPELINE,function(error){return dispatch(pipelineError(error));});/** @returns {*} */var viewVisibility=function viewVisibility(){return events.emit(UI_VISIBILITY_CHANGE);};event.addCallback('routeDidChange',viewVisibility);event.addCallback(APP_EVENT_VIEW_DID_DISAPPEAR,function(){dispatch(pwaDidDisappear());viewVisibility();});event.addCallback(APP_EVENT_VIEW_DID_APPEAR,function(){dispatch(pwaDidAppear());clearUpInAppBrowser(isAndroid(getState()));});});/** * Gets triggered when the app starts. */subscribe(appDidStart$,function(_ref10){var dispatch=_ref10.dispatch,getState=_ref10.getState;// Register for custom events registerEvents([EVENT_KEYBOARD_WILL_CHANGE,'showPreviousTab','closeInAppBrowser',// TODO The iOS apps don't emit the event to the webviews without registration till Lib 15.2. // This needs to be removed, when IOS-1886 is done and the the iOS apps are updated. 'httpResponse','connectivityDidChange']);// Add event callbacks event.addCallback('pageContext',pageContext);// Handle native/legacy navigation bar event.addCallback(APP_EVENT_VIEW_WILL_APPEAR,prepareLegacyNavigation);event.addCallback('showPreviousTab',showPreviousTab);/** * This event is triggered form the desktop shop in the inAppBrowser. * We have to close the inAppBrowser and redirect the user to the given url. */event.addCallback('closeInAppBrowser',function(){var data=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};if(data.redirectTo){dispatch(historyPush({pathname:data.redirectTo}));}closeInAppBrowser(isAndroid(getState()));});event.addCallback('connectivityDidChange',function(data){dispatch(receiveClientConnectivity(data));});/** * The following events are sometimes sent by the app, but don't need to be handled right now. * To avoid console warnings from the event system, empty handlers are registered here. */event.addCallback(APP_EVENT_VIEW_WILL_DISAPPEAR,function(){// Stop all playing video embeddedMedia.stop();});event.addCallback('pageInsetsChanged',function(){});/* * Onload must be send AFTER app did start. * Interjections events (like openPushMessage) would not work if this command is sent * before registering to interjections. */onload();});// Add platform to runtime config subscribe(clientInformationDidUpdate$,function(_ref11){var getState=_ref11.getState;var platform=getPlatform(getState());configuration.set(APP_PLATFORM,platform);});// Stop all playing video on navigation subscribe(navigate$,function(){embeddedMedia.stop();});}