Skip to content

Commit 422dd5e

Browse files
authored
fix(core): command name on plugin invoke handler (#1577)
1 parent c03e312 commit 422dd5e

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

.changes/fix-plugin-invoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Fixes the Message `command` name value on plugin invoke handler.

core/tauri-macros/src/command.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn generate_command(attrs: Vec<NestedMeta>, function: ItemFn) -> TokenStream
108108
Ok(parsed_args) => message.respond_async(async move {
109109
#return_value
110110
}),
111-
Err(e) => message.reject(::core::result::Result::<(), String>::Err(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string())),
111+
Err(e) => message.reject(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string()),
112112
}
113113
}
114114
}
@@ -135,9 +135,12 @@ pub fn generate_handler(item: proc_macro::TokenStream) -> TokenStream {
135135

136136
quote! {
137137
move |message| {
138-
match message.command() {
138+
let cmd = message.command().to_string();
139+
match cmd.as_str() {
139140
#(stringify!(#fn_names) => #fn_wrappers(message),)*
140-
_ => {},
141+
_ => {
142+
message.reject(format!("command {} not found", cmd))
143+
},
141144
}
142145
}
143146
}

core/tauri/src/hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) struct InvokePayload {
4848
/// An invoke message.
4949
pub struct InvokeMessage<M: Params> {
5050
window: Window<M>,
51-
command: String,
51+
pub(crate) command: String,
5252

5353
/// Allow our crate to access the payload without cloning it.
5454
pub(crate) payload: InvokePayload,

core/tauri/src/plugin.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,20 @@ impl<M: Params> PluginStore<M> {
105105
.for_each(|plugin| plugin.on_page_load(window.clone(), payload.clone()))
106106
}
107107

108-
pub(crate) fn extend_api(&mut self, command: String, message: InvokeMessage<M>) {
109-
let target = command
110-
.replace("plugin:", "")
111-
.split('|')
112-
.next()
113-
.expect("target plugin name empty")
114-
.to_string();
115-
116-
if let Some(plugin) = self.store.get_mut(target.as_str()) {
108+
pub(crate) fn extend_api(&mut self, mut message: InvokeMessage<M>) {
109+
let command = message.command.replace("plugin:", "");
110+
let mut tokens = command.split('|');
111+
// safe to unwrap: split always has a least one item
112+
let target = tokens.next().unwrap();
113+
114+
if let Some(plugin) = self.store.get_mut(target) {
115+
message.command = tokens
116+
.next()
117+
.map(|c| c.to_string())
118+
.unwrap_or_else(String::new);
117119
plugin.extend_api(message);
120+
} else {
121+
message.reject(format!("plugin {} not found", target));
118122
}
119123
}
120124
}

core/tauri/src/runtime/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,13 @@ impl<P: Params> WindowManager<P> {
405405
.expect("poisoned plugin store")
406406
.on_page_load(window, payload);
407407
}
408-
pub fn extend_api(&self, command: String, message: InvokeMessage<P>) {
408+
pub fn extend_api(&self, message: InvokeMessage<P>) {
409409
self
410410
.inner
411411
.plugins
412412
.lock()
413413
.expect("poisoned plugin store")
414-
.extend_api(command, message);
414+
.extend_api(message);
415415
}
416416
pub fn initialize_plugins(&self) -> crate::Result<()> {
417417
self

core/tauri/src/runtime/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub(crate) mod export {
182182
let module = module.to_string();
183183
crate::endpoints::handle(module, message, manager.config(), manager.package_info());
184184
} else if command.starts_with("plugin:") {
185-
manager.extend_api(command, message);
185+
manager.extend_api(message);
186186
} else {
187187
manager.run_invoke_handler(message);
188188
}

0 commit comments

Comments
 (0)