Skip to content

Commit 39f1b04

Browse files
authored
refactor(core): move event JS API to a core plugin (#6943)
1 parent 0ab5f40 commit 39f1b04

File tree

18 files changed

+328
-418
lines changed

18 files changed

+328
-418
lines changed

.changes/move-event.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"api": patch
3+
"tauri": patch
4+
"tauri-runtime": patch
5+
"tauri-runtime-wry": patch
6+
---
7+
8+
Moved the `event` JS APIs to a plugin.

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tauri_runtime::{
1212
webview::{WebviewIpcHandler, WindowBuilder, WindowBuilderBase},
1313
window::{
1414
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
15-
CursorIcon, DetachedWindow, FileDropEvent, JsEventListenerKey, PendingWindow, WindowEvent,
15+
CursorIcon, DetachedWindow, FileDropEvent, PendingWindow, WindowEvent,
1616
},
1717
DeviceEventFilter, Dispatch, Error, EventLoopProxy, ExitRequestedEventAction, Icon, Result,
1818
RunEvent, RunIteration, Runtime, RuntimeHandle, UserAttentionType, UserEvent,
@@ -90,7 +90,7 @@ use std::{
9090
cell::RefCell,
9191
collections::{
9292
hash_map::Entry::{Occupied, Vacant},
93-
HashMap, HashSet,
93+
HashMap,
9494
},
9595
fmt,
9696
ops::Deref,
@@ -206,7 +206,6 @@ impl<T: UserEvent> Context<T> {
206206
let label = pending.label.clone();
207207
let current_url = pending.current_url.clone();
208208
let menu_ids = pending.menu_ids.clone();
209-
let js_event_listeners = pending.js_event_listeners.clone();
210209
let context = self.clone();
211210
let window_id = rand::random();
212211

@@ -229,7 +228,6 @@ impl<T: UserEvent> Context<T> {
229228
current_url,
230229
dispatcher,
231230
menu_ids,
232-
js_event_listeners,
233231
})
234232
}
235233
}
@@ -1944,7 +1942,6 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
19441942
let label = pending.label.clone();
19451943
let current_url = pending.current_url.clone();
19461944
let menu_ids = pending.menu_ids.clone();
1947-
let js_event_listeners = pending.js_event_listeners.clone();
19481945
let window_id = rand::random();
19491946

19501947
let webview = create_webview(
@@ -1972,7 +1969,6 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
19721969
current_url,
19731970
dispatcher,
19741971
menu_ids,
1975-
js_event_listeners,
19761972
})
19771973
}
19781974

