Skip to content

Commit

Permalink
fix(core): use u32 for js listeners ids, closes #7119 (#7159)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Jun 7, 2023
1 parent fc2e408 commit 076e1a8
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/core-js-listenrid-u32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-runtime': 'patch'
'tauri-runtime-wry': 'patch'
---

Use `u32` instead of `u64` for js event listener ids
5 changes: 5 additions & 0 deletions .changes/core-js-listners-unlisten.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': 'patch:bug'
---

Fix unlistening to window events failing sometimes.
2 changes: 1 addition & 1 deletion core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
4 changes: 2 additions & 2 deletions core/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down Expand Up @@ -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> {
Expand Down
6 changes: 3 additions & 3 deletions core/tauri/src/endpoints/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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);
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {{
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 076e1a8

Please sign in to comment.