mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-28 18:41:20 -08:00
refactor(core): CORE-15 type subtitles and own request and track lifetimes
This commit is contained in:
1 parent
64d1939daa
commit
1b83081e8f
28 files changed
+11286
-405
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import type ResourceScope from '../lifecycle/scope'
|
||||
import { requestController } from '../lifecycle/resources'
|
||||
|
||||
// Cancellation settles callers even when a fetch adapter ignores AbortSignal.
|
||||
export default class SubtitleRequest {
|
||||
readonly scope: ResourceScope
|
||||
readonly signal?: AbortSignal
|
||||
private readonly cancelled: Promise<undefined>
|
||||
|
||||
constructor(parent: ResourceScope) {
|
||||
this.scope = parent.child()
|
||||
this.cancelled = new Promise((resolve) => {
|
||||
this.scope.add(() => {
|
||||
resolve(undefined)
|
||||
})
|
||||
})
|
||||
this.signal = requestController(this.scope)?.signal
|
||||
}
|
||||
|
||||
get active(): boolean {
|
||||
return !this.scope.closed
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.scope.dispose()
|
||||
}
|
||||
|
||||
async run<Result>(work: () => Promise<Result>): Promise<Result | undefined> {
|
||||
if (!this.active)
|
||||
return undefined
|
||||
try {
|
||||
return await Promise.race([work(), this.cancelled])
|
||||
}
|
||||
finally {
|
||||
this.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user