import uuid from 'uuid/v4' ; import { SOURCE_DATA_REQUEST_ID } from '../../../../../common/constants' ; import { Timeslice , VectorSourceRequestMeta } from '../../../../../common/descriptor_types' ; import { DataRequest } from '../../../util/data_request' ; import { DataRequestContext } from '../../../../actions' ; import { canSkipSourceUpdate } from '../../../util/can_skip_fetch' ; import { IMvtVectorSource } from '../../../sources/vector_source' ; export interface MvtSourceData { tileSourceLayer : string ; tileMinZoom : number ; tileMaxZoom : number ; tileUrl : string ; refreshToken : string ; } export async function syncMvtSourceData ( { layerId , prevDataRequest , requestMeta , source , syncContext , } ) { const requestToken = Symbol ( `${ layerId }-${ SOURCE_DATA_REQUEST_ID }` ) ; const prevData = prevDataRequest ? ( prevDataRequest . getData ( ) as MvtSourceData ) : undefined ; if ( prevData ) { const noChangesInSourceState = prevData . tileSourceLayer === source . getTileSourceLayer ( ) && prevData . tileMinZoom === source . getMinZoom ( ) && prevData . tileMaxZoom === source . getMaxZoom ( ) ; const noChangesInSearchState = await canSkipSourceUpdate ( { extentAware : false , source , prevDataRequest , nextRequestMeta : requestMeta , getUpdateDueToTimeslice : ( timeslice ? ) => { return true ; } , } ) ; const canSkip = noChangesInSourceState && noChangesInSearchState ; if ( canSkip ) { return ; } } syncContext . startLoading ( SOURCE_DATA_REQUEST_ID , requestToken , requestMeta ) ; try { const refreshToken = ! prevData || ( requestMeta . isForceRefresh && requestMeta . applyForceRefresh ) ? uuid ( ) : prevData . refreshToken ; const tileUrl = await source . getTileUrl ( requestMeta , refreshToken ) ; const sourceData = { tileUrl , tileSourceLayer : source . getTileSourceLayer ( ) , tileMinZoom : source . getMinZoom ( ) , tileMaxZoom : source . getMaxZoom ( ) , refreshToken , } ; syncContext . stopLoading ( SOURCE_DATA_REQUEST_ID , requestToken , sourceData , { } ) ; } catch ( error ) { syncContext . onLoadError ( SOURCE_DATA_REQUEST_ID , requestToken , error . message ) ; } }