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

Looking for a way to access Tauri App or AppHandle from within router. #244

Closed
harrismcc opened this issue Nov 8, 2023 · 4 comments
Closed

Comments

@harrismcc
Copy link

I'm using the Tauri transport for rspc, and I'm having some trouble with context. I need to be able to pass either the app (tauri::App) or app handle (tauri::AppHandle) into my router via context, but I can't figure out a way to do this.

If I create the tauri plugin at the "top level" like the docs demonstrate (by chaining it to tauri::Builder::default().plugin(...)), then I don't have access to app to pass in since it's passed into closures in later chained calls like setup().

When I try to create the plugin from within the setup() function like app.handle().plugin(rspc::integrations::tauri::plugin(...) I get yelled at by the compiler if I try to pass in anything to do with the app or AppHandle.

Is there a supported/recommended way to access the App or AppHandle using context? Or, if not, a workaround to access it from my router?

@oscartbeaumont
Copy link
Member

This has been fixed on the main branch. The closure passed to the plugin takes a required argument for the app handle. The main branch does have a lot of other breaking changes which aren't documented so upgrading isn't super easy atm. I apologize there isn't a better solution.

I get yelled at by the compiler

What exact error? If Tauri supports delayed plugin mounting it would theoretically be allowed to capture the handle as long as the closure has move on it like move || {}.

@harrismcc
Copy link
Author

Thanks for the help. If I try to use basically anything from app in the context I get an error mesasge like this. Doesn't seem to matter if I try to pass in the AppHandle itself, or try to distill it down to app_data_dir (the value I'm after in this case).

error[E0277]: `(dyn tauri_runtime_wry::Plugin<EventLoopMessage> + 'static)` cannot be shared between threads safely
    --> src/main.rs:92:17
     |
90   |               app.handle().plugin(rspc::integrations::tauri::plugin(
     |                                   --------------------------------- required by a bound introduced by this call
91   |                   ipc::build_router(),
92   | /                 move || ipc::Ctx {
93   | |                     db: Arc::clone(&db),
94   | |                     app_data_dir: app
95   | |                         .handle()
...    |
100  | |                         .to_string(),
101  | |                 },
     | |_________________^ `(dyn tauri_runtime_wry::Plugin<EventLoopMessage> + 'static)` cannot be shared between threads safely
     |
     = help: the trait `Sync` is not implemented for `(dyn tauri_runtime_wry::Plugin<EventLoopMessage> + 'static)`
     = note: required for `std::ptr::Unique<(dyn tauri_runtime_wry::Plugin<EventLoopMessage> + 'static)>` to implement `Sync`
note: required because it appears within the type `Box<dyn Plugin<EventLoopMessage>>`
    --> /private/tmp/rust-20230816-6788-aokyj4/rustc-1.71.1-src/library/alloc/src/boxed.rs:195:12
     = note: required for `std::ptr::Unique<Box<(dyn tauri_runtime_wry::Plugin<EventLoopMessage> + 'static)>>` to implement `Sync`
note: required because it appears within the type `RawVec<Box<dyn Plugin<EventLoopMessage>>>`
    --> /private/tmp/rust-20230816-6788-aokyj4/rustc-1.71.1-src/library/alloc/src/raw_vec.rs:51:19
note: required because it appears within the type `Vec<Box<dyn Plugin<EventLoopMessage>>>`
    --> /private/tmp/rust-20230816-6788-aokyj4/rustc-1.71.1-src/library/alloc/src/vec/mod.rs:396:12
note: required because it appears within the type `Wry<EventLoopMessage>`
    --> /Users/harris/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-runtime-wry-0.14.1/src/lib.rs:1777:12
     |
1777 | pub struct Wry<T: UserEvent> {
     |            ^^^
note: required because it appears within the type `Option<Wry<EventLoopMessage>>`
    --> /private/tmp/rust-20230816-6788-aokyj4/rustc-1.71.1-src/library/core/src/option.rs:563:10
note: required because it appears within the type `App`
    --> /Users/harris/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-1.5.2/src/app.rs:577:12
     |
577  | pub struct App<R: Runtime> {
     |            ^^^
     = note: required because it appears within the type `&mut App`
note: required because it's used within this closure
    --> src/main.rs:92:17
     |
92   |                 move || ipc::Ctx {
     |                 ^^^^^^^
note: required by a bound in `plugin`
    --> /Users/harris/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rspc-0.1.3/src/integrations/tauri.rs:16:40
     |
14   | pub fn plugin<R: Runtime, TCtx, TMeta>(
     |        ------ required by a bound in this function
15   |     router: Arc<Router<TCtx, TMeta>>,
16   |     ctx_fn: impl Fn() -> TCtx + Send + Sync + 'static,
     |                                        ^^^^ required by this bound in `plugin`

@harrismcc
Copy link
Author

Oh! Messing around with this, I got it to work by putting the value in an Arc before loading the plugin and then using Arc::clone in the arguments. That seems to have worked, but I'm fairly new to rust and don't have much familiarity with threads so maybe that's not the best way?

@oscartbeaumont
Copy link
Member

This if fixed by 0.2.0 which now takes in the AppHandle as the first argument to the context function.

Sorry, it's taken me so long to get to this.

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

No branches or pull requests

2 participants