@trackunit/react-map-adapter-google 0.0.197 → 0.0.199
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +129 -13
- package/index.esm.js +130 -14
- package/migrations/entry.js.map +1 -0
- package/package.json +5 -5
- package/src/GoogleMapsAdapterInstance.d.ts +17 -0
- package/src/constants.d.ts +4 -0
- package/src/draw/createGoogleDrawEngineLoader.d.ts +35 -4
- package/src/google-maps.d.ts +11 -0
package/index.cjs.js
CHANGED
|
@@ -29,6 +29,10 @@ const GOOGLE_MAP_IDS = {
|
|
|
29
29
|
*
|
|
30
30
|
* Temporary: Google deletes numbered versions after ~3 quarters. Remove the pin once
|
|
31
31
|
* `HeatmapLayer` and `DrawingManager` usages are migrated to replacements.
|
|
32
|
+
*
|
|
33
|
+
* Checklist for every change of this version: draw a polygon on both the Create Site
|
|
34
|
+
* wizard map and the react-map Storybook `useDraw` story (why: ADR 0030, open items,
|
|
35
|
+
* SAGA-985).
|
|
32
36
|
*/
|
|
33
37
|
const GOOGLE_MAPS_API_VERSION = "3.64";
|
|
34
38
|
|
|
@@ -56,18 +60,93 @@ const GoogleApiProvider = ({ apiKey, language, children }) => {
|
|
|
56
60
|
return (jsxRuntime.jsx(reactGoogleMaps.APIProvider, { apiKey: apiKey, language: language, libraries: libraries, version: GOOGLE_MAPS_API_VERSION, children: children }));
|
|
57
61
|
};
|
|
58
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The engine could not be built because the element `terra-draw-google-maps-adapter`
|
|
65
|
+
* listens on is not in the map container. Surfaces as the `cause` of the
|
|
66
|
+
* `DrawController`'s `engine-load-failed` error (ADR 0030, open items, SAGA-985).
|
|
67
|
+
*/
|
|
68
|
+
class GoogleDrawEventLayerMissingError extends Error {
|
|
69
|
+
constructor() {
|
|
70
|
+
super("Google Maps event layer not found after waiting for the map to render its panes (SAGA-985)");
|
|
71
|
+
this.name = "GoogleDrawEventLayerMissingError";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* How long a map gets to render its panes after the probe first misses before the
|
|
76
|
+
* loader gives up. The loader runs once `GoogleMapsAdapterInstance` has connected it,
|
|
77
|
+
* on the map's first `idle` or after its own fallback timer; the observer below then
|
|
78
|
+
* resolves as soon as the panes appear, and on a hidden or zero-sized map they may never
|
|
79
|
+
* appear, so this bound is what keeps the loader promise settling and the controller's
|
|
80
|
+
* "loads again on the next mode or value change" true.
|
|
81
|
+
*/
|
|
82
|
+
const PANES_RENDER_TIMEOUT_MS = 1000;
|
|
83
|
+
/**
|
|
84
|
+
* The adapter finds Google's event layer by an inline style (`z-index: 3`) that no Google
|
|
85
|
+
* Maps API documents, and types the lookup as never missing (SAGA-985). Callers treat a
|
|
86
|
+
* miss as "no element": the loader refuses to build an engine, the capture has nothing
|
|
87
|
+
* to capture.
|
|
88
|
+
*/
|
|
59
89
|
const findMapEventElement = (adapter) => {
|
|
60
90
|
const element = adapter.getMapEventElement();
|
|
61
91
|
return element;
|
|
62
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* Resolves with the element as soon as a DOM change inside `container` makes `probe`
|
|
95
|
+
* find it, or with `probe`'s answer after `PANES_RENDER_TIMEOUT_MS`, whichever comes
|
|
96
|
+
* first. Inline `style` changes are observed too, because the adapter's selector
|
|
97
|
+
* matches a style, not an element. The observer is registered before the timer is
|
|
98
|
+
* armed, and whichever side settles first disconnects the other; `probe` runs the
|
|
99
|
+
* adapter's full-subtree selector, so a burst of mutations (Google inserting tiles) is
|
|
100
|
+
* checked once per animation frame.
|
|
101
|
+
*/
|
|
102
|
+
const waitForEventLayer = (container, probe) => new Promise(resolve => {
|
|
103
|
+
let settled = false;
|
|
104
|
+
let frame = null;
|
|
105
|
+
const settle = (element) => {
|
|
106
|
+
if (settled)
|
|
107
|
+
return;
|
|
108
|
+
settled = true;
|
|
109
|
+
observer.disconnect();
|
|
110
|
+
clearTimeout(timer);
|
|
111
|
+
if (frame !== null)
|
|
112
|
+
cancelAnimationFrame(frame);
|
|
113
|
+
resolve(element);
|
|
114
|
+
};
|
|
115
|
+
const observer = new MutationObserver(() => {
|
|
116
|
+
if (frame !== null)
|
|
117
|
+
return;
|
|
118
|
+
frame = requestAnimationFrame(() => {
|
|
119
|
+
frame = null;
|
|
120
|
+
const element = probe();
|
|
121
|
+
if (element !== null)
|
|
122
|
+
settle(element);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
observer.observe(container, { childList: true, subtree: true, attributes: true, attributeFilter: ["style"] });
|
|
126
|
+
const timer = setTimeout(() => settle(probe()), PANES_RENDER_TIMEOUT_MS);
|
|
127
|
+
});
|
|
63
128
|
/**
|
|
64
129
|
* Builds the Google Maps bridge for the shared drawing engine (ADR 0030).
|
|
65
130
|
*
|
|
66
131
|
* One loader per `google.maps.Map` instance: theme switches and traffic toggles
|
|
67
|
-
* recreate the map, and `GoogleMapsAdapterInstance
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
132
|
+
* recreate the map, and `GoogleMapsAdapterInstance` hands the controller a fresh loader
|
|
133
|
+
* on each map's first `idle`, or after its `DRAW_CONNECT_TIMEOUT_MS` fallback for a map
|
|
134
|
+
* that never goes idle, so the engine is rebuilt on the live map once Google has built
|
|
135
|
+
* it. Terra Draw and its Google adapter are only downloaded when the loader runs, which
|
|
136
|
+
* the controller defers until drawing is actually needed.
|
|
137
|
+
*
|
|
138
|
+
* Readiness probe, the second line after that gate: before the engine is built,
|
|
139
|
+
* the loader looks for the event layer the adapter will listen on. A miss watches the
|
|
140
|
+
* map container for the layer to appear, bounded by `PANES_RENDER_TIMEOUT_MS`, and
|
|
141
|
+
* probes once more; a miss after that rejects with `GoogleDrawEventLayerMissingError`,
|
|
142
|
+
* which the `DrawController` reports as `engine-load-failed`, and the controller loads
|
|
143
|
+
* again on the next connect, mode or value change. There is no retry loop. The wait is
|
|
144
|
+
* not cancelled by a disconnect: the observer goes with the layer or the timer, and an
|
|
145
|
+
* engine built for a map the controller has left is destroyed by its generation check,
|
|
146
|
+
* the same cost as a slow chunk download. The failure path downloads the adapter chunk
|
|
147
|
+
* and constructs an adapter only to run its selector; that is accepted, because the
|
|
148
|
+
* selector is the adapter's to own and a copy of it here would drift from it. History,
|
|
149
|
+
* the Maps JS version checklist and the upstream fix: ADR 0030, open items (SAGA-985).
|
|
71
150
|
*/
|
|
72
151
|
const createGoogleDrawEngineLoader = (map) => async () => {
|
|
73
152
|
const { TerraDrawGoogleMapsAdapter } = await import('terra-draw-google-maps-adapter');
|
|
@@ -79,11 +158,14 @@ const createGoogleDrawEngineLoader = (map) => async () => {
|
|
|
79
158
|
isolatedData: true,
|
|
80
159
|
coordinatePrecision: reactMapDraw.DRAW_COORDINATE_PRECISION,
|
|
81
160
|
});
|
|
161
|
+
const probe = () => findMapEventElement(adapter);
|
|
162
|
+
const element = probe() ?? (await waitForEventLayer(map.getDiv(), probe));
|
|
163
|
+
if (element === null) {
|
|
164
|
+
throw new GoogleDrawEventLayerMissingError();
|
|
165
|
+
}
|
|
82
166
|
const engine = await reactMapDraw.loadTerraDrawEngine({ adapter });
|
|
83
|
-
// The adapter finds Google's event layer by an inline style (SAGA-985) and types the
|
|
84
|
-
// result as never missing; the capture treats a miss as "nothing to capture".
|
|
85
167
|
return reactMapDraw.withPointerCapture(engine, {
|
|
86
|
-
resolveElement:
|
|
168
|
+
resolveElement: probe,
|
|
87
169
|
resolveBounds: () => map.getDiv().getBoundingClientRect(),
|
|
88
170
|
});
|
|
89
171
|
};
|
|
@@ -1854,14 +1936,11 @@ class GoogleMapsLayerPort {
|
|
|
1854
1936
|
const container = document.createElement("div");
|
|
1855
1937
|
container.style.pointerEvents = "auto";
|
|
1856
1938
|
reactMapAdapterShared.fadeInElement(container);
|
|
1857
|
-
const transforms = [reactMapAdapterShared.anchorFromBottomCenter(anchor)];
|
|
1858
|
-
if (pixelOffset !== undefined)
|
|
1859
|
-
transforms.push(`translate(${pixelOffset.x}px, ${pixelOffset.y}px)`);
|
|
1860
|
-
container.style.transform = transforms.join(" ");
|
|
1861
1939
|
const marker = new google.maps.marker.AdvancedMarkerElement({
|
|
1862
1940
|
map,
|
|
1863
1941
|
position: coords,
|
|
1864
1942
|
content: container,
|
|
1943
|
+
...reactMapAdapterShared.anchorToGoogleOffsets(anchor, pixelOffset),
|
|
1865
1944
|
});
|
|
1866
1945
|
const descriptor = {
|
|
1867
1946
|
key: `${sourceId}:${featureId}`,
|
|
@@ -2987,6 +3066,14 @@ class GoogleMapsLayerPort {
|
|
|
2987
3066
|
}
|
|
2988
3067
|
GoogleMapsLayerPort.DBLCLICK_THRESHOLD_MS = 400;
|
|
2989
3068
|
|
|
3069
|
+
/**
|
|
3070
|
+
* How long a connected map gets to fire its first `idle` before the draw loader is
|
|
3071
|
+
* connected anyway (ADR 0030, SAGA-985). A visible map goes idle within a frame or two
|
|
3072
|
+
* of its first render, so three seconds covers a slow tile fetch on a weak connection
|
|
3073
|
+
* without leaving a hidden or stalled map's drawing disconnected (no engine, no error,
|
|
3074
|
+
* no hint) for longer than that; past it the loader's own probe and bound decide.
|
|
3075
|
+
*/
|
|
3076
|
+
const DRAW_CONNECT_TIMEOUT_MS = 3000;
|
|
2990
3077
|
/**
|
|
2991
3078
|
* Google Maps adapter instance implementation
|
|
2992
3079
|
* Manages the connection between our abstract interface and Google Maps API
|
|
@@ -3014,6 +3101,14 @@ class GoogleMapsAdapterInstance {
|
|
|
3014
3101
|
this.statusListeners = new Set();
|
|
3015
3102
|
this.eventListeners = new Map();
|
|
3016
3103
|
this.googleEventListeners = [];
|
|
3104
|
+
/**
|
|
3105
|
+
* The one-shot `idle` hook and its fallback timer that connect the draw loader once
|
|
3106
|
+
* Google has built the map, or after `DRAW_CONNECT_TIMEOUT_MS` if it never says so;
|
|
3107
|
+
* whichever fires first connects once, and both are cancelled with the other map
|
|
3108
|
+
* listeners so a map that is gone never connects a stale loader (ADR 0030, SAGA-985).
|
|
3109
|
+
*/
|
|
3110
|
+
this.pendingDrawConnect = null;
|
|
3111
|
+
this.pendingDrawConnectTimer = null;
|
|
3017
3112
|
/** Throttled pointermove channel shared by map mousemove and shape overlay mousemove. */
|
|
3018
3113
|
this.pointerMoveRaf = null;
|
|
3019
3114
|
this.lastPointerPosition = null;
|
|
@@ -3100,9 +3195,11 @@ class GoogleMapsAdapterInstance {
|
|
|
3100
3195
|
this.updateStatusCache();
|
|
3101
3196
|
this.layers.connect(map);
|
|
3102
3197
|
// Theme changes recreate the google.maps.Map, so traffic and drawing must re-attach on every (re)connect.
|
|
3103
|
-
// The draw controller
|
|
3198
|
+
// The draw controller keeps mode and value while disconnected and rebuilds its engine from the
|
|
3199
|
+
// committed value on the new map once it first goes idle (ADR 0030): the loader is connected from
|
|
3200
|
+
// the one-shot idle hook in setupEventListeners, not here.
|
|
3104
3201
|
this.syncTrafficLayer();
|
|
3105
|
-
this.draw.
|
|
3202
|
+
this.draw.disconnect();
|
|
3106
3203
|
this.setupEventListeners();
|
|
3107
3204
|
this.layers.setPointerMoveReporter(position => {
|
|
3108
3205
|
this.reportPointerMove(position);
|
|
@@ -3379,6 +3476,7 @@ class GoogleMapsAdapterInstance {
|
|
|
3379
3476
|
google.maps.event.removeListener(listener);
|
|
3380
3477
|
});
|
|
3381
3478
|
this.googleEventListeners = [];
|
|
3479
|
+
this.cancelPendingDrawConnect();
|
|
3382
3480
|
if (this.pointerMoveRaf !== null) {
|
|
3383
3481
|
cancelAnimationFrame(this.pointerMoveRaf);
|
|
3384
3482
|
this.pointerMoveRaf = null;
|
|
@@ -3405,6 +3503,16 @@ class GoogleMapsAdapterInstance {
|
|
|
3405
3503
|
this.updateState(true);
|
|
3406
3504
|
this.emitEvent({ type: "idle" });
|
|
3407
3505
|
}));
|
|
3506
|
+
// Drawing connects on the map's first idle, once Google has built the map and its
|
|
3507
|
+
// panes exist for the engine to listen on, or after DRAW_CONNECT_TIMEOUT_MS for a map
|
|
3508
|
+
// that never says so (hidden, zero-sized, stalled); the loader's probe and bound then
|
|
3509
|
+
// decide between a live engine and engine-load-failed (ADR 0030, SAGA-985).
|
|
3510
|
+
const connectDraw = () => {
|
|
3511
|
+
this.cancelPendingDrawConnect();
|
|
3512
|
+
this.draw.connect(createGoogleDrawEngineLoader(currentMap));
|
|
3513
|
+
};
|
|
3514
|
+
this.pendingDrawConnect = currentMap.addListener("idle", connectDraw);
|
|
3515
|
+
this.pendingDrawConnectTimer = setTimeout(connectDraw, DRAW_CONNECT_TIMEOUT_MS);
|
|
3408
3516
|
const markCameraActive = () => {
|
|
3409
3517
|
if (this.state.isIdle) {
|
|
3410
3518
|
this.state = { ...this.state, isIdle: false };
|
|
@@ -3448,6 +3556,14 @@ class GoogleMapsAdapterInstance {
|
|
|
3448
3556
|
this.reportPointerMove(latLngToPosition(latLng));
|
|
3449
3557
|
}));
|
|
3450
3558
|
}
|
|
3559
|
+
cancelPendingDrawConnect() {
|
|
3560
|
+
this.pendingDrawConnect?.remove();
|
|
3561
|
+
this.pendingDrawConnect = null;
|
|
3562
|
+
if (this.pendingDrawConnectTimer !== null) {
|
|
3563
|
+
clearTimeout(this.pendingDrawConnectTimer);
|
|
3564
|
+
this.pendingDrawConnectTimer = null;
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3451
3567
|
updateState(isIdle) {
|
|
3452
3568
|
const currentMap = this.map;
|
|
3453
3569
|
if (currentMap === null)
|
package/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { setHitTargetInert, canPatchAdaptiveMarker, canPatchAdaptiveViewport, canPatchMarkerInPlace, extractPointCoordinates, mergeAntimeridianFeatures, extractPolygonPaths, extractLineCoordinates, patchPortalDescriptors, collectFeatureIdSet, removeGoneIndexedMarkers, getAdaptiveDomFeatureIds, extractSourceData, buildAdaptiveDomRenderFn, resolveCircleSymbolDefaults, resolveSymbolDescriptor, buildAdaptiveSymbolStyleFn, createSymbolDotElement, anchorFromBottomCenter, createDefaultClusterElement, createClusterPinElement, fadeInElement, buildAdaptiveDomEntries, MAP_CURSORS, resolveSelectedStyle, resolveHoveredStyle, attachSafeAreaHoverListeners, INITIAL_MAP_STATE, DEFAULT_MAP_APPEARANCE, computeInitialState, getEffectiveRestrictBounds, TRAFFIC_MIN_ZOOM, cameraStateEquals, isEventOfType, DEFAULT_ZOOM, DEFAULT_CENTER, defineAdapter } from '@trackunit/react-map-adapter-shared';
|
|
1
|
+
import { setHitTargetInert, canPatchAdaptiveMarker, canPatchAdaptiveViewport, canPatchMarkerInPlace, extractPointCoordinates, mergeAntimeridianFeatures, extractPolygonPaths, extractLineCoordinates, patchPortalDescriptors, collectFeatureIdSet, removeGoneIndexedMarkers, getAdaptiveDomFeatureIds, extractSourceData, buildAdaptiveDomRenderFn, resolveCircleSymbolDefaults, resolveSymbolDescriptor, buildAdaptiveSymbolStyleFn, createSymbolDotElement, anchorFromBottomCenter, createDefaultClusterElement, createClusterPinElement, fadeInElement, anchorToGoogleOffsets, buildAdaptiveDomEntries, MAP_CURSORS, resolveSelectedStyle, resolveHoveredStyle, attachSafeAreaHoverListeners, INITIAL_MAP_STATE, DEFAULT_MAP_APPEARANCE, computeInitialState, getEffectiveRestrictBounds, TRAFFIC_MIN_ZOOM, cameraStateEquals, isEventOfType, DEFAULT_ZOOM, DEFAULT_CENTER, defineAdapter } from '@trackunit/react-map-adapter-shared';
|
|
2
2
|
export { googleMapCspDirectives } from '@trackunit/react-map-adapter-shared';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
import { APIProvider, RenderingType, APIProviderContext, useMap, useApiLoadingStatus, APILoadingStatus, Map as Map$1 } from '@vis.gl/react-google-maps';
|
|
@@ -28,6 +28,10 @@ const GOOGLE_MAP_IDS = {
|
|
|
28
28
|
*
|
|
29
29
|
* Temporary: Google deletes numbered versions after ~3 quarters. Remove the pin once
|
|
30
30
|
* `HeatmapLayer` and `DrawingManager` usages are migrated to replacements.
|
|
31
|
+
*
|
|
32
|
+
* Checklist for every change of this version: draw a polygon on both the Create Site
|
|
33
|
+
* wizard map and the react-map Storybook `useDraw` story (why: ADR 0030, open items,
|
|
34
|
+
* SAGA-985).
|
|
31
35
|
*/
|
|
32
36
|
const GOOGLE_MAPS_API_VERSION = "3.64";
|
|
33
37
|
|
|
@@ -55,18 +59,93 @@ const GoogleApiProvider = ({ apiKey, language, children }) => {
|
|
|
55
59
|
return (jsx(APIProvider, { apiKey: apiKey, language: language, libraries: libraries, version: GOOGLE_MAPS_API_VERSION, children: children }));
|
|
56
60
|
};
|
|
57
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The engine could not be built because the element `terra-draw-google-maps-adapter`
|
|
64
|
+
* listens on is not in the map container. Surfaces as the `cause` of the
|
|
65
|
+
* `DrawController`'s `engine-load-failed` error (ADR 0030, open items, SAGA-985).
|
|
66
|
+
*/
|
|
67
|
+
class GoogleDrawEventLayerMissingError extends Error {
|
|
68
|
+
constructor() {
|
|
69
|
+
super("Google Maps event layer not found after waiting for the map to render its panes (SAGA-985)");
|
|
70
|
+
this.name = "GoogleDrawEventLayerMissingError";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* How long a map gets to render its panes after the probe first misses before the
|
|
75
|
+
* loader gives up. The loader runs once `GoogleMapsAdapterInstance` has connected it,
|
|
76
|
+
* on the map's first `idle` or after its own fallback timer; the observer below then
|
|
77
|
+
* resolves as soon as the panes appear, and on a hidden or zero-sized map they may never
|
|
78
|
+
* appear, so this bound is what keeps the loader promise settling and the controller's
|
|
79
|
+
* "loads again on the next mode or value change" true.
|
|
80
|
+
*/
|
|
81
|
+
const PANES_RENDER_TIMEOUT_MS = 1000;
|
|
82
|
+
/**
|
|
83
|
+
* The adapter finds Google's event layer by an inline style (`z-index: 3`) that no Google
|
|
84
|
+
* Maps API documents, and types the lookup as never missing (SAGA-985). Callers treat a
|
|
85
|
+
* miss as "no element": the loader refuses to build an engine, the capture has nothing
|
|
86
|
+
* to capture.
|
|
87
|
+
*/
|
|
58
88
|
const findMapEventElement = (adapter) => {
|
|
59
89
|
const element = adapter.getMapEventElement();
|
|
60
90
|
return element;
|
|
61
91
|
};
|
|
92
|
+
/**
|
|
93
|
+
* Resolves with the element as soon as a DOM change inside `container` makes `probe`
|
|
94
|
+
* find it, or with `probe`'s answer after `PANES_RENDER_TIMEOUT_MS`, whichever comes
|
|
95
|
+
* first. Inline `style` changes are observed too, because the adapter's selector
|
|
96
|
+
* matches a style, not an element. The observer is registered before the timer is
|
|
97
|
+
* armed, and whichever side settles first disconnects the other; `probe` runs the
|
|
98
|
+
* adapter's full-subtree selector, so a burst of mutations (Google inserting tiles) is
|
|
99
|
+
* checked once per animation frame.
|
|
100
|
+
*/
|
|
101
|
+
const waitForEventLayer = (container, probe) => new Promise(resolve => {
|
|
102
|
+
let settled = false;
|
|
103
|
+
let frame = null;
|
|
104
|
+
const settle = (element) => {
|
|
105
|
+
if (settled)
|
|
106
|
+
return;
|
|
107
|
+
settled = true;
|
|
108
|
+
observer.disconnect();
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
if (frame !== null)
|
|
111
|
+
cancelAnimationFrame(frame);
|
|
112
|
+
resolve(element);
|
|
113
|
+
};
|
|
114
|
+
const observer = new MutationObserver(() => {
|
|
115
|
+
if (frame !== null)
|
|
116
|
+
return;
|
|
117
|
+
frame = requestAnimationFrame(() => {
|
|
118
|
+
frame = null;
|
|
119
|
+
const element = probe();
|
|
120
|
+
if (element !== null)
|
|
121
|
+
settle(element);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
observer.observe(container, { childList: true, subtree: true, attributes: true, attributeFilter: ["style"] });
|
|
125
|
+
const timer = setTimeout(() => settle(probe()), PANES_RENDER_TIMEOUT_MS);
|
|
126
|
+
});
|
|
62
127
|
/**
|
|
63
128
|
* Builds the Google Maps bridge for the shared drawing engine (ADR 0030).
|
|
64
129
|
*
|
|
65
130
|
* One loader per `google.maps.Map` instance: theme switches and traffic toggles
|
|
66
|
-
* recreate the map, and `GoogleMapsAdapterInstance
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
131
|
+
* recreate the map, and `GoogleMapsAdapterInstance` hands the controller a fresh loader
|
|
132
|
+
* on each map's first `idle`, or after its `DRAW_CONNECT_TIMEOUT_MS` fallback for a map
|
|
133
|
+
* that never goes idle, so the engine is rebuilt on the live map once Google has built
|
|
134
|
+
* it. Terra Draw and its Google adapter are only downloaded when the loader runs, which
|
|
135
|
+
* the controller defers until drawing is actually needed.
|
|
136
|
+
*
|
|
137
|
+
* Readiness probe, the second line after that gate: before the engine is built,
|
|
138
|
+
* the loader looks for the event layer the adapter will listen on. A miss watches the
|
|
139
|
+
* map container for the layer to appear, bounded by `PANES_RENDER_TIMEOUT_MS`, and
|
|
140
|
+
* probes once more; a miss after that rejects with `GoogleDrawEventLayerMissingError`,
|
|
141
|
+
* which the `DrawController` reports as `engine-load-failed`, and the controller loads
|
|
142
|
+
* again on the next connect, mode or value change. There is no retry loop. The wait is
|
|
143
|
+
* not cancelled by a disconnect: the observer goes with the layer or the timer, and an
|
|
144
|
+
* engine built for a map the controller has left is destroyed by its generation check,
|
|
145
|
+
* the same cost as a slow chunk download. The failure path downloads the adapter chunk
|
|
146
|
+
* and constructs an adapter only to run its selector; that is accepted, because the
|
|
147
|
+
* selector is the adapter's to own and a copy of it here would drift from it. History,
|
|
148
|
+
* the Maps JS version checklist and the upstream fix: ADR 0030, open items (SAGA-985).
|
|
70
149
|
*/
|
|
71
150
|
const createGoogleDrawEngineLoader = (map) => async () => {
|
|
72
151
|
const { TerraDrawGoogleMapsAdapter } = await import('terra-draw-google-maps-adapter');
|
|
@@ -78,11 +157,14 @@ const createGoogleDrawEngineLoader = (map) => async () => {
|
|
|
78
157
|
isolatedData: true,
|
|
79
158
|
coordinatePrecision: DRAW_COORDINATE_PRECISION,
|
|
80
159
|
});
|
|
160
|
+
const probe = () => findMapEventElement(adapter);
|
|
161
|
+
const element = probe() ?? (await waitForEventLayer(map.getDiv(), probe));
|
|
162
|
+
if (element === null) {
|
|
163
|
+
throw new GoogleDrawEventLayerMissingError();
|
|
164
|
+
}
|
|
81
165
|
const engine = await loadTerraDrawEngine({ adapter });
|
|
82
|
-
// The adapter finds Google's event layer by an inline style (SAGA-985) and types the
|
|
83
|
-
// result as never missing; the capture treats a miss as "nothing to capture".
|
|
84
166
|
return withPointerCapture(engine, {
|
|
85
|
-
resolveElement:
|
|
167
|
+
resolveElement: probe,
|
|
86
168
|
resolveBounds: () => map.getDiv().getBoundingClientRect(),
|
|
87
169
|
});
|
|
88
170
|
};
|
|
@@ -1853,14 +1935,11 @@ class GoogleMapsLayerPort {
|
|
|
1853
1935
|
const container = document.createElement("div");
|
|
1854
1936
|
container.style.pointerEvents = "auto";
|
|
1855
1937
|
fadeInElement(container);
|
|
1856
|
-
const transforms = [anchorFromBottomCenter(anchor)];
|
|
1857
|
-
if (pixelOffset !== undefined)
|
|
1858
|
-
transforms.push(`translate(${pixelOffset.x}px, ${pixelOffset.y}px)`);
|
|
1859
|
-
container.style.transform = transforms.join(" ");
|
|
1860
1938
|
const marker = new google.maps.marker.AdvancedMarkerElement({
|
|
1861
1939
|
map,
|
|
1862
1940
|
position: coords,
|
|
1863
1941
|
content: container,
|
|
1942
|
+
...anchorToGoogleOffsets(anchor, pixelOffset),
|
|
1864
1943
|
});
|
|
1865
1944
|
const descriptor = {
|
|
1866
1945
|
key: `${sourceId}:${featureId}`,
|
|
@@ -2986,6 +3065,14 @@ class GoogleMapsLayerPort {
|
|
|
2986
3065
|
}
|
|
2987
3066
|
GoogleMapsLayerPort.DBLCLICK_THRESHOLD_MS = 400;
|
|
2988
3067
|
|
|
3068
|
+
/**
|
|
3069
|
+
* How long a connected map gets to fire its first `idle` before the draw loader is
|
|
3070
|
+
* connected anyway (ADR 0030, SAGA-985). A visible map goes idle within a frame or two
|
|
3071
|
+
* of its first render, so three seconds covers a slow tile fetch on a weak connection
|
|
3072
|
+
* without leaving a hidden or stalled map's drawing disconnected (no engine, no error,
|
|
3073
|
+
* no hint) for longer than that; past it the loader's own probe and bound decide.
|
|
3074
|
+
*/
|
|
3075
|
+
const DRAW_CONNECT_TIMEOUT_MS = 3000;
|
|
2989
3076
|
/**
|
|
2990
3077
|
* Google Maps adapter instance implementation
|
|
2991
3078
|
* Manages the connection between our abstract interface and Google Maps API
|
|
@@ -3013,6 +3100,14 @@ class GoogleMapsAdapterInstance {
|
|
|
3013
3100
|
this.statusListeners = new Set();
|
|
3014
3101
|
this.eventListeners = new Map();
|
|
3015
3102
|
this.googleEventListeners = [];
|
|
3103
|
+
/**
|
|
3104
|
+
* The one-shot `idle` hook and its fallback timer that connect the draw loader once
|
|
3105
|
+
* Google has built the map, or after `DRAW_CONNECT_TIMEOUT_MS` if it never says so;
|
|
3106
|
+
* whichever fires first connects once, and both are cancelled with the other map
|
|
3107
|
+
* listeners so a map that is gone never connects a stale loader (ADR 0030, SAGA-985).
|
|
3108
|
+
*/
|
|
3109
|
+
this.pendingDrawConnect = null;
|
|
3110
|
+
this.pendingDrawConnectTimer = null;
|
|
3016
3111
|
/** Throttled pointermove channel shared by map mousemove and shape overlay mousemove. */
|
|
3017
3112
|
this.pointerMoveRaf = null;
|
|
3018
3113
|
this.lastPointerPosition = null;
|
|
@@ -3099,9 +3194,11 @@ class GoogleMapsAdapterInstance {
|
|
|
3099
3194
|
this.updateStatusCache();
|
|
3100
3195
|
this.layers.connect(map);
|
|
3101
3196
|
// Theme changes recreate the google.maps.Map, so traffic and drawing must re-attach on every (re)connect.
|
|
3102
|
-
// The draw controller
|
|
3197
|
+
// The draw controller keeps mode and value while disconnected and rebuilds its engine from the
|
|
3198
|
+
// committed value on the new map once it first goes idle (ADR 0030): the loader is connected from
|
|
3199
|
+
// the one-shot idle hook in setupEventListeners, not here.
|
|
3103
3200
|
this.syncTrafficLayer();
|
|
3104
|
-
this.draw.
|
|
3201
|
+
this.draw.disconnect();
|
|
3105
3202
|
this.setupEventListeners();
|
|
3106
3203
|
this.layers.setPointerMoveReporter(position => {
|
|
3107
3204
|
this.reportPointerMove(position);
|
|
@@ -3378,6 +3475,7 @@ class GoogleMapsAdapterInstance {
|
|
|
3378
3475
|
google.maps.event.removeListener(listener);
|
|
3379
3476
|
});
|
|
3380
3477
|
this.googleEventListeners = [];
|
|
3478
|
+
this.cancelPendingDrawConnect();
|
|
3381
3479
|
if (this.pointerMoveRaf !== null) {
|
|
3382
3480
|
cancelAnimationFrame(this.pointerMoveRaf);
|
|
3383
3481
|
this.pointerMoveRaf = null;
|
|
@@ -3404,6 +3502,16 @@ class GoogleMapsAdapterInstance {
|
|
|
3404
3502
|
this.updateState(true);
|
|
3405
3503
|
this.emitEvent({ type: "idle" });
|
|
3406
3504
|
}));
|
|
3505
|
+
// Drawing connects on the map's first idle, once Google has built the map and its
|
|
3506
|
+
// panes exist for the engine to listen on, or after DRAW_CONNECT_TIMEOUT_MS for a map
|
|
3507
|
+
// that never says so (hidden, zero-sized, stalled); the loader's probe and bound then
|
|
3508
|
+
// decide between a live engine and engine-load-failed (ADR 0030, SAGA-985).
|
|
3509
|
+
const connectDraw = () => {
|
|
3510
|
+
this.cancelPendingDrawConnect();
|
|
3511
|
+
this.draw.connect(createGoogleDrawEngineLoader(currentMap));
|
|
3512
|
+
};
|
|
3513
|
+
this.pendingDrawConnect = currentMap.addListener("idle", connectDraw);
|
|
3514
|
+
this.pendingDrawConnectTimer = setTimeout(connectDraw, DRAW_CONNECT_TIMEOUT_MS);
|
|
3407
3515
|
const markCameraActive = () => {
|
|
3408
3516
|
if (this.state.isIdle) {
|
|
3409
3517
|
this.state = { ...this.state, isIdle: false };
|
|
@@ -3447,6 +3555,14 @@ class GoogleMapsAdapterInstance {
|
|
|
3447
3555
|
this.reportPointerMove(latLngToPosition(latLng));
|
|
3448
3556
|
}));
|
|
3449
3557
|
}
|
|
3558
|
+
cancelPendingDrawConnect() {
|
|
3559
|
+
this.pendingDrawConnect?.remove();
|
|
3560
|
+
this.pendingDrawConnect = null;
|
|
3561
|
+
if (this.pendingDrawConnectTimer !== null) {
|
|
3562
|
+
clearTimeout(this.pendingDrawConnectTimer);
|
|
3563
|
+
this.pendingDrawConnectTimer = null;
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3450
3566
|
updateState(isIdle) {
|
|
3451
3567
|
const currentMap = this.map;
|
|
3452
3568
|
if (currentMap === null)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entry.js","sourceRoot":"","sources":["../../../../../libs/react/map-adapter-google/migrations/entry.ts"],"names":[],"mappings":"","sourcesContent":["export {};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-map-adapter-google",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.199",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -14,15 +14,15 @@
|
|
|
14
14
|
"@loaders.gl/core": "~4.5.2",
|
|
15
15
|
"@luma.gl/core": "^9.4.1",
|
|
16
16
|
"@luma.gl/engine": "^9.4.1",
|
|
17
|
-
"@trackunit/react-map-adapter-shared": "0.0.
|
|
18
|
-
"@trackunit/react-map-color-utils": "0.0.
|
|
19
|
-
"@trackunit/geo-json-utils": "1.21.
|
|
17
|
+
"@trackunit/react-map-adapter-shared": "0.0.209",
|
|
18
|
+
"@trackunit/react-map-color-utils": "0.0.182",
|
|
19
|
+
"@trackunit/geo-json-utils": "1.21.41",
|
|
20
20
|
"@googlemaps/markerclusterer": "^2.6.2",
|
|
21
21
|
"@vis.gl/react-google-maps": "^1.7.1",
|
|
22
22
|
"es-toolkit": "^1.52.0",
|
|
23
23
|
"react-device-detect": "^2.2.3",
|
|
24
24
|
"zod": "^3.25.76",
|
|
25
|
-
"@trackunit/react-map-draw": "0.0.
|
|
25
|
+
"@trackunit/react-map-draw": "0.0.47",
|
|
26
26
|
"terra-draw-google-maps-adapter": "^1.6.2"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { type AdapterInstance, type BaseAdapterConfig, type CameraState, type FitBoundsOptions, type GeoJsonBbox, type GeoJsonPosition, type MapEvent, type MapEventHandler, type MapStatus, type MapTheme, type MapType } from "@trackunit/react-map-adapter-shared";
|
|
2
2
|
import { DrawController } from "@trackunit/react-map-draw";
|
|
3
3
|
import { GoogleMapsLayerPort } from "./GoogleMapsLayerPort";
|
|
4
|
+
/**
|
|
5
|
+
* How long a connected map gets to fire its first `idle` before the draw loader is
|
|
6
|
+
* connected anyway (ADR 0030, SAGA-985). A visible map goes idle within a frame or two
|
|
7
|
+
* of its first render, so three seconds covers a slow tile fetch on a weak connection
|
|
8
|
+
* without leaving a hidden or stalled map's drawing disconnected (no engine, no error,
|
|
9
|
+
* no hint) for longer than that; past it the loader's own probe and bound decide.
|
|
10
|
+
*/
|
|
11
|
+
export declare const DRAW_CONNECT_TIMEOUT_MS = 3000;
|
|
4
12
|
/**
|
|
5
13
|
* Configuration for Google Maps adapter
|
|
6
14
|
*/
|
|
@@ -36,6 +44,14 @@ export declare class GoogleMapsAdapterInstance implements AdapterInstance {
|
|
|
36
44
|
private readonly statusListeners;
|
|
37
45
|
private readonly eventListeners;
|
|
38
46
|
private googleEventListeners;
|
|
47
|
+
/**
|
|
48
|
+
* The one-shot `idle` hook and its fallback timer that connect the draw loader once
|
|
49
|
+
* Google has built the map, or after `DRAW_CONNECT_TIMEOUT_MS` if it never says so;
|
|
50
|
+
* whichever fires first connects once, and both are cancelled with the other map
|
|
51
|
+
* listeners so a map that is gone never connects a stale loader (ADR 0030, SAGA-985).
|
|
52
|
+
*/
|
|
53
|
+
private pendingDrawConnect;
|
|
54
|
+
private pendingDrawConnectTimer;
|
|
39
55
|
/** Throttled pointermove channel shared by map mousemove and shape overlay mousemove. */
|
|
40
56
|
private pointerMoveRaf;
|
|
41
57
|
private lastPointerPosition;
|
|
@@ -106,6 +122,7 @@ export declare class GoogleMapsAdapterInstance implements AdapterInstance {
|
|
|
106
122
|
/** Emit a throttled pointermove event (map canvas + shape overlays). */
|
|
107
123
|
private reportPointerMove;
|
|
108
124
|
private setupEventListeners;
|
|
125
|
+
private cancelPendingDrawConnect;
|
|
109
126
|
private updateState;
|
|
110
127
|
private mapTypeToGoogleMapTypeId;
|
|
111
128
|
private updateCameraCache;
|
package/src/constants.d.ts
CHANGED
|
@@ -15,5 +15,9 @@ export declare const GOOGLE_MAP_IDS: {
|
|
|
15
15
|
*
|
|
16
16
|
* Temporary: Google deletes numbered versions after ~3 quarters. Remove the pin once
|
|
17
17
|
* `HeatmapLayer` and `DrawingManager` usages are migrated to replacements.
|
|
18
|
+
*
|
|
19
|
+
* Checklist for every change of this version: draw a polygon on both the Create Site
|
|
20
|
+
* wizard map and the react-map Storybook `useDraw` story (why: ADR 0030, open items,
|
|
21
|
+
* SAGA-985).
|
|
18
22
|
*/
|
|
19
23
|
export declare const GOOGLE_MAPS_API_VERSION = "3.64";
|
|
@@ -1,11 +1,42 @@
|
|
|
1
1
|
import { type DrawEngineLoader } from "@trackunit/react-map-draw";
|
|
2
|
+
/**
|
|
3
|
+
* The engine could not be built because the element `terra-draw-google-maps-adapter`
|
|
4
|
+
* listens on is not in the map container. Surfaces as the `cause` of the
|
|
5
|
+
* `DrawController`'s `engine-load-failed` error (ADR 0030, open items, SAGA-985).
|
|
6
|
+
*/
|
|
7
|
+
export declare class GoogleDrawEventLayerMissingError extends Error {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* How long a map gets to render its panes after the probe first misses before the
|
|
12
|
+
* loader gives up. The loader runs once `GoogleMapsAdapterInstance` has connected it,
|
|
13
|
+
* on the map's first `idle` or after its own fallback timer; the observer below then
|
|
14
|
+
* resolves as soon as the panes appear, and on a hidden or zero-sized map they may never
|
|
15
|
+
* appear, so this bound is what keeps the loader promise settling and the controller's
|
|
16
|
+
* "loads again on the next mode or value change" true.
|
|
17
|
+
*/
|
|
18
|
+
export declare const PANES_RENDER_TIMEOUT_MS = 1000;
|
|
2
19
|
/**
|
|
3
20
|
* Builds the Google Maps bridge for the shared drawing engine (ADR 0030).
|
|
4
21
|
*
|
|
5
22
|
* One loader per `google.maps.Map` instance: theme switches and traffic toggles
|
|
6
|
-
* recreate the map, and `GoogleMapsAdapterInstance
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
23
|
+
* recreate the map, and `GoogleMapsAdapterInstance` hands the controller a fresh loader
|
|
24
|
+
* on each map's first `idle`, or after its `DRAW_CONNECT_TIMEOUT_MS` fallback for a map
|
|
25
|
+
* that never goes idle, so the engine is rebuilt on the live map once Google has built
|
|
26
|
+
* it. Terra Draw and its Google adapter are only downloaded when the loader runs, which
|
|
27
|
+
* the controller defers until drawing is actually needed.
|
|
28
|
+
*
|
|
29
|
+
* Readiness probe, the second line after that gate: before the engine is built,
|
|
30
|
+
* the loader looks for the event layer the adapter will listen on. A miss watches the
|
|
31
|
+
* map container for the layer to appear, bounded by `PANES_RENDER_TIMEOUT_MS`, and
|
|
32
|
+
* probes once more; a miss after that rejects with `GoogleDrawEventLayerMissingError`,
|
|
33
|
+
* which the `DrawController` reports as `engine-load-failed`, and the controller loads
|
|
34
|
+
* again on the next connect, mode or value change. There is no retry loop. The wait is
|
|
35
|
+
* not cancelled by a disconnect: the observer goes with the layer or the timer, and an
|
|
36
|
+
* engine built for a map the controller has left is destroyed by its generation check,
|
|
37
|
+
* the same cost as a slow chunk download. The failure path downloads the adapter chunk
|
|
38
|
+
* and constructs an adapter only to run its selector; that is accepted, because the
|
|
39
|
+
* selector is the adapter's to own and a copy of it here would drift from it. History,
|
|
40
|
+
* the Maps JS version checklist and the upstream fix: ADR 0030, open items (SAGA-985).
|
|
10
41
|
*/
|
|
11
42
|
export declare const createGoogleDrawEngineLoader: (map: google.maps.Map) => DrawEngineLoader;
|
package/src/google-maps.d.ts
CHANGED
|
@@ -9,3 +9,14 @@ declare global {
|
|
|
9
9
|
google: typeof google;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `anchorLeft` / `anchorTop` ship with Maps JS API 3.62+ (we load 3.64) but are missing from the
|
|
15
|
+
* pinned @types/google.maps 3.54. Identical declarations merge cleanly once the types are bumped.
|
|
16
|
+
*/
|
|
17
|
+
declare namespace google.maps.marker {
|
|
18
|
+
interface AdvancedMarkerElementOptions {
|
|
19
|
+
anchorLeft?: string | null;
|
|
20
|
+
anchorTop?: string | null;
|
|
21
|
+
}
|
|
22
|
+
}
|