From 4976ad0779a1b70d73b7ee92cd2750864d900f7d Mon Sep 17 00:00:00 2001 From: airslice Date: Tue, 8 Aug 2023 11:55:51 +0800 Subject: [PATCH] refactor: add event listener when tileset ready --- .../lib/core/engines/Cesium/Feature/Tileset/hooks.ts | 9 ++++++++- .../classic/core/engines/Cesium/Feature/Tileset/hooks.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/web/src/beta/lib/core/engines/Cesium/Feature/Tileset/hooks.ts b/web/src/beta/lib/core/engines/Cesium/Feature/Tileset/hooks.ts index 05d94ec358..e0a5fd4f95 100644 --- a/web/src/beta/lib/core/engines/Cesium/Feature/Tileset/hooks.ts +++ b/web/src/beta/lib/core/engines/Cesium/Feature/Tileset/hooks.ts @@ -141,11 +141,13 @@ const convertStyle = (val: any, convert: StyleProperty["convert"]) => { const useFeature = ({ id, tileset, + tilesetReady, layer, evalFeature, }: { id?: string; tileset: MutableRefObject; + tilesetReady: boolean; layer?: ComputedLayer; evalFeature: EvalFeature; }) => { @@ -227,7 +229,7 @@ const useFeature = ({ await attachComputedFeature(feature); }); }); - }, [tileset, cachedFeaturesRef, attachComputedFeature, layerId]); + }, [tileset, tilesetReady, cachedFeaturesRef, attachComputedFeature, layerId]); useEffect(() => { cachedCalculatedLayerRef.current = layer; @@ -378,6 +380,7 @@ export const useHooks = ({ }), ); const tilesetRef = useRef(); + const [tilesetReady, setTilesReady] = useState(false); const ref = useCallback( (tileset: CesiumComponentRef | null) => { @@ -388,6 +391,9 @@ export const useHooks = ({ (tileset?.cesiumElement as any)[layerIdField] = layer.id; } tilesetRef.current = tileset?.cesiumElement; + if (tilesetRef.current) { + setTilesReady(true); + } }, [id, layer?.id, feature?.id], ); @@ -395,6 +401,7 @@ export const useHooks = ({ useFeature({ id, tileset: tilesetRef, + tilesetReady, layer, evalFeature, }); diff --git a/web/src/classic/core/engines/Cesium/Feature/Tileset/hooks.ts b/web/src/classic/core/engines/Cesium/Feature/Tileset/hooks.ts index ef9c125ca1..a96c2c6c7d 100644 --- a/web/src/classic/core/engines/Cesium/Feature/Tileset/hooks.ts +++ b/web/src/classic/core/engines/Cesium/Feature/Tileset/hooks.ts @@ -138,11 +138,13 @@ const convertStyle = (val: any, convert: StyleProperty["convert"]) => { const useFeature = ({ id, tileset, + tilesetReady, layer, evalFeature, }: { id?: string; tileset: MutableRefObject; + tilesetReady: boolean; layer?: ComputedLayer; evalFeature: EvalFeature; }) => { @@ -223,7 +225,7 @@ const useFeature = ({ await attachComputedFeature(feature); }); }); - }, [tileset, cachedFeaturesRef, attachComputedFeature, layerId]); + }, [tileset, tilesetReady, cachedFeaturesRef, attachComputedFeature, layerId]); useEffect(() => { cachedCalculatedLayerRef.current = layer; @@ -370,6 +372,7 @@ export const useHooks = ({ }), ); const tilesetRef = useRef(); + const [tilesetReady, setTilesReady] = useState(false); const ref = useCallback( (tileset: CesiumComponentRef | null) => { @@ -380,6 +383,9 @@ export const useHooks = ({ (tileset?.cesiumElement as any)[layerIdField] = layer.id; } tilesetRef.current = tileset?.cesiumElement; + if (tilesetRef.current) { + setTilesReady(true); + } }, [id, layer?.id, feature?.id], ); @@ -387,6 +393,7 @@ export const useHooks = ({ useFeature({ id, tileset: tilesetRef, + tilesetReady, layer, evalFeature, });