Files
douyin_cookie_yunsya/frontend/src/composables/useLivePoll.js
T
travel 9e0644095f 重构为 HTTP SSO 扫码方案并引入 Vue3 前端
移除 Playwright 浏览器自动化,改用 passport/SSO HTTP 接口获取二维码与轮询登录;后端模块化拆分,前端替换为 Vue3 SPA。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-25 10:47:55 +08:00

61 lines
1.2 KiB
JavaScript

import { ref, onUnmounted } from 'vue'
/**
* 自适应间隔轮询:tick 返回后按 getInterval() 调度下一次
* @param {() => Promise<void>|void} tickFn
* @param {() => number} getIntervalMs
*/
export function useLivePoll(tickFn, getIntervalMs = () => 1000) {
const lastTickAt = ref(null)
const ticking = ref(false)
let timer = null
let inFlight = false
let running = false
async function runTick() {
if (inFlight) return
inFlight = true
ticking.value = true
try {
await tickFn()
lastTickAt.value = Date.now()
} catch (e) {
console.error('[livePoll]', e)
} finally {
inFlight = false
ticking.value = false
}
}
function scheduleNext() {
if (!running) return
clearTimeout(timer)
const ms = Math.max(500, getIntervalMs())
timer = setTimeout(async () => {
await runTick()
scheduleNext()
}, ms)
}
function start() {
if (running) return
running = true
runTick().then(scheduleNext)
}
function stop() {
running = false
clearTimeout(timer)
timer = null
}
function restart() {
stop()
start()
}
onUnmounted(stop)
return { start, stop, restart, runTick, lastTickAt, ticking }
}