two-shots-later / vitruvian-vtt

A TTRP companion that is free, open-source and supported by the community.
BSD 3-Clause "New" or "Revised" License
2 stars 3 forks source link

Adding Support for Sending Data Between the Frontend and the Backend #28

Closed MrVintage710 closed 1 week ago

MrVintage710 commented 1 week ago

This PR is a small one, and is related to issue #26. This PR adds:

To send data over a command, you have to have something like this:

#[tauri::command]
fn get_test_data() -> Vec<Entity> {
    let mut entity : Entity = Entity::new();
    entity.add(Name("Test".to_string()));
    vec![entity]
}

and on the front end you do this:

let data = await invoke("get_test_data"); //Data should be {Name : "Test"}

To receive data on the backend, you do this:

#[tauri::command]
fn set_test_data(data : Value) {
    let mut entity : Entity = Entity::new();
    let name : Name = serde_json::from_value(data["Name"].clone()).unwrap();
    let damage : Damage = serde_json::from_value(data["Damage"].clone()).unwrap();
    entity.add(name);
    entity.add(damage);
    println!("Test {}", entity.to_string());
}

Sadly there is no functions that can do this nicely. Data that coms from the frontend must be sorted out by the backend. To send data from the front end you do this:

invoke("set_test_data", {data : entity});
Gearhartlove commented 1 week ago

Note: I really like the list registering of types, that's sweet!