Skip to content

Commit

Permalink
fix(core): command name on plugin invoke handler (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 22, 2021
1 parent c03e312 commit 422dd5e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-plugin-invoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Fixes the Message `command` name value on plugin invoke handler.
9 changes: 6 additions & 3 deletions core/tauri-macros/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
}
Expand All @@ -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))
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 13 additions & 9 deletions core/tauri/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
4 changes: 2 additions & 2 deletions core/tauri/src/runtime/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/runtime/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 422dd5e

Please sign in to comment.