Skip to content

Commit ad1dec2

Browse files
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 <[email protected]>
1 parent beffcd8 commit ad1dec2

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": patch:bug
3+
---
4+
5+
Fix `addPluginListener` fallback added in https://github.com/tauri-apps/tauri/pull/14132 didn't work properly

crates/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/src/core.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,16 @@ async function addPluginListener<T>(
186186
): Promise<PluginListener> {
187187
const handler = new Channel<T>(cb)
188188
try {
189-
return invoke(`plugin:${plugin}|register_listener`, {
189+
await invoke(`plugin:${plugin}|register_listener`, {
190190
event,
191191
handler
192-
}).then(() => new PluginListener(plugin, event, handler.id))
192+
})
193+
return new PluginListener(plugin, event, handler.id)
193194
} catch {
194195
// TODO(v3): remove this fallback
195196
// note: we must try with camelCase here for backwards compatibility
196-
return invoke(`plugin:${plugin}|registerListener`, { event, handler }).then(
197-
() => new PluginListener(plugin, event, handler.id)
198-
)
197+
await invoke(`plugin:${plugin}|registerListener`, { event, handler })
198+
return new PluginListener(plugin, event, handler.id)
199199
}
200200
}
201201

0 commit comments

Comments
 (0)