]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#540] Loadtime event api table compilation; Perf boosts for event util method calls;
authorrwldrn <waldron.rick@gmail.com>
Tue, 24 May 2011 15:54:38 +0000 (11:54 -0400)
committerrwldrn <waldron.rick@gmail.com>
Tue, 24 May 2011 15:54:38 +0000 (11:54 -0400)
popcorn.js
test/popcorn.unit.js

index e2b8de6b97bc3ac11c9c2ea4714220b100f86046..50639af082228d892fbbd24120e170c50e99faeb 100644 (file)
   readyBound = false,
   readyFired = false,
 
+  //  Non-public internal data object
+  internal = {
+    events: { 
+      hash: {},
+      apis: {}
+    }
+  },
 
   //  Declare constructor
   //  Returns an instance object.
   });
 
   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 " +
                            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 ) {
 
         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 ),
     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 );
 
index a45f06f71729fd79c3e65f8930c9e3660025609d..75be1f247367afd1dcb30d57d63f9c7dec1c1e77 100644 (file)
@@ -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"