Skip to content

Commit

Permalink
fix(core): listen receiving past events, closes #2323 (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 9, 2021
1 parent d7dd653 commit 1ecb865
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-event-queue.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Fix `listen` calls receiving past events.
13 changes: 7 additions & 6 deletions core/tauri/src/endpoints/dialog.rs
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 0 additions & 7 deletions core/tauri/src/endpoints/event.rs
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions core/tauri/src/event.rs
Expand Up @@ -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.
Expand All @@ -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(),
}),
}
}
Expand All @@ -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
Expand Down
13 changes: 1 addition & 12 deletions core/tauri/src/manager.rs
Expand Up @@ -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', {{
Expand All @@ -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()
);
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 1ecb865

Please sign in to comment.