signalk-livedepth-plugin 0.3.12 → 0.3.15

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.
@@ -897,13 +897,30 @@ var PLUGIN_ID = "signalk-livedepth-plugin";
897
897
  var ASSET_BASE = `/plotterext/${PLUGIN_ID}`;
898
898
  var BATHY_BASE = `${ASSET_BASE}/bathymetry`;
899
899
  var DEBOUNCE_MS = 400;
900
+ var TILE_PAD = 0.1;
901
+ var MAX_LAT = 85;
902
+ var fetchedBounds = null;
903
+ function padBounds(bounds, pad) {
904
+ const [west, south, east, north] = bounds;
905
+ const dx = (east - west) * pad;
906
+ const dy = (north - south) * pad;
907
+ return [
908
+ west - dx,
909
+ Math.max(-MAX_LAT, south - dy),
910
+ east + dx,
911
+ Math.min(MAX_LAT, north + dy)
912
+ ];
913
+ }
914
+ function boundsContain(outer, inner) {
915
+ return inner[0] >= outer[0] && inner[1] >= outer[1] && inner[2] <= outer[2] && inner[3] <= outer[3];
916
+ }
900
917
  var POLL_MS = 15e3;
901
918
  var MAX_TILE_FETCHES = 24;
902
919
  var LIVE_FLUSH_MS = 300;
903
920
  var DEFAULT_DEPTH_PATH = "environment.depth.belowTransducer";
904
921
  var client = await connectExtension();
905
922
  var worker = new Worker(
906
- new URL(`./render-worker.js?v=${"0.3.12"}`, import.meta.url),
923
+ new URL(`./render-worker.js?v=${"0.3.15"}`, import.meta.url),
907
924
  { type: "module" }
908
925
  );
909
926
  var settings = { ...DEFAULT_SETTINGS };
@@ -914,6 +931,7 @@ var showFootprint = false;
914
931
  var baseSamples = [];
915
932
  var lastReq = null;
916
933
  var busy = false;
934
+ var busyTimer;
917
935
  var dirty = false;
918
936
  var rendering = null;
919
937
  var renderingLive = false;
@@ -924,6 +942,8 @@ var perfFrameMs = 0;
924
942
  var framesSeen = 0;
925
943
  var framesSince = Date.now();
926
944
  var timer;
945
+ var paused = false;
946
+ var historyPoll;
927
947
  var pendingLive = [];
928
948
  var liveFlushTimer;
