Skip to content

Commit

Permalink
refactor(tauri): plugin trait with mutable references (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Feb 10, 2021
1 parent 2058cc3 commit fb607ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changes/plugin-mutable.md
@@ -0,0 +1,5 @@
---
"tauri": minor
---

The plugin instance is now mutable and must be `Send + Sync`.
30 changes: 15 additions & 15 deletions tauri/src/plugin.rs
Expand Up @@ -29,13 +29,13 @@ impl From<serde_json::Error> for Error {

/// The plugin interface.
#[async_trait::async_trait]
pub trait Plugin<D: ApplicationDispatcherExt + 'static>: Sync {
pub trait Plugin<D: ApplicationDispatcherExt + 'static>: 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(())
}

Expand All @@ -46,15 +46,15 @@ pub trait Plugin<D: ApplicationDispatcherExt + 'static>: 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)
}
}
Expand All @@ -75,9 +75,9 @@ pub(crate) async fn initialize<D: ApplicationDispatcherExt + 'static>(
store: &PluginStore<D>,
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));
}
Expand All @@ -92,9 +92,9 @@ pub(crate) async fn initialize<D: ApplicationDispatcherExt + 'static>(
pub(crate) async fn init_script<D: ApplicationDispatcherExt + 'static>(
store: &PluginStore<D>,
) -> 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());
}

Expand All @@ -111,9 +111,9 @@ pub(crate) async fn created<D: ApplicationDispatcherExt + 'static>(
store: &PluginStore<D>,
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;
Expand All @@ -123,9 +123,9 @@ pub(crate) async fn ready<D: ApplicationDispatcherExt + 'static>(
store: &PluginStore<D>,
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;
Expand All @@ -136,8 +136,8 @@ pub(crate) async fn extend_api<D: ApplicationDispatcherExt + 'static>(
dispatcher: &mut D,
arg: &str,
) -> Result<bool, Error> {
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);
Expand Down

0 comments on commit fb607ee

Please sign in to comment.