Просмотр исходного кода

fix(core): command name on plugin invoke handler (#1577)

Lucas Fernandes Nogueira 4 лет назад
Родитель
Сommit
422dd5e2a0

+ 5 - 0
.changes/fix-plugin-invoke.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Fixes the Message `command` name value on plugin invoke handler.

+ 6 - 3
core/tauri-macros/src/command.rs

@@ -108,7 +108,7 @@ pub fn generate_command(attrs: Vec<NestedMeta>, function: ItemFn) -> TokenStream
         Ok(parsed_args) => message.respond_async(async move {
           #return_value
         }),
-        Err(e) => message.reject(::core::result::Result::<(), String>::Err(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string())),
+        Err(e) => message.reject(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string()),
       }
     }
   }
@@ -135,9 +135,12 @@ pub fn generate_handler(item: proc_macro::TokenStream) -> TokenStream {
 
   quote! {
     move |message| {
-      match message.command() {
+      let cmd = message.command().to_string();
+      match cmd.as_str() {
         #(stringify!(#fn_names) => #fn_wrappers(message),)*
-        _ => {},
+        _ => {
+          message.reject(format!("command {} not found", cmd))
+        },
       }
     }
   }

+ 1 - 1
core/tauri/src/hooks.rs

@@ -48,7 +48,7 @@ pub(crate) struct InvokePayload {
 /// An invoke message.
 pub struct InvokeMessage<M: Params> {
   window: Window<M>,
-  command: String,
+  pub(crate) command: String,
 
   /// Allow our crate to access the payload without cloning it.
   pub(crate) payload: InvokePayload,

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

@@ -105,16 +105,20 @@ impl<M: Params> PluginStore<M> {
       .for_each(|plugin| plugin.on_page_load(window.clone(), payload.clone()))
   }
 
-  pub(crate) fn extend_api(&mut self, command: String, message: InvokeMessage<M>) {
-    let target = command
-      .replace("plugin:", "")
-      .split('|')
-      .next()
-      .expect("target plugin name empty")
-      .to_string();
-
-    if let Some(plugin) = self.store.get_mut(target.as_str()) {
+  pub(crate) fn extend_api(&mut self, mut message: InvokeMessage<M>) {
+    let command = message.command.replace("plugin:", "");
+    let mut tokens = command.split('|');
+    // safe to unwrap: split always has a least one item
+    let target = tokens.next().unwrap();
+
+    if let Some(plugin) = self.store.get_mut(target) {
+      message.command = tokens
+        .next()
+        .map(|c| c.to_string())
+        .unwrap_or_else(String::new);
       plugin.extend_api(message);
+    } else {
+      message.reject(format!("plugin {} not found", target));
     }
   }
 }

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

@@ -405,13 +405,13 @@ impl<P: Params> WindowManager<P> {
       .expect("poisoned plugin store")
       .on_page_load(window, payload);
   }
-  pub fn extend_api(&self, command: String, message: InvokeMessage<P>) {
+  pub fn extend_api(&self, message: InvokeMessage<P>) {
     self
       .inner
       .plugins
       .lock()
       .expect("poisoned plugin store")
-      .extend_api(command, message);
+      .extend_api(message);
   }
   pub fn initialize_plugins(&self) -> crate::Result<()> {
     self

+ 1 - 1
core/tauri/src/runtime/window.rs

@@ -182,7 +182,7 @@ pub(crate) mod export {
           let module = module.to_string();
           crate::endpoints::handle(module, message, manager.config(), manager.package_info());
         } else if command.starts_with("plugin:") {
-          manager.extend_api(command, message);
+          manager.extend_api(message);
         } else {
           manager.run_invoke_handler(message);
         }