From 209df676ccbe56e634f015e3091b72ac1c0204d4 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 20 Jul 2011 17:52:33 -0400 Subject: [PATCH] [#649] Trigger canplayall if readyState permits --- popcorn.js | 16 +++++++++++++--- test/popcorn.unit.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/popcorn.js b/popcorn.js index f7a6b6d..c1e76ec 100644 --- a/popcorn.js +++ b/popcorn.js @@ -652,7 +652,7 @@ // Execute hook add method if defined if ( eventHook.add ) { - eventHook.add.call( this ); + eventHook.add.call( this, {}, fn ); } // Reassign event type to our piggyback event type if defined @@ -715,9 +715,19 @@ hooks: { canplayall: { bind: "canplaythrough", - add: function() { + add: function( event, callback ) { + + var state = false; + + if ( this.media.readyState ) { + + callback.call( this, event ); + + state = true; + } + this.data.hooks.canplayall = { - fired: false + fired: state }; }, // declare special handling instructions diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 570e76f..54bc3ea 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -682,6 +682,39 @@ test("Popcorn.events.hooks: canplayall", function() { }); }); +test("Popcorn.events.hooks: canplayall fires immediately if ready", function() { + //qunit-fixture + + var $pop = Popcorn("#video"), + expects = 1, + count = 0, + fired = 0; + + expect(expects); + + function plus(){ + if ( ++count == expects ) { + start(); + } + } + + stop( 20000 ); + + function poll() { + if ( $pop.media.readyState >= 2 ) { + // this should trigger immediately + $pop.listen("canplayall", function( event ) { + equal( ++fired, 1, "canplayall is fired immediately if readyState permits" ); + plus(); + }); + } else { + setTimeout( poll, 10 ); + } + } + + poll(); +}); + /*