mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-27 01:21:20 -08:00
fix(site): [SITE-VCONSOLE-01] cancel stale console lifecycle work
This commit is contained in:
1 parent
838c3eb432
commit
e7f31b6fc8
22 files changed
+2318
-32
No files matched your search
@@ -0,0 +1,63 @@
|
||||
interface LogHost {
|
||||
ADDED_LOG_PLUGIN_ID: string[]
|
||||
flushLogScheduled: boolean
|
||||
logQueue: unknown[]
|
||||
_flushLogs: () => void
|
||||
unmockConsole: () => void
|
||||
}
|
||||
interface Frames {
|
||||
requestAnimationFrame: (callback: FrameRequestCallback) => number
|
||||
cancelAnimationFrame: (id: number) => void
|
||||
}
|
||||
const pending: { host: LogHost, id: number }[] = []
|
||||
|
||||
interface PanelHost {
|
||||
isInited: boolean
|
||||
pluginList: Record<string, { id: string }>
|
||||
}
|
||||
|
||||
export function renderTab(host: PanelHost, plugin: { id: string }, render: () => void): void {
|
||||
setTimeout(() => {
|
||||
if (host.isInited && host.pluginList[plugin.id] === plugin)
|
||||
render()
|
||||
}, 0)
|
||||
}
|
||||
|
||||
export function enqueue(host: LogHost, log: unknown, frames: Frames): void {
|
||||
if (host.ADDED_LOG_PLUGIN_ID.length === 0)
|
||||
return
|
||||
if (!host.flushLogScheduled) {
|
||||
const ticket = { host, id: 0 }
|
||||
pending.push(ticket)
|
||||
host.flushLogScheduled = true
|
||||
ticket.id = frames.requestAnimationFrame(() => {
|
||||
const index = pending.indexOf(ticket)
|
||||
if (index === -1)
|
||||
return
|
||||
pending.splice(index, 1)
|
||||
host.flushLogScheduled = false
|
||||
host._flushLogs()
|
||||
})
|
||||
}
|
||||
host.logQueue.push(log)
|
||||
}
|
||||
|
||||
export function unbind(host: LogHost, frames: Frames): void {
|
||||
let ticket
|
||||
for (let index = 0; index < pending.length; index++) {
|
||||
if (pending[index]!.host === host) {
|
||||
ticket = pending[index]
|
||||
pending.splice(index, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
host.logQueue = []
|
||||
host.flushLogScheduled = false
|
||||
try {
|
||||
if (ticket && typeof frames.cancelAnimationFrame === 'function')
|
||||
frames.cancelAnimationFrame(ticket.id)
|
||||
}
|
||||
finally {
|
||||
host.unmockConsole()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user