#[tauri-plugin-store] accessing a store on the frontend that was created on the rust layer?

1 messages · Page 1 of 1 (latest)

somber shell
#

Here is my fn main

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![select_directory,scan_directory, check_audio_channels, load_audio_file ])
        .plugin(tauri_plugin_store::Builder::default().build())
        .setup(|app| {
            // Get the path to the app's configuration directory
            let config_dir = app.path_resolver().app_config_dir().ok_or_else(|| {
                eprintln!("Failed to resolve app config directory");
                Box::<dyn std::error::Error>::from("Failed to resolve app config directory")
            })?;
            let settings_path = config_dir.join("settings.json");

            // Create the store
            let mut store = StoreBuilder::new(app.handle(), settings_path.clone()).build();

            // Insert the initial value for baseFolder
            match store.insert("baseFolder".to_string(), json!("testing")) {
                Ok(_) => {
                    match store.save() {
                        Ok(_) => Ok(()),
                        Err(e) => {
                            eprintln!("Failed to save store: {}", e);
                            Err(Box::new(e))
                        }
                    }
                },
                Err(e) => {
                    eprintln!("Failed to insert initial value: {}", e);
                    Err(Box::new(e))
                }
            }?;

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Now my question is does doing this on the frontend create a new instance of the store thats different from the one we created on the backend?

import { appConfigDir } from '@tauri-apps/api/path';
import { Store } from 'tauri-plugin-store-api';

const appConfigDirPath = await appConfigDir();

export const store = new Store(appConfigDirPath + 'settings.json');
lofty jacinth
#

I am not quite sure

frank needle
#

the plugin is supposed to share all the stores and identify them by the path so it should work, but as AHQ said, just try it out please x)

somber shell
#

@frank needle @lofty jacinth it does indeed work, I can read and write to it from the JS layer. However having issues persisting values on app restart, it overwrites the value set from another function and writes an empty string.

#

updated main.rs:

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![])
        .plugin(tauri_plugin_persisted_scope::init())
        .plugin(tauri_plugin_store::Builder::default().build())
        .setup(|app| {
            // Get the path to the app's configuration directory
            let config_dir = app.path_resolver().app_config_dir().ok_or_else(|| {

            eprintln!("Failed to resolve app config directory");

            Box::<dyn std::error::Error>::from("Failed to resolve app config directory")
            })?;
        
            let settings_path = config_dir.join("settings.json");

            // Create the store
            let mut store =     StoreBuilder::new(app.handle(), settings_path.clone()).build();

            // Check if baseFolder already has a value in the store
            let base_folder_value = store.get("baseFolder".to_string());
            println!("Initial baseFolder value: {:?}", base_folder_value);

            if base_folder_value.is_none()  {
            println!("baseFolder value is not set, initializing...");
                // Insert the initial value for baseFolder as an empty string if it doesn't exist
                match store.insert("baseFolder".to_string(), json!("")) {
                    Ok(_) => {
                        match store.save() {
                            Ok(_) => Ok(()),
                            Err(e) => {
                                eprintln!("Failed to save store: {}", e);
                                Err(Box::new(e))
                            }
                        }
                    },
                    Err(e) => {
                        eprintln!("Failed to insert initial value: {}", e);
                        Err(Box::new(e))
                    }
                }?;
            }   

        Ok(())
    })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}