@@ -2942,7 +2938,6 @@ fn create_webview<T: UserEvent>(
29422938
label,
29432939
current_url,
29442940
menu_ids,
2945-
js_event_listeners,
29462941
#[cfg(target_os = "android")]
29472942
on_webview_created,
29482943
..
@@ -3028,7 +3023,6 @@ fn create_webview<T: UserEvent>(
30283023
label.clone(),
30293024
current_url,
30303025
menu_ids,
3031-
js_event_listeners,
30323026
handler,
30333027
));
30343028
}
@@ -3152,7 +3146,6 @@ fn create_ipc_handler<T: UserEvent>(
31523146
label: String,
31533147
current_url: Arc<Mutex<Url>>,
31543148
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
3155-
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
31563149
handler: WebviewIpcHandler<T, Wry<T>>,
31573150
) -> Box<IpcHandler> {
31583151
Box::new(move |window, request| {
@@ -3166,7 +3159,6 @@ fn create_ipc_handler<T: UserEvent>(
31663159
},
31673160
label: label.clone(),
31683161
menu_ids: menu_ids.clone(),
3169-
js_event_listeners: js_event_listeners.clone(),
31703162
},
31713163
request,
31723164
);

core/tauri-runtime/src/window.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tauri_utils::{config::WindowConfig, Theme};
1515
use url::Url;
1616

1717
use std::{
18-
collections::{HashMap, HashSet},
18+
collections::HashMap,
1919
hash::{Hash, Hasher},
2020
path::PathBuf,
2121
sync::{mpsc::Sender, Arc, Mutex},
@@ -230,9 +230,6 @@ pub struct PendingWindow<T: UserEvent, R: Runtime<T>> {
230230
/// Maps runtime id to a string menu id.
231231
pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
232232

233-
/// A HashMap mapping JS event names with associated listener ids.
234-
pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
235-
236233
/// A handler to decide if incoming url is allowed to navigate.
237234
pub navigation_handler: Option<Box<dyn Fn(Url) -> bool + Send>>,
238235

@@ -280,7 +277,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
280277
label,
281278
ipc_handler: None,
282279
menu_ids: Arc::new(Mutex::new(menu_ids)),
283-
js_event_listeners: Default::default(),
284280
navigation_handler: Default::default(),
285281
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
286282
#[cfg(target_os = "android")]
@@ -312,7 +308,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
312308
label,
313309
ipc_handler: None,
314310
menu_ids: Arc::new(Mutex::new(menu_ids)),
315-
js_event_listeners: Default::default(),
316311
navigation_handler: Default::default(),
317312
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
318313
#[cfg(target_os = "android")]
@@ -356,15 +351,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
356351
}
357352
}
358353

359-
/// Key for a JS event listener.
360-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
361-
pub struct JsEventListenerKey {
362-
/// The associated window label.
363-
pub window_label: Option<String>,
364-
/// The event name.
365-
pub event: String,
366-
}
367-
368354
/// A webview window that is not yet managed by Tauri.
369355
#[derive(Debug)]
370356
pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
@@ -379,9 +365,6 @@ pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
379365

380366
/// Maps runtime id to a string menu id.
381367
pub menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
382-
383-
/// A HashMap mapping JS event names with associated listener ids.
384-
pub js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
385368
}
386369

387370
impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {
@@ -391,7 +374,6 @@ impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {
391374
label: self.label.clone(),
392375
dispatcher: self.dispatcher.clone(),
393376
menu_ids: self.menu_ids.clone(),
394-
js_event_listeners: self.js_event_listeners.clone(),
395377
}
396378
}
397379
}

core/tauri/scripts/bundle.global.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/api/ipc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Channel<R> {
6767
}
6868

6969
/// The `Callback` type is the return value of the `transformCallback` JavaScript function.
70-
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
70+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
7171
pub struct CallbackFn(pub usize);
7272

7373
/// The information about this is quite limited. On Chrome/Edge and Firefox, [the maximum string size is approximately 1 GB](https://stackoverflow.com/a/34958490).

core/tauri/src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ shared_app_impl!(AppHandle<R>);
612612
impl<R: Runtime> App<R> {
613613
fn register_core_plugins(&self) -> crate::Result<()> {
614614
self.handle.plugin(crate::path::init())?;
615+
self.handle.plugin(crate::event::init())?;
615616
Ok(())
616617
}
617618

core/tauri/src/endpoints.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use serde_json::Value as JsonValue;
1212

1313
use std::sync::Arc;
1414

15-
mod event;
1615
mod window;
1716

1817
/// The context passed to the invoke handler.
@@ -50,7 +49,6 @@ impl<T: Serialize> From<T> for InvokeResponse {
5049
#[serde(tag = "module", content = "message")]
5150
enum Module {
5251
Window(Box<window::Cmd>),
53-
Event(event::Cmd),
5452
}
5553

5654
impl Module {
@@ -74,12 +72,6 @@ impl Module {
7472
.and_then(|r| r.json)
7573
.map_err(InvokeError::from_anyhow)
7674
}),
77-
Self::Event(cmd) => resolver.respond_async(async move {
78-
cmd
79-
.run(context)
80-
.and_then(|r| r.json)
81-
.map_err(InvokeError::from_anyhow)
82-
}),
8375
}
8476
}
8577
}

core/tauri/src/endpoints/event.rs

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)