[feat] listen to steam user changes
This commit is contained in:
@@ -5,7 +5,7 @@ import { toolStore, useToolStore } from "@/store/tool"
|
||||
import { addToast } from "@heroui/react"
|
||||
import { invoke } from "@tauri-apps/api/core"
|
||||
import { listen } from "@tauri-apps/api/event"
|
||||
import { useDebounce } from "ahooks"
|
||||
import { useDebounce, useThrottleFn } from "ahooks"
|
||||
import { useEffect } from "react"
|
||||
import "./globals.css"
|
||||
import Providers from "./providers"
|
||||
@@ -75,8 +75,35 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
useEffect(() => {
|
||||
if (debounceSteamDirValid) {
|
||||
void steam.getUsers()
|
||||
// 启动文件监听
|
||||
void invoke("start_watch_loginusers", { steamDir: steam.state.steamDir })
|
||||
}
|
||||
}, [debounceSteamDirValid])
|
||||
}, [debounceSteamDirValid, steam.state.steamDir])
|
||||
|
||||
// 节流获取用户列表函数,3秒间隔,trailing模式
|
||||
const { run: throttledGetUsers } = useThrottleFn(
|
||||
async () => {
|
||||
if (steam.state.steamDirValid) {
|
||||
await steam.getUsers()
|
||||
}
|
||||
},
|
||||
{
|
||||
wait: 3000,
|
||||
leading: false,
|
||||
trailing: true,
|
||||
}
|
||||
)
|
||||
|
||||
// 监听 loginusers.vdf 文件变化事件
|
||||
useEffect(() => {
|
||||
const unlisten = listen("steam://loginusers_changed", async () => {
|
||||
// 文件变化时使用节流函数重新获取用户列表
|
||||
throttledGetUsers()
|
||||
})
|
||||
return () => {
|
||||
void unlisten.then((fn) => fn())
|
||||
}
|
||||
}, [steam.state.steamDirValid, throttledGetUsers])
|
||||
|
||||
return (
|
||||
<html lang="en">
|
||||
|
||||
Reference in New Issue
Block a user