Skip to content

Commit 326398d

Browse files
misteroneillgkatsev
authored andcommitted
feat: don't throw when re-registering a plugin unless it's a player method (#4140)
1 parent 0f57341 commit 326398d

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/js/plugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import evented from './mixins/evented';
55
import stateful from './mixins/stateful';
66
import * as Events from './utils/events';
77
import * as Fn from './utils/fn';
8+
import log from './utils/log';
89
import Player from './player';
910

1011
/**
@@ -305,8 +306,10 @@ class Plugin {
305306
throw new Error(`Illegal plugin name, "${name}", must be a string, was ${typeof name}.`);
306307
}
307308

308-
if (pluginExists(name) || Player.prototype.hasOwnProperty(name)) {
309-
throw new Error(`Illegal plugin name, "${name}", already exists.`);
309+
if (pluginExists(name)) {
310+
log.warn(`A plugin named "${name}" already exists. You may want to avoid re-registering plugins!`);
311+
} else if (Player.prototype.hasOwnProperty(name)) {
312+
throw new Error(`Illegal plugin name, "${name}", cannot share a name with an existing player method!`);
310313
}
311314

312315
if (typeof plugin !== 'function') {

test/unit/plugin-static.test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-env qunit */
2+
import sinon from 'sinon';
3+
import log from '../../src/js/utils/log';
24
import Player from '../../src/js/player';
35
import Plugin from '../../src/js/plugin';
46

@@ -53,12 +55,6 @@ QUnit.test('registerPlugin() illegal arguments', function(assert) {
5355
'plugins must have a name'
5456
);
5557

56-
assert.throws(
57-
() => Plugin.registerPlugin('play'),
58-
new Error('Illegal plugin name, "play", already exists.'),
59-
'plugins cannot share a name with an existing player method'
60-
);
61-
6258
assert.throws(
6359
() => Plugin.registerPlugin('foo'),
6460
new Error('Illegal plugin for "foo", must be a function, was undefined.'),
@@ -70,6 +66,18 @@ QUnit.test('registerPlugin() illegal arguments', function(assert) {
7066
new Error('Illegal plugin for "foo", must be a function, was object.'),
7167
'plugins must be functions'
7268
);
69+
70+
assert.throws(
71+
() => Plugin.registerPlugin('play', function() {}),
72+
new Error('Illegal plugin name, "play", cannot share a name with an existing player method!'),
73+
'plugins must be functions'
74+
);
75+
76+
sinon.spy(log, 'warn');
77+
Plugin.registerPlugin('foo', function() {});
78+
Plugin.registerPlugin('foo', function() {});
79+
assert.strictEqual(log.warn.callCount, 1, 'warn on re-registering a plugin');
80+
log.warn.restore();
7381
});
7482

7583
QUnit.test('getPlugin()', function(assert) {

0 commit comments

Comments
 (0)