]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Moved destroy around, fixed some leaking variables, abandoned error throwing for...
authorDavid Seifried <david.c.seifried@gmail.com>
Tue, 20 Sep 2011 15:29:58 +0000 (11:29 -0400)
committerDavid Seifried <david.c.seifried@gmail.com>
Tue, 20 Sep 2011 15:29:58 +0000 (11:29 -0400)
popcorn.js
testDestroy.html [deleted file]

index de90f0fc3c2e811ea873030b5c64a817800adda7..fe15842fed950f6bf37579a33bb367a736acabce 100644 (file)
@@ -4,8 +4,8 @@
   if ( !document.addEventListener ) {
     global.Popcorn = {};
 
-    var methods = ( "removeInstance destroy addInstance getInstanceById removeInstanceById " +
-          "forEach extend effects error guid sizeOf isArray nop position disable enable " +
+    var methods = ( "removeInstance addInstance getInstanceById removeInstanceById " +
+          "forEach extend effects error guid sizeOf isArray nop position disable enable destroy " +
           "addTrackEvent removeTrackEvent getTrackEvents getTrackEvent getLastTrackEventId " +
           "timeUpdate plugin removePlugin compose effect parser xhr getJSONP getScript" ).split(/\s+/);
 
@@ -32,6 +32,7 @@
   readyStack = [],
   readyBound = false,
   readyFired = false,
+  isDestroyed = false,
 
   //  Non-public internal data object
   internal = {
     return Popcorn.instances;
   };
 
-  Popcorn.destroy = function( instance ) {
-    var events = instance.data.events,
-        singleEvent;
-
-    //  Iterate through all events and remove them
-    for( item in events ) {
-      singleEvent = events[ item ];
-      for( fn in singleEvent ) {
-        delete singleEvent[ fn ];
-      }
-      events[ item ] = null;
-    }
-
-    if ( instance.media.readyState >= 2 ) {
-      instance.media.removeEventListener( "timeupdate", instance.data.timeUpdateFunction, false );
-    } else {
-      throw ( "Attempting to remove event listener before event has been added!" );
-    }  
-
-    Popcorn.removeInstance( instance );
-  };
-
   //  Addes a Popcorn instance to the Popcorn instance array
   Popcorn.addInstance = function( instance ) {
 
           } else {
 
             that.data.timeUpdateFunction = function( event ) {
-              console.log( "in time update" );
               Popcorn.timeUpdate( that, event );
             }
 
-            that.media.addEventListener( "timeupdate", that.data.timeUpdateFunction, false );
+            !isDestroyed && that.media.addEventListener( "timeupdate", that.data.timeUpdateFunction, false );
           }
         } else {
           global.setTimeout(function() {
       }
 
       return instance;
+    },
+    destroy: function( instance ) {
+      var events = instance.data.events,
+          singleEvent, item, fn;
+
+      //  Iterate through all events and remove them
+      for ( item in events ) {
+        singleEvent = events[ item ];
+        for ( fn in singleEvent ) {
+          delete singleEvent[ fn ];
+        }
+        events[ item ] = null;
+      }
+
+      instance.media.removeEventListener( "timeupdate", instance.data.timeUpdateFunction, false );
+      isDestroyed = true;
+
+      Popcorn.removeInstance( instance );
     }
   });
 
diff --git a/testDestroy.html b/testDestroy.html
deleted file mode 100644 (file)
index ca6b604..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-<html>
-  <head>
-    <script src="popcorn.js"></script>
-    <script>
-    var popcorn;
-
-    function destroy() {
-      popcorn.destroy();
-    }
-
-
-      document.addEventListener( "DOMContentLoaded", function( event ) {
-        popcorn = Popcorn( "#video" );
-        
-        popcorn.listen( "timeupdate", function() { });
-        popcorn.listen( "play", function() {});
-
-        for( item in popcorn.data.events ) {
-          console.log( "EVENT: ", popcorn.data.events[ item ] );
-        }
-
-        //popcorn.destroy();
-
-      }, false );
-    </script>
-  </head>
-  <body>
-    <button onclick="destroy()">DESTROY</button>
-  
-  <video id='video'
-    style="display:;width:300px"
-    controls
-    preload="auto">
-
-    <source id='mp4'
-      src="test/trailer.mp4"
-      type='video/mp4; codecs="avc1, mp4a"'>
-
-    <source id='ogv'
-      src="test/trailer.ogv"
-      type='video/ogg; codecs="theora, vorbis"'>
-      
-    <source id='webm'
-      src="test/trailer.webm"
-      type='video/webm; codecs="vp8, vorbis"'>
-
-    <p>Your user agent does not support the HTML5 Video element.</p>
-  </video>
-  </body>
-</html>