From 32c1338614809e9db684c084e6548f4ff662ba2e Mon Sep 17 00:00:00 2001 From: rwldrn Date: Wed, 9 Feb 2011 14:55:30 -0500 Subject: [PATCH] [#319] DRY out Popcorn.removePlugin() --- popcorn.js | 38 +++++------ test/popcorn.unit.js | 7 +- test/popcorn.unit.setup.js | 133 +++++++++++++++++++++---------------- 3 files changed, 97 insertions(+), 81 deletions(-) diff --git a/popcorn.js b/popcorn.js index 0c5b01f..ffc80d5 100644 --- a/popcorn.js +++ b/popcorn.js @@ -320,34 +320,34 @@ removePlugin: function( name ) { var byStart = this.data.trackEvents.byStart, - byEnd = this.data.trackEvents.byEnd; + byEnd = this.data.trackEvents.byEnd, + idx, rl, sl; delete Popcorn.p[ name ]; // remove plugin reference from registry - for ( var r = 0, rl = Popcorn.registry.length; r < rl; r++ ) { - if ( Popcorn.registry[r].type === name ) { - Popcorn.registry.splice(r, 1); + for ( idx = 0, rl = Popcorn.registry.length; idx < rl; idx++ ) { + if ( Popcorn.registry[ idx ].type === name ) { + Popcorn.registry.splice( idx, 1 ); break; // plugin found, stop checking } } // remove all trackEvents - for ( var s = 0, sl = byStart.length; s < sl; s++ ) { - if ( byStart[s] && byStart[s]._natives && byStart[s]._natives.type === name ) { - byStart.splice( s, 1 ); - s--; sl--; // update for loop if something removed, but keep checking - if ( this.data.trackEvents.startIndex <= s ) { - this.data.trackEvents.startIndex--; // write test for this - } - } - } - for ( var e = 0, el = byEnd.length; e < el; e++ ) { - if ( byEnd[e] && byEnd[e]._natives && byEnd[e]._natives.type === name ) { - byEnd.splice( e, 1 ); - e--; el--; // update for loop if something removed, but keep checking - if ( this.data.trackEvents.endIndex <= e ) { - this.data.trackEvents.endIndex--; // write test for this + for ( idx = 0, sl = byStart.length; idx < sl; idx++ ) { + + if ( ( byStart[ idx ] && byEnd[ idx ] ) && + ( byStart[ idx ]._natives && byStart[ idx ]._natives.type === name ) && + ( byEnd[ idx ]._natives && byEnd[ idx ]._natives.type === name ) ) { + + byStart.splice( idx, 1 ); + byEnd.splice( idx, 1 ); + + // update for loop if something removed, but keep checking + idx--; sl--; + if ( this.data.trackEvents.startIndex <= idx ) { + this.data.trackEvents.startIndex--; + this.data.trackEvents.endIndex--; } } } diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index ac2bfea..03fe924 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1403,12 +1403,13 @@ module("Popcorn Test Runner End"); test("Last Check", function () { // ALWAYS RUN LAST - - expect(1) try { equals( Setup.getGlobalSize(), Setup.globalSize + 1 , "Popcorn API did not leak"); - plus(); + + if ( !Setup.globalDiff.length ) { + //console.log(Setup.globalDiff); + } } catch (e) {}; diff --git a/test/popcorn.unit.setup.js b/test/popcorn.unit.setup.js index 07e68fd..79f4a1c 100644 --- a/test/popcorn.unit.setup.js +++ b/test/popcorn.unit.setup.js @@ -1,59 +1,74 @@ -// suppress console log errors -if ( !window['console'] ) { - var console = {}, - methods = 'log debug info warn error exception assert dir dirxml trace group groupEnd groupCollapsed time timeEnd profile profileEnd count clear notifyFirebug getFirebugElement firebug element'.split(' '); - - for ( var m in methods ) { - console[ methods[m] ] = function () { - //Array.prototype.slice.call(arguments) - }; - } -} - - -(function (global) { - - // Store the number of global properties that exist prior to Popcorn API definition - - var Setup = { - globalSize: 0 - }; - - Setup.getGlobalSize = function() { - - var size = 0; - - for( var p in window ) { - if ( p !== "_firebug" ) { - size++; - } - } - - // account for self - size++; - - // Store the number of global properties internally - if ( !Setup.globalSize ) { - Setup.globalSize = size; - } - - return size; - }; - - - - Setup.eventset = "loadstart progress suspend emptied stalled play pause " + - "loadedmetadata loadeddata waiting playing canplay canplaythrough " + - "seeking seeked timeupdate ended ratechange durationchange volumechange"; - - Setup.events = Setup.eventset.split(/\s+/g); - - - - global.Setup = Setup; - -})(window); - - -Setup.getGlobalSize(); - +// suppress console log errors +if ( !window['console'] ) { + var console = {}, + methods = 'log debug info warn error exception assert dir dirxml trace group groupEnd groupCollapsed time timeEnd profile profileEnd count clear notifyFirebug getFirebugElement firebug element'.split(' '); + + for ( var m in methods ) { + console[ methods[m] ] = function () { + //Array.prototype.slice.call(arguments) + }; + } +} + + +(function (global) { + + // Store the number of global properties that exist prior to Popcorn API definition + + var Setup = { + hasRun: false, + globalSize: 0, + globalCache: [], + globalDiff: [] + }; + + Setup.getGlobalSize = function() { + + var size = 0; + + for( var p in window ) { + if ( p !== "_firebug" ) { + size++; + + if ( !Setup.hasRun ) { + Setup.globalCache.push( p ); + } else { + if ( Setup.globalCache.indexOf( p ) === -1 ) { + Setup.globalDiff.push( p ); + } + } + } + } + + // account for self + size++; + + // Store the number of global properties internally + if ( !Setup.globalSize ) { + Setup.globalSize = size; + } + + if ( !Setup.hasRun ) { + Setup.hasRun = true; + } + + return size; + }; + + + + Setup.eventset = "loadstart progress suspend emptied stalled play pause " + + "loadedmetadata loadeddata waiting playing canplay canplaythrough " + + "seeking seeked timeupdate ended ratechange durationchange volumechange"; + + Setup.events = Setup.eventset.split(/\s+/g); + + + + global.Setup = Setup; + +})(window); + + +Setup.getGlobalSize(); + -- 2.43.0