Skip to content

Commit

Permalink
Better memory cleanup mechanism and implementation. Some generation c…
Browse files Browse the repository at this point in the history
…leanup.
  • Loading branch information
SlexAxton committed Dec 1, 2012
1 parent 62b5444 commit 005eea3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/ModernizrProto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ define(['tests'], function ( tests ) {
enableClasses : true
},

_q : [],

addTest : function( name, fn, options ) {
tests.push({name : name, fn : fn, options : options });
},
Expand Down
6 changes: 6 additions & 0 deletions src/addTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,11 @@ define(['ModernizrProto', 'Modernizr', 'docElement', 'hasOwnProp'], function( Mo
return Modernizr; // allow chaining.
}

// After all the tests are run, add self
// to the Modernizr prototype
Modernizr._q.push(function() {
ModernizrProto.addTest = addTest;
});

return addTest;
});
28 changes: 9 additions & 19 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ define(['underscore'], function( _ ) {

// Some special cases
var setClasses = _(config.options).contains('setClasses');
var addTest = _(config.options).contains('addTest');

// Remove the special cases
config.options = _(config.options).without('setClasses', 'addTest');
config.options = _(config.options).without('setClasses');

var output = 'require(["ModernizrProto", "Modernizr", "testRunner"';

// Needed named module requires
if (setClasses) {
output += ', "setClasses"';
}
if (addTest) {
output += ', "addTest"';
}

// Load in the rest of the options (they dont return values, so they aren't declared
_(config.options).forEach(function (option) {
Expand All @@ -40,9 +36,6 @@ define(['underscore'], function( _ ) {
if (setClasses) {
output += ', setClasses';
}
if (addTest) {
output += ', addTest';
}

output += ') {\n' +
' // Run each test\n' +
Expand All @@ -56,21 +49,18 @@ define(['underscore'], function( _ ) {
'\n';
}

// Leak the user-facing addTest if they want it
if (addTest) {
output += ' // Ovveride the addTest class with one that works\n' +
' // after the tests have run and kill the async\n' +
' // test function, since it doesn\'t make sense anymore\n' +
' ModernizrProto.addTest = addTest;\n';
}
// Otherwise remove the useless one
else {
output += ' delete ModernizrProto.addTest;\n';
}

output += ' delete ModernizrProto.addTest;\n';
output += ' delete ModernizrProto.addAsyncTest;\n' +
'\n';

// TODO:: if there's a way to figure out if there will be no
// items in this queue, then we could avoid the code.
output += ' // Run the things that are supposed to run after the tests\n' +
' for (var i = 0; i < Modernizr._q.length; i++) {\n' +
' Modernizr._q[i]();\n' +
' }\n\n';

output += ' // Leak Modernizr namespace\n' +
' window.Modernizr = Modernizr;\n' +
'\n' +
Expand Down
14 changes: 12 additions & 2 deletions src/mStyle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
define(['modElem'], function( modElem ) {
var mStyle = modElem.style;
define(['Modernizr', 'modElem'], function( Modernizr, modElem ) {
var mStyle = {
style : modElem.elem.style
};

// kill ref for gc, must happen before
// mod.elem is removed, so we unshift on to
// the front of the queue.
Modernizr._q.unshift(function() {
delete mStyle.style;
});

return mStyle;
});
12 changes: 10 additions & 2 deletions src/modElem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
define(['createElement'], function(createElement) {
define(['Modernizr', 'createElement'], function( Modernizr, createElement ) {
/**
* Create our "modernizr" element that we do most feature tests on.
*/
var modElem = createElement('modernizr');
var modElem = {
elem : createElement('modernizr')
};

// Clean up this element
Modernizr._q.push(function() {
delete modElem.elem;
});

return modElem;
});
11 changes: 9 additions & 2 deletions src/setCss.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
define(['mStyle'], function( mStyle ) {
define(['Modernizr', 'mStyle'], function( Modernizr, mStyle ) {
/**
* setCss applies given styles to the Modernizr DOM node.
*/
function setCss( str ) {
mStyle.cssText = str;
mStyle.style.cssText = str;
}

// Clean up used to happen, but probably isn't
// necessary since we delete the element
/*Modernizr._q.unshift(function(){
setCss('');
});*/

return setCss;
});
2 changes: 1 addition & 1 deletion src/testProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define(['contains', 'mStyle'], function( contains, mStyle ) {
function testProps( props, prefixed ) {
for ( var i in props ) {
var prop = props[i];
if ( !contains(prop, "-") && mStyle[prop] !== undefined ) {
if ( !contains(prop, "-") && mStyle.style[prop] !== undefined ) {
return prefixed == 'pfx' ? prop : true;
}
}
Expand Down

0 comments on commit 005eea3

Please sign in to comment.