Skip to content

Commit

Permalink
🐞 Prevent double autoPlay starting
Browse files Browse the repository at this point in the history
πŸ›  use player.state
βœ… add test
  • Loading branch information
desandro committed Mar 21, 2016
1 parent 8a527e5 commit 88aa207
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
28 changes: 14 additions & 14 deletions js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ if ( 'hidden' in document ) {
// -------------------------- Player -------------------------- //

function Player( parent ) {
this.isPlaying = false;
this.parent = parent;
this.state = 'stopped';
// visibility change event handler
if ( visibilityEvent ) {
var _this = this;
Expand All @@ -64,9 +64,10 @@ Player.prototype = new EventEmitter();

// start play
Player.prototype.play = function() {
this.isPlaying = true;
// playing kills pauses
delete this.isPaused;
if ( this.state == 'playing' ) {
return;
}
this.state = 'playing';
// listen to visibility change
if ( visibilityEvent ) {
document.addEventListener( visibilityEvent, this.onVisibilityChange, false );
Expand All @@ -76,26 +77,25 @@ Player.prototype.play = function() {
};

Player.prototype.tick = function() {
// do not tick if paused or not playing
if ( !this.isPlaying || this.isPaused ) {
// do not tick if not playing
if ( this.state != 'playing' ) {
return;
}
// keep track of when .tick()
this.tickTime = new Date();

var time = this.parent.options.autoPlay;
// default to 3 seconds
time = typeof time == 'number' ? time : 3000;
var _this = this;
// HACK: reset ticks if stopped and started within interval
this.clear();
this.timeout = setTimeout( function() {
_this.parent.next( true );
_this.tick();
}, time );
};

Player.prototype.stop = function() {
this.isPlaying = false;
// stopping kills pauses
delete this.isPaused;
this.state = 'stopped';
this.clear();
// remove visibility change event
if ( visibilityEvent ) {
Expand All @@ -108,15 +108,15 @@ Player.prototype.clear = function() {
};

Player.prototype.pause = function() {
if ( this.isPlaying ) {
this.isPaused = true;
if ( this.state == 'playing' ) {
this.state = 'paused';
this.clear();
}
};

Player.prototype.unpause = function() {
// re-start play if in unpaused state
if ( this.isPaused ) {
if ( this.state == 'paused' ) {
this.play();
}
};
Expand Down
22 changes: 19 additions & 3 deletions test/unit/auto-play.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test( 'auto play', function( assert ) {
var next = tests.shift();
next();
} else {
flkty.player.stop();
flkty.stopPlayer();
done();
}
}
Expand All @@ -44,15 +44,31 @@ test( 'auto play', function( assert ) {
ok( false, 'player ticked during pause' );
}
flkty.on( 'cellSelect', onPauseSelect );
flkty.player.pause();
flkty.pausePlayer();
setTimeout( function() {
ok( true, 'player did not tick during pause' );
flkty.off( 'cellSelect', onPauseSelect );
flkty.once( 'cellSelect', function() {
ok( true, 'player resumed after unpausing' );
nextTest();
});
flkty.player.unpause();
flkty.unpausePlayer();
}, flkty.options.autoPlay + 100 );
},
// double playPlayer()
function() {
var ticks = 0;
function onSelect() {
ticks++;
}
flkty.stopPlayer();
flkty.on( 'cellSelect', onSelect );
flkty.playPlayer();
flkty.playPlayer();
setTimeout( function() {
flkty.off( 'cellSelect', onSelect );
equal( ticks, 1, 'only one tick after double playPlayer' );
nextTest();
}, flkty.options.autoPlay + 100 );
}
];
Expand Down

0 comments on commit 88aa207

Please sign in to comment.