Skip to content

Commit

Permalink
Add FeaturesDroppedLoader to extract header value (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer authored Jan 12, 2023
1 parent 2ec86ef commit de7ded9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## 1.5

## 1.5.0-alpha.11 (2023-01-12)

- Remove custom fetch function. Fixes issues with icon loading and FillStyleExtensions

## 1.5.0-alpha.10 (2023-01-04)

- Allow to use custom version explicitly in GoogleMap (not 'beta' by default now) [#550](https://github.com/CartoDB/carto-react/pull/550)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('useCartoLayerProps', () => {
'binary',
'onViewportLoad',
'fetch',
'loaders',
'onDataLoad',
'id',
'visible',
Expand Down
13 changes: 13 additions & 0 deletions packages/react-api/src/hooks/FeaturesDroppedLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
name: 'FeaturesDroppedLoader',
module: 'carto',
category: 'geometry',
extensions: [],
// By only specifying `carto-vector-tile` the SpatialIndexLayer will not use this loader
mimeTypes: ['application/vnd.carto-vector-tile'],
parse: async (arrayBuffer, options, context) => {
const isDroppingFeatures = context.response.headers['features-dropped-from-tile'];
const result = await context.parse(arrayBuffer, options, context);
return result ? { ...result, isDroppingFeatures } : null;
}
};
2 changes: 2 additions & 0 deletions packages/react-api/src/hooks/useCartoLayerProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import useGeojsonFeatures from './useGeojsonFeatures';
import useTileFeatures from './useTileFeatures';
import { getDataFilterExtensionProps } from './dataFilterExtensionUtil';
import { getMaskExtensionProps } from './maskExtensionUtil';
import FeaturesDroppedLoader from './FeaturesDroppedLoader';

export default function useCartoLayerProps({
source,
Expand Down Expand Up @@ -51,6 +52,7 @@ export default function useCartoLayerProps({
...(viewportFeatures && {
onViewportLoad,
fetch,
loaders: [FeaturesDroppedLoader],
onDataLoad
})
};
Expand Down
19 changes: 1 addition & 18 deletions packages/react-api/src/hooks/useTileFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ export default function useTileFeatures({
const fetch = useCallback(
(...args) => {
stopAnyCompute();

if (spatialIndex) {
return Layer.defaultProps.fetch.value(...args);
}
return customFetch(...args);
return Layer.defaultProps.fetch.value(...args);
},
[stopAnyCompute, spatialIndex]
);
Expand All @@ -156,19 +152,6 @@ export default function useTileFeatures({
return [onDataLoad, onViewportLoad, fetch];
}

// WORKAROUND: To read headers and know if the tile is dropping features.
const customFetch = async (url, { layer, loaders, loadOptions, signal }) => {
loadOptions = loadOptions || layer.getLoadOptions();
loaders = loaders || layer.props.loaders;

const headers = loadOptions.fetch.headers;
const response = await fetch(url, { signal, headers });
const isDroppingFeatures =
response.headers.get('Features-Dropped-From-Tile') === 'true';
const result = await parse(response, loaders, loadOptions);
return result ? { ...result, isDroppingFeatures } : null;
};

const getColumnNameFromGeoColumn = (geoColumn) => {
const parts = geoColumn.split(':');
return parts.length === 1 ? parts[0] : parts.length === 2 ? parts[1] : null;
Expand Down
1 change: 1 addition & 0 deletions packages/react-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { getStats as _getStats } from './api/stats';
export { getTileJson as _getTileJson } from './api/tilejson';
export { executeModel as _executeModel } from './api/model';

export { default as FeaturesDroppedLoader } from './hooks/FeaturesDroppedLoader';
export { default as useCartoLayerProps } from './hooks/useCartoLayerProps';

export { getDataFilterExtensionProps } from './hooks/dataFilterExtensionUtil';

0 comments on commit de7ded9

Please sign in to comment.