#Event not working

9 messages · Page 1 of 1 (latest)

tidal sorrel
#

I have this in the setup() code:

tauri::WebviewWindowBuilder::new(
    app,
    "error",
    tauri::WebviewUrl::App("/migrate".into()),
)
.title(&app.package_info().name)
.build()?;

app.emit_to(
    EventTarget::webview_window("error"),
    "message",
    "oh shit".to_string(),
)
.unwrap();

And this in my +page.svelte

<script>
    import { listen } from "@tauri-apps/api/event"

    import { browser } from "$app/environment"
    if (browser) {
        listen("message", (event) => {
            console.log(event)
        })
    }
</script>

No event is ever printed to the console. Why?

placid oak
#

Timing issue. The setup hook runs before your js has a chance to run (or at least to run completely).

#

that was a copy of my message in another thread that didn't create the window manually - if you do then the location doesn't matter and it should always be too early to emit an event immediatly after window creation (it's just worse in the setup hook because the window creation will be delayed a bit iirc)

tidal sorrel
#

Does Tauri offer any solution to the issue? I want to create a window with a specific page that should display a message that comes from Tauri.
The message comes from any erors that are returned from e.g., the migration function.

rain oracle
#

invoke a function from the front-end that gets the data for you.

tidal sorrel
#

I know that would be a possibility, but it would also mean that I'd have to store the message in State, which would make the code a little too verbose. I was hoping that perhaps I could pass the message into a JS variable - if that's even possible. Or any similar way where I can write the frontend using something that is more similar to typical HTML template languages, like Mustache, Django Template Language, or Jinja, ...

placid oak
#

I could pass the message into a JS variable
As opposed to the event approach, that is actually possible.

You can either use the window builder's initialization_script (careful, that runs on every pageload, not just the initial one) or after building the window, use something like ```rs
let _ = window.eval("/your js here/window.SomeVar='hello'");

tidal sorrel
#

Thanks, I'll weight the options now! But that approach looks more like what I need 🙂