From 36a08bd6bbe172bbcc3a1a5551308063696e417a Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 10 Feb 2021 00:53:06 -0300 Subject: [PATCH] refactor(tauri): plugin trait with mutable references --- .changes/plugin-mutable.md | 5 +++++ tauri/src/plugin.rs | 30 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 .changes/plugin-mutable.md diff --git a/.changes/plugin-mutable.md b/.changes/plugin-mutable.md new file mode 100644 index 00000000000..edba67e76df --- /dev/null +++ b/.changes/plugin-mutable.md @@ -0,0 +1,5 @@ +--- +"tauri": minor +--- + +The plugin instance is now mutable and must be `Send + Sync`. diff --git a/tauri/src/plugin.rs b/tauri/src/plugin.rs index beed20621f7..3aece90e381 100644 --- a/tauri/src/plugin.rs +++ b/tauri/src/plugin.rs @@ -29,13 +29,13 @@ impl From for Error { /// The plugin interface. #[async_trait::async_trait] -pub trait Plugin: Sync { +pub trait Plugin: Send + Sync { /// The plugin name. Used as key on the plugin config object. fn name(&self) -> &'static str; /// Initialize the plugin. #[allow(unused_variables)] - async fn initialize(&self, config: String) -> Result<(), Error> { + async fn initialize(&mut self, config: String) -> Result<(), Error> { Ok(()) } @@ -46,15 +46,15 @@ pub trait Plugin: Sync { /// Callback invoked when the webview is created. #[allow(unused_variables)] - async fn created(&self, dispatcher: D) {} + async fn created(&mut self, dispatcher: D) {} /// Callback invoked when the webview is ready. #[allow(unused_variables)] - async fn ready(&self, dispatcher: D) {} + async fn ready(&mut self, dispatcher: D) {} /// Add invoke_handler API extension commands. #[allow(unused_variables)] - async fn extend_api(&self, dispatcher: D, payload: &str) -> Result<(), Error> { + async fn extend_api(&mut self, dispatcher: D, payload: &str) -> Result<(), Error> { Err(Error::UnknownApi) } } @@ -75,9 +75,9 @@ pub(crate) async fn initialize( store: &PluginStore, plugins_config: PluginConfig, ) -> crate::Result<()> { - let plugins = store.lock().await; + let mut plugins = store.lock().await; let mut futures = Vec::new(); - for plugin in plugins.iter() { + for plugin in plugins.iter_mut() { let plugin_config = plugins_config.get(plugin.name()); futures.push(plugin.initialize(plugin_config)); } @@ -92,9 +92,9 @@ pub(crate) async fn initialize( pub(crate) async fn init_script( store: &PluginStore, ) -> String { - let plugins = store.lock().await; + let mut plugins = store.lock().await; let mut futures = Vec::new(); - for plugin in plugins.iter() { + for plugin in plugins.iter_mut() { futures.push(plugin.init_script()); } @@ -111,9 +111,9 @@ pub(crate) async fn created( store: &PluginStore, dispatcher: &mut D, ) { - let plugins = store.lock().await; + let mut plugins = store.lock().await; let mut futures = Vec::new(); - for plugin in plugins.iter() { + for plugin in plugins.iter_mut() { futures.push(plugin.created(dispatcher.clone())); } join_all(futures).await; @@ -123,9 +123,9 @@ pub(crate) async fn ready( store: &PluginStore, dispatcher: &mut D, ) { - let plugins = store.lock().await; + let mut plugins = store.lock().await; let mut futures = Vec::new(); - for plugin in plugins.iter() { + for plugin in plugins.iter_mut() { futures.push(plugin.ready(dispatcher.clone())); } join_all(futures).await; @@ -136,8 +136,8 @@ pub(crate) async fn extend_api( dispatcher: &mut D, arg: &str, ) -> Result { - let plugins = store.lock().await; - for ext in plugins.iter() { + let mut plugins = store.lock().await; + for ext in plugins.iter_mut() { match ext.extend_api(dispatcher.clone(), arg).await { Ok(_) => { return Ok(true);