Răsfoiți Sursa

fix(tauri) event system init on windows fixed (#29)

Lucas Fernandes Nogueira 6 ani în urmă
părinte
comite
157a3ac40e
4 a modificat fișierele cu 46 adăugiri și 39 ștergeri
  1. 40 0
      lib/rust/src/api.rs
  2. 1 0
      lib/rust/src/api/cmd.rs
  3. 0 38
      lib/rust/src/app/runner.rs
  4. 5 1
      lib/tauri.js

+ 40 - 0
lib/rust/src/api.rs

@@ -9,6 +9,46 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
     Err(_) => false,
     Ok(command) => {
       match command {
+        Init {} => {
+          webview
+            .handle()
+            .dispatch(move |_webview| {
+              _webview
+                .eval(&format!(
+                  "window['{queue}'] = [];
+                  window['{fn}'] = function (payload, salt, ignoreQueue) {{
+                    window.tauri.promisified({{
+                      cmd: 'validateSalt',
+                      salt: salt
+                    }}).then(function () {{
+                      const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || []
+
+                      if (!ignoreQueue && listeners.length === 0) {{ 
+                        window['{queue}'].push({{ 
+                          payload: payload,
+                          salt: salt
+                        }})
+                      }}
+
+                      for (let i = listeners.length - 1; i >= 0; i--) {{ 
+                        const listener = listeners[i]
+                        if (listener.once)
+                          listeners.splice(i, 1)
+                        listener.handler(payload)
+                      }}
+                    }})
+                  }}", 
+                  fn = crate::event::emit_function_name(),
+                  listeners = crate::event::event_listeners_object_name(),
+                  queue = crate::event::event_queue_object_name()
+                ))
+                .unwrap();
+
+                Ok(())
+            })
+            .unwrap();
+
+        },
         #[cfg(any(feature = "all-api", feature = "readTextFile"))]
         ReadTextFile {
           path,

+ 1 - 0
lib/rust/src/api/cmd.rs

@@ -1,6 +1,7 @@
 #[derive(Deserialize)]
 #[serde(tag = "cmd", rename_all = "camelCase")]
 pub enum Cmd {
+  Init {},
   #[cfg(any(feature = "all-api", feature = "readTextFile"))]
   ReadTextFile {
     path: String,

+ 0 - 38
lib/rust/src/app/runner.rs

@@ -106,44 +106,6 @@ pub(crate) fn run(application: &mut crate::App) {
     .build()
     .unwrap();
 
-  webview
-    .handle()
-    .dispatch(move |_webview| {
-      _webview
-        .eval(&format!(
-          "window['{queue}'] = [];
-          window['{fn}'] = function (payload, salt, ignoreQueue) {{
-            window.tauri.promisified({{
-              cmd: 'validateSalt',
-              salt
-            }}).then(function () {{
-              const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || []
-
-              if (!ignoreQueue && listeners.length === 0) {{ 
-                window['{queue}'].push({{ 
-                  payload: payload,
-                  salt: salt
-                 }})
-              }}
-
-              for (let i = listeners.length - 1; i >= 0; i--) {{ 
-                const listener = listeners[i]
-                if (listener.once)
-                  listeners.splice(i, 1)
-                listener.handler(payload)
-              }}
-            }})
-          }}", 
-          fn = crate::event::emit_function_name(),
-          listeners = crate::event::event_listeners_object_name(),
-          queue = crate::event::event_queue_object_name()
-        ))
-        .unwrap();
-
-      Ok(())
-    })
-    .unwrap();
-
   #[cfg(not(feature = "dev"))]
   {
     #[cfg(feature = "embedded-server")]

+ 5 - 1
lib/tauri.js

@@ -7,7 +7,7 @@
  **/
 
 // open <a href="..."> links with the Tauri API
-document.querySelector('body').addEventListener('click', e => {
+document.querySelector('body').addEventListener('click', function (e) {
   let target = e.target
   while (target != null) {
     if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
@@ -18,6 +18,10 @@ document.querySelector('body').addEventListener('click', e => {
   }
 }, true)
 
+document.addEventListener('DOMContentLoaded', function () {
+  tauri.invoke({ cmd: 'init' })
+})
+
 /**
  * @module tauri
  * @description This API interface makes powerful interactions available