2026-07-14 18:36:30 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
2026-09-25 12:09:58 +02:00
|
|
|
use js_sys::Function;
|
2026-09-23 19:53:39 +02:00
|
|
|
use uuid::Uuid;
|
2026-07-14 18:36:30 +02:00
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
2026-09-23 19:53:39 +02:00
|
|
|
use crate::{
|
|
|
|
|
gpx::GPXFile,
|
2026-09-24 20:55:30 +02:00
|
|
|
io::parse,
|
|
|
|
|
selection::Selection,
|
2026-09-30 19:19:23 +02:00
|
|
|
state::{Stack, StackEntry},
|
2026-09-23 19:53:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Control flow
|
|
|
|
|
// - get lock to cancel any other updates
|
|
|
|
|
// - update files
|
|
|
|
|
// - update separate statistics
|
|
|
|
|
// - update selection
|
|
|
|
|
// - update selection statistics
|
2026-09-25 12:09:58 +02:00
|
|
|
// - notify UI through callback (changed files, updated selection, new stats)
|
2026-07-14 18:36:30 +02:00
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2026-09-25 12:09:58 +02:00
|
|
|
pub struct Engine {
|
2026-07-14 18:36:30 +02:00
|
|
|
stack: Stack,
|
2026-09-24 20:55:30 +02:00
|
|
|
selection: Selection,
|
2026-09-25 12:09:58 +02:00
|
|
|
callback: Function,
|
2026-07-14 18:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2026-09-25 12:09:58 +02:00
|
|
|
impl Engine {
|
2026-07-14 18:36:30 +02:00
|
|
|
#[wasm_bindgen(constructor)]
|
2026-09-25 12:09:58 +02:00
|
|
|
pub fn new(callback: Function) -> Self {
|
2026-07-14 18:36:30 +02:00
|
|
|
Self {
|
2026-09-24 20:55:30 +02:00
|
|
|
stack: Default::default(),
|
|
|
|
|
selection: Default::default(),
|
2026-09-25 12:09:58 +02:00
|
|
|
callback,
|
2026-07-14 18:36:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 19:53:39 +02:00
|
|
|
#[wasm_bindgen]
|
|
|
|
|
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| {
|
|
|
|
|
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| {
|
|
|
|
|
entry.insert(file.id, Rc::new(file));
|
|
|
|
|
true
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-09-25 12:09:58 +02:00
|
|
|
let _ = self.callback.call1(&JsValue::NULL, &JsValue::from("done!"));
|
2026-09-23 19:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-14 18:36:30 +02:00
|
|
|
}
|