@emuanalytics/flow-rdf-ui 4.3.1 → 4.4.0

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.
@@ -23,6 +23,9 @@ let mapIdCounter = 0;
23
23
  class FlowUiMap extends FlowRdfElement {
24
24
  constructor() {
25
25
  super();
26
+ // Builds that `destroy()` abandoned, until they have destroyed their map
27
+ this._abandonedBuilds = /* @__PURE__ */ new Set();
28
+ this._initializedResolved = false;
26
29
  this._mapLoading = false;
27
30
  this.baseStyle = "fjord";
28
31
  this.center = [-0.1, 51.5];
@@ -31,9 +34,8 @@ class FlowUiMap extends FlowRdfElement {
31
34
  this.fixed = false;
32
35
  this.bearingSnap = 7;
33
36
  this.fadeDuration = 300;
34
- this._initialized = new Promise((resolve) => {
35
- this._resolveInitialized = resolve;
36
- });
37
+ this.keepAlive = false;
38
+ this.resetInitialized();
37
39
  }
38
40
  /**
39
41
  * The underlying core FlowCoreMap instance. Note that this is
@@ -76,24 +78,94 @@ class FlowUiMap extends FlowRdfElement {
76
78
  super.connectedCallback();
77
79
  this.busy = boolean(this.busy, true);
78
80
  this.fixed = boolean(this.fixed, true);
81
+ this.keepAlive = boolean(this.keepAlive, true);
82
+ if (this._map || this._build) {
83
+ return;
84
+ }
85
+ const build = {};
86
+ this._build = build;
79
87
  await this.updateComplete;
80
- this._map = await this.initializeMap();
81
- this._resolveInitialized(this._map);
82
- this.emit("flow-ui-map-load", { detail: { map: this._map } });
88
+ if (this._build !== build) {
89
+ return;
90
+ }
91
+ const done = this.initializeMap(build);
92
+ build.done = done;
93
+ const map = await done;
94
+ if (!map) {
95
+ return;
96
+ }
97
+ this._build = void 0;
98
+ this._map = map;
99
+ this._initializedResolved = true;
100
+ this._resolveInitialized(map);
101
+ this.emit("flow-ui-map-load", { detail: { map } });
83
102
  }
84
103
  disconnectedCallback() {
85
104
  super.disconnectedCallback();
86
- if (this._map) {
87
- this._map.destroy();
88
- this._map = void 0;
105
+ if (!boolean(this.keepAlive, true)) {
106
+ this.destroy();
107
+ } else {
108
+ this._map?.maplibreMap.getCanvasContainer().dispatchEvent(new MouseEvent("mouseout"));
89
109
  }
90
110
  }
91
- async initializeMap() {
111
+ /**
112
+ * Destroys the map, which frees its WebGL context and stops it following application state, and removes its
113
+ * container from the element. `map` is then `undefined` and `initialized` waits for a new map. A map that is still
114
+ * being built is destroyed as soon as it exists, with no `flow-ui-map-load` event.
115
+ *
116
+ * The element builds a new map when it is next connected to the document, not before. Call this method when the
117
+ * application discards an element with `keep-alive`; without it, the element calls it when it is removed from the
118
+ * document. Calling it on an element with no map does nothing.
119
+ */
120
+ destroy() {
121
+ const abandoned = this._build;
122
+ this._build = void 0;
123
+ if (abandoned?.done) {
124
+ this._abandonedBuilds.add(abandoned);
125
+ const forget = () => this._abandonedBuilds.delete(abandoned);
126
+ abandoned.done.then(forget, forget);
127
+ }
128
+ this._mapContainer?.remove();
129
+ this._mapContainer = void 0;
130
+ const map = this._map;
131
+ if (!map) {
132
+ return;
133
+ }
134
+ this._map = void 0;
135
+ map.destroy();
136
+ this._mapLoading = false;
137
+ if (this._initializedResolved) {
138
+ this.resetInitialized();
139
+ }
140
+ this._pendingCenter = void 0;
141
+ this.handleCenterChange();
142
+ }
143
+ resetInitialized() {
144
+ this._initializedResolved = false;
145
+ this._initialized = new Promise((resolve) => {
146
+ this._resolveInitialized = resolve;
147
+ });
148
+ }
149
+ /**
150
+ * Builds a map in a new container in the map slot. Resolves to `undefined`, with the map destroyed, if `destroy()`
151
+ * abandoned the build while the map was being created.
152
+ */
153
+ async initializeMap(build) {
92
154
  const mapContainer = document.createElement("div");
93
155
  mapContainer.slot = "map";
94
156
  this.appendChild(mapContainer);
157
+ this._mapContainer = mapContainer;
158
+ const id = this.mapId ?? `flow-map-${mapIdCounter++}`;
159
+ build.id = id;
160
+ const sameId = [...this._abandonedBuilds].filter((abandoned) => abandoned.id === id);
161
+ if (sameId.length) {
162
+ await Promise.allSettled(sameId.map((abandoned) => abandoned.done));
163
+ if (this._build !== build) {
164
+ return void 0;
165
+ }
166
+ }
95
167
  const map = new FlowCoreMap({
96
- id: this.mapId ?? `flow-map-${mapIdCounter++}`,
168
+ id,
97
169
  container: mapContainer,
98
170
  baseStyle: this.baseStyle,
99
171
  center: this.center,
@@ -102,6 +174,10 @@ class FlowUiMap extends FlowRdfElement {
102
174
  fadeDuration: this.fadeDuration
103
175
  });
104
176
  await map.initialized;
177
+ if (this._build !== build) {
178
+ map.destroy();
179
+ return void 0;
180
+ }
105
181
  if (this._pendingCenter) {
106
182
  map.setCenter(this._pendingCenter);
107
183
  this._pendingCenter = void 0;
@@ -207,6 +283,9 @@ __decorateClass([
207
283
  __decorateClass([
208
284
  property({ type: Number, attribute: "fade-duration" })
209
285
  ], FlowUiMap.prototype, "fadeDuration", 2);
286
+ __decorateClass([
287
+ property({ type: Boolean, attribute: "keep-alive" })
288
+ ], FlowUiMap.prototype, "keepAlive", 2);
210
289
  __decorateClass([
211
290
  watch(["center", "zoom", "bearing", "pitch"])
212
291
  ], FlowUiMap.prototype, "handleCenterChange", 1);