import { cancellablePromise , CancelSymbol } from '../../utils/cancellable-promise' ; import { XActionContext } from '../actions.types' ; import { StatusMutations , StatusState } from './status-store.utils' ; public returns export function createFetchAndSaveActions < Context extends XActionContext < StatusState , object , StatusMutations , object > , Request , Response > ( { fetch , onSuccess , onError , onCancel } ) { let cancelPreviousRequest ; param context - The  https of the
     * actions, provided by Vuex.
     *  param response returns function handleResponse ( context , response ) { return Promise . resolve ( onSuccess ( context , response ) ) . then ( ( ) => { context . commit ( 'setStatus' , 'success' ) ; } ) ; } param context - The  https of the
     * actions, provided by Vuex.
      function handleCancel ( context ) { cancelPreviousRequest = undefined ; context . commit ( 'setStatus' , 'success' ) ; onCancel ?. ( ) ; } param context - The  https of the
     * actions, provided by Vuex.
     *  param error function handleError ( context , error ) { if ( error !== CancelSymbol ) { context . commit ( 'setStatus' , 'error' ) ; onError ?. ( error ) ; } } see FetchAndSaveActions . fetchAndSave function fetchAndSave ( context , request ) { cancelPrevious ( ) ; context . commit ( 'setStatus' , 'loading' ) ; const { promise , cancel } = cancellablePromise ( fetch ( context , request ) , ( ) => { handleCancel ( context ) ; } ) ; cancelPreviousRequest = cancel ; return promise . then ( response => handleResponse ( context , response ) ) . catch ( error => handleError ( context , error ) ) ; } see FetchAndSaveActions . cancelPrevious function cancelPrevious ( ) { cancelPreviousRequest ?. ( ) ; } return { fetchAndSave , cancelPrevious } ; } /**
 * Options to use with the  createFetchAndSaveActions factory.
 *
 *  public export interface FetchAndSaveHooks < Context extends XActionContext < StatusState , object , StatusMutations , object > , Request , Response > { param context - The  https of the
     * actions, provided by Vuex.
     *  param request returns fetch ( context , request ) : Promise < Response > ; /**
     * Asynchronous callback executed when the  FetchAndSaveHooks . fetch is
     * performed successfully.
     *
     *  param context - The  https of the
     * actions, provided by Vuex.
     *  param response - The data returned by  FetchAndSaveHooks . fetch .
      onSuccess ( context , response ) : void ; /**
     * Asynchronous callback executed when either the  FetchAndSaveHooks . fetch * or  FetchAndSaveHooks . onSuccess methods fail.
     *
     *  param error onError ? ( error ) : void ; /**
     * Synchronous callback executed when the request is cancelled. This can happen mainly for two
     * reasons:
     * - The  FetchAndSaveActions . cancelPrevious action is dispatched.
     * - A new  FetchAndSaveActions . fetchAndSave is dispatched before the previous one was
     * resolved.
      onCancel ? ( ) : void ; } /**
 * Actions returned from the  createFetchAndSaveActions .
 *
 *  public export interface FetchAndSaveActions < Context extends XActionContext < StatusState , object , StatusMutations , object > , Request > { param context - The  https of the
     * actions, provided by Vuex.
     *  returns fetchAndSave : ( context : Context , request : Request ) => void | Promise < void > ; /**
     * Action that cancels the previous request call if it stills in progress.
     */ cancelPrevious : ( ) => void ; }