Skip to content

Commit a4e3e1f

Browse files
committed
feat: enhance context menu cleanup logic to manage event listeners more effectively
1 parent 2e3d7c6 commit a4e3e1f

File tree

1 file changed

+43
-3
lines changed
  • packages/video-player/javascript/modules/context-menu

1 file changed

+43
-3
lines changed

packages/video-player/javascript/modules/context-menu/plugin.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type Player from 'video.js/dist/types/player';
55
import ContextMenu from './context-menu';
66
import { getPointerPosition } from './utils';
77
import { PluginOptions } from './types';
8+
import { addEventListener } from '../../utils';
89
import './types'; // Import for module augmentation side-effects
910

1011
function hasMenu(player: Player): boolean {
@@ -65,8 +66,28 @@ function onContextMenu(this: Player, e: MouseEvent): void {
6566
menu.dispose();
6667
};
6768

69+
// Store document listener cleanup function
70+
// @ts-ignore
71+
if (!this.contextmenuUICleanups_) {
72+
// @ts-ignore
73+
this.contextmenuUICleanups_ = [];
74+
}
75+
// @ts-ignore
76+
const documentCleanup = addEventListener(documentEl, 'click', menu.dispose);
77+
// @ts-ignore
78+
const tapCleanup = addEventListener(documentEl, 'tap', menu.dispose);
79+
// @ts-ignore
80+
this.contextmenuUICleanups_.push(documentCleanup, tapCleanup);
81+
6882
menu.on('dispose', () => {
69-
videojs.off(documentEl, ['click', 'tap'], menu.dispose);
83+
// Clean up document listeners
84+
// @ts-ignore
85+
if (this.contextmenuUICleanups_) {
86+
// @ts-ignore
87+
this.contextmenuUICleanups_.forEach(cleanup => cleanup());
88+
// @ts-ignore
89+
this.contextmenuUICleanups_ = [];
90+
}
7091
this.removeChild(menu);
7192
// @ts-ignore
7293
if (this.contextmenuUI) {
@@ -89,8 +110,6 @@ function onContextMenu(this: Player, e: MouseEvent): void {
89110
// @ts-ignore
90111
menu.el().style.top = `${Math.floor(constrainedTop)}px`;
91112
}
92-
93-
videojs.on(documentEl, ['click', 'tap'], menu.dispose);
94113
}
95114

96115
function contextmenuUI(this: Player, options: PluginOptions): void {
@@ -112,6 +131,14 @@ function contextmenuUI(this: Player, options: PluginOptions): void {
112131
this.contextmenuUI.menu?.dispose();
113132
// @ts-ignore
114133
this.off('contextmenu', this.contextmenuUI.onContextMenu);
134+
// Clean up any remaining document listeners
135+
// @ts-ignore
136+
if (this.contextmenuUICleanups_) {
137+
// @ts-ignore
138+
this.contextmenuUICleanups_.forEach(cleanup => cleanup());
139+
// @ts-ignore
140+
delete this.contextmenuUICleanups_;
141+
}
115142
// @ts-ignore
116143
delete this.contextmenuUI;
117144
}
@@ -134,6 +161,19 @@ function contextmenuUI(this: Player, options: PluginOptions): void {
134161
// @ts-ignore
135162
this.contextmenuUI.onContextMenu = onContextMenu.bind(this);
136163

164+
// Store contextmenu listener cleanup
165+
// @ts-ignore
166+
if (!this.contextmenuUICleanups_) {
167+
// @ts-ignore
168+
this.contextmenuUICleanups_ = [];
169+
}
170+
// @ts-ignore
171+
const contextMenuCleanup = () => {
172+
// @ts-ignore
173+
this.off('contextmenu', this.contextmenuUI.onContextMenu);
174+
};
175+
// @ts-ignore
176+
this.contextmenuUICleanups_.push(contextMenuCleanup);
137177
// @ts-ignore
138178
this.on('contextmenu', this.contextmenuUI.onContextMenu);
139179
this.ready(() => this.addClass('vjs-contextmenu-ui'));

0 commit comments

Comments
 (0)