Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tauri State being used for filepath constant #31

Open
mberry opened this issue Sep 6, 2023 · 1 comment
Open

Tauri State being used for filepath constant #31

mberry opened this issue Sep 6, 2023 · 1 comment

Comments

@mberry
Copy link
Collaborator

mberry commented Sep 6, 2023

The outcome of parsing the CLI is sent to tauri and then called back from it in multiple places, even though it doesn't change.

Sent to tauri:

     builder
        .manage(cli)

Pulled back from tauri:

async fn read_link(name: String, state: tauri::State<'_, Cli>) -> Result<LinkWrapper, ()> {
    let file_path = format!("{}/{name}", &state.path);
    let link = Link::load(&state.path, &file_path).expect(&format!("Error loading {file_path}"));

Really these could just be the global statics rather than state. There's a lazy static macro in use but OnceLock is also in the standard library now, which could initialise the global.

Which instead would look like:

pub static ARK_SHELF_WORKING_DIR: OnceLock<PathBuf> = OnceLock::new();
pub static SCORES_PATH: OnceLock<PathBuf> = OnceLock::new();

fn main() {
    let cli = Cli::parse();
    let base_dir = match cli.path {
        Ok(path) => PathBuf::from(path),
        // This is using platform defaults but otherwise stick with home_dir() + ark-shelf
        None => ProjectDirs::from("dev", "Ark Builders",  "Shelf-Desktop")
                .map_or(PathBuf::from(""), |proj| PathBuf::from(proj.data_dir()))
    };

    // Initialise global constants
    ARK_SHELF_WORKING_DIR.set(base_dir.clone()).expect("Setting Working Dir");
    SCORES_PATH.set(base_dir.join("scores")).expect("Setting Scores Path");

}
@gwendalF
Copy link
Collaborator

gwendalF commented Sep 6, 2023

Totally, would be beneficial since the working dir is constant. OnceLock being in the standard library I think it's better than using lazy static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants