Explorar el Código

fix(tauri) salt race condition

Lucas Nogueira hace 5 años
padre
commit
d04b15fc90
Se han modificado 2 ficheros con 35 adiciones y 46 borrados
  1. 28 34
      tauri/src/endpoints.rs
  2. 7 12
      tauri/src/salt.rs

+ 28 - 34
tauri/src/endpoints.rs

@@ -11,41 +11,35 @@ pub(crate) fn handle<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> boo
       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();
+            .eval(&format!(
+              "window['{queue}'] = [];
+                window['{fn}'] = function (payload, salt, ignoreQueue) {{
+                const listeners = (window['{listeners}'] && window['{listeners}'][payload.type]) || []
+                if (!ignoreQueue && listeners.length === 0) {{
+                  window['{queue}'].push({{
+                    payload: payload,
+                    salt: salt
+                  }})
+                }}
 
-                Ok(())
-            })
-            .unwrap();
+                if (listeners.length > 0) {{
+                  window.tauri.promisified({{
+                    cmd: 'validateSalt',
+                    salt: salt
+                  }}).then(function () {{
+                    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();
         }
         #[cfg(any(feature = "all-api", feature = "readTextFile"))]
         ReadTextFile {

+ 7 - 12
tauri/src/salt.rs

@@ -48,16 +48,11 @@ pub fn validate<T: 'static>(
   callback: String,
   error: String,
 ) {
-  crate::execute_promise(
-    webview,
-    move || {
-      if is_valid(salt) {
-        Ok("'VALID'".to_string())
-      } else {
-        Err("'INVALID SALT'".to_string())
-      }
-    },
-    callback,
-    error,
-  );
+  let response = if is_valid(salt) {
+    Ok("'VALID'".to_string())
+  } else {
+    Err("'INVALID SALT'".to_string())
+  };
+  let callback_string = crate::api::rpc::format_callback_result(response, callback, error);
+  webview.eval(callback_string.as_str()).unwrap();
 }