diff --git a/tauri/src/app/runner.rs b/tauri/src/app/runner.rs index 14ca250ce..8793038d3 100644 --- a/tauri/src/app/runner.rs +++ b/tauri/src/app/runner.rs @@ -73,8 +73,6 @@ pub(crate) fn run(application: &mut crate::App) { }); } - let mut ran_setup = false; - let webview = web_view::builder() .title(&config.tauri.window.title) .size(config.tauri.window.width, config.tauri.window.height) @@ -82,13 +80,15 @@ pub(crate) fn run(application: &mut crate::App) { .debug(debug) .user_data(()) .invoke_handler(|webview, arg| { - if !crate::endpoints::handle(webview, arg) { - application.run_invoke_handler(webview, arg); - } - // the first command is always the `init`, so we can safely run the setup hook here - if !ran_setup { - ran_setup = true; + if arg == r#"{"cmd":"__initialized"}"# { application.run_setup(webview); + webview.eval(" + if (window.onTauriInit !== void 0) { + window.onTauriInit() + } + ").expect("failed to evaluate window.onTauriInit"); + } else if !crate::endpoints::handle(webview, arg) { + application.run_invoke_handler(webview, arg); } Ok(()) diff --git a/tauri/src/endpoints.rs b/tauri/src/endpoints.rs index 5175d3ff7..4f71d7add 100644 --- a/tauri/src/endpoints.rs +++ b/tauri/src/endpoints.rs @@ -12,7 +12,7 @@ pub(crate) fn handle(webview: &mut WebView<'_, T>, arg: &str) -> boo Init {} => { webview .eval(&format!( - "window['{queue}'] = []; + r#"window['{queue}'] = []; window['{fn}'] = function (payload, salt, ignoreQueue) {{ const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || [] if (!ignoreQueue && listeners.length === 0) {{ @@ -37,9 +37,8 @@ pub(crate) fn handle(webview: &mut WebView<'_, T>, arg: &str) -> boo }} }} - if (window.onTauriInit !== void 0) {{ - window.onTauriInit() - }}", + window.external.invoke('{{"cmd":"__initialized"}}') + "#, fn = crate::event::emit_function_name(), listeners = crate::event::event_listeners_object_name(), queue = crate::event::event_queue_object_name()