Files
TREK/client/src/repo/tourPointsRepo.ts
T
Maurice f57a0dbd31 fix(tours): let an unwanted stage go and read a tour's stages once
Two review findings of MDT-02 in the planner's list and the day plan:

- A stage asked for while the draft has unsaved changes stayed pending when
  the changes were kept or the tour failed to open, so opening that tour
  later from its row jumped to a stage nobody asked for then. The rail now
  has cancel(), which the discard question calls when it is dismissed and
  the open calls when it fails, and it lets a stage go once the opened
  tour's points are in and none of its stages starts at that stop.
- Every day holding a stage of a tour read the whole tour on its own, so a
  five stage tour sent five identical requests on top of the list's own.
  tourPointsRepo shares one read per trip, tour and revision among the
  views showing it at the same time, through the tour repo as before, and
  drops it once the last view lets go or the read fails.
2026-10-20 15:28:23 +02:00

53 lines
1.9 KiB
TypeScript

import type { TourWaypoint } from '@trek/shared';
import { tourRepo } from './tourRepo';
/** One read of a tour's saved points, shared by everyone showing that tour at that revision. */
interface SharedRead {
points: Promise<readonly TourWaypoint[]>;
controller: AbortController;
holders: number;
}
const reads = new Map<string, SharedRead>();
/**
* MDT-02: the saved points of a tour, read once however many views show it at
* the same time: the fold in the planner's list and every day of the plan
* holding one of its stages would otherwise each ask for the same tour.
*/
export const tourPointsRepo = {
/**
* The points of the tour at this revision, through the tour repo (the cache
* standing in offline). A view holds the read while it shows the tour and
* releases it when it stops; once nobody holds it, it is dropped and, while
* still on its way, aborted. A failed read is dropped at once, so the next
* view to ask reads again, and a new revision is a read of its own.
*/
hold(tripId: number | string, placeId: number, revision: string) {
const key = `${tripId}:${placeId}:${revision}`;
const read = reads.get(key) ?? start(key, tripId, placeId);
read.holders += 1;
let held = true;
const release = () => {
if (!held) return;
held = false;
read.holders -= 1;
if (read.holders > 0) return;
if (reads.get(key) === read) reads.delete(key);
read.controller.abort();
};
return { points: read.points, release };
},
};
function start(key: string, tripId: number | string, placeId: number): SharedRead {
const controller = new AbortController();
const points = tourRepo.detail(tripId, placeId, controller.signal).then((detail) => detail.waypoints);
const read: SharedRead = { points, controller, holders: 0 };
points.catch(() => {
if (reads.get(key) === read) reads.delete(key);
});
reads.set(key, read);
return read;
}