新增: Tauri IPC 命令 + useStore 统一接口(B4)
- commands.rs: exec_op/undo/redo/can_undo/can_redo/get_deck/set_navigation/save_file Rust AppState 持有 deck + 历史栈,所有写操作通过 Op Log - bridge.ts: 安全 Tauri IPC 调用层(动态 import,Web 模式 mock) - useStore.ts: 统一 store 接口,检测 __TAURI__ 环境 Web 模式直接调 TS store;Tauri 模式走 invoke - 50 个测试全部通过,前后端编译零错误
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/* =====================================================================
|
||||
* bridge.ts — Tauri IPC 安全调用桥接
|
||||
*
|
||||
* 安全检测 __TAURI__ 环境,动态 import @tauri-apps/api/core。
|
||||
* Web 模式下返回 mock,组件无需关心环境。
|
||||
* ===================================================================== */
|
||||
|
||||
/** 动态获取 invoke 函数 */
|
||||
let _invoke: ((cmd: string, args?: Record<string, unknown>) => Promise<unknown>) | null = null
|
||||
|
||||
async function ensureInvoke() {
|
||||
if (_invoke) return true
|
||||
if (typeof window === 'undefined' || !('__TAURI__' in window)) return false
|
||||
try {
|
||||
const mod = await import('@tauri-apps/api/core')
|
||||
_invoke = mod.invoke
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全调用 Tauri IPC 命令。
|
||||
* Web 模式下返回 undefined(调用方需处理)。
|
||||
*/
|
||||
export async function invoke<T = unknown>(cmd: string, args?: Record<string, unknown>): Promise<T | undefined> {
|
||||
const ok = await ensureInvoke()
|
||||
if (!ok || !_invoke) return undefined
|
||||
return _invoke(cmd, args) as Promise<T>
|
||||
}
|
||||
|
||||
/** 注册 Tauri 事件监听(用于 Rust → 前端推送) */
|
||||
export async function listen<T>(event: string, handler: (payload: T) => void): Promise<() => void> {
|
||||
if (typeof window === 'undefined' || !('__TAURI__' in window)) {
|
||||
return () => {} // 空注销函数
|
||||
}
|
||||
try {
|
||||
const { listen } = await import('@tauri-apps/api/event')
|
||||
return listen(event, (e: { payload: T }) => handler(e.payload))
|
||||
} catch {
|
||||
return () => {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user