fix(core): `listen` receiving past events, closes #2323 (#2371)

This commit is contained in:
Lucas Fernandes Nogueira 2021-08-09 00:10:25 -03:00 committed by GitHub
parent d7dd653026
commit 1ecb8651a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 32 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Fix `listen` calls receiving past events.

View File

@ -115,9 +115,13 @@ impl Cmd {
}
#[cfg(all(target_os = "linux", any(dialog_open, dialog_save)))]
fn set_default_path(dialog_builder: FileDialogBuilder, default_path: PathBuf) -> FileDialogBuilder {
if default_path.is_file() {
dialog_builder.set_file_name(&default_path.to_string_lossy().to_string())
fn set_default_path(
mut dialog_builder: FileDialogBuilder,
default_path: PathBuf,
) -> FileDialogBuilder {
if default_path.is_file() || !default_path.exists() {
dialog_builder = dialog_builder.set_file_name(&default_path.to_string_lossy().to_string());
dialog_builder.set_directory(default_path.parent().unwrap())
} else {
dialog_builder.set_directory(default_path)
}
@ -211,9 +215,6 @@ pub fn save<R: Runtime>(
dialog_builder = dialog_builder.set_parent(&parent(window)?);
}
if let Some(default_path) = options.default_path {
if !default_path.exists() {
return Err(crate::Error::DialogDefaultPathNotExists(default_path));
}
dialog_builder = set_default_path(dialog_builder, default_path);
}
for filter in options.filters {

View File

@ -87,15 +87,8 @@ pub fn listen_js<R: Runtime>(
id: {event_id},
handler: window['{handler}']
}});
for (let i = 0; i < (window['{queue}'] || []).length; i++) {{
const e = window['{queue}'][i];
window['{emit}'](e.eventData, e.salt, true)
}}
",
listeners = window.manager().event_listeners_object_name(),
queue = window.manager().event_queue_object_name(),
emit = window.manager().event_emit_function_name(),
event = event,
event_id = event_id,
handler = handler

View File

@ -59,7 +59,6 @@ struct InnerListeners {
pending: Mutex<Vec<Pending>>,
function_name: Uuid,
listeners_object_name: Uuid,
queue_object_name: Uuid,
}
/// A self-contained event manager.
@ -75,7 +74,6 @@ impl Default for Listeners {
pending: Mutex::default(),
function_name: Uuid::new_v4(),
listeners_object_name: Uuid::new_v4(),
queue_object_name: Uuid::new_v4(),
}),
}
}
@ -100,11 +98,6 @@ impl Listeners {
self.inner.listeners_object_name.to_string()
}
/// Randomly generated queue object name to represent the JavaScript event queue object.
pub(crate) fn queue_object_name(&self) -> String {
self.inner.queue_object_name.to_string()
}
/// Insert a pending event action to the queue.
fn insert_pending(&self, action: Pending) {
self

View File

@ -445,15 +445,8 @@ impl<R: Runtime> WindowManager<R> {
fn event_initialization_script(&self, key: u32) -> String {
return format!(
"
window['{queue}'] = [];
window['{function}'] = function (eventData, salt, ignoreQueue) {{
window['{function}'] = function (eventData, salt) {{
const listeners = (window['{listeners}'] && window['{listeners}'][eventData.event]) || []
if (!ignoreQueue && listeners.length === 0) {{
window['{queue}'].push({{
eventData: eventData,
salt: salt
}})
}}
if (listeners.length > 0) {{
window.__TAURI__._invoke('tauri', {{
@ -476,7 +469,6 @@ impl<R: Runtime> WindowManager<R> {
",
key = key,
function = self.inner.listeners.function_name(),
queue = self.inner.listeners.queue_object_name(),
listeners = self.inner.listeners.listeners_object_name()
);
}
@ -714,9 +706,6 @@ impl<R: Runtime> WindowManager<R> {
pub fn event_listeners_object_name(&self) -> String {
self.inner.listeners.listeners_object_name()
}
pub fn event_queue_object_name(&self) -> String {
self.inner.listeners.queue_object_name()
}
pub fn event_emit_function_name(&self) -> String {
self.inner.listeners.function_name()
}