mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-04 21:04:08 +00:00
progress
This commit is contained in:
+107
-31
@@ -2,75 +2,151 @@
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use js_sys::Function;
|
||||
use js_sys::{Float64Array, Function, Int32Array};
|
||||
use uuid::Uuid;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::{
|
||||
gpx::GPXFile,
|
||||
buffer::StatisticsBuffer,
|
||||
gpx::{File, FileId},
|
||||
io::parse,
|
||||
selection::Selection,
|
||||
state::{Stack, StackEntry},
|
||||
statistics::StatisticsBuffer,
|
||||
statistics::StatisticsCache,
|
||||
};
|
||||
|
||||
extern crate console_error_panic_hook;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[wasm_bindgen]
|
||||
pub struct Engine {
|
||||
stack: Stack,
|
||||
selection: Selection,
|
||||
statistics: StatisticsBuffer,
|
||||
callback: Function,
|
||||
statistics_cache: StatisticsCache,
|
||||
statistics_buffer: StatisticsBuffer,
|
||||
callback: Option<Function>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Engine {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(callback: Function) -> Self {
|
||||
console_error_panic_hook::set_once();
|
||||
Self {
|
||||
stack: Default::default(),
|
||||
selection: Default::default(),
|
||||
statistics: Default::default(),
|
||||
callback,
|
||||
statistics_cache: Default::default(),
|
||||
statistics_buffer: Default::default(),
|
||||
callback: Some(callback),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn total_distance(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.total_distance) }
|
||||
}
|
||||
|
||||
pub fn moving_distance(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.moving_distance) }
|
||||
}
|
||||
|
||||
pub fn total_time(&self) -> Int32Array {
|
||||
unsafe { Int32Array::view(&self.statistics_buffer.total_time) }
|
||||
}
|
||||
|
||||
pub fn moving_time(&self) -> Int32Array {
|
||||
unsafe { Int32Array::view(&self.statistics_buffer.moving_time) }
|
||||
}
|
||||
|
||||
pub fn speed(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.speed) }
|
||||
}
|
||||
|
||||
pub fn elevation_gain(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.elevation_gain) }
|
||||
}
|
||||
|
||||
pub fn elevation_loss(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.elevation_loss) }
|
||||
}
|
||||
|
||||
pub fn slope(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.slope) }
|
||||
}
|
||||
|
||||
pub fn slope_segment_slope(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.slope_segment_slope) }
|
||||
}
|
||||
|
||||
pub fn slope_segment_distance(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.slope_segment_distance) }
|
||||
}
|
||||
|
||||
pub fn create_file(&mut self, name: &str) {
|
||||
let mut file = GPXFile::default();
|
||||
file.info.name = name.to_string();
|
||||
self.stack.create_and_push_next(|entry: &mut StackEntry| {
|
||||
self.event_loop(|entry| {
|
||||
let mut file = File::default();
|
||||
file.info.name = name.to_string();
|
||||
entry.insert(file.id, Rc::new(file));
|
||||
true
|
||||
});
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn load_file(&mut self, data: &[u8]) {
|
||||
if let Ok(file) = parse(data) {
|
||||
self.stack.create_and_push_next(|entry: &mut StackEntry| {
|
||||
self.event_loop(|entry| {
|
||||
if let Ok(file) = parse(data) {
|
||||
entry.insert(file.id, Rc::new(file));
|
||||
true
|
||||
});
|
||||
}
|
||||
let _ = self.callback.call1(&JsValue::NULL, &JsValue::from("done!"));
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn delete_file(&mut self, id: &[u8]) {
|
||||
if let Ok(id) = Uuid::from_slice(id) {
|
||||
self.stack
|
||||
.create_and_push_next(|entry: &mut StackEntry| entry.remove(&id).is_some());
|
||||
}
|
||||
self.event_loop(|entry| {
|
||||
if let Ok(id) = Uuid::from_slice(id) {
|
||||
let id = FileId(id);
|
||||
entry.remove(&id).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn event_loop(&mut self) {
|
||||
// Control flow
|
||||
// - get lock to cancel any other updates
|
||||
// - update files
|
||||
// - update separate statistics
|
||||
// - update selection
|
||||
// - update selection statistics
|
||||
// self.statistics.update(stats);
|
||||
// - notify UI through callback (changed files, updated selection, new stats)
|
||||
fn event_loop<F>(&mut self, f: F)
|
||||
where
|
||||
F: FnOnce(&mut StackEntry) -> bool,
|
||||
{
|
||||
if let Some(diff) = self.stack.create_and_push_next(f) {
|
||||
self.selection.select(diff.added[0]);
|
||||
self.statistics_cache.update(self.stack.current());
|
||||
self.statistics_buffer.update(
|
||||
&self
|
||||
.statistics_cache
|
||||
.get(self.stack.current(), &self.selection),
|
||||
);
|
||||
if let Some(c) = &self.callback {
|
||||
let _ = c.call0(&JsValue::NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_load_file() {
|
||||
let mut engine = Engine::default();
|
||||
|
||||
let mut f = File::open("data/simple.gpx").unwrap();
|
||||
let mut data = String::new();
|
||||
let _ = f.read_to_string(&mut data);
|
||||
|
||||
engine.load_file(data.as_bytes());
|
||||
|
||||
assert_eq!(engine.statistics_buffer.total_distance.len(), 80);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user