Skip to content

Commit

Permalink
[#1368] Ensure Start/End events are emitted and called appropriatly w…
Browse files Browse the repository at this point in the history
…hen manipulating data arrays
  • Loading branch information
mjschranz committed Dec 14, 2012
1 parent a9e7275 commit e3be942
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 6 deletions.
57 changes: 54 additions & 3 deletions popcorn.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,13 @@

event = instance.data.running[ plugin ][ i ];
event._natives.end.call( instance, null, event );

instance.emit( "trackend",
Popcorn.extend({}, event, {
plugin: event.type,
type: "trackend"
})
);
}
}

Expand All @@ -486,6 +493,13 @@

event = instance.data.running[ plugin ][ i ];
event._natives.start.call( instance, null, event );

instance.emit( "trackstart",
Popcorn.extend({}, event, {
plugin: event.type,
type: "trackstart"
})
);
}
}

Expand Down Expand Up @@ -1120,6 +1134,13 @@
if ( !obj.data.disabled[ track._natives.type ] ) {

track._natives.start.call( obj, null, track );

obj.emit( "trackstart",
Popcorn.extend({}, track, {
plugin: track.type,
type: "trackstart"
})
);
}
}
}
Expand All @@ -1135,7 +1156,8 @@
byEnd = [],
animating = [],
history = [],
track;
track,
runningPlugins;

while ( --length > -1 ) {
start = obj.data.trackEvents.byStart[ index ];
Expand Down Expand Up @@ -1224,6 +1246,28 @@

// Update ordered history array
obj.data.history = history;

// Call track event end immediately if it's enabled and current
if ( track.end > obj.media.currentTime &&
track.start <= obj.media.currentTime ) {

runningPlugins = obj.data.running[ track._natives.type ];

track._running = false;
runningPlugins.splice( runningPlugins.indexOf( track ), 1 );

if ( !obj.data.disabled[ track._natives.type ] ) {

track._natives.end.call( obj, null, track );

obj.emit( "trackend",
Popcorn.extend({}, track, {
plugin: track._natives.type,
type: "trackend"
})
);
}
}
}

// Helper function used to retrieve old values of properties that
Expand Down Expand Up @@ -1682,6 +1726,13 @@
args[ 1 ]._running &&
runningPlugins.splice( runningPlugins.indexOf( options ), 1 ) &&
natives.end.apply( this, args );

this.emit( "trackend",
Popcorn.extend({}, options, {
plugin: natives.type,
type: "trackend"
})
);
}, natives._teardown );

// extend teardown to always trigger trackteardown after teardown
Expand Down Expand Up @@ -1813,6 +1864,8 @@
// custom defined updating for a track event to be defined by the plugin author
if ( trackEvent._natives._update ) {

removeFromArray( this, trackEvent._id );

// It's safe to say that the intent of Start/End will never change
// Update them first before calling update
if ( hasOwn.call( options, "start" ) ) {
Expand All @@ -1824,8 +1877,6 @@
}

trackEvent._natives._update.call( this, trackEvent, options );

removeFromArray( this, trackEvent._id );
addToArray( this, trackEvent );
} else {
options = Popcorn.extend( {}, trackEvent, options );
Expand Down
91 changes: 88 additions & 3 deletions test/popcorn.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4658,7 +4658,6 @@ test( "Modify cue or trackevent w/ update function provided", 3, function() {
_update: function( trackEvent, newOptions ) {
ok( true, "Successfully called track events update function" );
deepEqual( newOptions.text, updateOptions.text, "Successfully received the new update options" );
equal( $pop.data.trackEvents.byStart.length, numTrackEvents, "Total number of track events didn't change" );
trackEvent.text = newOptions.text;
}
});
Expand All @@ -4668,6 +4667,7 @@ test( "Modify cue or trackevent w/ update function provided", 3, function() {
numTrackEvents = $pop.data.trackEvents.byStart.length;

$pop.updateprovided( id, updateOptions );
equal( $pop.data.trackEvents.byStart.length, numTrackEvents, "Total number of track events didn't change" );

Popcorn.removePlugin( "updateprovided" );
$pop.destroy();
Expand Down Expand Up @@ -4705,6 +4705,91 @@ test( "Modify cue or trackevent w/o update function provided", 3, function() {

});

test( "trackstart/trackend w/ update function provided", 3, function() {
var $pop = Popcorn( "#video" ),
id = "test-id",
endCalledFirst = false,
updateOptions = {
start: 4,
text: "New Text"
};

Popcorn.plugin( "updateprovided", {
_setup: function() {},
start: function() {},
end: function(){},
_teardown: function( trackEvent ) {},
_update: function( trackEvent, newOptions ) {}
});

$pop.updateprovided( id, { start: 2, end: 5 } );

$pop.on( "trackstart", function() {
ok( true, "trackstart was successfully fired when updating a plugin" );
ok( endCalledFirst, "End was called before start when updating a plugin" );
});

$pop.on( "trackend", function() {
endCalledFirst = true;
ok( true, "trackend was successfully fired when updating a plugin" );
$pop.currentTime( 4 );
});

$pop.currentTime( 3 );

$pop.updateprovided( id, updateOptions );

Popcorn.removePlugin( "updateprovided" );
$pop.destroy();

});

test( "trackstart/trackend fired appropriately w/o update function", 3, function() {
var $pop = Popcorn( "#video" ),
id = "test-id",
ignoreEnd = true,
endCalledFirst = false,
updateOptions = {
start: 3,
end: 5
};

Popcorn.plugin( "noupdateprovided", {
_setup: function() {},
start: function() {},
end: function(){},
_teardown: function() {
ignoreEnd = false;
}
});

$pop.noupdateprovided( id, {
start: 0,
end: 3
});

$pop.on( "trackstart", function() {
ok( true, "trackstart was successfully fired when updating a plugin" );
ok( endCalledFirst, "End was called first before start when updating a plugin with default update." );
});

$pop.on( "trackend", function() {
if ( !ignoreEnd ) {
endCalledFirst = true;
ok( true, "trackend was successfully fired when updating a plugin with default update." );
$pop.currentTime( 4 );
}
});

$pop.currentTime( 2 );

$pop.noupdateprovided( id, updateOptions );

Popcorn.removePlugin( "noupdateprovided" );
$pop.destroy();

});

test( "trackchange w/ update function provided", 3, function() {
var $pop = Popcorn( "#video" ),
id = "test-id",
Expand Down Expand Up @@ -4770,7 +4855,7 @@ test( "trackchange w/o update function provided", 3, function() {

});

test( "Modify plugin w/o provided update without setup for plugins that use function that returns object", 6, function() {
test( "Modify plugin w/o provided update without setup for plugins that use function that returns object", 7, function() {
var $pop = Popcorn( "#video" ),
count = 0,
id,
Expand Down Expand Up @@ -4813,7 +4898,7 @@ test( "Modify plugin w/o provided update without setup for plugins that use func
$pop.destroy();
});

test( "Modify plugin w/o provided update with setup for plugins that use function that returns object", 6, function() {
test( "Modify plugin w/o provided update with setup for plugins that use function that returns object", 7, function() {
var $pop = Popcorn( "#video" ),
count = 0,
id,
Expand Down

0 comments on commit e3be942

Please sign in to comment.