-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergio Clebal
authored
Dec 13, 2021
1 parent
ffdf939
commit ed5ce91
Showing
11 changed files
with
289 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useDispatch } from 'react-redux'; | ||
import { setViewportFeaturesReady } from '@carto/react-redux'; | ||
import { useState, useRef, useEffect, useCallback } from 'react'; | ||
|
||
export default function useFeaturesCommons({ source }) { | ||
const dispatch = useDispatch(); | ||
|
||
const [isDataLoaded, setDataLoaded] = useState(false); | ||
const debounceIdRef = useRef(null); | ||
|
||
useEffect(() => { | ||
if (!source) { | ||
setDataLoaded(false); | ||
} | ||
}, [source]); | ||
|
||
const clearDebounce = useCallback(() => { | ||
if (debounceIdRef.current) { | ||
clearTimeout(debounceIdRef.current); | ||
} | ||
debounceIdRef.current = null; | ||
}, []); | ||
|
||
const stopAnyCompute = useCallback(() => { | ||
clearDebounce(); | ||
setDataLoaded(false); | ||
}, [clearDebounce, setDataLoaded]); | ||
|
||
const sourceId = source?.id; | ||
|
||
const setSourceViewportFeaturesReady = useCallback( | ||
(ready) => { | ||
if (sourceId) { | ||
dispatch(setViewportFeaturesReady({ sourceId, ready })); | ||
} | ||
}, | ||
[dispatch, sourceId] | ||
); | ||
|
||
return [ | ||
debounceIdRef, | ||
isDataLoaded, | ||
setDataLoaded, | ||
clearDebounce, | ||
stopAnyCompute, | ||
setSourceViewportFeaturesReady | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useEffect, useCallback } from 'react'; | ||
import { debounce } from '@carto/react-core'; | ||
import { Methods, executeTask } from '@carto/react-workers'; | ||
import { throwError } from './utils'; | ||
import useFeaturesCommons from './useFeaturesCommons'; | ||
|
||
export default function useGeoJsonFeatures({ | ||
source, | ||
viewport, | ||
uniqueIdProperty = 'cartodb_id', | ||
debounceTimeout = 250 | ||
}) { | ||
const [ | ||
debounceIdRef, | ||
isGeoJsonLoaded, | ||
setGeoJsonLoaded, | ||
clearDebounce, | ||
stopAnyCompute, | ||
setSourceViewportFeaturesReady | ||
] = useFeaturesCommons({ source }); | ||
|
||
const sourceId = source?.id; | ||
|
||
const computeFeaturesGeoJson = useCallback( | ||
({ viewport, uniqueIdProperty }) => { | ||
executeTask(sourceId, Methods.VIEWPORT_FEATURES_GEOJSON, { | ||
viewport, | ||
uniqueIdProperty | ||
}) | ||
.then(() => { | ||
setSourceViewportFeaturesReady(true); | ||
}) | ||
.catch(throwError); | ||
}, | ||
[setSourceViewportFeaturesReady, sourceId] | ||
); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const debouncedComputeFeaturesGeoJson = useCallback( | ||
debounce(computeFeaturesGeoJson, debounceTimeout), | ||
[computeFeaturesGeoJson] | ||
); | ||
|
||
useEffect(() => { | ||
if (sourceId && isGeoJsonLoaded) { | ||
clearDebounce(); | ||
setSourceViewportFeaturesReady(false); | ||
debounceIdRef.current = debouncedComputeFeaturesGeoJson({ | ||
viewport, | ||
uniqueIdProperty | ||
}); | ||
} | ||
}, [ | ||
viewport, | ||
uniqueIdProperty, | ||
sourceId, | ||
isGeoJsonLoaded, | ||
debouncedComputeFeaturesGeoJson, | ||
setSourceViewportFeaturesReady, | ||
clearDebounce, | ||
debounceIdRef | ||
]); | ||
|
||
const onDataLoad = useCallback( | ||
(geojson) => { | ||
stopAnyCompute(); | ||
setSourceViewportFeaturesReady(false); | ||
executeTask(sourceId, Methods.LOAD_GEOJSON_FEATURES, { geojson }) | ||
.then(() => setGeoJsonLoaded(true)) | ||
.catch(throwError); | ||
}, | ||
[sourceId, setSourceViewportFeaturesReady, stopAnyCompute, setGeoJsonLoaded] | ||
); | ||
|
||
return [onDataLoad]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useEffect, useCallback } from 'react'; | ||
import { debounce } from '@carto/react-core'; | ||
import { Methods, executeTask } from '@carto/react-workers'; | ||
import { Layer } from '@deck.gl/core'; | ||
import { throwError } from './utils'; | ||
import useFeaturesCommons from './useFeaturesCommons'; | ||
|
||
export default function useTilesetFeatures({ | ||
source, | ||
viewport, | ||
uniqueIdProperty, | ||
debounceTimeout = 250 | ||
}) { | ||
const [ | ||
debounceIdRef, | ||
isTilesetLoaded, | ||
setTilesetLoaded, | ||
clearDebounce, | ||
stopAnyCompute, | ||
setSourceViewportFeaturesReady | ||
] = useFeaturesCommons({ source }); | ||
|
||
const sourceId = source?.id; | ||
|
||
const computeViewportFeatures = useCallback( | ||
({ viewport, uniqueIdProperty }) => { | ||
setSourceViewportFeaturesReady(false); | ||
|
||
executeTask(sourceId, Methods.VIEWPORT_FEATURES, { | ||
viewport, | ||
uniqueIdProperty | ||
}) | ||
.then(() => { | ||
setSourceViewportFeaturesReady(true); | ||
}) | ||
.catch(throwError) | ||
.finally(clearDebounce); | ||
}, | ||
[setSourceViewportFeaturesReady, sourceId, clearDebounce] | ||
); | ||
|
||
const loadTiles = useCallback( | ||
(tiles) => { | ||
const cleanedTiles = tiles.reduce((acc, { data, isVisible, bbox }) => { | ||
if (isVisible && data) { | ||
acc.push({ | ||
data, | ||
bbox | ||
}); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
executeTask(sourceId, Methods.LOAD_TILES, { tiles: cleanedTiles }) | ||
.then(() => setTilesetLoaded(true)) | ||
.catch(throwError); | ||
}, | ||
[sourceId, setTilesetLoaded] | ||
); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const debouncedComputeViewportFeatures = useCallback( | ||
debounce(computeViewportFeatures, debounceTimeout), | ||
[computeViewportFeatures] | ||
); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const debouncedLoadTiles = useCallback(debounce(loadTiles, debounceTimeout), [ | ||
loadTiles | ||
]); | ||
|
||
useEffect(() => { | ||
if (sourceId && isTilesetLoaded) { | ||
clearDebounce(); | ||
setSourceViewportFeaturesReady(false); | ||
debounceIdRef.current = debouncedComputeViewportFeatures({ | ||
viewport, | ||
uniqueIdProperty | ||
}); | ||
} | ||
}, [ | ||
viewport, | ||
uniqueIdProperty, | ||
debouncedComputeViewportFeatures, | ||
sourceId, | ||
isTilesetLoaded, | ||
setSourceViewportFeaturesReady, | ||
clearDebounce, | ||
debounceIdRef | ||
]); | ||
|
||
const onViewportLoad = useCallback( | ||
(tiles) => { | ||
stopAnyCompute(); | ||
setSourceViewportFeaturesReady(false); | ||
|
||
debounceIdRef.current = debouncedLoadTiles(tiles); | ||
}, | ||
[stopAnyCompute, setSourceViewportFeaturesReady, debouncedLoadTiles, debounceIdRef] | ||
); | ||
|
||
const fetch = useCallback( | ||
(...args) => { | ||
stopAnyCompute(); | ||
return Layer.defaultProps.fetch.value(...args); | ||
}, | ||
[stopAnyCompute] | ||
); | ||
|
||
return [onViewportLoad, fetch]; | ||
} |
Oops, something went wrong.