Skip to content

Commit

Permalink
Ease plugin hook restrictions (#3404)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
JonasKruckenberg and lucasfernog authored Feb 13, 2022
1 parent d24045e commit fd557e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-ease-plugin-hook-requirements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

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`.
37 changes: 20 additions & 17 deletions core/tauri/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! The Tauri plugin extension to expand Tauri functionality.

use crate::{
runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, InvokeHandler, OnPageLoad,
PageLoadPayload, RunEvent, Window,
runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, InvokeHandler, PageLoadPayload,
RunEvent, Window,
};
use serde::de::DeserializeOwned;
use serde_json::Value as JsonValue;
Expand Down Expand Up @@ -54,10 +54,11 @@ pub trait Plugin<R: Runtime>: Send {
fn extend_api(&mut self, invoke: Invoke<R>) {}
}

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

/// Builds a [`TauriPlugin`].
///
Expand Down Expand Up @@ -134,7 +135,7 @@ type OnEvent<R> = dyn Fn(&AppHandle<R>, &RunEvent) + Send + Sync;
pub struct Builder<R: Runtime, C: DeserializeOwned = ()> {
name: &'static str,
invoke_handler: Box<InvokeHandler<R>>,
setup: Box<SetupHook<R>>,
setup: Option<Box<SetupHook<R>>>,
setup_with_config: Option<Box<SetupWithConfigHook<R, C>>>,
js_init_script: Option<String>,
on_page_load: Box<OnPageLoad<R>>,
Expand All @@ -147,7 +148,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
pub fn new(name: &'static str) -> Self {
Self {
name,
setup: Box::new(|_| Ok(())),
setup: None,
setup_with_config: None,
js_init_script: None,
invoke_handler: Box::new(|_| ()),
Expand Down Expand Up @@ -250,9 +251,9 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
#[must_use]
pub fn setup<F>(mut self, setup: F) -> Self
where
F: Fn(&AppHandle<R>) -> Result<()> + Send + Sync + 'static,
F: FnOnce(&AppHandle<R>) -> Result<()> + Send + Sync + 'static,
{
self.setup = Box::new(setup);
self.setup.replace(Box::new(setup));
self
}

Expand Down Expand Up @@ -286,7 +287,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
#[must_use]
pub fn setup_with_config<F>(mut self, setup_with_config: F) -> Self
where
F: Fn(&AppHandle<R>, C) -> Result<()> + Send + Sync + 'static,
F: FnOnce(&AppHandle<R>, C) -> Result<()> + Send + Sync + 'static,
{
self.setup_with_config.replace(Box::new(setup_with_config));
self
Expand All @@ -310,7 +311,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
#[must_use]
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
where
F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
F: FnMut(Window<R>, PageLoadPayload) + Send + Sync + 'static,
{
self.on_page_load = Box::new(on_page_load);
self
Expand All @@ -334,7 +335,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
#[must_use]
pub fn on_webview_ready<F>(mut self, on_webview_ready: F) -> Self
where
F: Fn(Window<R>) + Send + Sync + 'static,
F: FnMut(Window<R>) + Send + Sync + 'static,
{
self.on_webview_ready = Box::new(on_webview_ready);
self
Expand Down Expand Up @@ -366,7 +367,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
#[must_use]
pub fn on_event<F>(mut self, on_event: F) -> Self
where
F: Fn(&AppHandle<R>, &RunEvent) + Send + Sync + 'static,
F: FnMut(&AppHandle<R>, &RunEvent) + Send + Sync + 'static,
{
self.on_event = Box::new(on_event);
self
Expand All @@ -391,7 +392,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
pub struct TauriPlugin<R: Runtime, C: DeserializeOwned = ()> {
name: &'static str,
invoke_handler: Box<InvokeHandler<R>>,
setup: Box<SetupHook<R>>,
setup: Option<Box<SetupHook<R>>>,
setup_with_config: Option<Box<SetupWithConfigHook<R, C>>>,
js_init_script: Option<String>,
on_page_load: Box<OnPageLoad<R>>,
Expand All @@ -405,8 +406,10 @@ impl<R: Runtime, C: DeserializeOwned> Plugin<R> for TauriPlugin<R, C> {
}

fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()> {
(self.setup)(app)?;
if let Some(s) = &self.setup_with_config {
if let Some(s) = self.setup.take() {
(s)(app)?;
}
if let Some(s) = self.setup_with_config.take() {
(s)(app, serde_json::from_value(config)?)?;
}
Ok(())
Expand Down

0 comments on commit fd557e9

Please sign in to comment.