0

I have a project that contains Tauri backend and Yew frontend. I'm trying to save a form variable to file with tauri_plugin_store library. But the documentation is horrible and I'm not sure how to call store variable from my frontend or just from save_to_file function to be able to save data there. This is how my Tauri main.rs looks like


// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::Error;
use tauri_plugin_store::StoreBuilder;
use serde_json::json;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greets(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn save_to_file(day: &str, text: &str, parent: &str) -> Result<(), Error>{
    println!(">>>>{}", text);
    return Ok(())
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greets, save_to_file])
        .plugin(tauri_plugin_store::Builder::default().build())
        .setup(|app| {
            let mut store = StoreBuilder::new(app.handle(), "/Users/a/Desktop/tauri_project/store.bin".parse()?).build();
            Ok(store.insert("a".to_string(), json!("b"))?)
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

As you can see I have save_to_file() to saving my data. I'm invoking this function from my form.rs inside of Yew app. I'm able to call it and pass the data, but no save it, because store is defined in main as local variable


fn execute_effect(user_record: Record) {
    spawn_local(async move {
        if user_record.day == "" || user_record.text == "" || user_record.parent == "" {
            return;
        } else {
            let args = to_value(&Record {
                day: user_record.day.clone(),
                text: user_record.text.clone(),
                parent: user_record.parent.clone(),
            })
            .unwrap();

            let new_msg = invoke("save_to_file", args).await.as_string();
            log!("{:#?}", new_msg);
        }
    });
}

Is there some workaround this?

I saw this example at the github page


//As you may have noticed, the Store crated above isn't accessible to the frontend. To interoperate with stores created by JS use the exported with_store method:

use tauri::Wry;
use tauri_plugin_store::with_store;

let stores = app.state::<StoreCollection<Wry>>();
let path = PathBuf::from("path/to/the/storefile");

with_store(app_handle, stores, path, |store| store.insert("a".to_string(), json!("b")))

But this really says nothing... what is StoreCollection? How can I import tauri from inside the Yew? Wtf is app??

Marcel Kopera
  • 179
  • 11

0 Answers0