Files
gpx.studio/website/src/lib/engine.ts
T

361 lines
12 KiB
TypeScript
Raw Normal View History

2026-10-03 19:50:38 +02:00
import { browser } from '$app/environment';
2026-10-04 08:21:39 +02:00
import { get, writable, type Readable, type Writable } from 'svelte/store';
2026-10-04 08:53:00 +02:00
import { FileColorAllocator, normalizeColor } from '$lib/file-colors';
2026-10-04 08:21:39 +02:00
import type { Feature, FeatureCollection, LineString, Point } from 'geojson';
import type { FileStatistics, FileStructure, Selection } from 'gpx-rs';
2026-09-25 12:09:58 +02:00
2026-10-03 19:50:38 +02:00
export type {
2026-10-04 08:21:39 +02:00
FileStatistics,
2026-10-03 19:50:38 +02:00
FileStructure,
FilesUpdate,
Selection,
TrackNode,
SegmentNode,
WaypointNode,
} from 'gpx-rs';
type Wasm = typeof import('gpx-rs');
2026-10-04 08:21:39 +02:00
export type SegmentProperties = {
fileId: string;
trackId: string;
segmentId: string;
trackIndex: number;
segmentIndex: number;
/** Changes when the trackpoints of the segment change. */
rev: string;
2026-10-04 08:53:00 +02:00
/** Color of the track, or else the base color of the file. */
color: string;
/** Only when the track defines it. */
opacity?: number;
width?: number;
2026-10-04 08:21:39 +02:00
};
export type WaypointProperties = {
fileId: string;
waypointId: string;
index: number;
name?: string;
2026-10-04 08:53:00 +02:00
sym?: string;
2026-10-04 08:21:39 +02:00
};
/** Everything the UI knows about one file. */
export type FileState = {
structure: FileStructure;
2026-10-04 08:53:00 +02:00
/** Color of the file: the one defined by its tracks if any, otherwise one from the palette. */
color: string;
2026-10-04 08:21:39 +02:00
/** Global statistics of the file. */
statistics: FileStatistics;
/** One LineString feature per track segment, in file order. */
segments: FeatureCollection<LineString, SegmentProperties>;
/** One Point feature per waypoint, in file order. */
waypoints: FeatureCollection<Point, WaypointProperties>;
};
const EMPTY_STATISTICS: FileStatistics = { totalDistance: 0, elevationGain: 0, elevationLoss: 0 };
function toPositions(flat: Float64Array): [number, number][] {
const positions: [number, number][] = new Array(flat.length / 2);
for (let i = 0; i < positions.length; i++) {
positions[i] = [flat[2 * i], flat[2 * i + 1]];
}
return positions;
}
2026-10-03 19:50:38 +02:00
/** Hyphenated UUID strings (as found in the file structures) to the concatenated 16-byte form. */
function idsToBytes(ids: string[]): Uint8Array {
const bytes = new Uint8Array(ids.length * 16);
ids.forEach((id, i) => {
const hex = id.replaceAll('-', '');
for (let j = 0; j < 16; j++) {
bytes[i * 16 + j] = parseInt(hex.slice(2 * j, 2 * j + 2), 16);
}
});
return bytes;
}
/**
* Thin wrapper around the WASM engine.
*
* The engine owns the files; this class mirrors what the UI needs in stores. After each action,
2026-10-04 08:21:39 +02:00
* only what the engine reports as changed is read again: the order of the files, and the state
* of the files that were added or modified. Within a modified file, the GeoJSON features of the
* segments and waypoints that did not change are reused.
2026-10-03 19:50:38 +02:00
*/
2026-09-25 12:09:58 +02:00
class Engine {
2026-10-03 19:50:38 +02:00
private wasm: Wasm | null = null;
/** Resolves once the engine can be used (never on the server). */
readonly ready: Promise<void>;
private _order = writable<string[]>([]);
2026-10-04 08:53:00 +02:00
private _colors = new FileColorAllocator();
2026-10-04 08:21:39 +02:00
private _files = writable<Map<string, Writable<FileState>>>(new Map());
2026-10-03 19:50:38 +02:00
private _selection = writable<Selection>({ type: 'empty' });
/** Ids of the files in display order. */
readonly order: Readable<string[]> = { subscribe: this._order.subscribe };
/** What is currently selected. */
readonly selection: Readable<Selection> = { subscribe: this._selection.subscribe };
2026-10-04 08:21:39 +02:00
/**
* The state of each file, in its own store. The map store only notifies when files are added
* or removed; editing a file only notifies the subscribers of that file's store.
*/
readonly files: Readable<Map<string, Readable<FileState>>> = {
subscribe: this._files.subscribe,
2026-10-03 19:50:38 +02:00
};
2026-09-25 12:09:58 +02:00
constructor() {
2026-10-03 19:50:38 +02:00
this.ready = browser
? import('gpx-rs').then((wasm) => {
wasm.start();
this.wasm = wasm;
})
: new Promise(() => {});
2026-09-25 12:09:58 +02:00
}
2026-10-03 19:50:38 +02:00
// Actions. Each one resolves to whether the engine changed something.
newFile(name: string) {
return this.run((w) => w.new_file(name));
2026-09-25 12:09:58 +02:00
}
2026-10-03 19:50:38 +02:00
loadFile(data: Uint8Array) {
return this.run((w) => w.load_file(data));
}
duplicate() {
return this.run((w) => w.duplicate());
}
delete() {
return this.run((w) => w._delete());
}
deleteAll() {
return this.run((w) => w.delete_all());
}
undo() {
return this.run((w) => w.undo());
}
redo() {
return this.run((w) => w.redo());
}
select(fileIds: string[]) {
return this.run((w) => w.select(idsToBytes(fileIds)));
}
addSelect(fileIds: string[]) {
return this.run((w) => w.add_select(idsToBytes(fileIds)));
}
selectAll() {
return this.run((w) => w.select_all());
}
/** Moves the files, in the given order, to `index` among the other files. */
reorder(fileIds: string[], index: number) {
return this.run((w) => w.reorder(idsToBytes(fileIds), index));
}
// Coordinates, as flat [lng, lat, ...] arrays. They are copied out of the WASM memory.
// Refetch them when the `rev` of the segment (or the `waypointsRev` of the file) changed.
segmentCoordinates(segmentId: string): Float64Array {
return this.wasm?.segment_coordinates(segmentId).slice() ?? new Float64Array();
}
waypointCoordinates(fileId: string): Float64Array {
return this.wasm?.waypoint_coordinates(fileId).slice() ?? new Float64Array();
}
private async run(action: (wasm: Wasm) => boolean): Promise<boolean> {
await this.ready;
const wasm = this.wasm!;
const changed = action(wasm);
this.sync(wasm);
return changed;
}
/** Reads what the last action changed. */
private sync(wasm: Wasm) {
const update = wasm.last_update();
if (update.selectionChanged) {
this._selection.set(wasm.selection());
}
if (update.orderChanged) {
this._order.set(wasm.file_order());
}
2026-10-04 08:21:39 +02:00
const added: [string, FileState][] = [];
for (const id of update.added) {
const state = this.readFile(wasm, id);
if (state) {
added.push([id, state]);
}
}
if (added.length > 0 || update.removed.length > 0) {
this._files.update((files) => {
const next = new Map(files);
2026-10-04 08:53:00 +02:00
update.removed.forEach((id) => {
next.delete(id);
this._colors.release(id);
});
2026-10-04 08:21:39 +02:00
added.forEach(([id, state]) => next.set(id, writable(state)));
2026-10-03 19:50:38 +02:00
return next;
});
}
2026-10-04 08:21:39 +02:00
if (update.modified.length > 0) {
const files = get(this._files);
for (const id of update.modified) {
const store = files.get(id);
const state = store && this.readFile(wasm, id, get(store));
if (store && state) {
store.set(state);
}
}
}
}
/** Reads the state of a file, reusing from `previous` what did not change. */
private readFile(wasm: Wasm, id: string, previous?: FileState): FileState | null {
const structure = wasm.file_structure(id);
if (!structure) {
return null;
}
2026-10-04 08:53:00 +02:00
const color = this._colors.resolve(
id,
structure.tracks.map((track) => track.color)
);
2026-10-04 08:21:39 +02:00
const previousSegments = new Map(
previous?.segments.features.map((f) => [f.properties.segmentId, f]) ?? []
);
const segments: Feature<LineString, SegmentProperties>[] = [];
structure.tracks.forEach((track, trackIndex) => {
track.segments.forEach((segment, segmentIndex) => {
2026-10-04 08:53:00 +02:00
const properties: SegmentProperties = {
fileId: id,
trackId: track.id,
segmentId: segment.id,
trackIndex,
segmentIndex,
rev: segment.rev,
color: track.color !== undefined ? normalizeColor(track.color) : color,
opacity: track.opacity,
width: track.width,
};
2026-10-04 08:21:39 +02:00
const old = previousSegments.get(segment.id);
const unchanged =
2026-10-04 08:53:00 +02:00
old !== undefined &&
(Object.keys(properties) as (keyof SegmentProperties)[]).every(
(key) => old.properties[key] === properties[key]
);
2026-10-04 08:21:39 +02:00
segments.push(
unchanged
? old
: {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: toPositions(wasm.segment_coordinates(segment.id)),
},
2026-10-04 08:53:00 +02:00
properties,
2026-10-04 08:21:39 +02:00
}
);
});
});
const previousStructure = previous?.structure;
const waypointsUnchanged =
previousStructure !== undefined &&
previousStructure.waypointsRev === structure.waypointsRev &&
previousStructure.waypoints.length === structure.waypoints.length &&
previousStructure.waypoints.every(
(w, i) =>
2026-10-04 08:53:00 +02:00
w.id === structure.waypoints[i].id &&
w.name === structure.waypoints[i].name &&
w.sym === structure.waypoints[i].sym
2026-10-04 08:21:39 +02:00
);
let waypoints = previous?.waypoints;
if (!waypoints || !waypointsUnchanged) {
const coordinates = toPositions(wasm.waypoint_coordinates(id));
waypoints = {
type: 'FeatureCollection',
features: structure.waypoints.map((waypoint, index) => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: coordinates[index] },
2026-10-04 08:53:00 +02:00
properties: {
fileId: id,
waypointId: waypoint.id,
index,
name: waypoint.name,
sym: waypoint.sym,
},
2026-10-04 08:21:39 +02:00
})),
};
}
return {
structure,
2026-10-04 08:53:00 +02:00
color,
2026-10-04 08:21:39 +02:00
statistics: wasm.file_statistics(id) ?? EMPTY_STATISTICS,
segments: { type: 'FeatureCollection', features: segments },
waypoints,
};
2026-09-25 12:09:58 +02:00
}
}
export const engine = new Engine();
2026-10-04 08:26:15 +02:00
export type FileStatesCallback = (files: Map<string, Readable<FileState>>) => void;
/**
* Tells when file stores appear and disappear, so that their subscribers know when to
* subscribe and unsubscribe. `onFilesAdded` is called with the stores of the new files only
* (including the ones already present when the observer is created).
*/
export class FileStateCollectionObserver {
private _fileIds = new Set<string>();
private _onFilesAdded: FileStatesCallback;
private _onFileRemoved: (fileId: string) => void;
private _onDestroy: () => void;
private _unsubscribe: () => void;
constructor(
onFilesAdded: FileStatesCallback,
onFileRemoved: (fileId: string) => void,
onDestroy: () => void
) {
this._onFilesAdded = onFilesAdded;
this._onFileRemoved = onFileRemoved;
this._onDestroy = onDestroy;
this._unsubscribe = engine.files.subscribe((files) => {
this._fileIds.forEach((fileId) => {
if (!files.has(fileId)) {
this._onFileRemoved(fileId);
this._fileIds.delete(fileId);
}
});
const newFiles = new Map<string, Readable<FileState>>();
files.forEach((store, fileId) => {
if (!this._fileIds.has(fileId)) {
newFiles.set(fileId, store);
this._fileIds.add(fileId);
}
});
if (newFiles.size > 0) {
this._onFilesAdded(newFiles);
}
});
}
destroy() {
this._onDestroy();
this._unsubscribe();
}
}