Skip to content

Commit

Permalink
Tech change handler spam fixes videojs#882 videojs#1485 videojs#1505 v…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjipott committed Feb 11, 2015
1 parent 789c729 commit 6bfc50e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/js/media/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,14 @@ vjs.MediaTechController = vjs.Component.extend({
* any controls will still keep the user active
*/
vjs.MediaTechController.prototype.initControlsListeners = function(){
var player, activateControls;
var player;

player = this.player();

activateControls = function(){
if (player.controls() && !player.usingNativeControls()) {
this.addControlsListeners();
}
};

// Set up event listeners once the tech is ready and has an element to apply
// listeners to
this.ready(activateControls);
this.on(player, 'controlsenabled', activateControls);
this.ready(this.activateControls);
this.on(player, 'controlsenabled', this.activateControls);
this.on(player, 'controlsdisabled', this.removeControlsListeners);

// if we're loading the playback object after it has started loading or playing the
Expand All @@ -81,6 +75,15 @@ vjs.MediaTechController.prototype.initControlsListeners = function(){
});
};

vjs.MediaTechController.prototype.removeControlsListeners = function () {
var player;

player = this.player();

this.off(player, 'controlsenabled', this.activateControls);
this.off(player, 'controlsdisabled', this.removeControlsListeners);
};

vjs.MediaTechController.prototype.addControlsListeners = function(){
var userWasActive;

Expand Down Expand Up @@ -133,6 +136,19 @@ vjs.MediaTechController.prototype.removeControlsListeners = function(){
this.off('mousedown');
};

/**
* Activate contols listeners handler
*/
vjs.MediaTechController.prototype.activateControls = function () {
var player;

player = this.player();

if (player.controls() && !player.usingNativeControls()) {
this.addControlsListeners();
}
};

/**
* Handle a click on the media element. By default will play/pause the media.
*/
Expand Down Expand Up @@ -218,6 +234,7 @@ vjs.MediaTechController.prototype.manualTimeUpdatesOff = function(){
var player = this.player_;

this.manualTimeUpdates = false;

this.stopTrackingCurrentTime();
this.off(player, 'play', this.trackCurrentTime);
this.off(player, 'pause', this.stopTrackingCurrentTime);
Expand All @@ -239,7 +256,8 @@ vjs.MediaTechController.prototype.stopTrackingCurrentTime = function(){
this.player().trigger('timeupdate');
};

vjs.MediaTechController.prototype.dispose = function() {
vjs.MediaTechController.prototype.dispose = function(){
this.removeControlsListeners();
// Turn off any manual progress or timeupdate tracking
if (this.manualProgress) { this.manualProgressOff(); }

Expand All @@ -248,7 +266,7 @@ vjs.MediaTechController.prototype.dispose = function() {
vjs.Component.prototype.dispose.call(this);
};

vjs.MediaTechController.prototype.setCurrentTime = function() {
vjs.MediaTechController.prototype.setCurrentTime = function(){
// improve the accuracy of manual timeupdates
if (this.manualTimeUpdates) { this.player().trigger('timeupdate'); }
};
Expand Down
1 change: 1 addition & 0 deletions test/unit/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test('stops timeupdates if the tech produces them natively', function() {
playHandler = handler;
}
},
off: this.noop,
bufferedPercent: this.noop,
trigger: function(event) {
if (event === 'timeupdate') {
Expand Down

0 comments on commit 6bfc50e

Please sign in to comment.