From f2461a3841d8abed09d3bf766a4a29854847b1ec Mon Sep 17 00:00:00 2001 From: rwldrn Date: Tue, 24 May 2011 11:54:38 -0400 Subject: [PATCH] [#540] Loadtime event api table compilation; Perf boosts for event util method calls; --- popcorn.js | 70 +++++++++++++++++++++++++++++++------------- test/popcorn.unit.js | 13 +++++++- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/popcorn.js b/popcorn.js index e2b8de6..50639af 100644 --- a/popcorn.js +++ b/popcorn.js @@ -20,6 +20,13 @@ readyBound = false, readyFired = false, + // Non-public internal data object + internal = { + events: { + hash: {}, + apis: {} + } + }, // Declare constructor // Returns an instance object. @@ -482,7 +489,7 @@ }); Popcorn.Events = { - UIEvents: "blur focus focusin focusout load resize scroll unload ", + UIEvents: "blur focus focusin focusout load resize scroll unload", MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick", Events: "loadstart progress suspend emptied stalled play pause " + "loadedmetadata loadeddata waiting playing canplay canplaythrough " + @@ -493,19 +500,37 @@ Popcorn.Events.MouseEvents + " " + Popcorn.Events.Events; - Popcorn.events = { + internal.events.apiTypes = [ "UIEvents", "MouseEvents", "Events" ]; - isNative: function( type ) { + // Privately compile events table at load time + (function( events, data ) { - var checks = Popcorn.Events.Natives.split( /\s+/g ); + var apis = internal.events.apiTypes, + eventsList = events.Natives.split( /\s+/g ), + idx = 0, len = eventsList.length, prop; - for ( var i = 0; i < checks.length; i++ ) { - if ( checks[ i ] === type ) { - return true; - } + for( ; idx < len; idx++ ) { + data.hash[ eventsList[idx] ] = true; + } + + apis.forEach(function( val, idx ) { + + data.apis[ val ] = {}; + + var apiEvents = events[ val ].split( /\s+/g ), + len = apiEvents.length, + k = 0; + + for ( ; k < len; k++ ) { + data.apis[ val ][ apiEvents[ k ] ] = true; } + }); + })( Popcorn.Events, internal.events ); + + Popcorn.events = { - return false; + isNative: function( type ) { + return !!internal.events.hash[ type ]; }, getInterface: function( type ) { @@ -513,16 +538,20 @@ return false; } - var natives = Popcorn.Events, - proto; - - for ( var p in natives ) { - if ( p !== "Natives" && natives[ p ].indexOf( type ) > -1 ) { - proto = p; + var eventApi = internal.events, + apis = eventApi.apiTypes, + apihash = eventApi.apis, + idx = 0, len = apis.length, api, tmp; + + for ( ; idx < len; idx++ ) { + tmp = apis[ idx ]; + + if ( apihash[ tmp ][ type ] ) { + api = tmp; + break; } } - - return proto; + return api; }, // Compile all native events to single array all: Popcorn.Events.Natives.split( /\s+/g ), @@ -530,15 +559,16 @@ fn: { trigger: function( type, data ) { + var eventInterface, evt; // setup checks for custom event system if ( this.data.events[ type ] && Popcorn.sizeOf( this.data.events[ type ] ) ) { - var eventInterface = Popcorn.events.getInterface( type ); + eventInterface = Popcorn.events.getInterface( type ); if ( eventInterface ) { - var evt = document.createEvent( eventInterface ); - evt.initEvent( type, true, true, global, 1 ); + evt = document.createEvent( eventInterface ); + evt.initEvent( type, true, true, global, 1 ); this.media.dispatchEvent( evt ); diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a45f06f..75be1f2 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -575,6 +575,18 @@ test("position called from plugin", function () { module("Popcorn Events"); +test("Can Detect Native event types", function() { + + var tests = [ "play", "pause", "rough loade", "data seek" ], + expects = [ true, true, false, false ]; + + expect( tests.length ); + + tests.forEach(function( type, idx ) { + equal( Popcorn.events.isNative( type ), expects[ idx ], type + ( expects[ idx ] ? " is " : " is not " ) + "a native valid event" ); + }); +}); + test("Stored By Type", function () { QUnit.reset(); @@ -1730,7 +1742,6 @@ test("Popcorn.disable/enable/toggle", function() { $pop.exec( 40, function() { - console.log( "WOO!" ); // make sure toggler never happened // look for: "toggler-test" -- 2.43.0