Skip to content

Commit 076e1a8

Browse files
authored
fix(core): use u32 for js listeners ids, closes #7119 (#7159)
1 parent fc2e408 commit 076e1a8

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

.changes/core-js-listenrid-u32.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri-runtime': 'patch'
3+
'tauri-runtime-wry': 'patch'
4+
---
5+
6+
Use `u32` instead of `u64` for js event listener ids
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:bug'
3+
---
4+
5+
Fix unlistening to window events failing sometimes.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3308,7 +3308,7 @@ fn create_ipc_handler<T: UserEvent>(
33083308
context: Context<T>,
33093309
label: String,
33103310
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
3311-
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
3311+
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u32>>>>,
33123312
handler: WebviewIpcHandler<T, Wry<T>>,
33133313
) -> Box<IpcHandler> {
33143314
Box::new(move |window, request| {

core/tauri-runtime/src/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub struct PendingWindow<T: UserEvent, R: Runtime<T>> {
231231
pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
232232

233233
/// A HashMap mapping JS event names with associated listener ids.
234-
pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
234+
pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u32>>>>,
235235

236236
/// A handler to decide if incoming url is allowed to navigate.
237237
pub navigation_handler: Option<Box<dyn Fn(Url) -> bool + Send>>,
@@ -362,7 +362,7 @@ pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
362362
pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
363363

364364
/// A HashMap mapping JS event names with associated listener ids.
365-
pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
365+
pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u32>>>>,
366366
}
367367

368368
impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {

core/tauri/src/endpoints/event.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub enum Cmd {
6767
},
6868
/// Unlisten to an event.
6969
#[serde(rename_all = "camelCase")]
70-
Unlisten { event: EventId, event_id: u64 },
70+
Unlisten { event: EventId, event_id: u32 },
7171
/// Emit an event to the webview associated with the given window.
7272
/// If the window_label is omitted, the event will be triggered on all listeners.
7373
#[serde(rename_all = "camelCase")]
@@ -84,7 +84,7 @@ impl Cmd {
8484
event: EventId,
8585
window_label: Option<WindowLabel>,
8686
handler: CallbackFn,
87-
) -> super::Result<u64> {
87+
) -> super::Result<u32> {
8888
let event_id = rand::random();
8989

9090
let window_label = window_label.map(|l| l.0);
@@ -110,7 +110,7 @@ impl Cmd {
110110
fn unlisten<R: Runtime>(
111111
context: InvokeContext<R>,
112112
event: EventId,
113-
event_id: u64,
113+
event_id: u32,
114114
) -> super::Result<()> {
115115
context
116116
.window

core/tauri/src/event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ mod test {
299299
}
300300
}
301301

302-
pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id: u64) -> String {
302+
pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id: u32) -> String {
303303
format!(
304304
"
305305
(function () {{
@@ -318,7 +318,7 @@ pub fn unlisten_js(listeners_object_name: String, event_name: String, event_id:
318318
pub fn listen_js(
319319
listeners_object_name: String,
320320
event: String,
321-
event_id: u64,
321+
event_id: u32,
322322
window_label: Option<String>,
323323
handler: String,
324324
) -> String {

core/tauri/src/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ impl<R: Runtime> Window<R> {
15941594
self.window.dispatcher.eval_script(js).map_err(Into::into)
15951595
}
15961596

1597-
pub(crate) fn register_js_listener(&self, window_label: Option<String>, event: String, id: u64) {
1597+
pub(crate) fn register_js_listener(&self, window_label: Option<String>, event: String, id: u32) {
15981598
self
15991599
.window
16001600
.js_event_listeners
@@ -1608,7 +1608,7 @@ impl<R: Runtime> Window<R> {
16081608
.insert(id);
16091609
}
16101610

1611-
pub(crate) fn unregister_js_listener(&self, id: u64) {
1611+
pub(crate) fn unregister_js_listener(&self, id: u32) {
16121612
let mut empty = None;
16131613
let mut js_listeners = self.window.js_event_listeners.lock().unwrap();
16141614
let iter = js_listeners.iter_mut();

0 commit comments

Comments
 (0)