Browse Source

fix(tauri) run setup hook after init has been eval'd (#243)

* fix(tauri) run setup hook after init has been eval'd

* fix(endpoints) merge conflict
Lucas Fernandes Nogueira 5 năm trước cách đây
mục cha
commit
3fbb297064
2 tập tin đã thay đổi với 11 bổ sung12 xóa
  1. 8 8
      tauri/src/app/runner.rs
  2. 3 4
      tauri/src/endpoints.rs

+ 8 - 8
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(())

+ 3 - 4
tauri/src/endpoints.rs

@@ -12,7 +12,7 @@ pub(crate) fn handle<T: 'static>(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<T: 'static>(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()