dioxus_leaflet/
interop.rs

1use dioxus::prelude::*;
2use serde::Serialize;
3
4use crate::{MapMarker, MapOptions, MapPosition, Polygon};
5
6pub const DL_JS: Asset = asset!("/assets/dioxus_leaflet.js");
7const CALL_INIT_JS: &str = r#"
8while (!window.L || !window.DioxusLeaflet) {
9    await new Promise(cb => setTimeout(cb, 100));
10}
11await window.DioxusLeaflet.updateAsync(() => dioxus.recv(), (x) => dioxus.send(x));
12"#;
13
14#[derive(Serialize)]
15struct UpdateProps<'a> {
16    map_id: &'a str,
17    initial_position: &'a MapPosition,
18    markers: &'a Vec<MapMarker>,
19    polygons: &'a Vec<Polygon>,
20    options: &'a MapOptions,
21}
22
23pub async fn update(
24    map_id: &str, 
25    initial_position: &MapPosition, 
26    markers: &Vec<MapMarker>,
27    polygons: &Vec<Polygon>,
28    options: &MapOptions,
29) -> Result<(), String> {
30    let mut eval = document::eval(CALL_INIT_JS);
31
32    eval.send(UpdateProps { map_id, initial_position, markers, polygons, options })
33        .map_err(|e| e.to_string())?;
34
35    let ret = eval.recv::<Option<String>>().await
36        .map_err(|e| e.to_string())?;
37
38    if let Some(e) = ret {
39        Err(e)
40    }
41    else {
42        Ok(())
43    }
44}