瀏覽代碼

fix(core): use `u32` for js listeners ids, closes #7119 (#7159)

Amr Bashir 2 年之前
父節點
當前提交
076e1a81a5

+ 6 - 0
.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

+ 5 - 0
.changes/core-js-listners-unlisten.md

@@ -0,0 +1,5 @@
+---
+'tauri': 'patch:bug'
+---
+
+Fix unlistening to window events failing sometimes.

+ 1 - 1
core/tauri-runtime-wry/src/lib.rs

@@ -3308,7 +3308,7 @@ fn create_ipc_handler<T: UserEvent>(
   context: Context<T>,
   label: String,
   menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
-  js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
+  js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u32>>>>,
   handler: WebviewIpcHandler<T, Wry<T>>,
 ) -> Box<IpcHandler> {
   Box::new(move |window, request| {

+ 2 - 2
core/tauri-runtime/src/window.rs

@@ -231,7 +231,7 @@ pub struct PendingWindow<T: UserEvent, R: Runtime<T>> {
   pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
 
   /// A HashMap mapping JS event names with associated listener ids.
-  pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
+  pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u32>>>>,
 
   /// A handler to decide if incoming url is allowed to navigate.
   pub navigation_handler: Option<Box<dyn Fn(Url) -> bool + Send>>,
@@ -362,7 +362,7 @@ pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
   pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
 
   /// A HashMap mapping JS event names with associated listener ids.
-  pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
+  pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u32>>>>,
 }
 
 impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {

+ 3 - 3
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<WindowLabel>,
     handler: CallbackFn,
-  ) -> super::Result<u64> {
+  ) -> super::Result<u32> {
     let event_id = rand::random();
 
     let window_label = window_label.map(|l| l.0);
@@ -110,7 +110,7 @@ impl Cmd {
   fn unlisten<R: Runtime>(
     context: InvokeContext<R>,
     event: EventId,
-    event_id: u64,
+    event_id: u32,
   ) -> super::Result<()> {
     context
       .window

+ 2 - 2
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<String>,
   handler: String,
 ) -> String {

+ 2 - 2
core/tauri/src/window.rs

@@ -1594,7 +1594,7 @@ impl<R: Runtime> Window<R> {
     self.window.dispatcher.eval_script(js).map_err(Into::into)
   }
 
-  pub(crate) fn register_js_listener(&self, window_label: Option<String>, event: String, id: u64) {
+  pub(crate) fn register_js_listener(&self, window_label: Option<String>, event: String, id: u32) {
     self
       .window
       .js_event_listeners
@@ -1608,7 +1608,7 @@ impl<R: Runtime> Window<R> {
       .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();