@classic-homes/maps-svelte 0.6.0 → 0.6.1

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.
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy } from 'svelte';
2
+ import { getContext, onDestroy, untrack } from 'svelte';
3
3
  import type maplibregl from 'maplibre-gl';
4
4
  import type {
5
5
  GeoJSONFeatureCollection,
@@ -12,6 +12,7 @@
12
12
  fingerprintEqual,
13
13
  } from '@classic-homes/maps-core';
14
14
  import type { MapContextValue } from '../../composables/useMapContext.svelte';
15
+ import { getSourceRegistry } from './sourceRegistry.js';
15
16
 
16
17
  /**
17
18
  * Source component props - extends MapLibre source specifications
@@ -163,6 +164,7 @@
163
164
  } catch {
164
165
  // Ignore cleanup errors
165
166
  }
167
+ untrack(() => getSourceRegistry(map).removed(sourceId));
166
168
  return removed;
167
169
  }
168
170
 
@@ -305,6 +307,11 @@
305
307
  console.error(`[maps-svelte] Failed to restore layer "${spec.id}" on "${id}":`, err);
306
308
  }
307
309
  }
310
+
311
+ // Layers drawn from this id that are not back yet (they came off with a
312
+ // source this or another Source removed) add themselves on the new
313
+ // version; the ones restored above are found in place
314
+ untrack(() => getSourceRegistry(map).added(id));
308
315
  } catch (err) {
309
316
  console.error(`[maps-svelte] Failed to add source "${id}":`, err);
310
317
  }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Which sources each map holds, by id, with a version that changes every time
3
+ * Source adds one. Removing a source removes the layers drawn from it: on an id
4
+ * change, or when a Source unmounts and mounts again. A layer reads the version
5
+ * for its own `source` id, so it adds itself back once a source with that id is
6
+ * on the map again.
7
+ *
8
+ * Keyed by the map instance rather than held in context, so any provider of
9
+ * the map context (Map, or a test's) shares one registry per map.
10
+ */
11
+ export interface SourceRegistry {
12
+ /** The current version for a source id (reactive); 0 when Source has not added it */
13
+ version(id: string): number;
14
+ /** Source added `id` to the map */
15
+ added(id: string): void;
16
+ /** Source removed `id` from the map */
17
+ removed(id: string): void;
18
+ }
19
+ /** The registry for one map instance, created on first use */
20
+ export declare function getSourceRegistry(map: object): SourceRegistry;
@@ -0,0 +1,26 @@
1
+ import { SvelteMap } from 'svelte/reactivity';
2
+ function createSourceRegistry() {
3
+ const versions = new SvelteMap();
4
+ // One counter for every id, so a remove followed by an add never lands back
5
+ // on the version a layer last saw
6
+ let counter = 0;
7
+ return {
8
+ version: (id) => versions.get(id) ?? 0,
9
+ added(id) {
10
+ versions.set(id, ++counter);
11
+ },
12
+ removed(id) {
13
+ versions.delete(id);
14
+ },
15
+ };
16
+ }
17
+ const registries = new WeakMap();
18
+ /** The registry for one map instance, created on first use */
19
+ export function getSourceRegistry(map) {
20
+ let registry = registries.get(map);
21
+ if (!registry) {
22
+ registry = createSourceRegistry();
23
+ registries.set(map, registry);
24
+ }
25
+ return registry;
26
+ }
@@ -1,5 +1,6 @@
1
1
  import { getContext, onDestroy } from 'svelte';
2
2
  import { getChangedPaintProps, serializeFilter } from '@classic-homes/maps-core';
3
+ import { getSourceRegistry } from '../core/sourceRegistry.js';
3
4
  /**
4
5
  * Svelte 5 composable to manage layer lifecycle and events
5
6
  */
@@ -14,6 +15,9 @@ export function useLayer(getOptions) {
14
15
  // it was built from. Plain variables: they are bookkeeping, not reactive state.
15
16
  let addedLayerId = null;
16
17
  let addedLayerKey = null;
18
+ // The source registry version the layer was added against. A newer one means
19
+ // its source was re-created, which took the layer off the map with it.
20
+ let addedSourceVersion = 0;
17
21
  // Track last applied values to prevent redundant MapLibre API calls
18
22
  let lastAppliedVisible = undefined;
19
23
  let lastAppliedPaint = undefined;
@@ -58,6 +62,7 @@ export function useLayer(getOptions) {
58
62
  }
59
63
  addedLayerId = null;
60
64
  addedLayerKey = null;
65
+ addedSourceVersion = 0;
61
66
  }
62
67
  const createFeatureParams = (e, options, map) => {
63
68
  const feature = e.features?.[0];
@@ -91,8 +96,19 @@ export function useLayer(getOptions) {
91
96
  layerAdded = false;
92
97
  resetAppliedState();
93
98
  }
99
+ // Changes each time a Source adds this layer's source (see sourceRegistry),
100
+ // so the effect re-runs when the source is back, wherever the Source sits
101
+ const sourceVersion = getSourceRegistry(map).version(source);
94
102
  // Check if source is available
95
103
  if (!map.getSource(source)) {
104
+ // The source went away and took the layer with it (a Source id change or
105
+ // unmount): forget it, so the layer is added again when the source returns
106
+ if (addedLayerId !== null && !map.getLayer(addedLayerId)) {
107
+ addedLayerId = null;
108
+ addedLayerKey = null;
109
+ layerAdded = false;
110
+ resetAppliedState();
111
+ }
96
112
  let timeoutId = null;
97
113
  const checkSource = () => {
98
114
  if (map.getSource(source)) {
@@ -127,15 +143,25 @@ export function useLayer(getOptions) {
127
143
  }
128
144
  addLayer();
129
145
  function addLayer() {
130
- if (layerAdded || map.getLayer(id)) {
146
+ // A newer source version than the one the layer was added against: the
147
+ // source was re-created, and the layer must go back on unless Source
148
+ // already restored it
149
+ const sourceRecreated = addedLayerId !== null && addedSourceVersion !== sourceVersion;
150
+ // layerAdded is read first, always: it is what re-runs this effect after
151
+ // a style change, and short-circuiting past it drops that dependency
152
+ if ((layerAdded && !sourceRecreated) || map.getLayer(id)) {
131
153
  // Already on the map: a styleVersion bump can land after the layer
132
154
  // survived the style swap, or a second bump after the first re-added
133
155
  // it. Adopt it; setting layerAdded re-attaches the handlers.
134
156
  addedLayerId = id;
135
157
  addedLayerKey = layerKey;
158
+ addedSourceVersion = sourceVersion;
136
159
  layerAdded = true;
137
160
  return;
138
161
  }
162
+ // The re-created source has no feature state, hovered or otherwise
163
+ if (sourceRecreated)
164
+ hoveredFeatureId = null;
139
165
  try {
140
166
  const layerConfig = {
141
167
  id,
@@ -162,6 +188,7 @@ export function useLayer(getOptions) {
162
188
  map.addLayer(layerConfig, beforeId);
163
189
  addedLayerId = id;
164
190
  addedLayerKey = layerKey;
191
+ addedSourceVersion = sourceVersion;
165
192
  layerAdded = true;
166
193
  }
167
194
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-svelte",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Svelte MapLibre components for Classic Theme",
5
5
  "keywords": [
6
6
  "maps",