fix(tauri) salt race condition

This commit is contained in:
Lucas Nogueira 2019-12-22 23:31:38 -03:00
parent 4db6171c37
commit d04b15fc90
2 changed files with 35 additions and 46 deletions

View File

@ -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
}})
}}
.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 {

View File

@ -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();
}