diff --git a/.changes/core-js-listenrid-u32.md b/.changes/core-js-listenrid-u32.md new file mode 100644 index 000000000000..9e495ba9a611 --- /dev/null +++ b/.changes/core-js-listenrid-u32.md @@ -0,0 +1,6 @@ +--- +'tauri-runtime': 'patch' +'tauri-runtime-wry': 'patch' +--- + +Use `u32` instead of `u64` for js event listener ids diff --git a/.changes/core-js-listners-unlisten.md b/.changes/core-js-listners-unlisten.md new file mode 100644 index 000000000000..957a37e028f4 --- /dev/null +++ b/.changes/core-js-listners-unlisten.md @@ -0,0 +1,5 @@ +--- +'tauri': 'patch:bug' +--- + +Fix unlistening to window events failing sometimes. diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index eaefab956713..efd76c9b2e03 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -3308,7 +3308,7 @@ fn create_ipc_handler( context: Context, label: String, menu_ids: Arc>>, - js_event_listeners: Arc>>>, + js_event_listeners: Arc>>>, handler: WebviewIpcHandler>, ) -> Box { Box::new(move |window, request| { diff --git a/core/tauri-runtime/src/window.rs b/core/tauri-runtime/src/window.rs index 142e3940257f..7c154cdea58d 100644 --- a/core/tauri-runtime/src/window.rs +++ b/core/tauri-runtime/src/window.rs @@ -231,7 +231,7 @@ pub struct PendingWindow> { pub menu_ids: Arc>>, /// A HashMap mapping JS event names with associated listener ids. - pub js_event_listeners: Arc>>>, + pub js_event_listeners: Arc>>>, /// A handler to decide if incoming url is allowed to navigate. pub navigation_handler: Option bool + Send>>, @@ -362,7 +362,7 @@ pub struct DetachedWindow> { pub menu_ids: Arc>>, /// A HashMap mapping JS event names with associated listener ids. - pub js_event_listeners: Arc>>>, + pub js_event_listeners: Arc>>>, } impl> Clone for DetachedWindow { diff --git a/core/tauri/src/endpoints/event.rs b/core/tauri/src/endpoints/event.rs index 78287191ac26..471db9c3c089 100644 --- a/core/tauri/src/endpoints/event.rs +++ b/core/tauri/src/endpoints/event.rs @@ -67,7 +67,7 @@ pub enum Cmd { }, /// Unlisten to an event. #[serde(rename_all = "camelCase")] - Unlisten { event: EventId, event_id: u64 }, + Unlisten { event: EventId, event_id: u32 }, /// Emit an event to the webview associated with the given window. /// If the window_label is omitted, the event will be triggered on all listeners. #[serde(rename_all = "camelCase")] @@ -84,7 +84,7 @@ impl Cmd { event: EventId, window_label: Option, handler: CallbackFn, - ) -> super::Result { + ) -> super::Result { let event_id = rand::random(); let window_label = window_label.map(|l| l.0); @@ -110,7 +110,7 @@ impl Cmd { fn unlisten( context: InvokeContext, event: EventId, - event_id: u64, + event_id: u32, ) -> super::Result<()> { context .window diff --git a/core/tauri/src/event.rs b/core/tauri/src/event.rs index c47e1ee131ae..68d173f5a07b 100644 --- a/core/tauri/src/event.rs +++ b/core/tauri/src/event.rs @@ -299,7 +299,7 @@ mod test { } } -pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id: u64) -> String { +pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id: u32) -> String { format!( " (function () {{ @@ -318,7 +318,7 @@ pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id: pub fn listen_js( listeners_object_name: String, event: String, - event_id: u64, + event_id: u32, window_label: Option, handler: String, ) -> String { diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 1009867e505a..459121c1159d 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -1594,7 +1594,7 @@ impl Window { self.window.dispatcher.eval_script(js).map_err(Into::into) } - pub(crate) fn register_js_listener(&self, window_label: Option, event: String, id: u64) { + pub(crate) fn register_js_listener(&self, window_label: Option, event: String, id: u32) { self .window .js_event_listeners @@ -1608,7 +1608,7 @@ impl Window { .insert(id); } - pub(crate) fn unregister_js_listener(&self, id: u64) { + pub(crate) fn unregister_js_listener(&self, id: u32) { let mut empty = None; let mut js_listeners = self.window.js_event_listeners.lock().unwrap(); let iter = js_listeners.iter_mut();