MCPcopy
hub / github.com/visgl/deck.gl / resolveLayerGroups

Function resolveLayerGroups

modules/mapbox/src/resolve-layer-groups.ts:26–88  ·  view source on GitHub ↗
(map?: Map, oldLayers?: LayersList, newLayers?: LayersList)

Source from the content-addressed store, hash-verified

24 **/
25// eslint-disable-next-line complexity, max-statements
26export function resolveLayerGroups(map?: Map, oldLayers?: LayersList, newLayers?: LayersList) {
27 // Wait until map style is loaded
28 // @ts-ignore non-public map property
29 if (!map || !map.style || !map.style._loaded) {
30 return;
31 }
32
33 const layers = flatten(newLayers, Boolean) as Layer<LayerOverlayProps>[];
34
35 if (oldLayers !== newLayers) {
36 // Step 1: remove "group" layers that no longer exist
37 const prevLayers = flatten(oldLayers, Boolean) as Layer<LayerOverlayProps>[];
38 const prevLayerGroupIds = new Set<string>(prevLayers.map(l => getLayerGroupId(l)));
39 const newLayerGroupIds = new Set<string>(layers.map(l => getLayerGroupId(l)));
40
41 for (const groupId of prevLayerGroupIds) {
42 if (!newLayerGroupIds.has(groupId)) {
43 if (map.getLayer(groupId)) {
44 map.removeLayer(groupId);
45 }
46 }
47 }
48 }
49
50 // Step 2: add missing "group" layers
51 const layerGroups: Record<string, MapboxLayerGroup> = {};
52 for (const layer of layers) {
53 const groupId = getLayerGroupId(layer);
54 const mapboxGroup = map.getLayer(groupId) as MapboxLayerGroup;
55 if (mapboxGroup) {
56 // Mapbox's map.getLayer() had a breaking change in v3.6.0, see https://github.com/visgl/deck.gl/issues/9086
57 // @ts-expect-error not typed
58 const groupInstance = mapboxGroup.implementation || mapboxGroup;
59 layerGroups[groupId] = groupInstance;
60 } else {
61 const newGroup = new MapboxLayerGroup({
62 id: groupId,
63 slot: layer.props.slot,
64 beforeId: layer.props.beforeId
65 });
66 layerGroups[groupId] = newGroup;
67 map.addLayer(newGroup, layer.props.beforeId);
68 }
69 }
70
71 // Step 3: check the order of layers
72 // If beforeId move "group" layers to proper position in the mapbox layer order
73 // @ts-ignore non-public map property
74 const mapLayers: string[] = map.style._order;
75
76 for (const [groupId, group] of Object.entries(layerGroups)) {
77 const beforeId = group.beforeId || UNDEFINED_BEFORE_ID;
78
79 const expectedGroupIndex =
80 beforeId === UNDEFINED_BEFORE_ID ? mapLayers.length : mapLayers.indexOf(beforeId);
81
82 const currentGropupIndex = mapLayers.indexOf(groupId);
83 if (currentGropupIndex !== expectedGroupIndex - 1) {

Callers 2

_resolveLayersMethod · 0.90

Calls 7

flattenFunction · 0.85
getLayerGroupIdFunction · 0.85
hasMethod · 0.80
getLayerMethod · 0.65
removeLayerMethod · 0.65
addLayerMethod · 0.65
moveLayerMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…