大哥 主播 即时消息 三合一
This commit is contained in:
131
src/hooks/useUpdate.js
Normal file
131
src/hooks/useUpdate.js
Normal file
@@ -0,0 +1,131 @@
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
import { isElectron } from '../utils/electronBridge'
|
||||
|
||||
// NOTE: Since we are using JS, we don't have interfaces, but structure remains same.
|
||||
|
||||
/**
|
||||
* 应用更新 Hook (Vue Composable)
|
||||
* 管理更新状态、进度和操作
|
||||
* 注意:此 Composable 仅在 Electron 环境中有效
|
||||
*/
|
||||
export function useUpdate() {
|
||||
const status = ref('idle')
|
||||
const updateInfo = ref(null)
|
||||
const progress = ref(null)
|
||||
const error = ref(null)
|
||||
const currentVersion = ref('')
|
||||
|
||||
// 获取当前版本
|
||||
const fetchVersion = () => {
|
||||
if (!isElectron()) {
|
||||
currentVersion.value = 'web'
|
||||
return
|
||||
}
|
||||
window.electronAPI.getAppVersion().then(v => {
|
||||
currentVersion.value = v
|
||||
}).catch(console.error)
|
||||
}
|
||||
|
||||
let unsubList = []
|
||||
|
||||
// 监听更新事件
|
||||
const setupListeners = () => {
|
||||
if (!isElectron()) return
|
||||
|
||||
const api = window.electronAPI
|
||||
|
||||
// 正在检查
|
||||
if (api.onUpdateChecking) {
|
||||
unsubList.push(api.onUpdateChecking(() => {
|
||||
status.value = 'checking'
|
||||
}))
|
||||
}
|
||||
|
||||
// 发现新版本
|
||||
unsubList.push(api.onUpdateAvailable((info) => {
|
||||
updateInfo.value = info
|
||||
status.value = 'available'
|
||||
error.value = null
|
||||
}))
|
||||
|
||||
// 无可用更新
|
||||
if (api.onUpdateNotAvailable) {
|
||||
unsubList.push(api.onUpdateNotAvailable(() => {
|
||||
status.value = 'idle'
|
||||
}))
|
||||
}
|
||||
|
||||
unsubList.push(api.onUpdateProgress((prog) => {
|
||||
progress.value = prog
|
||||
status.value = 'downloading'
|
||||
}))
|
||||
|
||||
unsubList.push(api.onUpdateDownloaded(() => {
|
||||
status.value = 'downloaded'
|
||||
progress.value = null
|
||||
}))
|
||||
|
||||
unsubList.push(api.onUpdateError((err) => {
|
||||
error.value = err.message
|
||||
status.value = 'error'
|
||||
}))
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
fetchVersion()
|
||||
setupListeners()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
unsubList.forEach(unsub => unsub && unsub())
|
||||
unsubList = []
|
||||
})
|
||||
|
||||
const checkForUpdates = () => {
|
||||
if (!isElectron()) return
|
||||
|
||||
status.value = 'checking'
|
||||
error.value = null
|
||||
window.electronAPI.checkForUpdates().then((hasUpdate) => {
|
||||
if (!hasUpdate) {
|
||||
status.value = 'idle'
|
||||
}
|
||||
}).catch((e) => {
|
||||
error.value = e.message
|
||||
status.value = 'error'
|
||||
})
|
||||
}
|
||||
|
||||
const downloadUpdate = () => {
|
||||
if (!isElectron()) return
|
||||
|
||||
status.value = 'downloading'
|
||||
progress.value = { percent: 0, bytesPerSecond: 0, transferred: 0, total: 0 }
|
||||
window.electronAPI.downloadUpdate().catch((e) => {
|
||||
error.value = e.message
|
||||
status.value = 'error'
|
||||
})
|
||||
}
|
||||
|
||||
const installUpdate = () => {
|
||||
if (!isElectron()) return
|
||||
window.electronAPI.installUpdate()
|
||||
}
|
||||
|
||||
const dismissUpdate = () => {
|
||||
status.value = 'idle'
|
||||
updateInfo.value = null
|
||||
}
|
||||
|
||||
return {
|
||||
status,
|
||||
updateInfo,
|
||||
progress,
|
||||
error,
|
||||
currentVersion,
|
||||
checkForUpdates,
|
||||
downloadUpdate,
|
||||
installUpdate,
|
||||
dismissUpdate
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user