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

This commit is contained in:
Lucas Fernandes Nogueira
2021-04-21 23:31:27 -03:00
committed by GitHub
parent c03e312c28
commit 422dd5e2a0
6 changed files with 27 additions and 15 deletions

View File

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

View File

@@ -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))
},
}
}
}

View File

@@ -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,

View File

@@ -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();
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.as_str()) {
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));
}
}
}

View File

@@ -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

View File

@@ -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);
}