@azohra/meteo.station 0.1.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.
- package/LICENSE +21 -0
- package/README.md +53 -0
- package/dist/air.d.ts +13 -0
- package/dist/air.js +76 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.js +4 -0
- package/dist/client/poll.d.ts +42 -0
- package/dist/client/poll.js +121 -0
- package/dist/client/stores.d.ts +46 -0
- package/dist/client/stores.js +128 -0
- package/dist/client/ticker.d.ts +4 -0
- package/dist/client/ticker.js +19 -0
- package/dist/contract.d.ts +381 -0
- package/dist/contract.js +143 -0
- package/dist/derive.d.ts +54 -0
- package/dist/derive.js +134 -0
- package/dist/display.d.ts +28 -0
- package/dist/display.js +27 -0
- package/dist/elements/elements/AirMatrixElement.d.ts +14 -0
- package/dist/elements/elements/AirMatrixElement.js +83 -0
- package/dist/elements/elements/CurrentConditionsElement.d.ts +9 -0
- package/dist/elements/elements/CurrentConditionsElement.js +52 -0
- package/dist/elements/elements/DailyPatternElement.d.ts +15 -0
- package/dist/elements/elements/DailyPatternElement.js +200 -0
- package/dist/elements/elements/DialElement.d.ts +9 -0
- package/dist/elements/elements/DialElement.js +128 -0
- package/dist/elements/elements/FreshnessBadgeElement.d.ts +8 -0
- package/dist/elements/elements/FreshnessBadgeElement.js +22 -0
- package/dist/elements/elements/SparklineElement.d.ts +9 -0
- package/dist/elements/elements/SparklineElement.js +74 -0
- package/dist/elements/elements/StationCardElement.d.ts +53 -0
- package/dist/elements/elements/StationCardElement.js +211 -0
- package/dist/elements/elements/StationFeedElement.d.ts +32 -0
- package/dist/elements/elements/StationFeedElement.js +215 -0
- package/dist/elements/elements/StationStripElement.d.ts +9 -0
- package/dist/elements/elements/StationStripElement.js +43 -0
- package/dist/elements/elements/StationTableElement.d.ts +18 -0
- package/dist/elements/elements/StationTableElement.js +102 -0
- package/dist/elements/elements/TrendChartElement.d.ts +10 -0
- package/dist/elements/elements/TrendChartElement.js +174 -0
- package/dist/elements/elements/WindArrowElement.d.ts +8 -0
- package/dist/elements/elements/WindArrowElement.js +14 -0
- package/dist/elements/elements/WindHistoryChartElement.d.ts +10 -0
- package/dist/elements/elements/WindHistoryChartElement.js +277 -0
- package/dist/elements/elements/WindRoseElement.d.ts +17 -0
- package/dist/elements/elements/WindRoseElement.js +106 -0
- package/dist/elements/elements/atoms.d.ts +48 -0
- package/dist/elements/elements/atoms.js +131 -0
- package/dist/elements/index.d.ts +48 -0
- package/dist/elements/index.js +57 -0
- package/dist/elements/lib/ambient.d.ts +15 -0
- package/dist/elements/lib/ambient.js +5 -0
- package/dist/elements/lib/attributes.js +21 -0
- package/dist/elements/lib/base.d.ts +45 -0
- package/dist/elements/lib/base.js +152 -0
- package/dist/elements/lib/context.js +28 -0
- package/dist/elements/lib/fragments.js +45 -0
- package/dist/elements/lib/h.js +35 -0
- package/dist/elements/register.d.ts +1 -0
- package/dist/elements/register.js +5 -0
- package/dist/endpoints.d.ts +5 -0
- package/dist/endpoints.js +10 -0
- package/dist/fixtures.d.ts +23 -0
- package/dist/fixtures.js +67 -0
- package/dist/format.d.ts +31 -0
- package/dist/format.js +82 -0
- package/dist/geometry.d.ts +84 -0
- package/dist/geometry.js +333 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/instruments.d.ts +75 -0
- package/dist/instruments.js +173 -0
- package/dist/merge-current.d.ts +13 -0
- package/dist/merge-current.js +54 -0
- package/dist/react/components/AirMatrix.d.ts +11 -0
- package/dist/react/components/AirMatrix.js +82 -0
- package/dist/react/components/CurrentConditions.d.ts +17 -0
- package/dist/react/components/CurrentConditions.js +98 -0
- package/dist/react/components/DailyPattern.d.ts +19 -0
- package/dist/react/components/DailyPattern.js +212 -0
- package/dist/react/components/Dial.d.ts +17 -0
- package/dist/react/components/Dial.js +133 -0
- package/dist/react/components/FreshnessBadge.d.ts +10 -0
- package/dist/react/components/FreshnessBadge.js +20 -0
- package/dist/react/components/Sparkline.d.ts +17 -0
- package/dist/react/components/Sparkline.js +60 -0
- package/dist/react/components/StationCard.d.ts +46 -0
- package/dist/react/components/StationCard.js +140 -0
- package/dist/react/components/StationFeedProvider.d.ts +27 -0
- package/dist/react/components/StationFeedProvider.js +40 -0
- package/dist/react/components/StationStrip.d.ts +16 -0
- package/dist/react/components/StationStrip.js +84 -0
- package/dist/react/components/StationTable.d.ts +18 -0
- package/dist/react/components/StationTable.js +115 -0
- package/dist/react/components/TrendChart.d.ts +14 -0
- package/dist/react/components/TrendChart.js +150 -0
- package/dist/react/components/WindArrow.d.ts +7 -0
- package/dist/react/components/WindArrow.js +21 -0
- package/dist/react/components/WindHistoryChart.d.ts +18 -0
- package/dist/react/components/WindHistoryChart.js +278 -0
- package/dist/react/components/WindRose.d.ts +17 -0
- package/dist/react/components/WindRose.js +99 -0
- package/dist/react/components/atoms.d.ts +33 -0
- package/dist/react/components/atoms.js +136 -0
- package/dist/react/hooks/useFreshness.d.ts +6 -0
- package/dist/react/hooks/useFreshness.js +21 -0
- package/dist/react/hooks/usePolledJson.js +17 -0
- package/dist/react/hooks/useStation.d.ts +23 -0
- package/dist/react/hooks/useStation.js +42 -0
- package/dist/react/hooks/useStationCurrent.d.ts +23 -0
- package/dist/react/hooks/useStationCurrent.js +32 -0
- package/dist/react/hooks/useStationFeed.d.ts +22 -0
- package/dist/react/hooks/useStationFeed.js +28 -0
- package/dist/react/index.d.ts +20 -0
- package/dist/react/index.js +21 -0
- package/dist/react/lib/cells.js +29 -0
- package/dist/scene/air-matrix.js +71 -0
- package/dist/scene/atoms.js +102 -0
- package/dist/scene/card.js +54 -0
- package/dist/scene/chart.js +28 -0
- package/dist/scene/current.js +66 -0
- package/dist/scene/daily-pattern.js +81 -0
- package/dist/scene/dial.js +141 -0
- package/dist/scene/glyphs.js +24 -0
- package/dist/scene/rose.js +120 -0
- package/dist/scene/sparkline.js +73 -0
- package/dist/scene/strip.js +63 -0
- package/dist/scene/table.js +76 -0
- package/dist/scene/trend.js +160 -0
- package/dist/scene/wind-history.js +148 -0
- package/dist/scene/wind-plot.js +214 -0
- package/dist/server/adapter.d.ts +28 -0
- package/dist/server/adapter.js +32 -0
- package/dist/server/adapters/campbell.d.ts +112 -0
- package/dist/server/adapters/campbell.js +414 -0
- package/dist/server/adapters/tempest.d.ts +27 -0
- package/dist/server/adapters/tempest.js +137 -0
- package/dist/server/adapters/windnerd.d.ts +39 -0
- package/dist/server/adapters/windnerd.js +159 -0
- package/dist/server/config.d.ts +135 -0
- package/dist/server/config.js +57 -0
- package/dist/server/environment.d.ts +48 -0
- package/dist/server/environment.js +128 -0
- package/dist/server/feed.d.ts +31 -0
- package/dist/server/feed.js +216 -0
- package/dist/server/handler.d.ts +17 -0
- package/dist/server/handler.js +133 -0
- package/dist/server/index.d.ts +10 -0
- package/dist/server/index.js +10 -0
- package/dist/strings.d.ts +118 -0
- package/dist/strings.js +237 -0
- package/dist/windnerd.d.ts +5 -0
- package/dist/windnerd.js +25 -0
- package/package.json +97 -0
- package/schema/README.md +14 -0
- package/schema/example-current.json +48 -0
- package/schema/example-feed.json +147 -0
- package/schema/stationcurrent.schema.json +641 -0
- package/schema/stationfeed.schema.json +656 -0
- package/styles.css +1141 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Justin Watts
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# `@azohra/meteo.station`
|
|
2
|
+
|
|
3
|
+
The **station** capability of meteo by Azohra: one wire contract, vendor
|
|
4
|
+
adapters that normalize into it (WindNerd, WeatherFlow Tempest, Campbell
|
|
5
|
+
Scientific loggers, or your own), a mountable `Request → Response` handler
|
|
6
|
+
that serves the whole inventory as a single feed, and hooks and components
|
|
7
|
+
that render it — natively, in your design system, with no vendor iframe.
|
|
8
|
+
|
|
9
|
+
## Surface
|
|
10
|
+
|
|
11
|
+
| Entry point | What it is |
|
|
12
|
+
|---|---|
|
|
13
|
+
| `@azohra/meteo.station` | The isomorphic root: the wire contract (zod), pure derivations (period stats, compass, freshness, unit and threshold conversion), framework-free chart and instrument geometry, and the shared display rules (strings, formatting, air sentences, display resolution, merge policy) every binding renders from. |
|
|
14
|
+
| `@azohra/meteo.station/client` | The framework-free client data layer: `createJsonPoller` and the station stores (`createStationFeedStore`, `createStationCurrentStore`, `createStationStore`) every binding subscribes to. Client-only at runtime, import-safe anywhere. |
|
|
15
|
+
| `@azohra/meteo.station/server` | Vendor adapters plus the custom-adapter interface and `defineStationAdapter`, data-level `loadStationFeed()` / `loadStationCurrent()`, and the mountable feed handler. Server-only, so it can never leak into a client bundle. |
|
|
16
|
+
| `@azohra/meteo.station/react` | `StationFeedProvider`, polling hooks (`useStation`, `useStationFeed`, `useStationCurrent`), the component set — `StationCard`, `CurrentConditions`, `WindHistoryChart`, `TrendChart`, `WindRose`, `DailyPattern`, `StationTable`, `StationStrip`, `AirMatrix`, `FreshnessBadge` — and an atoms layer of inline primitives (`Speed`, `Gust`, `Lull`, `Temperature`, `Pressure`, `Direction`, `UpdatedAt`, `BandChip`, `Dial`, `Sparkline`) for composing your own layouts. |
|
|
17
|
+
| `@azohra/meteo.station/elements` | The same surface as light-DOM custom elements (`<meteo-station-feed>`, `<meteo-station-card>`, `<meteo-station-table>`, the charts, the atoms…) — a full peer of the react binding, framework-free, held byte-identical by a parity suite. `/register` is the auto-defining one-liner. |
|
|
18
|
+
| `@azohra/meteo.station/styles.css` | The default skin (an intentional side effect), shared by both bindings. |
|
|
19
|
+
|
|
20
|
+
## Taste
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { StationFeedProvider, useStation, StationCard } from "@azohra/meteo.station/react";
|
|
24
|
+
import "@azohra/meteo.station/styles.css";
|
|
25
|
+
|
|
26
|
+
function LiveWind() {
|
|
27
|
+
const { feed, receivedAtMs } = useStation("/api/wind", "launch");
|
|
28
|
+
if (!feed) return null;
|
|
29
|
+
return (
|
|
30
|
+
<div className="meteo-root">
|
|
31
|
+
<StationFeedProvider feed={feed} receivedAtMs={receivedAtMs}
|
|
32
|
+
thresholds={{ unit: "kmh", values: [12, 20, 28] }}>
|
|
33
|
+
<StationCard />
|
|
34
|
+
</StationFeedProvider>
|
|
35
|
+
</div>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Documentation
|
|
41
|
+
|
|
42
|
+
Station's reference lives in [`docs/`](docs/) and is served at
|
|
43
|
+
<https://meteo.azohra.com/docs/station/>.
|
|
44
|
+
|
|
45
|
+
JSON Schema for the wire documents lives in [`../schema/`](../schema/).
|
|
46
|
+
|
|
47
|
+
## Stability
|
|
48
|
+
|
|
49
|
+
Pre-1.0: the wire contract and environment helpers are stable; handler
|
|
50
|
+
internals are not. Pin a minor version if you reach past the documented
|
|
51
|
+
surface.
|
|
52
|
+
|
|
53
|
+
MIT © Justin Watts
|
package/dist/air.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AirConditions } from "./contract.js";
|
|
2
|
+
import { FormatTime, StationStrings } from "./strings.js";
|
|
3
|
+
//#region src/air.d.ts
|
|
4
|
+
declare function airSummary(conditions: AirConditions, strings?: StationStrings): string;
|
|
5
|
+
declare function lastStrikeWords(conditions: AirConditions, formatTime: FormatTime, strings?: StationStrings): string;
|
|
6
|
+
type AirRow = {
|
|
7
|
+
label: string;
|
|
8
|
+
unit: string;
|
|
9
|
+
value: (conditions: AirConditions) => string | null;
|
|
10
|
+
};
|
|
11
|
+
declare function airRows(words: StationStrings): AirRow[];
|
|
12
|
+
//#endregion
|
|
13
|
+
export { AirRow, airRows, airSummary, lastStrikeWords };
|
package/dist/air.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { defaultStrings } from "./strings.js";
|
|
2
|
+
//#region src/air.ts
|
|
3
|
+
const round1 = (value) => Math.round(value * 10) / 10;
|
|
4
|
+
function airSummary(conditions, strings = defaultStrings) {
|
|
5
|
+
const words = strings.air;
|
|
6
|
+
const rain = conditions.precipitationRateMmPerHour != null && conditions.precipitationRateMmPerHour > 0 ? words.summaryRaining(round1(conditions.precipitationRateMmPerHour)) : conditions.precipitationTodayMm == null ? null : conditions.precipitationTodayMm > 0 ? words.summaryRainToday(round1(conditions.precipitationTodayMm)) : words.summaryDry;
|
|
7
|
+
const pieces = [
|
|
8
|
+
conditions.relativeHumidityPercent == null ? null : words.summaryHumidity(Math.round(conditions.relativeHumidityPercent)),
|
|
9
|
+
rain,
|
|
10
|
+
conditions.lightningStrikeCountLastHour != null && conditions.lightningStrikeCountLastHour > 0 ? words.summaryStrikes(conditions.lightningStrikeCountLastHour) : null
|
|
11
|
+
].filter((piece) => piece != null);
|
|
12
|
+
return pieces.length > 0 ? pieces.join(" · ") : words.summaryFallback;
|
|
13
|
+
}
|
|
14
|
+
function lastStrikeWords(conditions, formatTime, strings = defaultStrings) {
|
|
15
|
+
const words = strings.air;
|
|
16
|
+
if (conditions.lastLightningStrikeAt == null) return words.noStrike;
|
|
17
|
+
const time = formatTime(new Date(conditions.lastLightningStrikeAt));
|
|
18
|
+
return conditions.lastLightningStrikeDistanceKm == null ? words.lastStrikeNoDistance(time) : words.lastStrike(Math.round(conditions.lastLightningStrikeDistanceKm), time);
|
|
19
|
+
}
|
|
20
|
+
function airRows(words) {
|
|
21
|
+
const air = words.air;
|
|
22
|
+
return [
|
|
23
|
+
{
|
|
24
|
+
label: air.humidity,
|
|
25
|
+
unit: air.unitPercent,
|
|
26
|
+
value: (c) => c.relativeHumidityPercent == null ? null : `${Math.round(c.relativeHumidityPercent)}`
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
label: air.dewPoint,
|
|
30
|
+
unit: words.degC,
|
|
31
|
+
value: (c) => c.dewPointC?.toFixed(1) ?? null
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
label: air.pressure,
|
|
35
|
+
unit: air.unitHpa,
|
|
36
|
+
value: (c) => c.seaLevelPressureHpa?.toFixed(1) ?? null
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
label: air.pressureTrend,
|
|
40
|
+
unit: "",
|
|
41
|
+
value: (c) => c.pressureTrend === "falling" ? air.trendFalling : c.pressureTrend === "rising" ? air.trendRising : c.pressureTrend === "steady" ? air.trendSteady : null
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
label: air.solar,
|
|
45
|
+
unit: air.unitWm2,
|
|
46
|
+
value: (c) => c.solarRadiationWm2 == null ? null : `${Math.round(c.solarRadiationWm2)}`
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
label: air.uv,
|
|
50
|
+
unit: air.unitIndex,
|
|
51
|
+
value: (c) => c.uvIndex == null ? null : `${Math.round(c.uvIndex * 10) / 10}`
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
label: air.rainRate,
|
|
55
|
+
unit: air.unitMmPerHour,
|
|
56
|
+
value: (c) => c.precipitationRateMmPerHour?.toFixed(1) ?? null
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
label: air.rainToday,
|
|
60
|
+
unit: air.unitMm,
|
|
61
|
+
value: (c) => c.precipitationTodayMm?.toFixed(1) ?? null
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
label: air.rainMinutes,
|
|
65
|
+
unit: air.unitMinutes,
|
|
66
|
+
value: (c) => c.precipitationMinutesToday == null ? null : `${c.precipitationMinutesToday}`
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
label: air.lightning,
|
|
70
|
+
unit: air.unitStrikesPastHour,
|
|
71
|
+
value: (c) => c.lightningStrikeCountLastHour == null ? null : `${c.lightningStrikeCountLastHour}`
|
|
72
|
+
}
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { airRows, airSummary, lastStrikeWords };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FRESHNESS_REEVALUATE_MS, JsonPoller, JsonPollerOptions, ParseOutcome, PollError, PollSeed, PollSnapshot, REQUEST_TIMEOUT_MS, createJsonPoller } from "./poll.js";
|
|
2
|
+
import { StationSnapshot, StationStore, createStationCurrentStore, createStationFeedStore, createStationStore, parseCurrentText, parseFeedText } from "./stores.js";
|
|
3
|
+
import { subscribeTicker } from "./ticker.js";
|
|
4
|
+
export { FRESHNESS_REEVALUATE_MS, type JsonPoller, type JsonPollerOptions, type ParseOutcome, type PollError, type PollSeed, type PollSnapshot, REQUEST_TIMEOUT_MS, type StationSnapshot, type StationStore, createJsonPoller, createStationCurrentStore, createStationFeedStore, createStationStore, parseCurrentText, parseFeedText, subscribeTicker };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FRESHNESS_REEVALUATE_MS, REQUEST_TIMEOUT_MS, createJsonPoller } from "./poll.js";
|
|
2
|
+
import { createStationCurrentStore, createStationFeedStore, createStationStore, parseCurrentText, parseFeedText } from "./stores.js";
|
|
3
|
+
import { subscribeTicker } from "./ticker.js";
|
|
4
|
+
export { FRESHNESS_REEVALUATE_MS, REQUEST_TIMEOUT_MS, createJsonPoller, createStationCurrentStore, createStationFeedStore, createStationStore, parseCurrentText, parseFeedText, subscribeTicker };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/client/poll.d.ts
|
|
2
|
+
type PollError = {
|
|
3
|
+
kind: "network";
|
|
4
|
+
status?: number;
|
|
5
|
+
} | {
|
|
6
|
+
kind: "contract";
|
|
7
|
+
cause?: unknown;
|
|
8
|
+
};
|
|
9
|
+
type ParseOutcome<T> = {
|
|
10
|
+
ok: true;
|
|
11
|
+
data: T;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
cause: unknown;
|
|
15
|
+
};
|
|
16
|
+
type PollSeed<T> = {
|
|
17
|
+
data: T;
|
|
18
|
+
receivedAtMs: number;
|
|
19
|
+
};
|
|
20
|
+
type PollSnapshot<T> = {
|
|
21
|
+
data: T | null;
|
|
22
|
+
error: PollError | null;
|
|
23
|
+
receivedAtMs: number | null;
|
|
24
|
+
};
|
|
25
|
+
declare const REQUEST_TIMEOUT_MS = 15000;
|
|
26
|
+
declare const FRESHNESS_REEVALUATE_MS = 30000;
|
|
27
|
+
type JsonPollerOptions<T> = {
|
|
28
|
+
parse: (text: string) => ParseOutcome<T>;
|
|
29
|
+
intervalMsFor: (last: T | null) => number;
|
|
30
|
+
fetchInit?: RequestInit | (() => RequestInit | undefined);
|
|
31
|
+
initial?: PollSeed<T>;
|
|
32
|
+
};
|
|
33
|
+
type JsonPoller<T> = {
|
|
34
|
+
getSnapshot(): PollSnapshot<T>;
|
|
35
|
+
subscribe(listener: () => void): () => void;
|
|
36
|
+
start(): void;
|
|
37
|
+
stop(): void;
|
|
38
|
+
refresh(): void;
|
|
39
|
+
};
|
|
40
|
+
declare function createJsonPoller<T>(url: string, options: JsonPollerOptions<T>): JsonPoller<T>;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { FRESHNESS_REEVALUATE_MS, JsonPoller, JsonPollerOptions, ParseOutcome, PollError, PollSeed, PollSnapshot, REQUEST_TIMEOUT_MS, createJsonPoller };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
//#region src/client/poll.ts
|
|
2
|
+
const REQUEST_TIMEOUT_MS = 15e3;
|
|
3
|
+
const FRESHNESS_REEVALUATE_MS = 3e4;
|
|
4
|
+
function createJsonPoller(url, options) {
|
|
5
|
+
const { parse, intervalMsFor, initial } = options;
|
|
6
|
+
const fetchInitFor = () => typeof options.fetchInit === "function" ? options.fetchInit() : options.fetchInit;
|
|
7
|
+
let snapshot = initial != null ? {
|
|
8
|
+
data: initial.data,
|
|
9
|
+
error: null,
|
|
10
|
+
receivedAtMs: initial.receivedAtMs
|
|
11
|
+
} : {
|
|
12
|
+
data: null,
|
|
13
|
+
error: null,
|
|
14
|
+
receivedAtMs: null
|
|
15
|
+
};
|
|
16
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
17
|
+
let loop = null;
|
|
18
|
+
const setSnapshot = (next) => {
|
|
19
|
+
snapshot = next;
|
|
20
|
+
for (const listener of [...listeners]) listener();
|
|
21
|
+
};
|
|
22
|
+
const hidden = () => typeof document !== "undefined" && document.hidden;
|
|
23
|
+
const run = async (current) => {
|
|
24
|
+
if (current.inFlight) return;
|
|
25
|
+
current.inFlight = true;
|
|
26
|
+
const requestController = new AbortController();
|
|
27
|
+
current.controller = requestController;
|
|
28
|
+
const deadline = setTimeout(() => requestController.abort(), REQUEST_TIMEOUT_MS);
|
|
29
|
+
try {
|
|
30
|
+
const response = await fetch(url, {
|
|
31
|
+
...fetchInitFor(),
|
|
32
|
+
signal: requestController.signal
|
|
33
|
+
});
|
|
34
|
+
const body = await response.text();
|
|
35
|
+
if (current.disposed) return;
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
setSnapshot({
|
|
38
|
+
...snapshot,
|
|
39
|
+
error: {
|
|
40
|
+
kind: "network",
|
|
41
|
+
status: response.status
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const parsed = parse(body);
|
|
47
|
+
if (!parsed.ok) {
|
|
48
|
+
setSnapshot({
|
|
49
|
+
...snapshot,
|
|
50
|
+
error: {
|
|
51
|
+
kind: "contract",
|
|
52
|
+
cause: parsed.cause
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
setSnapshot({
|
|
58
|
+
data: parsed.data,
|
|
59
|
+
error: null,
|
|
60
|
+
receivedAtMs: Date.now()
|
|
61
|
+
});
|
|
62
|
+
} catch {
|
|
63
|
+
if (!current.disposed) setSnapshot({
|
|
64
|
+
...snapshot,
|
|
65
|
+
error: { kind: "network" }
|
|
66
|
+
});
|
|
67
|
+
} finally {
|
|
68
|
+
clearTimeout(deadline);
|
|
69
|
+
if (current.controller === requestController) current.controller = null;
|
|
70
|
+
current.inFlight = false;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const schedule = (current) => {
|
|
74
|
+
current.timer = setTimeout(async () => {
|
|
75
|
+
if (!hidden()) await run(current);
|
|
76
|
+
if (!current.disposed) schedule(current);
|
|
77
|
+
}, intervalMsFor(snapshot.data));
|
|
78
|
+
};
|
|
79
|
+
const start = () => {
|
|
80
|
+
if (loop != null) return;
|
|
81
|
+
const current = {
|
|
82
|
+
disposed: false,
|
|
83
|
+
timer: void 0,
|
|
84
|
+
controller: null,
|
|
85
|
+
inFlight: false,
|
|
86
|
+
onVisibilityChange: () => {
|
|
87
|
+
if (!hidden()) run(current);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
loop = current;
|
|
91
|
+
run(current).then(() => {
|
|
92
|
+
if (!current.disposed) schedule(current);
|
|
93
|
+
});
|
|
94
|
+
if (typeof document !== "undefined") document.addEventListener("visibilitychange", current.onVisibilityChange);
|
|
95
|
+
};
|
|
96
|
+
const stop = () => {
|
|
97
|
+
const current = loop;
|
|
98
|
+
if (current == null) return;
|
|
99
|
+
loop = null;
|
|
100
|
+
current.disposed = true;
|
|
101
|
+
current.controller?.abort();
|
|
102
|
+
if (current.timer != null) clearTimeout(current.timer);
|
|
103
|
+
if (typeof document !== "undefined") document.removeEventListener("visibilitychange", current.onVisibilityChange);
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
getSnapshot: () => snapshot,
|
|
107
|
+
subscribe: (listener) => {
|
|
108
|
+
listeners.add(listener);
|
|
109
|
+
return () => listeners.delete(listener);
|
|
110
|
+
},
|
|
111
|
+
start,
|
|
112
|
+
stop,
|
|
113
|
+
refresh: () => {
|
|
114
|
+
if (loop == null) return;
|
|
115
|
+
stop();
|
|
116
|
+
start();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
export { FRESHNESS_REEVALUATE_MS, REQUEST_TIMEOUT_MS, createJsonPoller };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { JsonPoller, ParseOutcome, PollError } from "./poll.js";
|
|
2
|
+
import { Station, StationCurrent, StationFeed } from "../contract.js";
|
|
3
|
+
//#region src/client/stores.d.ts
|
|
4
|
+
declare function parseFeedText(text: string): ParseOutcome<StationFeed>;
|
|
5
|
+
declare function parseCurrentText(text: string): ParseOutcome<StationCurrent>;
|
|
6
|
+
type FetchInitOption = RequestInit | (() => RequestInit | undefined);
|
|
7
|
+
declare function createStationFeedStore(base: string, options?: {
|
|
8
|
+
pollSeconds?: number;
|
|
9
|
+
fetchInit?: FetchInitOption;
|
|
10
|
+
initial?: {
|
|
11
|
+
feed: StationFeed;
|
|
12
|
+
receivedAtMs: number;
|
|
13
|
+
};
|
|
14
|
+
}): JsonPoller<StationFeed>;
|
|
15
|
+
declare function createStationCurrentStore(base: string, stationId: string, options?: {
|
|
16
|
+
pollSeconds?: number;
|
|
17
|
+
fetchInit?: FetchInitOption;
|
|
18
|
+
initial?: {
|
|
19
|
+
current: StationCurrent;
|
|
20
|
+
receivedAtMs: number;
|
|
21
|
+
};
|
|
22
|
+
}): JsonPoller<StationCurrent>;
|
|
23
|
+
type StationSnapshot = {
|
|
24
|
+
feed: StationFeed | null;
|
|
25
|
+
station: Station | null;
|
|
26
|
+
receivedAtMs: number | null;
|
|
27
|
+
error: PollError | null;
|
|
28
|
+
};
|
|
29
|
+
type StationStore = {
|
|
30
|
+
getSnapshot(): StationSnapshot;
|
|
31
|
+
subscribe(listener: () => void): () => void;
|
|
32
|
+
start(): void;
|
|
33
|
+
stop(): void;
|
|
34
|
+
refresh(): void;
|
|
35
|
+
};
|
|
36
|
+
declare function createStationStore(base: string, stationId: string, options?: {
|
|
37
|
+
pollSeconds?: number;
|
|
38
|
+
currentPollSeconds?: number;
|
|
39
|
+
fetchInit?: FetchInitOption;
|
|
40
|
+
initialData?: {
|
|
41
|
+
feed: StationFeed;
|
|
42
|
+
receivedAtMs: number;
|
|
43
|
+
};
|
|
44
|
+
}): StationStore;
|
|
45
|
+
//#endregion
|
|
46
|
+
export { StationSnapshot, StationStore, createStationCurrentStore, createStationFeedStore, createStationStore, parseCurrentText, parseFeedText };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { stationCurrentSchema, stationFeedSchema } from "../contract.js";
|
|
2
|
+
import { currentEndpoint, feedEndpoint } from "../endpoints.js";
|
|
3
|
+
import { foldCurrent } from "../merge-current.js";
|
|
4
|
+
import { createJsonPoller } from "./poll.js";
|
|
5
|
+
//#region src/client/stores.ts
|
|
6
|
+
const FEED_DEFAULT_POLL_SECONDS = 60;
|
|
7
|
+
const CURRENT_DEFAULT_POLL_SECONDS = 15;
|
|
8
|
+
function parseFeedText(text) {
|
|
9
|
+
let json;
|
|
10
|
+
try {
|
|
11
|
+
json = JSON.parse(text);
|
|
12
|
+
} catch (cause) {
|
|
13
|
+
return {
|
|
14
|
+
ok: false,
|
|
15
|
+
cause
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const result = stationFeedSchema.safeParse(json);
|
|
19
|
+
return result.success ? {
|
|
20
|
+
ok: true,
|
|
21
|
+
data: result.data
|
|
22
|
+
} : {
|
|
23
|
+
ok: false,
|
|
24
|
+
cause: result.error
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function parseCurrentText(text) {
|
|
28
|
+
let json;
|
|
29
|
+
try {
|
|
30
|
+
json = JSON.parse(text);
|
|
31
|
+
} catch (cause) {
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
cause
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const result = stationCurrentSchema.safeParse(json);
|
|
38
|
+
return result.success ? {
|
|
39
|
+
ok: true,
|
|
40
|
+
data: result.data
|
|
41
|
+
} : {
|
|
42
|
+
ok: false,
|
|
43
|
+
cause: result.error
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function createStationFeedStore(base, options = {}) {
|
|
47
|
+
const { pollSeconds, fetchInit, initial } = options;
|
|
48
|
+
return createJsonPoller(feedEndpoint(base), {
|
|
49
|
+
parse: parseFeedText,
|
|
50
|
+
intervalMsFor: (last) => {
|
|
51
|
+
if (pollSeconds != null) return pollSeconds * 1e3;
|
|
52
|
+
const advised = last?.stations.map((station) => station.recommendedPollSeconds) ?? [];
|
|
53
|
+
return (advised.length > 0 ? Math.min(...advised) : FEED_DEFAULT_POLL_SECONDS) * 1e3;
|
|
54
|
+
},
|
|
55
|
+
...fetchInit != null ? { fetchInit } : {},
|
|
56
|
+
...initial != null ? { initial: {
|
|
57
|
+
data: initial.feed,
|
|
58
|
+
receivedAtMs: initial.receivedAtMs
|
|
59
|
+
} } : {}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function createStationCurrentStore(base, stationId, options = {}) {
|
|
63
|
+
const { pollSeconds, fetchInit, initial } = options;
|
|
64
|
+
return createJsonPoller(currentEndpoint(base, stationId), {
|
|
65
|
+
parse: parseCurrentText,
|
|
66
|
+
intervalMsFor: (last) => (pollSeconds ?? last?.station.recommendedPollSeconds ?? CURRENT_DEFAULT_POLL_SECONDS) * 1e3,
|
|
67
|
+
...fetchInit != null ? { fetchInit } : {},
|
|
68
|
+
...initial != null ? { initial: {
|
|
69
|
+
data: initial.current,
|
|
70
|
+
receivedAtMs: initial.receivedAtMs
|
|
71
|
+
} } : {}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function createStationStore(base, stationId, options = {}) {
|
|
75
|
+
const { pollSeconds, currentPollSeconds, fetchInit, initialData } = options;
|
|
76
|
+
const feedStore = createStationFeedStore(base, {
|
|
77
|
+
...pollSeconds != null ? { pollSeconds } : {},
|
|
78
|
+
...fetchInit != null ? { fetchInit } : {},
|
|
79
|
+
...initialData != null ? { initial: initialData } : {}
|
|
80
|
+
});
|
|
81
|
+
const currentStore = createStationCurrentStore(base, stationId, {
|
|
82
|
+
...currentPollSeconds != null ? { pollSeconds: currentPollSeconds } : {},
|
|
83
|
+
...fetchInit != null ? { fetchInit } : {}
|
|
84
|
+
});
|
|
85
|
+
let cached = null;
|
|
86
|
+
let lastFeed = null;
|
|
87
|
+
let lastCurrent = null;
|
|
88
|
+
return {
|
|
89
|
+
getSnapshot: () => {
|
|
90
|
+
const feedSnapshot = feedStore.getSnapshot();
|
|
91
|
+
const currentSnapshot = currentStore.getSnapshot();
|
|
92
|
+
if (cached == null || feedSnapshot !== lastFeed || currentSnapshot !== lastCurrent) {
|
|
93
|
+
lastFeed = feedSnapshot;
|
|
94
|
+
lastCurrent = currentSnapshot;
|
|
95
|
+
const folded = foldCurrent(feedSnapshot.data, feedSnapshot.receivedAtMs, currentSnapshot.data, currentSnapshot.receivedAtMs);
|
|
96
|
+
cached = {
|
|
97
|
+
feed: folded.feed,
|
|
98
|
+
station: folded.feed?.stations.find((entry) => entry.id === stationId) ?? null,
|
|
99
|
+
receivedAtMs: folded.receivedAtMs,
|
|
100
|
+
error: feedSnapshot.error ?? currentSnapshot.error
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return cached;
|
|
104
|
+
},
|
|
105
|
+
subscribe: (listener) => {
|
|
106
|
+
const unsubscribeFeed = feedStore.subscribe(listener);
|
|
107
|
+
const unsubscribeCurrent = currentStore.subscribe(listener);
|
|
108
|
+
return () => {
|
|
109
|
+
unsubscribeFeed();
|
|
110
|
+
unsubscribeCurrent();
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
start: () => {
|
|
114
|
+
feedStore.start();
|
|
115
|
+
currentStore.start();
|
|
116
|
+
},
|
|
117
|
+
stop: () => {
|
|
118
|
+
feedStore.stop();
|
|
119
|
+
currentStore.stop();
|
|
120
|
+
},
|
|
121
|
+
refresh: () => {
|
|
122
|
+
feedStore.refresh();
|
|
123
|
+
currentStore.refresh();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
export { createStationCurrentStore, createStationFeedStore, createStationStore, parseCurrentText, parseFeedText };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FRESHNESS_REEVALUATE_MS } from "./poll.js";
|
|
2
|
+
//#region src/client/ticker.ts
|
|
3
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
4
|
+
let timer = null;
|
|
5
|
+
function subscribeTicker(listener) {
|
|
6
|
+
listeners.add(listener);
|
|
7
|
+
timer ??= setInterval(() => {
|
|
8
|
+
for (const entry of [...listeners]) entry();
|
|
9
|
+
}, FRESHNESS_REEVALUATE_MS);
|
|
10
|
+
return () => {
|
|
11
|
+
listeners.delete(listener);
|
|
12
|
+
if (listeners.size === 0 && timer != null) {
|
|
13
|
+
clearInterval(timer);
|
|
14
|
+
timer = null;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { subscribeTicker };
|