Skip to content

Commit

Permalink
refactor(core): move event JS API to a core plugin (#6943)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 12, 2023
1 parent 0ab5f40 commit 39f1b04
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 418 deletions.
8 changes: 8 additions & 0 deletions .changes/move-event.md
@@ -0,0 +1,8 @@
---
"api": patch
"tauri": patch
"tauri-runtime": patch
"tauri-runtime-wry": patch
---

Moved the `event` JS APIs to a plugin.
12 changes: 2 additions & 10 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -12,7 +12,7 @@ use tauri_runtime::{
webview::{WebviewIpcHandler, WindowBuilder, WindowBuilderBase},
window::{
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
CursorIcon, DetachedWindow, FileDropEvent, JsEventListenerKey, PendingWindow, WindowEvent,
CursorIcon, DetachedWindow, FileDropEvent, PendingWindow, WindowEvent,
},
DeviceEventFilter, Dispatch, Error, EventLoopProxy, ExitRequestedEventAction, Icon, Result,
RunEvent, RunIteration, Runtime, RuntimeHandle, UserAttentionType, UserEvent,
Expand Down Expand Up @@ -90,7 +90,7 @@ use std::{
cell::RefCell,
collections::{
hash_map::Entry::{Occupied, Vacant},
HashMap, HashSet,
HashMap,
},
fmt,
ops::Deref,
Expand Down Expand Up @@ -206,7 +206,6 @@ impl<T: UserEvent> Context<T> {
let label = pending.label.clone();
let current_url = pending.current_url.clone();
let menu_ids = pending.menu_ids.clone();
let js_event_listeners = pending.js_event_listeners.clone();
let context = self.clone();
let window_id = rand::random();

Expand All @@ -229,7 +228,6 @@ impl<T: UserEvent> Context<T> {
current_url,
dispatcher,
menu_ids,
js_event_listeners,
})
}
}
Expand Down Expand Up @@ -1944,7 +1942,6 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
let label = pending.label.clone();
let current_url = pending.current_url.clone();
let menu_ids = pending.menu_ids.clone();
let js_event_listeners = pending.js_event_listeners.clone();
let window_id = rand::random();

let webview = create_webview(
Expand Down Expand Up @@ -1972,7 +1969,6 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
current_url,
dispatcher,
menu_ids,
js_event_listeners,
})
}

Expand Down Expand Up @@ -2942,7 +2938,6 @@ fn create_webview<T: UserEvent>(
label,
current_url,
menu_ids,
js_event_listeners,
#[cfg(target_os = "android")]
on_webview_created,
..
Expand Down Expand Up @@ -3028,7 +3023,6 @@ fn create_webview<T: UserEvent>(
label.clone(),
current_url,
menu_ids,
js_event_listeners,
handler,
));
}
Expand Down Expand Up @@ -3152,7 +3146,6 @@ fn create_ipc_handler<T: UserEvent>(
label: String,
current_url: Arc<Mutex<Url>>,
menu_ids: Arc<Mutex<HashMap<MenuHash, MenuId>>>,
js_event_listeners: Arc<Mutex<HashMap<JsEventListenerKey, HashSet<u64>>>>,
handler: WebviewIpcHandler<T, Wry<T>>,
) -> Box<IpcHandler> {
Box::new(move |window, request| {
Expand All @@ -3166,7 +3159,6 @@ fn create_ipc_handler<T: UserEvent>(
},
label: label.clone(),
menu_ids: menu_ids.clone(),
js_event_listeners: js_event_listeners.clone(),
},
request,
);
Expand Down
20 changes: 1 addition & 19 deletions core/tauri-runtime/src/window.rs
Expand Up @@ -15,7 +15,7 @@ use tauri_utils::{config::WindowConfig, Theme};
use url::Url;

use std::{
collections::{HashMap, HashSet},
collections::HashMap,
hash::{Hash, Hasher},
path::PathBuf,
sync::{mpsc::Sender, Arc, Mutex},
Expand Down Expand Up @@ -230,9 +230,6 @@ pub struct PendingWindow<T: UserEvent, R: Runtime<T>> {
/// Maps runtime id to a string menu id.
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>>>>,

/// 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 @@ -280,7 +277,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
label,
ipc_handler: None,
menu_ids: Arc::new(Mutex::new(menu_ids)),
js_event_listeners: Default::default(),
navigation_handler: Default::default(),
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -312,7 +308,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
label,
ipc_handler: None,
menu_ids: Arc::new(Mutex::new(menu_ids)),
js_event_listeners: Default::default(),
navigation_handler: Default::default(),
current_url: Arc::new(Mutex::new("tauri://localhost".parse().unwrap())),
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -356,15 +351,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
}
}

/// Key for a JS event listener.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct JsEventListenerKey {
/// The associated window label.
pub window_label: Option<String>,
/// The event name.
pub event: String,
}

/// A webview window that is not yet managed by Tauri.
#[derive(Debug)]
pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {
Expand All @@ -379,9 +365,6 @@ pub struct DetachedWindow<T: UserEvent, R: Runtime<T>> {

/// Maps runtime id to a string menu id.
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>>>>,
}

impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {
Expand All @@ -391,7 +374,6 @@ impl<T: UserEvent, R: Runtime<T>> Clone for DetachedWindow<T, R> {
label: self.label.clone(),
dispatcher: self.dispatcher.clone(),
menu_ids: self.menu_ids.clone(),
js_event_listeners: self.js_event_listeners.clone(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/tauri/src/api/ipc.rs
Expand Up @@ -67,7 +67,7 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Channel<R> {
}

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

/// 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).
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/app.rs
Expand Up @@ -612,6 +612,7 @@ shared_app_impl!(AppHandle<R>);
impl<R: Runtime> App<R> {
fn register_core_plugins(&self) -> crate::Result<()> {
self.handle.plugin(crate::path::init())?;
self.handle.plugin(crate::event::init())?;
Ok(())
}

Expand Down
8 changes: 0 additions & 8 deletions core/tauri/src/endpoints.rs
Expand Up @@ -12,7 +12,6 @@ use serde_json::Value as JsonValue;

use std::sync::Arc;

mod event;
mod window;

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

impl Module {
Expand All @@ -74,12 +72,6 @@ impl Module {
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
Self::Event(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
}
}
}
Expand Down
161 changes: 0 additions & 161 deletions core/tauri/src/endpoints/event.rs

This file was deleted.

0 comments on commit 39f1b04

Please sign in to comment.