929
949
  async function loadSettings() {
@@ -992,14 +1012,18 @@ async function fetchHistory() {
992
1012
  publishBase();
993
1013
  }
994
1014
  async function fetchTiles(req) {
1015
+ if (fetchedBounds && boundsContain(fetchedBounds, req.bounds)) {
1016
+ return;
1017
+ }
1018
+ const fetchBounds = padBounds(req.bounds, TILE_PAD);
995
1019
  const cellMetres = Math.max(req.resolution, 0.01);
996
1020
  let zoom = zoomForCellSize(cellMetres, MIN_ZOOM, BASE_ZOOM);
997
1021
  while (zoom > MIN_ZOOM) {
998
- const { xs: xs2, ys: ys2 } = tileRangeForBbox(req.bounds, zoom);
1022
+ const { xs: xs2, ys: ys2 } = tileRangeForBbox(fetchBounds, zoom);
999
1023
  if (xs2.length * ys2.length <= MAX_TILE_FETCHES) break;
1000
1024
  zoom--;
1001
1025
  }
1002
- const { xs, ys } = tileRangeForBbox(req.bounds, zoom);
1026
+ const { xs, ys } = tileRangeForBbox(fetchBounds, zoom);
1003
1027
  const out = [];
1004
1028
  await Promise.all(
1005
1029
  xs.flatMap(
@@ -1017,14 +1041,16 @@ async function fetchTiles(req) {
1017
1041
  )
1018
1042
  );
1019
1043
  baseSamples = out;
1044
+ fetchedBounds = fetchBounds;
1020
1045
  publishBase();
1021
1046
  }
1022
1047
  function schedule() {
1048
+ if (paused) return;
1023
1049
  clearTimeout(timer);
1024
1050
  timer = setTimeout(() => void refresh(), DEBOUNCE_MS);
1025
1051
  }
1026
1052
  async function refresh() {
1027
- if (!settings.enabled) return;
1053
+ if (paused || !settings.enabled) return;
1028
1054
  if (settings.source === "history") {
1029
1055
  await fetchHistory();
1030
1056
  } else if (lastReq) {
@@ -1070,6 +1096,33 @@ function stopLive() {
1070
1096
  void liveUnsub?.();
1071
1097
  liveUnsub = void 0;
1072
1098
  }
1099
+ function pauseRenderer() {
1100
+ if (paused) return;
1101
+ paused = true;
1102
+ clearTimeout(timer);
1103
+ timer = void 0;
1104
+ clearTimeout(busyTimer);
1105
+ busyTimer = void 0;
1106
+ busy = false;
1107
+ rendering = null;
1108
+ if (liveFlushTimer) {
1109
+ clearInterval(liveFlushTimer);
1110
+ liveFlushTimer = void 0;
1111
+ }
1112
+ if (historyPoll) {
1113
+ clearInterval(historyPoll);
1114
+ historyPoll = void 0;
1115
+ }
1116
+ }
1117
+ function resumeRenderer() {
1118
+ if (!paused) return;
1119
+ paused = false;
1120
+ if (liveUnsub) liveFlushTimer = setInterval(flushLive, LIVE_FLUSH_MS);
1121
+ historyPoll = setInterval(() => {
1122
+ if (settings.enabled && settings.source === "history") schedule();
1123
+ }, POLL_MS);
1124
+ schedule();
1125
+ }
1073
1126
  var livePosition = null;
1074
1127
  var liveDepth = null;
1075
1128
  var liveHeading;
@@ -1091,7 +1144,7 @@ function sampleLive() {
1091
1144
  pendingLive.push({ lon, lat, depth: liveDepth });
1092
1145
  }
1093
1146
  function flushLive() {
1094
- if (pendingLive.length === 0) return;
1147
+ if (paused || pendingLive.length === 0) return;
1095
1148
  const live = pendingLive;
1096
1149
  pendingLive = [];
1097
1150
  worker.postMessage({ type: "live", samples: live });
@@ -1099,14 +1152,30 @@ function flushLive() {
1099
1152
  if (lastReq) draw(true);
1100
1153
  }
1101
1154
  function draw(live = false) {
1102
- if (busy || !lastReq || !settings.enabled) return;
1155
+ if (paused || busy || !lastReq || !settings.enabled) return;
1103
1156
  if (live && Date.now() - lastLiveAt < liveInterval) return;
1104
1157
  busy = true;
1105
1158
  rendering = lastReq;
1106
1159
  renderingLive = live;
1107
1160
  dirty = false;
1161
+ clearTimeout(busyTimer);
1162
+ busyTimer = setTimeout(() => {
1163
+ if (!busy) return;
1164
+ console.warn("[LiveDepth] render worker did not answer; retrying");
1165
+ busy = false;
1166
+ rendering = null;
1167
+ if (lastReq) draw();
1168
+ }, 4e3);
1108
1169
  worker.postMessage({ type: "render", req: rendering });
1109
1170
  }
1171
+ worker.onerror = (e) => {
1172
+ console.warn("[LiveDepth] render worker error:", e.message);
1173
+ clearTimeout(busyTimer);
1174
+ busy = false;
1175
+ rendering = null;
1176
+ dirty = true;
1177
+ if (lastReq) draw();
1178
+ };
1110
1179
  worker.onmessage = (e) => {
1111
1180
  const msg = e.data;
1112
1181
  if (msg.type === "perf") {
@@ -1114,6 +1183,7 @@ worker.onmessage = (e) => {
1114
1183
  perfFrameMs = msg.frameMs;
1115
1184
  return;
1116
1185
  }
1186
+ clearTimeout(busyTimer);
1117
1187
  const req = rendering;
1118
1188
  const live = renderingLive;
1119
1189
  busy = false;
@@ -1154,6 +1224,7 @@ await client.subscribe(["overlay.state"], (_name, payload) => {
1154
1224
  const state = payload;
1155
1225
  if (!state.visible || state.suspended) {
1156
1226
  worker.postMessage({ type: "release" });
1227
+ fetchedBounds = null;
1157
1228
  stopLive();
1158
1229
  } else {
1159
1230
  startLive();
@@ -1164,7 +1235,11 @@ await client.subscribe(["state.changed"], () => void loadSettings());
1164
1235
  await loadMeta();
1165
1236
  await loadSettings();
1166
1237
  await loadUnits();
1167
- setInterval(() => {
1238
+ historyPoll = setInterval(() => {
1168
1239
  if (settings.enabled && settings.source === "history") schedule();
1169
1240
  }, POLL_MS);
1241
+ document.addEventListener("visibilitychange", () => {
1242
+ if (document.visibilityState === "hidden") pauseRenderer();
1243
+ else resumeRenderer();
1244
+ });
1170
1245
  //# sourceMappingURL=renderer.js.map