tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
920 stars 254 forks source link

Plugin Store StoreExt ctx doesnt exist #1890

Open nickheyer opened 1 week ago

nickheyer commented 1 week ago

The below example seems to be no longer valid in V2. Please update V2 docs when a plugin has changed it's functionality. (See https://github.com/tauri-apps/tauri-plugin-store/tree/v2 for partially updated docs)

use tauri::Wry;
use tauri_plugin_store::StoreExt;
use serde_json::json;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_store::Builder::default().build())
        .setup(|app| {
            let store = app.handle().store_builder("store.bin").build();

            // Note that values must be serde_json::Value instances,
            // otherwise, they will not be compatible with the JavaScript bindings.
            store.insert("some-key", json!({ "value": 5 }))?;

            // Get a value from the store.
            let value = store.get("some-key").expect("Failed to get value from store");
            println!("{}", value); // {"value":5}

            // You can manually save the store after making changes.
            // Otherwise, it will save upon graceful exit as described above.
            store.save()?;

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

(Primary plugin docs here: https://v2.tauri.app/plugin/store/)

cleveng commented 1 week ago
    let mut defaults = std::collections::HashMap::new();
    defaults.insert(DISPLAY_CONFIG.to_string(), json!(get_display_config()));
    defaults.insert(SWIPER_CONFIG.to_string(), json!(get_swiper_config()));

    let store = app
        .store_builder("store.bin")
        .defaults(defaults)
        .auto_save(Duration::from_millis(100))
        .build();

    let _ = store.load().expect("file is not existed");

panic: file is not existed: Io(Os { code: 2, kind: NotFound, message: "没有那个文件或目录" }) i don't know how to fix it .

Legend-Master commented 1 week ago

@nickheyer StoreExt does exist, the problem is that it should be a ? at the end of build() and insert is actually set

            let store = app.handle().store_builder("store.bin").build()?;

            // Note that values must be serde_json::Value instances,
            // otherwise, they will not be compatible with the JavaScript bindings.
            store.set("some-key", json!({ "value": 5 }));

@cleveng Don't call let _ = store.load().expect("file is not existed");, the store builder will load the store on build

I'm still working on a store plugin rework here at #1860, and we can improve the documentation after it's merged

nickheyer commented 5 days ago

@Legend-Master Is there currently a workaround for #1860 ? As last I checked, it was breaking my app. Also, am I understanding this right - there is no way to share a store between js and rust?

Legend-Master commented 5 days ago

There're no way to share them easily, if you really need a workaround, you can put the store manually to webview's resource table and emit the resource id to js side and create the store with that resource id manually

nickheyer commented 9 hours ago

Is this done? I saw pr was merged.

Legend-Master commented 2 hours ago

Yeah, we just need to release it and get the docs updated