Skip to content

Commit 361dc76

Browse files
authored
revert: "fix: Allow evented objects, such as components and plugins, to listen to the window object in addition to DOM objects. (videojs#5255)" (videojs#5301)
This reverts commit 7fd29b4. With this change, playing back HLS content via VHS caused an infinite event loop to be printed out. See issue videojs#5281
1 parent 8257a37 commit 361dc76

File tree

2 files changed

+17
-237
lines changed

2 files changed

+17
-237
lines changed

src/js/mixins/evented.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@ import * as Fn from '../utils/fn';
99
import * as Obj from '../utils/obj';
1010
import EventTarget from '../event-target';
1111

12-
/**
13-
* Returns whether or not an object supports native events.
14-
*
15-
* @param {Object} object
16-
* An object to test.
17-
*
18-
* @return {boolean}
19-
* Whether or not an object supports native events.
20-
*/
21-
const supportsNativeEvents = (object) => typeof object.addEventListener === 'function';
22-
2312
/**
2413
* Returns whether or not an object has had the evented mixin applied.
2514
*
@@ -61,7 +50,7 @@ const isValidEventType = (type) =>
6150
* The object to test.
6251
*/
6352
const validateTarget = (target) => {
64-
if (!supportsNativeEvents(target) && !isEvented(target)) {
53+
if (!target.nodeName && !isEvented(target)) {
6554
throw new Error('Invalid target; must be a DOM node or evented object.');
6655
}
6756
};
@@ -165,7 +154,7 @@ const normalizeListenArgs = (self, args) => {
165154
const listen = (target, method, type, listener) => {
166155
validateTarget(target);
167156

168-
if (supportsNativeEvents(target)) {
157+
if (target.nodeName) {
169158
Events[method](target, type, listener);
170159
} else {
171160
target[method](type, listener);
@@ -318,7 +307,7 @@ const EventedMixin = {
318307
// the same guid as the event listener in on().
319308
this.off('dispose', listener);
320309

321-
if (supportsNativeEvents(target)) {
310+
if (target.nodeName) {
322311
Events.off(target, type, listener);
323312
Events.off(target, 'dispose', listener);
324313
} else if (isEvented(target)) {
@@ -357,7 +346,7 @@ const EventedMixin = {
357346
* @param {String} [options.eventBusKey]
358347
* By default, adds a `eventBusEl_` DOM element to the target object,
359348
* which is used as an event bus. If the target object already has a
360-
* DOM element which should be used, pass its key here.
349+
* DOM element that should be used, pass its key here.
361350
*
362351
* @return {Object}
363352
* The target object.

test/unit/mixins/evented.test.js

Lines changed: 13 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/* eslint-env qunit */
2-
import document from 'global/document';
3-
import window from 'global/window';
42
import sinon from 'sinon';
53
import evented from '../../../src/js/mixins/evented';
64
import * as Dom from '../../../src/js/utils/dom';
@@ -13,20 +11,6 @@ const errors = {
1311
target: new Error('Invalid target; must be a DOM node or evented object.')
1412
};
1513

16-
// Cross-browser event creation.
17-
const createEvent = (type) => {
18-
let event;
19-
20-
try {
21-
event = new window.Event(type);
22-
} catch (x) {
23-
event = document.createEvent('Event');
24-
event.initEvent(type, true, true);
25-
}
26-
27-
return event;
28-
};
29-
3014
const validateListenerCall = (call, thisValue, eventExpectation) => {
3115
const eventActual = call.args[0];
3216

@@ -47,13 +31,7 @@ QUnit.module('mixins: evented', {
4731
},
4832

4933
afterEach() {
50-
Object.keys(this.targets).forEach(k => {
51-
if (typeof this.targets[k].trigger === 'function') {
52-
this.targets[k].trigger('dispose');
53-
} else {
54-
this.targets[k] = null;
55-
}
56-
});
34+
Object.keys(this.targets).forEach(k => this.targets[k].trigger('dispose'));
5735
}
5836
});
5937

@@ -83,7 +61,7 @@ QUnit.test('evented() with custom element', function(assert) {
8361
);
8462
});
8563

86-
QUnit.test('on() and one() error states', function(assert) {
64+
QUnit.test('on() and one() errors', function(assert) {
8765
const target = this.targets.a = evented({});
8866

8967
['on', 'one'].forEach(method => {
@@ -96,7 +74,7 @@ QUnit.test('on() and one() error states', function(assert) {
9674
});
9775
});
9876

99-
QUnit.test('off() error states', function(assert) {
77+
QUnit.test('off() errors', function(assert) {
10078
const target = this.targets.a = evented({});
10179

10280
// An invalid event actually causes an invalid target error because it
@@ -108,7 +86,7 @@ QUnit.test('off() error states', function(assert) {
10886
assert.throws(() => target.off(evented({}), 'x', null), errors.listener, 'the expected error is thrown');
10987
});
11088

111-
QUnit.test('on() can add a listener to one event type on itself', function(assert) {
89+
QUnit.test('on() can add a listener to one event type on this object', function(assert) {
11290
const a = this.targets.a = evented({});
11391
const spy = sinon.spy();
11492

@@ -123,7 +101,7 @@ QUnit.test('on() can add a listener to one event type on itself', function(asser
123101
});
124102
});
125103

126-
QUnit.test('on() can add a listener to an array of event types on itself', function(assert) {
104+
QUnit.test('on() can add a listener to an array of event types on this object', function(assert) {
127105
const a = this.targets.a = evented({});
128106
const spy = sinon.spy();
129107

@@ -144,7 +122,7 @@ QUnit.test('on() can add a listener to an array of event types on itself', funct
144122
});
145123
});
146124

147-
QUnit.test('one() can add a listener to one event type on itself', function(assert) {
125+
QUnit.test('one() can add a listener to one event type on this object', function(assert) {
148126
const a = this.targets.a = evented({});
149127
const spy = sinon.spy();
150128

@@ -160,7 +138,7 @@ QUnit.test('one() can add a listener to one event type on itself', function(asse
160138
});
161139
});
162140

163-
QUnit.test('one() can add a listener to an array of event types on itself', function(assert) {
141+
QUnit.test('one() can add a listener to an array of event types on this object', function(assert) {
164142
const a = this.targets.a = evented({});
165143
const spy = sinon.spy();
166144

@@ -183,7 +161,7 @@ QUnit.test('one() can add a listener to an array of event types on itself', func
183161
});
184162
});
185163

186-
QUnit.test('on() can add a listener to one event type on a different evented object', function(assert) {
164+
QUnit.test('on() can add a listener to one event type on a different target object', function(assert) {
187165
const a = this.targets.a = evented({});
188166
const b = this.targets.b = evented({});
189167
const spy = sinon.spy();
@@ -202,47 +180,7 @@ QUnit.test('on() can add a listener to one event type on a different evented obj
202180
});
203181
});
204182

205-
QUnit.test('on() can add a listener to one event type on a node object', function(assert) {
206-
const a = this.targets.a = evented({});
207-
const b = this.targets.b = Dom.createEl('div');
208-
const spy = sinon.spy();
209-
const event = createEvent('x');
210-
211-
a.on(b, 'x', spy);
212-
b.dispatchEvent(event);
213-
214-
// Make sure we aren't magically binding a listener to "a".
215-
a.trigger('x');
216-
217-
assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times');
218-
219-
validateListenerCall(spy.getCall(0), a, {
220-
type: 'x',
221-
target: b
222-
});
223-
});
224-
225-
QUnit.test('on() can add a listener to one event type on the window object', function(assert) {
226-
const a = this.targets.a = evented({});
227-
const b = this.targets.b = window;
228-
const spy = sinon.spy();
229-
const event = createEvent('x');
230-
231-
a.on(b, 'x', spy);
232-
b.dispatchEvent(event);
233-
234-
// Make sure we aren't magically binding a listener to "a".
235-
a.trigger('x');
236-
237-
assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times');
238-
239-
validateListenerCall(spy.getCall(0), a, {
240-
type: 'x',
241-
target: b
242-
});
243-
});
244-
245-
QUnit.test('on() can add a listener to an array of event types on a different evented object', function(assert) {
183+
QUnit.test('on() can add a listener to an array of event types on a different target object', function(assert) {
246184
const a = this.targets.a = evented({});
247185
const b = this.targets.b = evented({});
248186
const spy = sinon.spy();
@@ -268,70 +206,13 @@ QUnit.test('on() can add a listener to an array of event types on a different ev
268206
});
269207
});
270208

271-
QUnit.test('on() can add a listener to an array of event types on a node object', function(assert) {
272-
const a = this.targets.a = evented({});
273-
const b = this.targets.b = Dom.createEl('div');
274-
const spy = sinon.spy();
275-
const x = createEvent('x');
276-
const y = createEvent('y');
277-
278-
a.on(b, ['x', 'y'], spy);
279-
b.dispatchEvent(x);
280-
b.dispatchEvent(y);
281-
282-
// Make sure we aren't magically binding a listener to "a".
283-
a.trigger('x');
284-
a.trigger('y');
285-
286-
assert.strictEqual(spy.callCount, 2, 'the listener was called the expected number of times');
287-
288-
validateListenerCall(spy.getCall(0), a, {
289-
type: 'x',
290-
target: b
291-
});
292-
293-
validateListenerCall(spy.getCall(1), a, {
294-
type: 'y',
295-
target: b
296-
});
297-
});
298-
299-
QUnit.test('on() can add a listener to an array of event types on the window object', function(assert) {
300-
const a = this.targets.a = evented({});
301-
const b = this.targets.b = window;
302-
const spy = sinon.spy();
303-
const x = createEvent('x');
304-
const y = createEvent('y');
305-
306-
a.on(b, ['x', 'y'], spy);
307-
b.dispatchEvent(x);
308-
b.dispatchEvent(y);
309-
310-
// Make sure we aren't magically binding a listener to "a".
311-
a.trigger('x');
312-
a.trigger('y');
313-
314-
assert.strictEqual(spy.callCount, 2, 'the listener was called the expected number of times');
315-
316-
validateListenerCall(spy.getCall(0), a, {
317-
type: 'x',
318-
target: b
319-
});
320-
321-
validateListenerCall(spy.getCall(1), a, {
322-
type: 'y',
323-
target: b
324-
});
325-
});
326-
327-
QUnit.test('one() can add a listener to one event type on a different evented object', function(assert) {
209+
QUnit.test('one() can add a listener to one event type on a different target object', function(assert) {
328210
const a = this.targets.a = evented({});
329211
const b = this.targets.b = evented({});
330212
const spy = sinon.spy();
331213

332214
a.one(b, 'x', spy);
333215
b.trigger('x');
334-
b.trigger('x');
335216

336217
// Make sure we aren't magically binding a listener to "a".
337218
a.trigger('x');
@@ -344,49 +225,9 @@ QUnit.test('one() can add a listener to one event type on a different evented ob
344225
});
345226
});
346227

347-
QUnit.test('one() can add a listener to one event type on a node object', function(assert) {
348-
const a = this.targets.a = evented({});
349-
const b = this.targets.b = Dom.createEl('div');
350-
const spy = sinon.spy();
351-
const event = createEvent('x');
352-
353-
a.one(b, 'x', spy);
354-
b.dispatchEvent(event);
355-
b.dispatchEvent(event);
356-
357-
// Make sure we aren't magically binding a listener to "a".
358-
a.trigger('x');
359-
360-
assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times');
361-
362-
validateListenerCall(spy.getCall(0), a, {
363-
type: 'x',
364-
target: b
365-
});
366-
});
367-
368-
QUnit.test('one() can add a listener to one event type on the window object', function(assert) {
369-
const a = this.targets.a = evented({});
370-
const b = this.targets.b = window;
371-
const spy = sinon.spy();
372-
const event = createEvent('x');
373-
374-
a.one(b, 'x', spy);
375-
b.dispatchEvent(event);
376-
b.dispatchEvent(event);
377-
378-
// Make sure we aren't magically binding a listener to "a".
379-
a.trigger('x');
380-
381-
assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times');
382-
383-
validateListenerCall(spy.getCall(0), a, {
384-
type: 'x',
385-
target: b
386-
});
387-
});
388-
389-
QUnit.test('one() can add a listener to an array of event types on a different evented object', function(assert) {
228+
// The behavior here unfortunately differs from the identical case where "a"
229+
// listens to itself. This is something that should be resolved...
230+
QUnit.test('one() can add a listener to an array of event types on a different target object', function(assert) {
390231
const a = this.targets.a = evented({});
391232
const b = this.targets.b = evented({});
392233
const spy = sinon.spy();
@@ -409,56 +250,6 @@ QUnit.test('one() can add a listener to an array of event types on a different e
409250
});
410251
});
411252

412-
QUnit.test('one() can add a listener to an array of event types on a node object', function(assert) {
413-
const a = this.targets.a = evented({});
414-
const b = this.targets.b = Dom.createEl('div');
415-
const spy = sinon.spy();
416-
const x = createEvent('x');
417-
const y = createEvent('y');
418-
419-
a.one(b, ['x', 'y'], spy);
420-
b.dispatchEvent(x);
421-
b.dispatchEvent(y);
422-
b.dispatchEvent(x);
423-
b.dispatchEvent(y);
424-
425-
// Make sure we aren't magically binding a listener to "a".
426-
a.trigger('x');
427-
a.trigger('y');
428-
429-
assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times');
430-
431-
validateListenerCall(spy.getCall(0), a, {
432-
type: 'x',
433-
target: b
434-
});
435-
});
436-
437-
QUnit.test('one() can add a listener to an array of event types on the window object', function(assert) {
438-
const a = this.targets.a = evented({});
439-
const b = this.targets.b = window;
440-
const spy = sinon.spy();
441-
const x = createEvent('x');
442-
const y = createEvent('y');
443-
444-
a.one(b, ['x', 'y'], spy);
445-
b.dispatchEvent(x);
446-
b.dispatchEvent(y);
447-
b.dispatchEvent(x);
448-
b.dispatchEvent(y);
449-
450-
// Make sure we aren't magically binding a listener to "a".
451-
a.trigger('x');
452-
a.trigger('y');
453-
454-
assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times');
455-
456-
validateListenerCall(spy.getCall(0), a, {
457-
type: 'x',
458-
target: b
459-
});
460-
});
461-
462253
QUnit.test('off() with no arguments will remove all listeners from all events on this object', function(assert) {
463254
const a = this.targets.a = evented({});
464255
const spyX = sinon.spy();

0 commit comments

Comments
 (0)