Skip to content

Commit f8e9bfc

Browse files
fix: registering new player component (videojs#8932)
Do not allow to register new Player component, if any instance of the current component still exists. Fixes videojs#8925 Co-authored-by: mister-ben <1676039+mister-ben@users.noreply.github.com>
1 parent 19ca3f2 commit f8e9bfc

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/js/component.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,12 +2023,14 @@ class Component {
20232023

20242024
// If we have players that were disposed, then their name will still be
20252025
// in Players.players. So, we must loop through and verify that the value
2026-
// for each item is not null. This allows registration of the Player component
2026+
// for each item is null. This allows registration of the Player component
20272027
// after all players have been disposed or before any were created.
2028-
if (players &&
2029-
playerNames.length > 0 &&
2030-
playerNames.map((pname) => players[pname]).every(Boolean)) {
2031-
throw new Error('Can not register Player component after player has been created.');
2028+
if (players && playerNames.length > 0) {
2029+
for (let i = 0; i < playerNames.length; i++) {
2030+
if (players[playerNames[i]] !== null) {
2031+
throw new Error('Can not register Player component after player has been created.');
2032+
}
2033+
}
20322034
}
20332035
}
20342036

test/unit/player.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,41 @@ QUnit.test('should not allow to register custom player when any player has been
23452345
videojs.registerComponent('Player', Player);
23462346
});
23472347

2348+
QUnit.test('should not allow to register custom player when any player still exists', function(assert) {
2349+
const videoTag1 = document.createElement('video');
2350+
const videoTag2 = document.createElement('video');
2351+
2352+
const fixture = document.getElementById('qunit-fixture');
2353+
2354+
fixture.appendChild(videoTag1);
2355+
fixture.appendChild(videoTag2);
2356+
2357+
const player1 = videojs(videoTag1);
2358+
const player2 = videojs(videoTag2);
2359+
2360+
class CustomPlayer extends Player {}
2361+
2362+
assert.throws(function() {
2363+
videojs.registerComponent('Player', CustomPlayer);
2364+
}, 'Can not register Player component after player has been created');
2365+
2366+
player1.dispose();
2367+
2368+
// still throws, because player2 still exists
2369+
assert.throws(function() {
2370+
videojs.registerComponent('Player', CustomPlayer);
2371+
}, 'Can not register Player component after player has been created');
2372+
2373+
player2.dispose();
2374+
2375+
// successfully registers, because no player exists anymore
2376+
// should not throw
2377+
videojs.registerComponent('Player', CustomPlayer);
2378+
2379+
// reset the Player to the original value;
2380+
videojs.registerComponent('Player', Player);
2381+
});
2382+
23482383
QUnit.test('setters getters passed to tech', function(assert) {
23492384
const tag = TestHelpers.makeTag();
23502385
const fixture = document.getElementById('qunit-fixture');

0 commit comments

Comments
 (0)