diff --git a/popcorn.js b/popcorn.js index a2333420..256b8fdb 100644 --- a/popcorn.js +++ b/popcorn.js @@ -1365,6 +1365,12 @@ natives.start = natives.start || natives[ "in" ]; natives.end = natives.end || natives[ "out" ]; + if ( options.once ) { + natives.end = combineFn( natives.end, function() { + this.removeTrackEvent( options._id ); + }); + } + // extend teardown to always call end if running natives._teardown = combineFn(function() { diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 7d5efee7..04b81a07 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -4174,6 +4174,46 @@ test( "end undefined or false should never be fired", function() { $pop.currentTime( $pop.duration() ); }); +asyncTest( "Plug-ins with a `once` attribute should be removed after `end` is fired.", 3, function() { + + var $pop = Popcorn( "#video" ), + startFired = 0; + endFired = 0; + + Popcorn.plugin( "onceplugin", { + start: function() { + if ( !startFired ) { + ok( true, "start called once" ); + startFired++; + } else { + ok( false, "Start should oly execute once!" ) + startFired++; + } + }, + end: function() { + if ( !endFired ) { + ok( true, "end called once" ); + this.currentTime( 0 ); + endFired++; + } else { + ok( false, "End should only execute once!" ); + endFired++; + } + } + }); + + $pop.onceplugin({ start: 2, end: 3, once: true }); + + $pop.cue( 4, function() { + ok( startFired === 1 && endFired === 1, "start and end called one each" ); + $pop.removePlugin( "onceplugin" ); + $pop.destroy(); + start(); + }); + + $pop.play( 0 ); +}); + module( "Popcorn XHR" ); test( "Basic", function() {