Skip to content

Commit

Permalink
fix(video-speed-controller): fix the1812#1504
Browse files Browse the repository at this point in the history
* API 变化
  - `getInstance` 和 `constructor` 均新增一个参数,用于传递原生倍速值
  - 在 `containerElement` 元素上分发的自定义事件的 `detail` 属性新增一个 `isNativeSpeed` 属性,用于表示变更的倍速是否为原生倍速
* 功能变化
  - 现在缓存命中机制除了考虑 `containerElement` 元素,还会比较上一次倍数值和原生倍速值
  - 修正了内部原生倍速值的获取方法,现在优先使用传入的原生倍速值,其次尝试使用上一次倍数值,最后才使用默认的 1.0x 值
  • Loading branch information
LonelySteve committed Jan 30, 2021
1 parent a408d89 commit 5de960c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/video/video-speed/video-speed-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class VideoSpeedController {
settings.rememberVideoSpeedList = settings.rememberVideoSpeedList
}

static async getInstance(previousSpeed?: number) {
static async getInstance(previousSpeed?: number, previousNativeSpeed?: number) {
const containerElement = await SpinQuery.select(`.${VideoSpeedController.classNameMap.speedContainer}`)
const videoElement = await SpinQuery.select(`.${VideoSpeedController.classNameMap.video} video`) as HTMLVideoElement

Expand All @@ -103,19 +103,20 @@ export class VideoSpeedController {
throw "video element not found!"
}

return new VideoSpeedController(containerElement, videoElement, previousSpeed)
return new VideoSpeedController(containerElement, videoElement, previousSpeed, previousNativeSpeed)
}

static init = _.once(() => {
// 分 P 切换时共享同一个倍数
let sharedSpeed = 1
let sharedNativeSpeed = 1
// 持有菜单容器元素的引用,videoChange 时更换(以前缓存的 VideoController 就没有意义了)
let containerElement: HTMLElement

Observer.videoChange(async () => {
const { getExtraSpeedMenuItemElements } = await import("./extend-video-speed")
// 有必要传递之前的 nativeSpeedVal,跨分 P 时原生倍数将保持一样
const controller = await VideoSpeedController.getInstance(sharedSpeed)
// 有必要传递之前的 nativeSpeed,跨分 P 时原生倍数将保持一样
const controller = await VideoSpeedController.getInstance(sharedSpeed, sharedNativeSpeed)
controller.observe();
if (settings.extendVideoSpeed) {
controller._menuListElement.prepend(...await getExtraSpeedMenuItemElements())
Expand All @@ -136,10 +137,13 @@ export class VideoSpeedController {
}
})
}
// 理论上这里的 controller 一定是非缓存的,因此不用太担心事件监听注册重复
// 理论上这里的 controller 一定是非缓存的(换 P 的时候 containerElement 是会发生改变的),因此不用担心重复注册事件监听
({ containerElement } = controller)
containerElement.addEventListener("changed", ({ detail: { speed } }: CustomEvent) => {
containerElement.addEventListener("changed", ({ detail: { speed, isNativeSpeed } }: CustomEvent) => {
sharedSpeed = speed
if (isNativeSpeed) {
sharedNativeSpeed = speed
}
})
// 首次加载可能会遇到意外情况,导致内部强制更新失效,因此延时 100 ms 再触发速度设置
setTimeout(() => {
Expand All @@ -156,9 +160,9 @@ export class VideoSpeedController {
private _nativeSpeedVal: number
private _previousSpeedVal: number

constructor(containerElement: HTMLElement, videoElement: HTMLVideoElement, previousSpeed?: number) {
constructor(containerElement: HTMLElement, videoElement: HTMLVideoElement, previousSpeed?: number, nativeSpeed?: number) {
const controller = VideoSpeedController.instanceMap.get(containerElement)
if (controller && (!previousSpeed || controller.playbackRate === previousSpeed)) {
if (controller && (!previousSpeed || controller._previousSpeedVal === previousSpeed) && (!nativeSpeed || controller._nativeSpeedVal === nativeSpeed)) {
return controller
}

Expand All @@ -168,7 +172,7 @@ export class VideoSpeedController {

this._containerElement = containerElement
this._previousSpeedVal = previousSpeed
this._nativeSpeedVal = VideoSpeedController.nativeSupportedRates.includes(previousSpeed) ? previousSpeed : 1
this._nativeSpeedVal = nativeSpeed ? nativeSpeed : (VideoSpeedController.nativeSupportedRates.includes(previousSpeed) ? previousSpeed : 1)
this._nameBtn = this._containerElement.querySelector(`.${VideoSpeedController.classNameMap.speedNameBtn}`) as HTMLButtonElement
this._menuListElement = this._containerElement.querySelector(`.${VideoSpeedController.classNameMap.speedMenuList}`) as HTMLElement

Expand Down Expand Up @@ -213,11 +217,13 @@ export class VideoSpeedController {

currentSpeed = parseFloat(selectedSpeedOption.dataset.value ?? '1')

this._containerElement.dispatchEvent(new CustomEvent("changed", { detail: { speed: currentSpeed, previousSpeed: this._previousSpeedVal } }))

let isNativeSpeed = false
if (VideoSpeedController.nativeSupportedRates.includes(currentSpeed)) {
this._nativeSpeedVal = currentSpeed
isNativeSpeed = true
}

this._containerElement.dispatchEvent(new CustomEvent("changed", { detail: { speed: currentSpeed, isNativeSpeed, previousSpeed: this._previousSpeedVal } }))
// 原生支持倍数的应用后,有必要清除扩展倍数选项上的样式
if (settings.extendVideoSpeed && VideoSpeedController.nativeSupportedRates.includes(currentSpeed)) {
this._menuListElement.querySelector(`.${VideoSpeedController.classNameMap.speedMenuItem}.extended.${VideoSpeedController.classNameMap.active}`)?.classList.remove(VideoSpeedController.classNameMap.active)
Expand Down

0 comments on commit 5de960c

Please sign in to comment.