import { FeatureCollection } from 'geojson' ; import { EMPTY_FEATURE_COLLECTION , SOURCE_DATA_REQUEST_ID , VECTOR_SHAPE_TYPE , } from '../../../../../common/constants' ; import { DataRequestMeta , Timeslice , VectorSourceRequestMeta , } from '../../../../../common/descriptor_types' ; import { DataRequestContext } from '../../../../actions' ; import { IVectorSource } from '../../../sources/vector_source' ; import { DataRequestAbortError } from '../../../util/data_request' ; import { DataRequest } from '../../../util/data_request' ; import { getCentroidFeatures } from './get_centroid_features' ; import { canSkipSourceUpdate } from '../../../util/can_skip_fetch' ; import { assignFeatureIds } from './assign_feature_ids' ; export async function syncGeojsonSourceData ( { layerId , layerName , prevDataRequest , requestMeta , syncContext , source , getUpdateDueToTimeslice , } ) { const { startLoading , stopLoading , onLoadError , registerCancelCallback , isRequestStillActive } = syncContext ; const dataRequestId = SOURCE_DATA_REQUEST_ID ; const requestToken = Symbol ( `${ layerId }-${ dataRequestId }` ) ; const canSkipFetch = syncContext . forceRefreshDueToDrawing ? false : await canSkipSourceUpdate ( { source , prevDataRequest , nextRequestMeta : requestMeta , extentAware : source . isFilterByMapBounds ( ) , getUpdateDueToTimeslice , } ) ; if ( canSkipFetch ) { return { refreshed : false , featureCollection : prevDataRequest ? ( prevDataRequest . getData ( ) as FeatureCollection ) : EMPTY_FEATURE_COLLECTION , } ; } try { startLoading ( dataRequestId , requestToken , requestMeta ) ; const { data : sourceFeatureCollection , meta } = await source . getGeoJsonWithMeta ( layerName , requestMeta , registerCancelCallback . bind ( null , requestToken ) , ( ) => { return isRequestStillActive ( dataRequestId , requestToken ) ; } ) ; const layerFeatureCollection = assignFeatureIds ( sourceFeatureCollection ) ; const supportedShapes = await source . getSupportedShapeTypes ( ) ; if ( supportedShapes . includes ( VECTOR_SHAPE_TYPE . LINE ) || supportedShapes . includes ( VECTOR_SHAPE_TYPE . POLYGON ) ) { layerFeatureCollection . features . push ( ... getCentroidFeatures ( layerFeatureCollection ) ) ; } const responseMeta = meta ? { ... meta } : { } ; if ( requestMeta . applyGlobalTime && ( await source . isTimeAware ( ) ) ) { const timesliceMaskField = await source . getTimesliceMaskFieldName ( ) ; if ( timesliceMaskField ) { responseMeta . timesliceMaskField = timesliceMaskField ; } } stopLoading ( dataRequestId , requestToken , layerFeatureCollection , responseMeta ) ; return { refreshed : true , featureCollection : layerFeatureCollection , } ; } catch ( error ) { if ( ! ( error instanceof DataRequestAbortError ) ) { onLoadError ( dataRequestId , requestToken , error . message ) ; } throw error ; } }