reduce data exchanges across boundary

This commit is contained in:
vcoppe committed 2026-10-07 08:45:50 +02:00
1 parent da8a9e3d84
commit b843a18441
8 files changed
+573 -234

No files matched your search

+51 -20
View File
@@ -14,7 +14,7 @@ use std::cell::RefCell;
use wasm_bindgen::prelude::*;
use gpx_engine::{self as engine, Action, Command, Engine, FileId, LngLat, LngLatBounds};
use js_sys::{Array, BigInt64Array, Float64Array, Object, Reflect, Uint8Array};
use js_sys::{Array, BigInt64Array, Float64Array, Object, Reflect, Uint8Array, Uint32Array};
#[wasm_bindgen]
#[derive(Clone, Copy)]
@@ -146,32 +146,63 @@ stats_getter!(lng, Float64Array);
stats_getter!(lat, Float64Array);
stats_getter!(ele, Float64Array);
/// Timestamps in milliseconds since the epoch (`NO_TIME`, the smallest i64, when missing).
#[wasm_bindgen]
pub fn timestamps() -> BigInt64Array {
with_stats(|s| unsafe { BigInt64Array::view(&s.time) })
.unwrap_or_else(|| BigInt64Array::new_with_length(0))
// The optional values are `undefined` when no trackpoint of the selection has one, else they have
// an entry per trackpoint, the missing ones being NaN (`NO_TIME` for the timestamps).
macro_rules! optional_stats_getter {
($name:ident, $field:ident, $array:ident) => {
#[wasm_bindgen]
pub fn $name() -> Option<$array> {
with_stats(|s| {
s.$field
.as_ref()
.map(|values| unsafe { $array::view(values) })
})
.flatten()
}
};
}
// Timestamps in milliseconds since the epoch (`NO_TIME`, the smallest i64, when missing).
optional_stats_getter!(timestamps, time, BigInt64Array);
/// Value of the timestamps of the trackpoints that have none.
#[wasm_bindgen]
pub fn no_time() -> i64 {
engine::NO_TIME
}
stats_getter!(hr, Float64Array);
stats_getter!(cad, Float64Array);
stats_getter!(atemp, Float64Array);
stats_getter!(power, Float64Array);
optional_stats_getter!(hr, hr, Float64Array);
optional_stats_getter!(cad, cad, Float64Array);
optional_stats_getter!(atemp, atemp, Float64Array);
optional_stats_getter!(power, power, Float64Array);
// Surface, highway, SAC scale and MTB scale of the trackpoints: 0 when unknown, else 1 + the index
// in `surfaces()`, `highways()`, `sac_scales()` or `mtb_scales()`.
stats_getter!(surface, Uint8Array);
stats_getter!(highway, Uint8Array);
stats_getter!(sac_scale, Uint8Array);
stats_getter!(mtb_scale, Uint8Array);
// Surface, highway, SAC scale and MTB scale of the trackpoints, as intervals of trackpoints that
// have the same value: `<name>_starts` holds the index of the first trackpoint of each interval,
// and `<name>_values` its value (0 when unknown, else 1 + the index in `surfaces()`,
// `highways()`, `sac_scales()` or `mtb_scales()`). A value that is unknown for every trackpoint is
// a single interval.
macro_rules! intervals_getters {
($starts:ident, $values:ident, $field:ident) => {
#[wasm_bindgen]
pub fn $starts() -> Uint32Array {
with_stats(|s| unsafe { Uint32Array::view(&s.$field.starts) })
.unwrap_or_else(|| Uint32Array::new_with_length(0))
}
/// The names of the surfaces, in the order of their codes (see `surface`). The list only grows:
#[wasm_bindgen]
pub fn $values() -> Uint8Array {
with_stats(|s| unsafe { Uint8Array::view(&s.$field.values) })
.unwrap_or_else(|| Uint8Array::new_with_length(0))
}
};
}
intervals_getters!(surface_starts, surface_values, surface);
intervals_getters!(highway_starts, highway_values, highway);
intervals_getters!(sac_scale_starts, sac_scale_values, sac_scale);
intervals_getters!(mtb_scale_starts, mtb_scale_values, mtb_scale);
/// The names of the surfaces, in the order of their codes (see `surface_values`). The list only grows:
/// a name keeps its index.
#[wasm_bindgen]
pub fn surfaces() -> StringList {
@@ -180,7 +211,7 @@ pub fn surfaces() -> StringList {
.unchecked_into()
}
/// The names of the highways, in the order of their codes (see `highway`).
/// The names of the highways, in the order of their codes (see `highway_values`).
#[wasm_bindgen]
pub fn highways() -> StringList {
with_engine(|e| array(e.categories().highway.names(), |name| name.as_str().into()))
@@ -188,7 +219,7 @@ pub fn highways() -> StringList {
.unchecked_into()
}
/// The names of the SAC scales, in the order of their codes (see `sac_scale`).
/// The names of the SAC scales, in the order of their codes (see `sac_scale_values`).
#[wasm_bindgen]
pub fn sac_scales() -> StringList {
with_engine(|e| {
@@ -200,7 +231,7 @@ pub fn sac_scales() -> StringList {
.unchecked_into()
}
/// The names of the MTB scales, in the order of their codes (see `mtb_scale`).
/// The names of the MTB scales, in the order of their codes (see `mtb_scale_values`).
#[wasm_bindgen]
pub fn mtb_scales() -> StringList {
with_engine(|e| {