@sjvair/monitor-map 4.0.3 → 4.2.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.
- package/README.md +94 -14
- package/dist/lib/LazyMapLayout.svelte +53 -0
- package/dist/lib/LazyMapLayout.svelte.d.ts +7 -0
- package/dist/lib/LazyMonitorDetailPanel.svelte +33 -0
- package/dist/lib/LazyMonitorDetailPanel.svelte.d.ts +4 -0
- package/dist/lib/LoadError.svelte +19 -0
- package/dist/lib/LoadError.svelte.d.ts +7 -0
- package/dist/lib/LoadScreen.svelte +10 -1
- package/dist/lib/LoadScreen.svelte.d.ts +6 -16
- package/dist/lib/MonitorDetailPanel.svelte +108 -28
- package/dist/lib/MonitorMapLayout.svelte +21 -1
- package/dist/lib/collocation-sites/collocations-map-integration.svelte.js +1 -1
- package/dist/lib/core.d.ts +12 -0
- package/dist/lib/core.js +10 -0
- package/dist/lib/data-chart/DataChart.svelte +63 -13
- package/dist/lib/data-chart/DataChart.svelte.d.ts +10 -5
- package/dist/lib/data-chart/DateRange.d.ts +4 -0
- package/dist/lib/data-chart/DateRange.js +9 -1
- package/dist/lib/data-chart/chart-utils.d.ts +2 -1
- package/dist/lib/data-chart/chart-utils.js +19 -23
- package/dist/lib/data-chart/level-colors.d.ts +6 -0
- package/dist/lib/data-chart/level-colors.js +5 -0
- package/dist/lib/data-chart/requests.d.ts +5 -2
- package/dist/lib/data-chart/requests.js +2 -3
- package/dist/lib/data-chart/tooltip.d.ts +2 -1
- package/dist/lib/data-chart/tooltip.js +1 -9
- package/dist/lib/ev-stations/ev-stations-cluster-renderer.js +1 -1
- package/dist/lib/ev-stations/ev-stations-map-integration.svelte.js +5 -7
- package/dist/lib/hms/hms-fire-map-integration.svelte.js +1 -1
- package/dist/lib/hms/hms-smoke-map-integration.svelte.js +1 -1
- package/dist/lib/index.d.ts +8 -3
- package/dist/lib/index.js +6 -1
- package/dist/lib/lazy-chunks.d.ts +2 -0
- package/dist/lib/lazy-chunks.js +6 -0
- package/dist/lib/lazy-routes.d.ts +12 -0
- package/dist/lib/lazy-routes.js +18 -0
- package/dist/lib/map/Map.svelte +1 -1
- package/dist/lib/map/MapShell.svelte +9 -3
- package/dist/lib/map/MapShell.svelte.d.ts +5 -0
- package/dist/lib/map/integrations/click-manager.js +1 -1
- package/dist/lib/map/integrations/map-geojson-integration.svelte.d.ts +13 -0
- package/dist/lib/map/integrations/map-geojson-integration.svelte.js +31 -5
- package/dist/lib/map/integrations/map-icon-manager.js +1 -1
- package/dist/lib/map/integrations/map-layer-integration.svelte.js +1 -1
- package/dist/lib/map/integrations/tooltip.svelte.js +1 -1
- package/dist/lib/map/map-style-state.svelte.d.ts +10 -0
- package/dist/lib/map/map-style-state.svelte.js +57 -6
- package/dist/lib/monitors/components/MonitorClusterTooltip.svelte +1 -1
- package/dist/lib/monitors/monitor-details.svelte.d.ts +15 -0
- package/dist/lib/monitors/monitor-details.svelte.js +63 -0
- package/dist/lib/monitors/monitors-cluster-renderer.js +1 -1
- package/dist/lib/monitors/monitors-icon-manager.svelte.js +1 -1
- package/dist/lib/monitors/monitors-map-integration.svelte.js +5 -7
- package/dist/lib/monitors/monitors.svelte.d.ts +22 -1
- package/dist/lib/monitors/monitors.svelte.js +75 -8
- package/dist/lib/monitors/panel-pollutant.d.ts +37 -0
- package/dist/lib/monitors/panel-pollutant.js +43 -0
- package/dist/lib/options/OptionsBar.svelte +8 -1
- package/dist/lib/options/OptionsMenu.svelte +10 -1
- package/dist/lib/search/Search.svelte +1 -1
- package/dist/lib/shared-load.d.ts +5 -0
- package/dist/lib/shared-load.js +14 -0
- package/package.json +13 -2
- package/src/lib/LazyMapLayout.svelte +53 -0
- package/src/lib/LazyMonitorDetailPanel.svelte +33 -0
- package/src/lib/LoadError.svelte +19 -0
- package/src/lib/LoadScreen.svelte +10 -1
- package/src/lib/MonitorDetailPanel.svelte +108 -28
- package/src/lib/MonitorMapLayout.svelte +21 -1
- package/src/lib/collocation-sites/collocations-map-integration.svelte.ts +1 -1
- package/src/lib/core.ts +17 -0
- package/src/lib/data-chart/DataChart.svelte +63 -13
- package/src/lib/data-chart/DateRange.ts +11 -1
- package/src/lib/data-chart/chart-utils.ts +23 -26
- package/src/lib/data-chart/level-colors.ts +13 -0
- package/src/lib/data-chart/requests.ts +9 -5
- package/src/lib/data-chart/tooltip.ts +2 -10
- package/src/lib/ev-stations/ev-stations-cluster-renderer.ts +1 -1
- package/src/lib/ev-stations/ev-stations-map-integration.svelte.ts +5 -5
- package/src/lib/hms/hms-fire-map-integration.svelte.ts +1 -1
- package/src/lib/hms/hms-smoke-map-integration.svelte.ts +1 -1
- package/src/lib/index.ts +8 -3
- package/src/lib/lazy-chunks.ts +7 -0
- package/src/lib/lazy-routes.ts +20 -0
- package/src/lib/map/Map.svelte +1 -1
- package/src/lib/map/MapShell.svelte +9 -3
- package/src/lib/map/integrations/click-manager.ts +1 -1
- package/src/lib/map/integrations/map-geojson-integration.svelte.ts +33 -4
- package/src/lib/map/integrations/map-icon-manager.ts +1 -1
- package/src/lib/map/integrations/map-layer-integration.svelte.ts +1 -1
- package/src/lib/map/integrations/tooltip.svelte.ts +1 -1
- package/src/lib/map/map-style-state.svelte.ts +59 -7
- package/src/lib/monitors/components/MonitorClusterTooltip.svelte +1 -1
- package/src/lib/monitors/monitor-details.svelte.ts +66 -0
- package/src/lib/monitors/monitors-cluster-renderer.ts +1 -1
- package/src/lib/monitors/monitors-icon-manager.svelte.ts +1 -1
- package/src/lib/monitors/monitors-map-integration.svelte.ts +5 -5
- package/src/lib/monitors/monitors.svelte.ts +93 -10
- package/src/lib/monitors/panel-pollutant.ts +76 -0
- package/src/lib/options/OptionsBar.svelte +8 -1
- package/src/lib/options/OptionsMenu.svelte +10 -1
- package/src/lib/search/Search.svelte +1 -1
- package/src/lib/shared-load.ts +15 -0
- /package/dist/lib/map/{map.svelte.d.ts → map-manager.svelte.d.ts} +0 -0
- /package/dist/lib/map/{map.svelte.js → map-manager.svelte.js} +0 -0
- /package/src/lib/map/{map.svelte.ts → map-manager.svelte.ts} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MapLayerIntegration } from "./map-layer-integration.svelte";
|
|
2
|
-
import { mapManager } from "../map.svelte";
|
|
2
|
+
import { mapManager } from "../map-manager.svelte.js";
|
|
3
3
|
export class MapGeoJSONIntegration extends MapLayerIntegration {
|
|
4
4
|
remove() {
|
|
5
5
|
this.removeLayerAndSource();
|
|
@@ -19,16 +19,42 @@ export class MapGeoJSONIntegration extends MapLayerIntegration {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
export class MapIconLayerIntegration extends MapGeoJSONIntegration {
|
|
22
|
+
generation = 0;
|
|
23
|
+
/**
|
|
24
|
+
* Drops every callback still waiting on icons. `apply()` and `remove()` call it, so a
|
|
25
|
+
* subclass overriding `apply()` without calling `super.apply()` first must call it itself.
|
|
26
|
+
*/
|
|
27
|
+
supersedePending() {
|
|
28
|
+
this.generation++;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Runs `run` once the icons have loaded, unless an `apply()` or `remove()` has happened
|
|
32
|
+
* since (the state it was scheduled for is gone) or a basemap change is in flight (the
|
|
33
|
+
* refresh on "style.load" re-applies).
|
|
34
|
+
*/
|
|
35
|
+
whenIconsLoad(run) {
|
|
36
|
+
const generation = this.generation;
|
|
37
|
+
this.icons
|
|
38
|
+
.loadIcons()
|
|
39
|
+
.then(() => {
|
|
40
|
+
if (generation !== this.generation || mapManager.styleLoading)
|
|
41
|
+
return;
|
|
42
|
+
run();
|
|
43
|
+
})
|
|
44
|
+
.catch(console.error);
|
|
45
|
+
}
|
|
22
46
|
apply() {
|
|
23
47
|
if (!mapManager.map)
|
|
24
48
|
return;
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
if (mapManager.styleLoading)
|
|
28
|
-
return;
|
|
49
|
+
this.supersedePending();
|
|
50
|
+
this.whenIconsLoad(() => {
|
|
29
51
|
this.removeLayerAndSource();
|
|
30
52
|
mapManager.map?.addSource(this.referenceId, this.mapSource);
|
|
31
53
|
super.apply();
|
|
32
54
|
});
|
|
33
55
|
}
|
|
56
|
+
remove() {
|
|
57
|
+
this.supersedePending();
|
|
58
|
+
super.remove();
|
|
59
|
+
}
|
|
34
60
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { untrack } from "svelte";
|
|
2
|
-
import { mapManager } from "../map.svelte";
|
|
2
|
+
import { mapManager } from "../map-manager.svelte.js";
|
|
3
3
|
import { MapIntegration } from "./map-integration.svelte";
|
|
4
4
|
export class MapLayerIntegration extends MapIntegration {
|
|
5
5
|
beforeLayer;
|
|
@@ -4,10 +4,20 @@ declare class MapStyleState {
|
|
|
4
4
|
referenceStyle: ReferenceMapStyle;
|
|
5
5
|
variant: MapStyleVariant;
|
|
6
6
|
private applied;
|
|
7
|
+
private loaded;
|
|
8
|
+
private stopWaiting;
|
|
7
9
|
selectReferenceStyle(style: ReferenceMapStyle): void;
|
|
8
10
|
selectVariant(variant: MapStyleVariant): void;
|
|
9
11
|
reset(): void;
|
|
10
12
|
private apply;
|
|
13
|
+
private load;
|
|
14
|
+
/**
|
|
15
|
+
* A style that fails to load leaves the map with no style at all, so go back to the last one
|
|
16
|
+
* that loaded and make the selection say so. When that one fails too (e.g. offline) there is
|
|
17
|
+
* nothing to draw on: `styleLoading` stays set, since integrations cannot add layers to a
|
|
18
|
+
* missing style, and the next selection starts a fresh attempt.
|
|
19
|
+
*/
|
|
20
|
+
private recover;
|
|
11
21
|
}
|
|
12
22
|
export declare const mapStyleState: MapStyleState;
|
|
13
23
|
export type { MapStyleState };
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { DefaultMapStyle, mapManager } from "./map.svelte";
|
|
1
|
+
import { DefaultMapStyle, mapManager } from "./map-manager.svelte.js";
|
|
2
2
|
import { integrationsManager } from "./integrations/integrations-manager";
|
|
3
3
|
import { variantForStyle } from "./basemap";
|
|
4
4
|
/** Basemap + theme selection, shared so the Settings menu and Reset Map Options agree. */
|
|
5
5
|
class MapStyleState {
|
|
6
6
|
referenceStyle = $state.raw(DefaultMapStyle.getReferenceStyle());
|
|
7
7
|
variant = $state.raw(DefaultMapStyle);
|
|
8
|
+
// The variant last handed to setStyle (loaded or still loading); null once a load has failed
|
|
9
|
+
// with nothing left to fall back to, so that any later selection is tried again.
|
|
8
10
|
applied = DefaultMapStyle;
|
|
11
|
+
// The variant the map last finished loading, which a failed change falls back to.
|
|
12
|
+
loaded = DefaultMapStyle;
|
|
13
|
+
stopWaiting = null;
|
|
9
14
|
selectReferenceStyle(style) {
|
|
10
15
|
this.referenceStyle = style;
|
|
11
16
|
this.selectVariant(variantForStyle(style, this.variant));
|
|
@@ -19,17 +24,63 @@ class MapStyleState {
|
|
|
19
24
|
this.selectVariant(DefaultMapStyle);
|
|
20
25
|
}
|
|
21
26
|
apply() {
|
|
27
|
+
if (!mapManager.map || this.variant === this.applied)
|
|
28
|
+
return;
|
|
29
|
+
this.load(this.variant);
|
|
30
|
+
}
|
|
31
|
+
load(variant) {
|
|
22
32
|
const map = mapManager.map;
|
|
23
|
-
if (!map
|
|
33
|
+
if (!map)
|
|
24
34
|
return;
|
|
25
|
-
|
|
35
|
+
// A newer change supersedes one still in flight.
|
|
36
|
+
this.stopWaiting?.();
|
|
37
|
+
this.applied = variant;
|
|
26
38
|
// setStyle wipes every custom source/layer; re-add enabled integrations once it loads.
|
|
27
|
-
|
|
39
|
+
const onLoad = () => {
|
|
40
|
+
this.stopWaiting?.();
|
|
41
|
+
this.loaded = variant;
|
|
28
42
|
mapManager.styleLoading = false;
|
|
29
43
|
integrationsManager.refresh();
|
|
30
|
-
}
|
|
44
|
+
};
|
|
45
|
+
// "error" also fires for tiles, sprites and glyphs; only the style document failing means
|
|
46
|
+
// the change failed.
|
|
47
|
+
const onError = (event) => {
|
|
48
|
+
if (!isRequestFor(event.error, variant.getExpandedStyleURL()))
|
|
49
|
+
return;
|
|
50
|
+
this.stopWaiting?.();
|
|
51
|
+
this.recover(variant);
|
|
52
|
+
};
|
|
53
|
+
this.stopWaiting = () => {
|
|
54
|
+
map.off("style.load", onLoad);
|
|
55
|
+
map.off("error", onError);
|
|
56
|
+
this.stopWaiting = null;
|
|
57
|
+
};
|
|
58
|
+
map.on("style.load", onLoad);
|
|
59
|
+
map.on("error", onError);
|
|
31
60
|
mapManager.styleLoading = true;
|
|
32
|
-
map.setStyle(
|
|
61
|
+
map.setStyle(variant);
|
|
33
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* A style that fails to load leaves the map with no style at all, so go back to the last one
|
|
65
|
+
* that loaded and make the selection say so. When that one fails too (e.g. offline) there is
|
|
66
|
+
* nothing to draw on: `styleLoading` stays set, since integrations cannot add layers to a
|
|
67
|
+
* missing style, and the next selection starts a fresh attempt.
|
|
68
|
+
*/
|
|
69
|
+
recover(failed) {
|
|
70
|
+
if (failed === this.loaded) {
|
|
71
|
+
this.applied = null;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.variant = this.loaded;
|
|
75
|
+
this.referenceStyle = this.loaded.getReferenceStyle();
|
|
76
|
+
this.load(this.loaded);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Whether a map "error" event's error is a failed request for `url`, ignoring the query. */
|
|
80
|
+
function isRequestFor(error, url) {
|
|
81
|
+
const failed = error?.url;
|
|
82
|
+
if (typeof failed !== "string")
|
|
83
|
+
return false;
|
|
84
|
+
return failed.split("?")[0] === url.split("?")[0];
|
|
34
85
|
}
|
|
35
86
|
export const mapStyleState = new MapStyleState();
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { area } from "@turf/area";
|
|
7
7
|
import { convertArea } from "@turf/helpers";
|
|
8
8
|
import DataBox from "../../components/DataBox.svelte";
|
|
9
|
-
import { mapManager } from "../../map/map.svelte";
|
|
9
|
+
import { mapManager } from "../../map/map-manager.svelte.js";
|
|
10
10
|
import type { MonitorClusterMapFeature, MonitorMapFeature } from "../types";
|
|
11
11
|
import { monitorsManager } from "../monitors.svelte";
|
|
12
12
|
import { getCurrentLevel } from "../monitor-utils";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type MonitorDetails } from "@sjvair/sdk";
|
|
2
|
+
export type MonitorDetailsStatus = "loading" | "ready" | "not-found" | "failed";
|
|
3
|
+
/**
|
|
4
|
+
* One monitor, fetched by id. Unlike `monitorsManager.latest`, this does not depend on the
|
|
5
|
+
* map's pollutant or on the monitor being active, so the detail panel can show any monitor.
|
|
6
|
+
*/
|
|
7
|
+
export declare class MonitorDetailsState {
|
|
8
|
+
#private;
|
|
9
|
+
monitor: MonitorDetails | null;
|
|
10
|
+
status: MonitorDetailsStatus;
|
|
11
|
+
load(id: string): Promise<void>;
|
|
12
|
+
/** Re-fetches the monitor already shown. A failure keeps the data on screen. */
|
|
13
|
+
refresh(): Promise<void>;
|
|
14
|
+
retry(): Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { getMonitorDetails } from "@sjvair/sdk";
|
|
2
|
+
/**
|
|
3
|
+
* One monitor, fetched by id. Unlike `monitorsManager.latest`, this does not depend on the
|
|
4
|
+
* map's pollutant or on the monitor being active, so the detail panel can show any monitor.
|
|
5
|
+
*/
|
|
6
|
+
export class MonitorDetailsState {
|
|
7
|
+
monitor = $state.raw(null);
|
|
8
|
+
status = $state("loading");
|
|
9
|
+
#id = null;
|
|
10
|
+
// Bumped per request; a response for anything but the latest request is dropped.
|
|
11
|
+
#request = 0;
|
|
12
|
+
async load(id) {
|
|
13
|
+
this.#id = id;
|
|
14
|
+
const request = ++this.#request;
|
|
15
|
+
this.monitor = null;
|
|
16
|
+
this.status = "loading";
|
|
17
|
+
try {
|
|
18
|
+
const monitor = await getMonitorDetails(id);
|
|
19
|
+
if (request !== this.#request)
|
|
20
|
+
return;
|
|
21
|
+
this.monitor = monitor;
|
|
22
|
+
this.status = "ready";
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (request !== this.#request)
|
|
26
|
+
return;
|
|
27
|
+
if (isNotFound(error)) {
|
|
28
|
+
this.status = "not-found";
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
console.error(error);
|
|
32
|
+
this.status = "failed";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Re-fetches the monitor already shown. A failure keeps the data on screen. */
|
|
36
|
+
async refresh() {
|
|
37
|
+
if (this.status !== "ready" || this.#id === null)
|
|
38
|
+
return;
|
|
39
|
+
const request = ++this.#request;
|
|
40
|
+
try {
|
|
41
|
+
const monitor = await getMonitorDetails(this.#id);
|
|
42
|
+
if (request !== this.#request)
|
|
43
|
+
return;
|
|
44
|
+
this.monitor = monitor;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (request === this.#request)
|
|
48
|
+
console.error(error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
retry() {
|
|
52
|
+
return this.#id === null ? Promise.resolve() : this.load(this.#id);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The SDK rejects a failed request with `cause.result.status` set to the HTTP status, or,
|
|
57
|
+
* when it could not parse the error body, with the `Response` itself as the cause.
|
|
58
|
+
*/
|
|
59
|
+
function isNotFound(error) {
|
|
60
|
+
const cause = error
|
|
61
|
+
?.cause;
|
|
62
|
+
return cause?.result?.status === 404 || cause?.status === 404;
|
|
63
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asDataURI, circle, square, triangle } from "../map/icons";
|
|
2
2
|
import { MapIconManager } from "../map/integrations/map-icon-manager";
|
|
3
|
-
import { mapManager } from "../map/map.svelte";
|
|
3
|
+
import { mapManager } from "../map/map-manager.svelte.js";
|
|
4
4
|
const MONITOR_ICONS = { circle, square, triangle };
|
|
5
5
|
const MONITOR_ICON_BORDER_WIDTH = 2;
|
|
6
6
|
const MONITOR_ICON_DEFAULT_COLOR = "#969696"; // light gray
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { untrack } from "svelte";
|
|
2
|
-
import { mapManager } from "../map/map.svelte";
|
|
2
|
+
import { mapManager } from "../map/map-manager.svelte.js";
|
|
3
3
|
import { clickManager } from "../map/integrations/click-manager";
|
|
4
4
|
import { MapIconLayerIntegration } from "../map/integrations/map-geojson-integration.svelte";
|
|
5
5
|
import { monitorsManager } from "./monitors.svelte";
|
|
@@ -257,9 +257,9 @@ class MonitorsMapIntegration extends MapIconLayerIntegration {
|
|
|
257
257
|
clickManager.unregister([this.referenceId]);
|
|
258
258
|
this.tooltipManager.disable();
|
|
259
259
|
this.renderer.remove();
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
260
|
+
// Clustering may be toggled again before the icons arrive; only the latest apply runs.
|
|
261
|
+
this.supersedePending();
|
|
262
|
+
this.whenIconsLoad(() => {
|
|
263
263
|
this.renderer.apply(this.handleMonitorClick);
|
|
264
264
|
this.tooltipManager.enable();
|
|
265
265
|
this.applySelectedState();
|
|
@@ -271,9 +271,7 @@ class MonitorsMapIntegration extends MapIconLayerIntegration {
|
|
|
271
271
|
this.tooltipManager.enable();
|
|
272
272
|
clickManager.unregister([this.referenceId]);
|
|
273
273
|
super.apply();
|
|
274
|
-
this.
|
|
275
|
-
if (mapManager.styleLoading)
|
|
276
|
-
return;
|
|
274
|
+
this.whenIconsLoad(() => {
|
|
277
275
|
clickManager.register([this.referenceId], this.handleMonitorClick);
|
|
278
276
|
this.applySelectedState();
|
|
279
277
|
});
|
|
@@ -2,7 +2,15 @@ import { type MonitorData, type MonitorLatestType, type MonitorsMeta, type SJVAi
|
|
|
2
2
|
import { XMap } from "@tstk/builtin-extensions";
|
|
3
3
|
import { Interval } from "@tstk/utils";
|
|
4
4
|
import type { MonitorsDataSource } from "./types";
|
|
5
|
+
export interface MonitorsInitOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Start the 2-minute polling once the initial load finishes. Defaults to true; pass false to
|
|
8
|
+
* load the data without showing a map (`resumeAutoUpdate()` starts polling later).
|
|
9
|
+
*/
|
|
10
|
+
autoUpdate?: boolean;
|
|
11
|
+
}
|
|
5
12
|
declare class MonitorsManager implements MonitorsDataSource {
|
|
13
|
+
#private;
|
|
6
14
|
autoUpdate: Interval;
|
|
7
15
|
initialized: boolean;
|
|
8
16
|
latest: XMap<string, MonitorLatestType<"pm25" | "o3">> | null;
|
|
@@ -11,7 +19,20 @@ declare class MonitorsManager implements MonitorsDataSource {
|
|
|
11
19
|
pollutant: "pm25" | "o3" | null;
|
|
12
20
|
levels: Array<SJVAirEntryLevel> | null;
|
|
13
21
|
constructor();
|
|
14
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Safe to call from several places: overlapping calls share one run, and a failed run can
|
|
24
|
+
* be retried. An explicit "pm25"/"o3" wins over a call without one, whichever came first;
|
|
25
|
+
* between two explicit values the most recent wins. Polling starts when the run finishes
|
|
26
|
+
* unless every call sharing it passed `autoUpdate: false`. A no-op once initialized, so it
|
|
27
|
+
* never restarts polling either: that is `resumeAutoUpdate()`.
|
|
28
|
+
*/
|
|
29
|
+
init(urlPollutant?: string | number | boolean, options?: MonitorsInitOptions): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Starts polling when it is not running: after something stopped it (MonitorMapLayout stops
|
|
32
|
+
* it on destroy) or after an init that opted out of it. Refreshes first since the data may
|
|
33
|
+
* be stale. A no-op before init or while polling.
|
|
34
|
+
*/
|
|
35
|
+
resumeAutoUpdate(): void;
|
|
15
36
|
update(): Promise<void>;
|
|
16
37
|
}
|
|
17
38
|
export declare const monitorsManager: MonitorsManager;
|
|
@@ -10,6 +10,9 @@ class MonitorsManager {
|
|
|
10
10
|
meta = $state(null);
|
|
11
11
|
pollutant = $state(null);
|
|
12
12
|
levels = $derived(this.meta?.entryType(this.pollutant ?? this.meta.default_pollutant).asIter.levels || null);
|
|
13
|
+
#initPromise = null;
|
|
14
|
+
#requestedPollutant = null;
|
|
15
|
+
#requestedAutoUpdate = false;
|
|
13
16
|
constructor() {
|
|
14
17
|
// Refetch monitors when the pollutant changes after the initial load, wherever the change
|
|
15
18
|
// comes from (Pollutant menu, URL sync, a host's own UI). Clearing first blanks the markers
|
|
@@ -31,23 +34,87 @@ class MonitorsManager {
|
|
|
31
34
|
});
|
|
32
35
|
});
|
|
33
36
|
}
|
|
34
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Safe to call from several places: overlapping calls share one run, and a failed run can
|
|
39
|
+
* be retried. An explicit "pm25"/"o3" wins over a call without one, whichever came first;
|
|
40
|
+
* between two explicit values the most recent wins. Polling starts when the run finishes
|
|
41
|
+
* unless every call sharing it passed `autoUpdate: false`. A no-op once initialized, so it
|
|
42
|
+
* never restarts polling either: that is `resumeAutoUpdate()`.
|
|
43
|
+
*/
|
|
44
|
+
init(urlPollutant, options = {}) {
|
|
35
45
|
if (this.initialized)
|
|
46
|
+
return Promise.resolve();
|
|
47
|
+
if (options.autoUpdate !== false)
|
|
48
|
+
this.#requestedAutoUpdate = true;
|
|
49
|
+
if (urlPollutant === "pm25" || urlPollutant === "o3") {
|
|
50
|
+
this.#requestedPollutant = urlPollutant;
|
|
51
|
+
}
|
|
52
|
+
this.#initPromise ??= this.#runInit().finally(() => {
|
|
53
|
+
this.#initPromise = null;
|
|
54
|
+
this.#requestedPollutant = null;
|
|
55
|
+
this.#requestedAutoUpdate = false;
|
|
56
|
+
});
|
|
57
|
+
return this.#initPromise;
|
|
58
|
+
}
|
|
59
|
+
async #runInit() {
|
|
60
|
+
// The pollutant the latest readings were requested for.
|
|
61
|
+
let fetchedPollutant = this.#requestedPollutant;
|
|
62
|
+
const metaRequest = getMonitorsMeta();
|
|
63
|
+
const listAndMeta = Promise.all([getMonitorsList(), metaRequest]).then(([list, meta]) => {
|
|
64
|
+
this.list = list;
|
|
65
|
+
this.meta = meta;
|
|
66
|
+
return meta;
|
|
67
|
+
});
|
|
68
|
+
// The latest readings only need the pollutant, so they never wait for the list. An explicit
|
|
69
|
+
// pollutant may still arrive before meta does, in which case the default is never fetched.
|
|
70
|
+
const latestRequest = fetchedPollutant
|
|
71
|
+
? getMonitorsLatestMap(fetchedPollutant)
|
|
72
|
+
: metaRequest.then((meta) => {
|
|
73
|
+
const pollutant = this.#requestedPollutant ?? meta.default_pollutant;
|
|
74
|
+
fetchedPollutant = pollutant;
|
|
75
|
+
return getMonitorsLatestMap(pollutant);
|
|
76
|
+
});
|
|
77
|
+
// One Promise.all so a failure in either never leaves the other unhandled.
|
|
78
|
+
const [meta, firstLatest] = await Promise.all([listAndMeta, latestRequest]);
|
|
79
|
+
let latest = firstLatest;
|
|
80
|
+
// An explicit pollutant may have arrived while the requests above were in flight.
|
|
81
|
+
let wanted = this.#requestedPollutant ?? meta.default_pollutant;
|
|
82
|
+
while (wanted !== fetchedPollutant) {
|
|
83
|
+
latest = await getMonitorsLatestMap(wanted);
|
|
84
|
+
fetchedPollutant = wanted;
|
|
85
|
+
wanted = this.#requestedPollutant ?? meta.default_pollutant;
|
|
86
|
+
}
|
|
87
|
+
this.pollutant = fetchedPollutant;
|
|
88
|
+
this.latest = latest;
|
|
89
|
+
if (this.#requestedAutoUpdate)
|
|
90
|
+
this.autoUpdate.start();
|
|
91
|
+
this.initialized = true;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Starts polling when it is not running: after something stopped it (MonitorMapLayout stops
|
|
95
|
+
* it on destroy) or after an init that opted out of it. Refreshes first since the data may
|
|
96
|
+
* be stale. A no-op before init or while polling.
|
|
97
|
+
*/
|
|
98
|
+
resumeAutoUpdate() {
|
|
99
|
+
if (!this.initialized || this.autoUpdate.isActive)
|
|
36
100
|
return;
|
|
37
|
-
|
|
38
|
-
this.pollutant =
|
|
39
|
-
urlPollutant === "pm25" || urlPollutant === "o3" ? urlPollutant : this.meta.default_pollutant;
|
|
40
|
-
this.latest = await getMonitorsLatestMap(this.pollutant);
|
|
101
|
+
this.update().catch(console.error);
|
|
41
102
|
this.autoUpdate.start();
|
|
42
|
-
this.initialized = true;
|
|
43
103
|
}
|
|
44
104
|
async update() {
|
|
45
105
|
if (!this.initialized)
|
|
46
106
|
return;
|
|
47
|
-
|
|
107
|
+
const pollutant = this.pollutant;
|
|
108
|
+
const [list, latest] = await Promise.all([
|
|
48
109
|
getMonitorsList(),
|
|
49
|
-
getMonitorsLatestMap(
|
|
110
|
+
getMonitorsLatestMap(pollutant || this.meta?.default_pollutant || "pm25")
|
|
50
111
|
]);
|
|
112
|
+
// The pollutant changed while this was in flight: these readings belong to the previous
|
|
113
|
+
// one, and the change has already started its own update.
|
|
114
|
+
if (this.pollutant !== pollutant)
|
|
115
|
+
return;
|
|
116
|
+
this.list = list;
|
|
117
|
+
this.latest = latest;
|
|
51
118
|
}
|
|
52
119
|
}
|
|
53
120
|
async function getMonitorsLatestMap(pollutant) {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** The pollutants the detail panel can show, in order of preference. */
|
|
2
|
+
export declare const PANEL_POLLUTANTS: readonly ["pm25", "o3"];
|
|
3
|
+
export type PanelPollutant = (typeof PANEL_POLLUTANTS)[number];
|
|
4
|
+
/** The slice of the SDK's `MonitorDetails` this module reads. */
|
|
5
|
+
export interface PanelMonitor {
|
|
6
|
+
type: string;
|
|
7
|
+
is_active: boolean;
|
|
8
|
+
latest: Partial<Record<PanelPollutant, {
|
|
9
|
+
value?: string;
|
|
10
|
+
}>>;
|
|
11
|
+
}
|
|
12
|
+
/** The slice of the SDK's `MonitorsMeta` this module reads. */
|
|
13
|
+
export interface PanelMeta {
|
|
14
|
+
monitors: Record<string, {
|
|
15
|
+
entries: Record<string, unknown>;
|
|
16
|
+
} | undefined>;
|
|
17
|
+
}
|
|
18
|
+
export interface PanelPollutantChoice {
|
|
19
|
+
/** Null when the monitor has no reading and its type publishes neither pollutant. */
|
|
20
|
+
pollutant: PanelPollutant | null;
|
|
21
|
+
/** Whether the monitor has a current reading for `pollutant`. */
|
|
22
|
+
hasReading: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** The panel pollutants this monitor has a current, numeric reading for. */
|
|
25
|
+
export declare function readingPollutants(monitor: Pick<PanelMonitor, "latest">): PanelPollutant[];
|
|
26
|
+
/**
|
|
27
|
+
* The panel pollutants this monitor type publishes. The API marks each entry type with a
|
|
28
|
+
* `published` flag the SDK does not type; a type that carries the entry without the flag
|
|
29
|
+
* counts as publishing it.
|
|
30
|
+
*/
|
|
31
|
+
export declare function publishedPollutants(monitorType: string, meta: PanelMeta): PanelPollutant[];
|
|
32
|
+
/**
|
|
33
|
+
* The pollutant the detail panel shows: the map's when the monitor has it, otherwise the one
|
|
34
|
+
* the monitor has. A monitor with no current reading (inactive, or nothing reported) still
|
|
35
|
+
* gets a pollutant when there is one to chart, so its past data can be browsed.
|
|
36
|
+
*/
|
|
37
|
+
export declare function choosePanelPollutant(mapPollutant: PanelPollutant | null, monitor: PanelMonitor, meta: PanelMeta): PanelPollutantChoice;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** The pollutants the detail panel can show, in order of preference. */
|
|
2
|
+
export const PANEL_POLLUTANTS = ["pm25", "o3"];
|
|
3
|
+
/** The panel pollutants this monitor has a current, numeric reading for. */
|
|
4
|
+
export function readingPollutants(monitor) {
|
|
5
|
+
return PANEL_POLLUTANTS.filter((pollutant) => {
|
|
6
|
+
const value = monitor.latest[pollutant]?.value;
|
|
7
|
+
return typeof value === "string" && value.trim() !== "" && Number.isFinite(Number(value));
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The panel pollutants this monitor type publishes. The API marks each entry type with a
|
|
12
|
+
* `published` flag the SDK does not type; a type that carries the entry without the flag
|
|
13
|
+
* counts as publishing it.
|
|
14
|
+
*/
|
|
15
|
+
export function publishedPollutants(monitorType, meta) {
|
|
16
|
+
const entries = meta.monitors[monitorType]?.entries ?? {};
|
|
17
|
+
return PANEL_POLLUTANTS.filter((pollutant) => {
|
|
18
|
+
const entry = entries[pollutant];
|
|
19
|
+
return entry !== undefined && entry.published !== false;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The pollutant the detail panel shows: the map's when the monitor has it, otherwise the one
|
|
24
|
+
* the monitor has. A monitor with no current reading (inactive, or nothing reported) still
|
|
25
|
+
* gets a pollutant when there is one to chart, so its past data can be browsed.
|
|
26
|
+
*/
|
|
27
|
+
export function choosePanelPollutant(mapPollutant, monitor, meta) {
|
|
28
|
+
const withReading = readingPollutants(monitor);
|
|
29
|
+
// The API keeps returning an inactive monitor's last reading, however old, so only an
|
|
30
|
+
// active monitor's readings count as current.
|
|
31
|
+
if (monitor.is_active && withReading.length > 0) {
|
|
32
|
+
const pollutant = mapPollutant && withReading.includes(mapPollutant) ? mapPollutant : withReading[0];
|
|
33
|
+
return { pollutant, hasReading: true };
|
|
34
|
+
}
|
|
35
|
+
// Nothing current: chart what the type publishes, or failing that what it last reported.
|
|
36
|
+
const published = publishedPollutants(monitor.type, meta);
|
|
37
|
+
const chartable = published.length > 0 ? published : withReading;
|
|
38
|
+
if (chartable.length > 0) {
|
|
39
|
+
const pollutant = mapPollutant && chartable.includes(mapPollutant) ? mapPollutant : chartable[0];
|
|
40
|
+
return { pollutant, hasReading: false };
|
|
41
|
+
}
|
|
42
|
+
return { pollutant: null, hasReading: false };
|
|
43
|
+
}
|
|
@@ -42,7 +42,14 @@
|
|
|
42
42
|
function onkeydown(e: KeyboardEvent) {
|
|
43
43
|
if (e.key !== "Escape") return;
|
|
44
44
|
panelOpen = false;
|
|
45
|
-
if (isWideLayout())
|
|
45
|
+
if (!isWideLayout()) return;
|
|
46
|
+
// Focus on a row would be left inside the hidden dropdown; hand it back to the trigger.
|
|
47
|
+
const focused = document.activeElement;
|
|
48
|
+
const trigger = focused
|
|
49
|
+
?.closest("[data-options-menu]")
|
|
50
|
+
?.querySelector<HTMLElement>(":scope > button");
|
|
51
|
+
menus.closeAll();
|
|
52
|
+
if (trigger !== focused) trigger?.focus();
|
|
46
53
|
}
|
|
47
54
|
</script>
|
|
48
55
|
|
|
@@ -33,10 +33,19 @@
|
|
|
33
33
|
|
|
34
34
|
<div
|
|
35
35
|
role="presentation"
|
|
36
|
+
data-options-menu
|
|
36
37
|
class="group/menu relative md:first:[&>button]:rounded-l-md md:last:[&>button]:rounded-r-md"
|
|
37
38
|
onmouseenter={() => {
|
|
38
39
|
if (isWideLayout()) menus.closeOthers(id);
|
|
39
40
|
}}
|
|
41
|
+
onfocusout={(e) => {
|
|
42
|
+
// Toolbar only: focus moving on to something outside this menu (tabbing past the last
|
|
43
|
+
// row) closes its dropdown. No destination means a click on something unfocusable, which
|
|
44
|
+
// the outside-click handler in OptionsBar decides.
|
|
45
|
+
const next = e.relatedTarget;
|
|
46
|
+
if (!isWideLayout() || !(next instanceof Node) || e.currentTarget.contains(next)) return;
|
|
47
|
+
menus.close(id);
|
|
48
|
+
}}
|
|
40
49
|
>
|
|
41
50
|
<button
|
|
42
51
|
type="button"
|
|
@@ -61,7 +70,7 @@
|
|
|
61
70
|
"max-md:pt-1 md:absolute md:top-full md:z-20 md:min-w-64 md:pt-1",
|
|
62
71
|
alignRight ? "md:right-0" : "md:left-0",
|
|
63
72
|
open ? "block" : "hidden",
|
|
64
|
-
!disabled && "md:group-hover/menu:block
|
|
73
|
+
!disabled && "md:group-hover/menu:block"
|
|
65
74
|
]}
|
|
66
75
|
>
|
|
67
76
|
<div class="py-1 md:max-h-[69vh] md:overflow-y-auto md:rounded-md md:bg-white md:shadow-lg">
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { Marker } from "@maptiler/sdk";
|
|
4
4
|
import { MapPinIcon, SearchIcon } from "@lucide/svelte";
|
|
5
5
|
import { useMonitorMapRouter } from "../router-context";
|
|
6
|
-
import { mapManager } from "../map/map.svelte";
|
|
6
|
+
import { mapManager } from "../map/map-manager.svelte.js";
|
|
7
7
|
import { debounce } from "../utils";
|
|
8
8
|
import { monitorSearch, geocode, type SearchResult } from "./service";
|
|
9
9
|
import { monitorsMapIntegration } from "../monitors/monitors-map-integration.svelte";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a loader so overlapping calls share one run and a resolved run is reused, while a
|
|
3
|
+
* rejected run is forgotten: the next call tries again.
|
|
4
|
+
*/
|
|
5
|
+
export function sharedLoad(load) {
|
|
6
|
+
let pending = null;
|
|
7
|
+
return () => {
|
|
8
|
+
pending ??= load().catch((error) => {
|
|
9
|
+
pending = null;
|
|
10
|
+
throw error;
|
|
11
|
+
});
|
|
12
|
+
return pending;
|
|
13
|
+
};
|
|
14
|
+
}
|