fix(core): properly handle async errors in addPluginListener (#14464)

* fix(core): properly handle async errors in addPluginListener

The previous implementation used .then() after invoke() without await, which prevented the catch block from handling rejected promises. Now using await to properly catch errors and allow fallback to camelCase registerListener method.

* Change file and generate `bundle.global.js`

---------

Co-authored-by: Tony <legendmastertony@gmail.com>
This commit is contained in:
Aleksey Ponomarev
2025-11-15 06:07:51 +02:00
committed by GitHub
parent beffcd880f
commit ad1dec2e24
3 changed files with 11 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
---
"@tauri-apps/api": patch:bug
---
Fix `addPluginListener` fallback added in https://github.com/tauri-apps/tauri/pull/14132 didn't work properly

File diff suppressed because one or more lines are too long

View File

@@ -186,16 +186,16 @@ async function addPluginListener<T>(
): Promise<PluginListener> {
const handler = new Channel<T>(cb)
try {
return invoke(`plugin:${plugin}|register_listener`, {
await invoke(`plugin:${plugin}|register_listener`, {
event,
handler
}).then(() => new PluginListener(plugin, event, handler.id))
})
return new PluginListener(plugin, event, handler.id)
} catch {
// TODO(v3): remove this fallback
// note: we must try with camelCase here for backwards compatibility
return invoke(`plugin:${plugin}|registerListener`, { event, handler }).then(
() => new PluginListener(plugin, event, handler.id)
)
await invoke(`plugin:${plugin}|registerListener`, { event, handler })
return new PluginListener(plugin, event, handler.id)
}
}