diff --git a/tauri/src/endpoints.rs b/tauri/src/endpoints.rs index 675edc60f..159269b35 100644 --- a/tauri/src/endpoints.rs +++ b/tauri/src/endpoints.rs @@ -11,41 +11,35 @@ pub(crate) fn handle(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 - }}) - }} + .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 + }}) + }} - 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(); + 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 { diff --git a/tauri/src/salt.rs b/tauri/src/salt.rs index b5de81465..161603510 100644 --- a/tauri/src/salt.rs +++ b/tauri/src/salt.rs @@ -48,16 +48,11 @@ pub fn validate( 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(); }