fix(web-mode): consolidate Tauri event subscription helper to silence transformCallback errors (#256)

- Add shared safeTauriListen helper in tauri-api.js that returns a noop
  unsubscriber when running outside Tauri, so dynamic-importing
  @tauri-apps/api/event in the browser no longer throws
  'Cannot read properties of undefined (reading transformCallback)'.
- Replace 4 bare 'await import(@tauri-apps/api/event)' call sites
  (about.js hermes upgrade button + channels.js three install/action flows)
  that previously crashed the page on web mode.
- Drop the duplicated local tauriListen helpers in hermes dashboard / chat
  store and route them through the shared helper.
This commit is contained in:
晴天
2026-05-14 01:31:58 +08:00
parent 081ad4af25
commit d0d6950628
5 changed files with 36 additions and 48 deletions

View File

@@ -9,6 +9,27 @@ export function isTauriRuntime() {
return !!window.__TAURI_INTERNALS__ || !!window.__TAURI__ || window.location?.hostname === 'tauri.localhost'
}
let _tauriListenFn = null
/**
* 安全订阅 Tauri 事件。Web 模式下返回 noop unsubscriber
* 避免动态 import `@tauri-apps/api/event` 时触碰
* `window.__TAURI_INTERNALS__.transformCallback` 引发
* "Cannot read properties of undefined" 报错issue #256
*
* 用法:
* const unlisten = await safeTauriListen('hermes-install-log', e => ...)
* unlisten() // 取消订阅
*/
export async function safeTauriListen(event, cb) {
if (!isTauriRuntime()) return () => {}
if (!_tauriListenFn) {
const mod = await import('@tauri-apps/api/event')
_tauriListenFn = mod.listen
}
return _tauriListenFn(event, cb)
}
// 仅在 Node.js 后端实现的命令Tauri Rust 不处理),强制走 webInvoke
const WEB_ONLY_CMDS = new Set([
'instance_list', 'instance_add', 'instance_remove', 'instance_set_active',