Skip to content

Commit fd557e9

Browse files
Ease plugin hook restrictions (#3404)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent d24045e commit fd557e9

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Ease the requirements for plugin hooks. `setup` and `setup_with_config` can now be `FnOnce` and `on_webview_ready`, `on_event` and `on_page_load` can be `FnMut`.

core/tauri/src/plugin.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! The Tauri plugin extension to expand Tauri functionality.
66
77
use crate::{
8-
runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, InvokeHandler, OnPageLoad,
9-
PageLoadPayload, RunEvent, Window,
8+
runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, InvokeHandler, PageLoadPayload,
9+
RunEvent, Window,
1010
};
1111
use serde::de::DeserializeOwned;
1212
use serde_json::Value as JsonValue;
@@ -54,10 +54,11 @@ pub trait Plugin<R: Runtime>: Send {
5454
fn extend_api(&mut self, invoke: Invoke<R>) {}
5555
}
5656

57-
type SetupHook<R> = dyn Fn(&AppHandle<R>) -> Result<()> + Send + Sync;
58-
type SetupWithConfigHook<R, T> = dyn Fn(&AppHandle<R>, T) -> Result<()> + Send + Sync;
59-
type OnWebviewReady<R> = dyn Fn(Window<R>) + Send + Sync;
60-
type OnEvent<R> = dyn Fn(&AppHandle<R>, &RunEvent) + Send + Sync;
57+
type SetupHook<R> = dyn FnOnce(&AppHandle<R>) -> Result<()> + Send + Sync;
58+
type SetupWithConfigHook<R, T> = dyn FnOnce(&AppHandle<R>, T) -> Result<()> + Send + Sync;
59+
type OnWebviewReady<R> = dyn FnMut(Window<R>) + Send + Sync;
60+
type OnEvent<R> = dyn FnMut(&AppHandle<R>, &RunEvent) + Send + Sync;
61+
type OnPageLoad<R> = dyn FnMut(Window<R>, PageLoadPayload) + Send + Sync;
6162

6263
/// Builds a [`TauriPlugin`].
6364
///
@@ -134,7 +135,7 @@ type OnEvent<R> = dyn Fn(&AppHandle<R>, &RunEvent) + Send + Sync;
134135
pub struct Builder<R: Runtime, C: DeserializeOwned = ()> {
135136
name: &'static str,
136137
invoke_handler: Box<InvokeHandler<R>>,
137-
setup: Box<SetupHook<R>>,
138+
setup: Option<Box<SetupHook<R>>>,
138139
setup_with_config: Option<Box<SetupWithConfigHook<R, C>>>,
139140
js_init_script: Option<String>,
140141
on_page_load: Box<OnPageLoad<R>>,
@@ -147,7 +148,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
147148
pub fn new(name: &'static str) -> Self {
148149
Self {
149150
name,
150-
setup: Box::new(|_| Ok(())),
151+
setup: None,
151152
setup_with_config: None,
152153
js_init_script: None,
153154
invoke_handler: Box::new(|_| ()),
@@ -250,9 +251,9 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
250251
#[must_use]
251252
pub fn setup<F>(mut self, setup: F) -> Self
252253
where
253-
F: Fn(&AppHandle<R>) -> Result<()> + Send + Sync + 'static,
254+
F: FnOnce(&AppHandle<R>) -> Result<()> + Send + Sync + 'static,
254255
{
255-
self.setup = Box::new(setup);
256+
self.setup.replace(Box::new(setup));
256257
self
257258
}
258259

@@ -286,7 +287,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
286287
#[must_use]
287288
pub fn setup_with_config<F>(mut self, setup_with_config: F) -> Self
288289
where
289-
F: Fn(&AppHandle<R>, C) -> Result<()> + Send + Sync + 'static,
290+
F: FnOnce(&AppHandle<R>, C) -> Result<()> + Send + Sync + 'static,
290291
{
291292
self.setup_with_config.replace(Box::new(setup_with_config));
292293
self
@@ -310,7 +311,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
310311
#[must_use]
311312
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
312313
where
313-
F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
314+
F: FnMut(Window<R>, PageLoadPayload) + Send + Sync + 'static,
314315
{
315316
self.on_page_load = Box::new(on_page_load);
316317
self
@@ -334,7 +335,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
334335
#[must_use]
335336
pub fn on_webview_ready<F>(mut self, on_webview_ready: F) -> Self
336337
where
337-
F: Fn(Window<R>) + Send + Sync + 'static,
338+
F: FnMut(Window<R>) + Send + Sync + 'static,
338339
{
339340
self.on_webview_ready = Box::new(on_webview_ready);
340341
self
@@ -366,7 +367,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
366367
#[must_use]
367368
pub fn on_event<F>(mut self, on_event: F) -> Self
368369
where
369-
F: Fn(&AppHandle<R>, &RunEvent) + Send + Sync + 'static,
370+
F: FnMut(&AppHandle<R>, &RunEvent) + Send + Sync + 'static,
370371
{
371372
self.on_event = Box::new(on_event);
372373
self
@@ -391,7 +392,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
391392
pub struct TauriPlugin<R: Runtime, C: DeserializeOwned = ()> {
392393
name: &'static str,
393394
invoke_handler: Box<InvokeHandler<R>>,
394-
setup: Box<SetupHook<R>>,
395+
setup: Option<Box<SetupHook<R>>>,
395396
setup_with_config: Option<Box<SetupWithConfigHook<R, C>>>,
396397
js_init_script: Option<String>,
397398
on_page_load: Box<OnPageLoad<R>>,
@@ -405,8 +406,10 @@ impl<R: Runtime, C: DeserializeOwned> Plugin<R> for TauriPlugin<R, C> {
405406
}
406407

407408
fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()> {
408-
(self.setup)(app)?;
409-
if let Some(s) = &self.setup_with_config {
409+
if let Some(s) = self.setup.take() {
410+
(s)(app)?;
411+
}
412+
if let Some(s) = self.setup_with_config.take() {
410413
(s)(app, serde_json::from_value(config)?)?;
411414
}
412415
Ok(())

0 commit comments

Comments
 (0)