@@ -12,6 +12,7 @@ import type {
1212 TransformRequestFunction ,
1313 Light ,
1414 Fog ,
15+ Point ,
1516 TerrainSpecification ,
1617 MapboxStyle ,
1718 ImmutableLike ,
@@ -789,6 +790,19 @@ export default class Mapbox {
789790 }
790791 } ;
791792
793+ private _queryRenderedFeatures ( point : Point ) {
794+ const map = this . _map ;
795+ const { interactiveLayerIds = [ ] } = this . props ;
796+ try {
797+ return map . queryRenderedFeatures ( point , {
798+ layers : interactiveLayerIds . filter ( map . getLayer . bind ( map ) )
799+ } ) ;
800+ } catch {
801+ // May fail if style is not loaded
802+ return [ ] ;
803+ }
804+ }
805+
792806 _updateHover ( e : MapMouseEvent ) {
793807 const { props} = this ;
794808 const shouldTrackHoveredFeatures =
@@ -797,18 +811,7 @@ export default class Mapbox {
797811 if ( shouldTrackHoveredFeatures ) {
798812 const eventType = e . type ;
799813 const wasHovering = this . _hoveredFeatures ?. length > 0 ;
800- let features ;
801- if ( eventType === 'mousemove' ) {
802- try {
803- features = this . _map . queryRenderedFeatures ( e . point , {
804- layers : props . interactiveLayerIds . filter ( this . _map . getLayer )
805- } ) ;
806- } catch {
807- features = [ ] ;
808- }
809- } else {
810- features = [ ] ;
811- }
814+ const features = this . _queryRenderedFeatures ( e . point ) ;
812815 const isHovering = features . length > 0 ;
813816
814817 if ( ! isHovering && wasHovering ) {
@@ -835,17 +838,7 @@ export default class Mapbox {
835838 const cb = this . props [ pointerEvents [ e . type ] ] ;
836839 if ( cb ) {
837840 if ( this . props . interactiveLayerIds && e . type !== 'mouseover' && e . type !== 'mouseout' ) {
838- let features ;
839- try {
840- features =
841- this . _hoveredFeatures ||
842- this . _map . queryRenderedFeatures ( e . point , {
843- layers : this . props . interactiveLayerIds . filter ( this . _map . getLayer )
844- } ) ;
845- } catch {
846- features = [ ] ;
847- }
848- e . features = features ;
841+ e . features = this . _hoveredFeatures || this . _queryRenderedFeatures ( e . point ) ;
849842 }
850843 cb ( e ) ;
851844 delete e . features ;
@@ -875,7 +868,7 @@ export default class Mapbox {
875868 }
876869 if ( eventType in cameraEvents ) {
877870 if ( typeof event === 'object' ) {
878- ( event as ViewStateChangeEvent ) . viewState = transformToViewState ( tr ) ;
871+ ( event as unknown as ViewStateChangeEvent ) . viewState = transformToViewState ( tr ) ;
879872 }
880873 if ( this . _map . isMoving ( ) ) {
881874 // Replace map.transform with ours during the callbacks
0 commit comments