Explorar el Código

refactor(core): change Plugin `initialize` signature, move register t… (#2347)

* refactor(core): change Plugin `initialize` signature, move register to AppHandle

* clippy
Lucas Fernandes Nogueira hace 4 años
padre
commit
c17532f741

+ 1 - 1
.changes/dynamic-plugin.md

@@ -2,4 +2,4 @@
 "tauri": patch
 ---
 
-Allow registering a plugin on structs that implements the `Manager` trait (`App`, `AppHandle`, `Window`) using the trait's `plugin` method.
+Allow registering a plugin through an `AppHandle` instance using the `plugin` method.

+ 5 - 0
.changes/plugin-initialize-app-handle.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+**Breaking change**: The `Plugin` trait `initialize` method now takes an `AppHandle` reference instead of `App`.

+ 25 - 1
core/tauri/src/app.rs

@@ -199,6 +199,30 @@ impl<R: Runtime> AppHandle<R> {
   fn remove_system_tray(&self) -> crate::Result<()> {
     self.runtime_handle.remove_system_tray().map_err(Into::into)
   }
+
+  /// Adds a plugin to the runtime.
+  pub fn plugin<P: Plugin<R> + 'static>(&self, mut plugin: P) -> crate::Result<()> {
+    plugin
+      .initialize(
+        self,
+        self
+          .config()
+          .plugins
+          .0
+          .get(plugin.name())
+          .cloned()
+          .unwrap_or_default(),
+      )
+      .map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))?;
+    self
+      .manager()
+      .inner
+      .plugins
+      .lock()
+      .unwrap()
+      .register(plugin);
+    Ok(())
+  }
 }
 
 impl<R: Runtime> Manager<R> for AppHandle<R> {}
@@ -832,7 +856,7 @@ impl<R: Runtime> Builder<R> {
       },
     };
 
-    app.manager.initialize_plugins(&app)?;
+    app.manager.initialize_plugins(&app.handle())?;
 
     let pending_labels = self
       .pending_windows

+ 0 - 11
core/tauri/src/lib.rs

@@ -323,17 +323,6 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
   {
     self.manager().inner.state.try_get()
   }
-
-  /// Adds a plugin to the runtime.
-  fn plugin<P: plugin::Plugin<R> + 'static>(&self, plugin: P) {
-    self
-      .manager()
-      .inner
-      .plugins
-      .lock()
-      .unwrap()
-      .register(plugin);
-  }
 }
 
 /// Prevent implementation details from leaking out of the [`Manager`] trait.

+ 2 - 2
core/tauri/src/manager.rs

@@ -20,7 +20,7 @@ use crate::{
     window::{dpi::PhysicalSize, DetachedWindow, PendingWindow, WindowEvent},
     Icon, Runtime,
   },
-  App, Context, Invoke, StateManager, Window,
+  Context, Invoke, StateManager, Window,
 };
 
 #[cfg(target_os = "windows")]
@@ -534,7 +534,7 @@ impl<R: Runtime> WindowManager<R> {
       .extend_api(invoke);
   }
 
-  pub fn initialize_plugins(&self, app: &App<R>) -> crate::Result<()> {
+  pub fn initialize_plugins(&self, app: &AppHandle<R>) -> crate::Result<()> {
     self
       .inner
       .plugins

+ 9 - 3
core/tauri/src/plugin.rs

@@ -4,7 +4,9 @@
 
 //! Extend Tauri functionality.
 
-use crate::{api::config::PluginConfig, runtime::Runtime, App, Invoke, PageLoadPayload, Window};
+use crate::{
+  api::config::PluginConfig, runtime::Runtime, AppHandle, Invoke, PageLoadPayload, Window,
+};
 use serde_json::Value as JsonValue;
 use std::collections::HashMap;
 
@@ -20,7 +22,7 @@ pub trait Plugin<R: Runtime>: Send {
 
   /// Initialize the plugin.
   #[allow(unused_variables)]
-  fn initialize(&mut self, app: &App<R>, config: JsonValue) -> Result<()> {
+  fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()> {
     Ok(())
   }
 
@@ -69,7 +71,11 @@ impl<R: Runtime> PluginStore<R> {
   }
 
   /// Initializes all plugins in the store.
-  pub(crate) fn initialize(&mut self, app: &App<R>, config: &PluginConfig) -> crate::Result<()> {
+  pub(crate) fn initialize(
+    &mut self,
+    app: &AppHandle<R>,
+    config: &PluginConfig,
+  ) -> crate::Result<()> {
     self.store.values_mut().try_for_each(|plugin| {
       plugin
         .initialize(

+ 2 - 2
docs/usage/guides/plugin.md

@@ -17,7 +17,7 @@ Plugins allow you to hook into the Tauri application lifecycle and introduce new
 To write a plugin you just need to implement the `tauri::plugin::Plugin` trait:
 
 ```rust
-use tauri::{plugin::{Plugin, Result as PluginResult}, Runtime, PageLoadPayload, Window, Invoke, App};
+use tauri::{plugin::{Plugin, Result as PluginResult}, Runtime, PageLoadPayload, Window, Invoke, AppHandle};
 
 struct MyAwesomePlugin<R: Runtime> {
   invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
@@ -59,7 +59,7 @@ impl<R: Runtime> Plugin<R> for MyAwesomePlugin<R> {
   }
 
   /// initialize plugin with the config provided on `tauri.conf.json > plugins > $yourPluginName` or the default value.
-  fn initialize(&mut self, app: &App<R>, config: serde_json::Value) -> PluginResult<()> {
+  fn initialize(&mut self, app: &AppHandle<R>, config: serde_json::Value) -> PluginResult<()> {
     Ok(())
   }