import { SOURCE_DATA_REQUEST_ID } from '../../../common/constants' ; import { findLayerById , setLayer } from './layer_utils' ; import { DataRequestMeta , DataRequestDescriptor } from '../../../common/descriptor_types' ; import { MapState } from './types' ; export function startDataRequest ( state , layerId , dataRequestId , requestToken , requestMeta ) { const layerDescriptor = findLayerById ( state , layerId ) ; if ( ! layerDescriptor ) { return state ; } const prevDataRequest = getDataRequest ( state , layerId , dataRequestId ) ; const dataRequest = prevDataRequest ? { ... prevDataRequest , } : { dataId : dataRequestId , } ; dataRequest . dataRequestMetaAtStart = requestMeta ; dataRequest . dataRequestToken = requestToken ; return setDataRequest ( state , layerId , dataRequest ) ; } export function updateSourceDataRequest ( state , layerId , newData ) { const dataRequest = getDataRequest ( state , layerId , SOURCE_DATA_REQUEST_ID ) ; return dataRequest ? setDataRequest ( state , layerId , { ... dataRequest , data : newData } ) : state ; } export function stopDataRequest ( state , layerId , dataRequestId , requestToken , responseMeta ? , data ? ) { const dataRequest = getDataRequest ( state , layerId , dataRequestId , requestToken ) ; return dataRequest ? setDataRequest ( state , layerId , { ... dataRequest , data , dataRequestMeta : { ... ( dataRequest . dataRequestMetaAtStart ? dataRequest . dataRequestMetaAtStart : { } ) , ... ( responseMeta ? responseMeta : { } ) , } , dataRequestMetaAtStart : undefined , dataRequestToken : undefined , } ) : state ; } export function setDataRequest ( state , layerId , dataRequest ) { const layerDescriptor = findLayerById ( state , layerId ) ; if ( ! layerDescriptor ) { return state ; } const updatedLayerDescriptor = { ... layerDescriptor , __dataRequests : layerDescriptor . __dataRequests ? [ ... layerDescriptor . __dataRequests ] : [ ] , } ; const dataRequestIndex = updatedLayerDescriptor . __dataRequests . findIndex ( ( { dataId } ) => dataRequest . dataId === dataId ) ; if ( dataRequestIndex === - 1 ) { updatedLayerDescriptor . __dataRequests . push ( dataRequest ) ; } else { updatedLayerDescriptor . __dataRequests [ dataRequestIndex ] = dataRequest ; } return { ... state , layerList : setLayer ( state . layerList , updatedLayerDescriptor ) , } ; } export function getDataRequest ( state , layerId , dataRequestId , requestToken ? ) { const layerDescriptor = findLayerById ( state , layerId ) ; if ( ! layerDescriptor || ! layerDescriptor . __dataRequests ) { return ; } return layerDescriptor . __dataRequests . find ( ( dataRequest ) => { return requestToken ? dataRequest . dataRequestToken === requestToken && dataRequest . dataId === dataRequestId : dataRequest . dataId === dataRequestId ; } ) ; }