Permalink
Browse files

[#1335]

1. Remove Popcorn instaces from Popcorn.instances Array when destroyed
2. Fix Ready state monitoring when building a Popcorn instance, looks for readyState >= 1 now, and uses loadedmetadata
3. Fix Popcorn.destroy tests to work in Firefox, Chrome, Safari, and IE9
  • Loading branch information...
1 parent 157c116 commit 480b475287ae30187237a91ceeece62e9e8f3c16 @cadecairos cadecairos committed Sep 17, 2012
Showing with 33 additions and 13 deletions.
  1. +18 −6 popcorn.js
  2. +15 −7 test/popcorn.unit.js
View
@@ -233,7 +233,7 @@
self.media.currentTime = 0;
}
- self.media.removeEventListener( "loadeddata", isReady, false );
+ self.media.removeEventListener( "loadedmetadata", isReady, false );
var duration, videoDurationPlus,
runningPlugins, runningPlugin, rpLength, rpNatives;
@@ -305,12 +305,25 @@
}
});
- if ( self.media.readyState >= 2 ) {
+ // http://www.whatwg.org/specs/web-apps/current-work/#dom-media-readystate
+ //
+ // If media is in readyState (rS) >= 1, we know the media's duration,
+ // which is required before running the isReady function.
+ // If rS is 0, attach a listener for "loadedmetadata",
+ // ( Which indicates that the media has moved from rS 0 to 1 )
+ //
+ // This has been changed from a check for rS 2 because
+ // in certain conditions, Firefox can enter this code after dropping
+ // to rS 1 from a higher state such as 2 or 3. This caused a "loadeddata"
+ // listener to be attached to the media object, an event that had
+ // already triggered and would not trigger again. This left Popcorn with an
+ // instance that could never start a timeUpdate loop.
+ if ( self.media.readyState >= 1 ) {
isReady();
} else {
- self.media.addEventListener( "loadeddata", isReady, false );
+ self.media.addEventListener( "loadedmetadata", isReady, false );
}
return this;
@@ -505,6 +518,8 @@
instance.data.timeUpdate && instance.media.removeEventListener( "timeupdate", instance.data.timeUpdate, false );
instance.isDestroyed = true;
}
+
+ Popcorn.instances.splice( Popcorn.instances.indexOf( instance ), 1 );
}
});
@@ -1299,7 +1314,6 @@
};
Popcorn.timeUpdate = function( obj, event ) {
-
var currentTime = obj.media.currentTime,
previousTime = obj.data.trackEvents.previousUpdateTime,
tracks = obj.data.trackEvents,
@@ -1359,12 +1373,10 @@
byStart = tracks.byStart[ start ];
natives = byStart._natives;
type = natives && natives.type;
-
// If plugin does not exist on this instance, remove it
if ( !natives ||
( !!registryByName[ type ] ||
!!obj[ type ] ) ) {
-
if ( byStart.end > currentTime &&
byStart._running === false ) {
View
@@ -513,7 +513,7 @@ test( "Popcorn.util.toSeconds" , function() {
}
});
-asyncTest( "Popcorn.destroy", 9, function() {
+asyncTest( "Popcorn.destroy", 10, function() {
var popcorn = Popcorn( "#video" ),
pcorn,
playCounter = 0,
@@ -527,16 +527,17 @@ asyncTest( "Popcorn.destroy", 9, function() {
equal( timeUpdateCounter, 0, "timeUpdateCounter is intially 0" );
// add some event listeners for testing
- popcorn.on( "timeupdate", function( event ) { timeUpdateCounter++; } );
+ popcorn.on( "timeupdate", function( event ) {
+ timeUpdateCounter++;
+ popcorn.pause();
+ });
popcorn.on( "play", function( event ) {
playCounter++;
-
- this.pause();
});
popcorn.on( "pause", function() {
- equal( Popcorn.sizeOf( popcorn.data.events ), 3, "popcorn.data.events has correct number of events - before Popcorn.destroy" );
+ equal( Popcorn.sizeOf( popcorn.data.events ), 5, "popcorn.data.events has correct number of events - before Popcorn.destroy" );
ok( playCounter > 0, "playCounter is greater than 0, events are being triggered" );
@@ -546,6 +547,8 @@ asyncTest( "Popcorn.destroy", 9, function() {
popcorn.destroy();
+ equal( Popcorn.instances.length, 0, "The instance was removed" );
+
// Doing this to ensure we are working, a fail will run before this if the old popcorn instances events were
// not properly destroyed
pcorn = Popcorn( "#video" );
@@ -568,10 +571,13 @@ asyncTest( "Popcorn.destroy", 9, function() {
ok( false, "This cue should never have been run, destroy not working" );
});
- popcorn.play( 0 );
+ popcorn.on( "canplayall", function ready() {
+ popcorn.off( "canplayall", ready );
+ popcorn.play( 0 );
+ });
});
-test( "Popcorn.destroy (events & trackEvents)", 2, function() {
+test( "Popcorn.destroy (events & trackEvents)", 3, function() {
var p = Popcorn( "#video" );
Popcorn.plugin( "destroyable", {
@@ -589,6 +595,8 @@ test( "Popcorn.destroy (events & trackEvents)", 2, function() {
p.destroy();
+ equal( Popcorn.instances.length, 0, "The instance was removed from the Popcorn.instances array." );
+
equal( p.data.trackEvents.byStart.length, 0, "Zero trackEvents.byStart after destroy" );
equal( p.data.trackEvents.byEnd.length, 0, "Zero trackEvents.byEnd after destroy" );

0 comments on commit 480b475

Please sign in to comment.