-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Camera state tracking allows the Map component to avoid a feedback-loop when camera values published by the map are fed back into the Map component props. I measured round-trip times (dispatching an event into the react-application and receiving them back as props) of around 4-5ms. However, there are cases where this roundtrip crosses the frame-boundary, and times more like 20ms can be observed. When this happens, stuttering, janky animations and suboptimal interactions will happen.
- Loading branch information
1 parent
f01d4ea
commit 7a162fa
Showing
5 changed files
with
111 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {MutableRefObject, useRef} from 'react'; | ||
import {MapCameraChangedEvent, MapEvent} from './use-map-events'; | ||
|
||
export type InternalCameraState = { | ||
center: google.maps.LatLngLiteral; | ||
heading: number; | ||
tilt: number; | ||
zoom: number; | ||
}; | ||
|
||
export type InternalCameraStateRef = MutableRefObject<InternalCameraState>; | ||
|
||
/** | ||
* Creates a mutable ref object to track the last known state of the map camera. | ||
* This is updated by `trackDispatchedEvent` and used in `useMapOptions`. | ||
*/ | ||
export function useInternalCameraState(): InternalCameraStateRef { | ||
return useRef<InternalCameraState>({ | ||
center: {lat: 0, lng: 0}, | ||
heading: 0, | ||
tilt: 0, | ||
zoom: 0 | ||
}); | ||
} | ||
|
||
/** | ||
* Records camera data from the last event dispatched to the React application | ||
* in a mutable `IternalCameraStateRef`. | ||
* This data can then be used to prevent feeding these values back to the | ||
* map-instance when a typical "controlled component" setup (state variable is | ||
* fed into and updated by the map). | ||
*/ | ||
export function trackDispatchedEvent( | ||
ev: MapEvent, | ||
cameraStateRef: InternalCameraStateRef | ||
) { | ||
const cameraEvent = ev as MapCameraChangedEvent; | ||
|
||
// we're only interested in the camera-events here | ||
if (!cameraEvent.detail.center) return; | ||
const {center, zoom, heading, tilt} = cameraEvent.detail; | ||
|
||
cameraStateRef.current.center = center; | ||
cameraStateRef.current.heading = heading; | ||
cameraStateRef.current.tilt = tilt; | ||
cameraStateRef.current.zoom = zoom; | ||
} |
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,8 @@ | ||
export function isLatLngLiteral( | ||
obj: unknown | ||
): obj is google.maps.LatLngLiteral { | ||
if (!obj || typeof obj !== 'object') return false; | ||
if (!('lat' in obj && 'lng' in obj)) return false; | ||
|
||
return Number.isFinite(obj.lat) && Number.isFinite(obj.lng); | ||
} |