-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setSrc clears currentSource_ after loadstart #3285
Changes from all commits
06dcc61
8d89829
132e107
47873df
f10fc92
84810c2
64be2ab
43aac04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,7 +11,7 @@ EventTarget.prototype.on = function(type, fn) { | |||||||||||||||||||||||||
// Remove the addEventListener alias before calling Events.on | ||||||||||||||||||||||||||
// so we don't get into an infinite type loop | ||||||||||||||||||||||||||
let ael = this.addEventListener; | ||||||||||||||||||||||||||
this.addEventListener = Function.prototype; | ||||||||||||||||||||||||||
this.addEventListener = () => {}; | ||||||||||||||||||||||||||
Events.on(this, type, fn); | ||||||||||||||||||||||||||
this.addEventListener = ael; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
@@ -23,7 +23,12 @@ EventTarget.prototype.off = function(type, fn) { | |||||||||||||||||||||||||
EventTarget.prototype.removeEventListener = EventTarget.prototype.off; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
EventTarget.prototype.one = function(type, fn) { | ||||||||||||||||||||||||||
// Remove the addEventListener alias before calling Events.on | ||||||||||||||||||||||||||
// so we don't get into an infinite type loop | ||||||||||||||||||||||||||
let ael = this.addEventListener; | ||||||||||||||||||||||||||
this.addEventListener = () => {}; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if one of the listeners registers an event listener? Something like this: function maybeReRegister() {
if (someCondition()) {
player.one('play', maybeReRegister);
return;
}
alert('done!');
};
player.one('play', maybeReRegister); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After reviewing, I don't think this is a problem right now but the event stuff sure is confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it works fine because the event system creates a clone of the handlers before it starts triggering events: video.js/src/js/utils/events.js Lines 52 to 63 in 2e2dbde
|
||||||||||||||||||||||||||
Events.one(this, type, fn); | ||||||||||||||||||||||||||
this.addEventListener = ael; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
EventTarget.prototype.trigger = function(event) { | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -814,18 +814,44 @@ Tech.withSourceHandlers = function(_Tech){ | |
if (this.currentSource_) { | ||
this.clearTracks(['audio', 'video']); | ||
} | ||
this.currentSource_ = source; | ||
|
||
if (sh !== _Tech.nativeSourceHandler) { | ||
|
||
this.currentSource_ = source; | ||
|
||
// Catch if someone replaced the src without calling setSource. | ||
// If they do, set currentSource_ to null and dispose our source handler. | ||
this.off(this.el_, 'loadstart', _Tech.prototype.firstLoadStartListener_); | ||
this.off(this.el_, 'loadstart', _Tech.prototype.successiveLoadStartListener_); | ||
this.one(this.el_, 'loadstart', _Tech.prototype.firstLoadStartListener_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of using event handler registration as instance-level state. The current set of registered event handlers are difficult to introspect and control flow is harder to trace. I don't see any logical issues with this code so we can move ahead with this but in general, I think adding an instance variable is preferable to this kind of solution. |
||
|
||
} | ||
|
||
this.sourceHandler_ = sh.handleSource(source, this, this.options_); | ||
this.on('dispose', this.disposeSourceHandler); | ||
|
||
return this; | ||
}; | ||
|
||
/* | ||
* Clean up any existing source handler | ||
*/ | ||
_Tech.prototype.disposeSourceHandler = function(){ | ||
// On the first loadstart after setSource | ||
_Tech.prototype.firstLoadStartListener_ = function() { | ||
this.one(this.el_, 'loadstart', _Tech.prototype.successiveLoadStartListener_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use one() if you want the successiveLoadStartListener_ to run on every subsequent loadstart? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because |
||
}; | ||
|
||
// On successive loadstarts when setSource has not been called again | ||
_Tech.prototype.successiveLoadStartListener_ = function() { | ||
this.currentSource_ = null; | ||
this.disposeSourceHandler(); | ||
this.one(this.el_, 'loadstart', _Tech.prototype.successiveLoadStartListener_); | ||
}; | ||
|
||
/* | ||
* Clean up any existing source handler | ||
*/ | ||
_Tech.prototype.disposeSourceHandler = function() { | ||
if (this.sourceHandler_ && this.sourceHandler_.dispose) { | ||
this.off(this.el_, 'loadstart', _Tech.prototype.firstLoadStartListener_); | ||
this.off(this.el_, 'loadstart', _Tech.prototype.successiveLoadStartListener_); | ||
this.sourceHandler_.dispose(); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point for discussion: should we be careful about using fat-arrows all over the place? I know calling addEventListener() with a non-
this
receiver is a weird thing to do but we could use a plain function here and avoid possibly subverting people's expectations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're gonna change this to
function() {}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it matters here at all since we just want an empty function while
Events.on
runs so we don't get into an infinite loop, so, there's no expectations being subverted here.As for elsewhere, it really depends on what you want the context of your function to be since in arrow functions
this
andarguments
are never bound.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the crux of the discussion I think we should have. Using fat-arrows removes the ability of the caller to rebind
this
. I've felt it necessary to rebindthis
more than once in my life. Are we intentionally removing that ability or just doing it to save some keystrokes?