mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
feat: 语音
This commit is contained in:
1 parent
79ed94bc57
commit
580c7019d7
49 files changed
+3013
-154
No files matched your search
@@ -0,0 +1,61 @@
|
||||
type ScheduledTask<T> = {
|
||||
key: string
|
||||
run: (signal: AbortSignal) => Promise<T>
|
||||
controller: AbortController
|
||||
resolve: (value: T) => void
|
||||
reject: (reason: unknown) => void
|
||||
}
|
||||
|
||||
export class VoiceTaskScheduler {
|
||||
private readonly queue: ScheduledTask<unknown>[] = []
|
||||
private active: ScheduledTask<unknown> | null = null
|
||||
|
||||
schedule<T>(key: string, run: (signal: AbortSignal) => Promise<T>): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
this.queue.push({
|
||||
key,
|
||||
run,
|
||||
controller: new AbortController(),
|
||||
resolve: resolve as (value: unknown) => void,
|
||||
reject
|
||||
})
|
||||
this.pump()
|
||||
})
|
||||
}
|
||||
|
||||
cancel(key: string): boolean {
|
||||
if (this.active?.key === key) {
|
||||
this.active.controller.abort()
|
||||
return true
|
||||
}
|
||||
const index = this.queue.findIndex((task) => task.key === key)
|
||||
if (index < 0) return false
|
||||
const [task] = this.queue.splice(index, 1)
|
||||
task.controller.abort()
|
||||
task.reject(new DOMException('Recognition cancelled', 'AbortError'))
|
||||
return true
|
||||
}
|
||||
|
||||
cancelAll(): void {
|
||||
this.active?.controller.abort()
|
||||
while (this.queue.length) {
|
||||
const task = this.queue.shift()
|
||||
task?.controller.abort()
|
||||
task?.reject(new DOMException('Recognition cancelled', 'AbortError'))
|
||||
}
|
||||
}
|
||||
|
||||
private pump(): void {
|
||||
if (this.active || this.queue.length === 0) return
|
||||
const task = this.queue.shift()
|
||||
if (!task) return
|
||||
this.active = task
|
||||
void task
|
||||
.run(task.controller.signal)
|
||||
.then(task.resolve, task.reject)
|
||||
.finally(() => {
|
||||
this.active = null
|
||||
this.pump()
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user