Merge pull request #418 from lgy2020/fix/simulator-mask-raf-leak
fix(mask): dispose 后 requestAnimationFrame 循环未停止导致内存泄漏
This commit is contained in:
@@ -10,6 +10,8 @@ export class SimulatorMask extends EventTarget {
|
|||||||
wrapper = document.createElement('div')
|
wrapper = document.createElement('div')
|
||||||
motion: Motion | null = null
|
motion: Motion | null = null
|
||||||
|
|
||||||
|
#disposed = false
|
||||||
|
|
||||||
#cursor = document.createElement('div')
|
#cursor = document.createElement('div')
|
||||||
|
|
||||||
#currentCursorX = 0
|
#currentCursorX = 0
|
||||||
@@ -129,6 +131,8 @@ export class SimulatorMask extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#moveCursorToTarget() {
|
#moveCursorToTarget() {
|
||||||
|
if (this.#disposed) return
|
||||||
|
|
||||||
const newX = this.#currentCursorX + (this.#targetCursorX - this.#currentCursorX) * 0.2
|
const newX = this.#currentCursorX + (this.#targetCursorX - this.#currentCursorX) * 0.2
|
||||||
const newY = this.#currentCursorY + (this.#targetCursorY - this.#currentCursorY) * 0.2
|
const newY = this.#currentCursorY + (this.#targetCursorY - this.#currentCursorY) * 0.2
|
||||||
|
|
||||||
@@ -156,11 +160,15 @@ export class SimulatorMask extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setCursorPosition(x: number, y: number) {
|
setCursorPosition(x: number, y: number) {
|
||||||
|
if (this.#disposed) return
|
||||||
|
|
||||||
this.#targetCursorX = x
|
this.#targetCursorX = x
|
||||||
this.#targetCursorY = y
|
this.#targetCursorY = y
|
||||||
}
|
}
|
||||||
|
|
||||||
triggerClickAnimation() {
|
triggerClickAnimation() {
|
||||||
|
if (this.#disposed) return
|
||||||
|
|
||||||
this.#cursor.classList.remove(cursorStyles.clicking)
|
this.#cursor.classList.remove(cursorStyles.clicking)
|
||||||
// Force reflow to restart animation
|
// Force reflow to restart animation
|
||||||
void this.#cursor.offsetHeight
|
void this.#cursor.offsetHeight
|
||||||
@@ -168,7 +176,7 @@ export class SimulatorMask extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
if (this.shown) return
|
if (this.shown || this.#disposed) return
|
||||||
|
|
||||||
this.shown = true
|
this.shown = true
|
||||||
this.motion?.start()
|
this.motion?.start()
|
||||||
@@ -186,7 +194,7 @@ export class SimulatorMask extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hide() {
|
hide() {
|
||||||
if (!this.shown) return
|
if (!this.shown || this.#disposed) return
|
||||||
|
|
||||||
this.shown = false
|
this.shown = false
|
||||||
this.motion?.fadeOut()
|
this.motion?.fadeOut()
|
||||||
@@ -200,6 +208,7 @@ export class SimulatorMask extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
|
this.#disposed = true
|
||||||
console.log('dispose SimulatorMask')
|
console.log('dispose SimulatorMask')
|
||||||
this.motion?.dispose()
|
this.motion?.dispose()
|
||||||
this.wrapper.remove()
|
this.wrapper.remove()
|
||||||
|
|||||||
Reference in New Issue
Block a user