Skip to content

🎥 Make videos programmatically with React

Fix map lifecycle locally and disable unsafe SDK telemetry

Igor Samokhovetsbde56bba3bcd
diff --git a/packages/core/src/SequenceActivityBoundary.tsx b/packages/core/src/SequenceActivityBoundary.tsx
index eba4f140c3..16b30faaee 100644
--- a/packages/core/src/SequenceActivityBoundary.tsx
+++ b/packages/core/src/SequenceActivityBoundary.tsx
@@ -1,8 +1,7 @@
-import React, {useContext, useMemo, useState} from 'react';
+import React, {useContext, useMemo} from 'react';
import {
SequenceActivityAbsoluteTimeContext,
SequenceActivityDormantContext,
- SequenceActivityGenerationContext,
SequenceActivitySuspensionContext,
SequenceActivityTimelineContext,
} from './sequence-activity-context.js';
@@ -93,17 +92,6 @@ export const SequenceActivityBoundary: React.FC<{
readonly frame: number;
}> = ({children, dormant, frame}) => {
const parentDormant = useContext(SequenceActivityDormantContext);
- const parentGeneration = useContext(SequenceActivityGenerationContext);
- const [activity, setActivity] = useState({dormant, generation: 0});
- if (activity.dormant !== dormant) {
- // Hidden trees can suspend before SequenceContent renders null. Give
- // resource-owning content a fresh identity even if that render is skipped.
- setActivity({
- dormant,
- generation: activity.generation + (dormant ? 1 : 0),
- });
- }
-
const timeline = useContext(
parentDormant ? SequenceActivityTimelineContext : TimelineContext,
);
@@ -146,13 +134,9 @@ export const SequenceActivityBoundary: React.FC<{
<React.Activity
mode={dormant && !parentDormant ? 'hidden' : 'visible'}
>
- <SequenceActivityGenerationContext.Provider
- value={parentGeneration + activity.generation}
- >
- <SuspendSequenceActivity dormant={dormant}>
- {children}
- </SuspendSequenceActivity>
- </SequenceActivityGenerationContext.Provider>
+ <SuspendSequenceActivity dormant={dormant}>
+ {children}
+ </SuspendSequenceActivity>
</React.Activity>
</AbsoluteTimeContext.Provider>
</TimelineContext.Provider>
diff --git a/packages/core/src/sequence-activity-context.ts b/packages/core/src/sequence-activity-context.ts
index 63d6b8c810..9f0b0e71d5 100644
--- a/packages/core/src/sequence-activity-context.ts
+++ b/packages/core/src/sequence-activity-context.ts
@@ -1,4 +1,4 @@
-import {createContext, createElement, Fragment, useContext} from 'react';
+import {createContext, useContext} from 'react';
import type {TimelineContextValue} from './TimelineContext.js';
export {DEFAULT_SEQUENCE_ACTIVITY_LIMIT} from './sequence-activity-defaults.js';
@@ -7,7 +7,6 @@ export {DEFAULT_SEQUENCE_ACTIVITY_LIMIT} from './sequence-activity-defaults.js';
// when the sequence manager falls back to effects.
export const SequenceActivityContext = createContext(false);
export const SequenceActivityDormantContext = createContext(false);
-export const SequenceActivityGenerationContext = createContext(0);
export const SequenceActivitySuspensionContext = createContext(false);
export const SequenceActivitySettingsContext = createContext<{
readonly limit: number;
@@ -24,7 +23,5 @@ export const SequenceActivityAbsoluteTimeContext =
export const SequenceContent: React.FC<{
readonly children: React.ReactNode;
}> = ({children}) => {
- const dormant = useContext(SequenceActivityDormantContext);
- const generation = useContext(SequenceActivityGenerationContext);
- return dormant ? null : createElement(Fragment, {key: generation}, children);
+ return useContext(SequenceActivityDormantContext) ? null : children;
};
diff --git a/packages/maptiler/src/MapLayer.tsx b/packages/maptiler/src/MapLayer.tsx
index 2e7e647c31..5e497c84ce 100644
--- a/packages/maptiler/src/MapLayer.tsx
+++ b/packages/maptiler/src/MapLayer.tsx
@@ -36,7 +36,7 @@ const MapLayerContent = ({beforeId, layer: passedLayer}: MapLayerProps) => {
const {continueRender, delayRender} = useDelayRender();
useEffect(() => {
- if (!map) {
+ if (!map || !map.style) {
return;
}
@@ -133,6 +133,10 @@ const MapLayerContent = ({beforeId, layer: passedLayer}: MapLayerProps) => {
}
return () => {
+ if (!map.style) {
+ return;
+ }
+
if (map.getLayer(layer.id)) {
map.removeLayer(layer.id);
map.triggerRepaint();
diff --git a/packages/maptiler/src/MapRegion.tsx b/packages/maptiler/src/MapRegion.tsx
index a551aac56e..1ad62bda10 100644
--- a/packages/maptiler/src/MapRegion.tsx
+++ b/packages/maptiler/src/MapRegion.tsx
@@ -1,4 +1,4 @@
-import type {GeoJSONSource} from '@maptiler/sdk';
+import type {GeoJSONSource, Map as MapTilerMap} from '@maptiler/sdk';
import {length as getLineLength, lineSliceAlong, lineString} from '@turf/turf';
import {
useContext,
@@ -121,7 +121,8 @@ const MapRegionDrawing = ({
}: MapRegionDrawingProps) => {
const {map} = useContext(MapTilerContext);
const {continueRender, delayRender} = useDelayRender();
- const [isReady, setIsReady] = useState(false);
+ const [readyMap, setReadyMap] = useState<MapTilerMap | null>(null);
+ const isReady = map !== null && readyMap === map;
useLayoutEffect(() => {
if (isReady) {
return;
@@ -145,7 +146,8 @@ const MapRegionDrawing = ({
]);
useLayoutEffect(() => {
- if (!map) {
+ // Reconnected effects can still capture the map destroyed while hidden.
+ if (!map || !map.style) {
return;
}
@@ -221,7 +223,7 @@ const MapRegionDrawing = ({
map.setPaintProperty(outlineSourceId, 'line-width', strokeWidth);
applyPremountVisibility();
- const onIdle = () => setIsReady(true);
+ const onIdle = () => setReadyMap(map);
map.once('idle', onIdle);
map.triggerRepaint();
return () => {
@@ -243,7 +245,7 @@ const MapRegionDrawing = ({
]);
useLayoutEffect(() => {
- if (!isReady || !map) {
+ if (!isReady || !map || !map.style) {
return;
}
diff --git a/packages/maptiler/src/MapRoute.tsx b/packages/maptiler/src/MapRoute.tsx
index eb0bca0d2e..77413aa566 100644
--- a/packages/maptiler/src/MapRoute.tsx
+++ b/packages/maptiler/src/MapRoute.tsx
@@ -1,4 +1,4 @@
-import type {GeoJSONSource} from '@maptiler/sdk';
+import type {GeoJSONSource, Map as MapTilerMap} from '@maptiler/sdk';
import {length as getLineLength, lineSliceAlong, lineString} from '@turf/turf';
import {
useContext,
@@ -95,7 +95,8 @@ const MapRouteDrawing = ({
}: MapRouteDrawingProps) => {
const {map} = useContext(MapTilerContext);
const {continueRender, delayRender} = useDelayRender();
- const [isReady, setIsReady] = useState(false);
+ const [readyMap, setReadyMap] = useState<MapTilerMap | null>(null);
+ const isReady = map !== null && readyMap === map;
useLayoutEffect(() => {
if (isReady) {
return;
@@ -117,7 +118,8 @@ const MapRouteDrawing = ({
]);
useLayoutEffect(() => {
- if (!map) {
+ // Reconnected effects can still capture the map destroyed while hidden.
+ if (!map || !map.style) {
return;
}
@@ -170,7 +172,7 @@ const MapRouteDrawing = ({
map.setPaintProperty(sourceId, 'line-width', strokeWidth);
applyPremountVisibility();
- const onIdle = () => setIsReady(true);
+ const onIdle = () => setReadyMap(map);
map.once('idle', onIdle);
map.triggerRepaint();
return () => {
@@ -189,7 +191,7 @@ const MapRouteDrawing = ({
]);
useLayoutEffect(() => {
- if (!isReady || !map) {
+ if (!isReady || !map || !map.style) {
return;
}
diff --git a/packages/maptiler/src/MapSource.tsx b/packages/maptiler/src/MapSource.tsx
index a25ab9a2d4..22453573a0 100644
--- a/packages/maptiler/src/MapSource.tsx
+++ b/packages/maptiler/src/MapSource.tsx
@@ -29,7 +29,7 @@ const MapSourceContent = ({children, id, source}: MapSourceProps) => {
sourceRef.current = source;
useEffect(() => {
- if (!map) {
+ if (!map || !map.style) {
return;
}
@@ -55,6 +55,10 @@ const MapSourceContent = ({children, id, source}: MapSourceProps) => {
map.off('idle', finish);
finish();
+ if (!map.style) {
+ return;
+ }
+
for (const layer of map.getStyle().layers ?? []) {
if (
'source' in layer &&
@@ -74,7 +78,7 @@ const MapSourceContent = ({children, id, source}: MapSourceProps) => {
}, [continueRender, delayRender, id, map, styleRevision]);
useEffect(() => {
- if (!map || source.type !== 'geojson') {
+ if (!map || !map.style || source.type !== 'geojson') {
return;
}
diff --git a/packages/maptiler/src/MapViewport.tsx b/packages/maptiler/src/MapViewport.tsx
index ef59ffd8a2..8fb10104ba 100644
--- a/packages/maptiler/src/MapViewport.tsx
+++ b/packages/maptiler/src/MapViewport.tsx
@@ -1,10 +1,14 @@
-import {MapStyle, Map as MapTilerMap, type MapOptions} from '@maptiler/sdk';
+import {
+ config as maptilerConfig,
+ MapStyle,
+ Map as MapTilerMap,
+ type MapOptions,
+} from '@maptiler/sdk';
import '@maptiler/sdk/style.css';
import type {ComponentType, CSSProperties, FC, ReactNode} from 'react';
import {useLayoutEffect, useMemo, useRef, useState} from 'react';
import {
Interactive,
- Internals,
useDelayRender,
type InteractiveBaseProps,
type InteractiveCropProps,
@@ -281,7 +285,7 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
map: MapTilerMap;
handle: number;
} | null>(null);
- const map = loadedMap?.map ?? null;
+ const map = loadedMap?.map.style ? loadedMap.map : null;
const [cameraRevision, setCameraRevision] = useState(0);
const [styleRevision, setStyleRevision] = useState(0);
const contextValue = useMemo(
@@ -302,6 +306,14 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
let cancelled = false;
const initialCamera = initialCameraRef.current;
const initialOptions = initialMapOptionsRef.current;
+ lastMapStyleRef.current = initialOptions.mapStyle;
+ lastLayerVisibilityRef.current = {
+ administrativeBorders: initialOptions.administrativeBorders,
+ showLabels: initialOptions.showLabels,
+ };
+ // SDK 4.0.2's telemetry timer outlives map.remove() and reads the removed
+ // style after two seconds. The SDK only offers a global telemetry opt-out.
+ maptilerConfig.telemetry = false;
// Keep MapTiler's logo and attribution visible:
// https://docs.maptiler.com/guides/map-design/attribution/remove-attribution/
const mapInstance = new MapTilerMap({
@@ -338,6 +350,10 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
mapRef.current = mapInstance;
mapInstance.on('load', () => {
+ if (cancelled) {
+ return;
+ }
+
stripStyleLayers({
administrativeBorders: initialOptions.administrativeBorders,
map: mapInstance,
@@ -357,6 +373,9 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
return () => {
cancelled = true;
+ // Activity tears down effects while retaining state. Never publish the
+ // removed map when these effects reconnect on becoming visible again.
+ setLoadedMap(null);
mapInstance.remove();
mapRef.current = null;
continueRender(loadingHandle);
@@ -370,7 +389,7 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
}, [continueRender, loadedMap]);
useLayoutEffect(() => {
- if (!map) {
+ if (!map || !map.style) {
return;
}
@@ -442,6 +461,7 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
useLayoutEffect(() => {
if (
!map ||
+ !map.style ||
(lastMapStyleRef.current === mapStyle &&
lastLayerVisibilityRef.current.showLabels === showLabels &&
lastLayerVisibilityRef.current.administrativeBorders ===
@@ -492,14 +512,14 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
]);
useLayoutEffect(() => {
- if (map && language) {
+ if (map?.style && language) {
map.setLanguage(language);
map.triggerRepaint();
}
}, [language, map]);
useLayoutEffect(() => {
- if (!map) {
+ if (!map || !map.style) {
return;
}
@@ -513,7 +533,7 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
}, [map, terrain, terrainExaggeration]);
useLayoutEffect(() => {
- if (map && projection) {
+ if (map?.style && projection) {
map.setProjection(projection, {persist: true});
map.triggerRepaint();
}
@@ -551,11 +571,7 @@ const MapViewportContent: FC<MapViewportContentProps> = ({
export const MapViewport: ComponentType<MapViewportProps> =
Interactive.withSchema({
- Component: (props: MapViewportContentProps) => (
- <Internals.SequenceContent>
- <MapViewportContent {...props} />
- </Internals.SequenceContent>
- ),
+ Component: MapViewportContent,
componentName: '<MapViewport>',
schema: mapViewportSchema,
wrapInSequence: true,