Skip to content
Browse files

Updating README and comments to stress perf hit on monitoring DOM Mut…

…ation
  • Loading branch information...
1 parent 7d7e046 commit fdee3e34a84424c4be0a5c4d6aae1639808036ef @ifandelse committed Jan 30, 2012
View
3 README.md
@@ -10,9 +10,10 @@ livecache can:
* automatically invalidate the cache by monitoring DOM mutator events (`DOMNodeInserted` & `DOMNodeRemoved`)
* If any node being removed/inserted matches a cached selector, the selector is removed from the cache
* subsequent queries for that same selector would re-create the cache entry
+ * **WARNING** This will be expensive. Useful for debugging and experimentation? Yes. Helpful for good DOM performance in live production? NO.
* automatically *update* the cache by monitoring DOM mutator events
* If any node being removed/inserted matches a cached selector, that cache is immediately refreshed
- * beware - this can be expensive
+ * **WARNING** This will be expensive. Useful for debugging and experimentation? Yes. Helpful for good DOM performance in live production? NO.
## Examples
re-query for a node list until results exist:
View
12 build-amd.json
@@ -0,0 +1,12 @@
+{
+ "source": "src",
+ "output": "lib/amd",
+ "lint": {},
+ "uglify": {},
+ "gzip": {},
+ "extensions": { "uglify": "min", "gzip": "gz" },
+ "wrap": {
+ "prefix": "define(['jquery'], function($) {",
+ "suffix": "});"
+ }
+}
View
6 build.json
@@ -1,12 +1,12 @@
{
"source": "src",
- "output": "lib",
+ "output": "lib/standard",
"lint": {},
"uglify": {},
"gzip": {},
"extensions": { "uglify": "min", "gzip": "gz" },
"wrap": {
- "prefix": "",
- "suffix": ""
+ "prefix": "(function($) {",
+ "suffix": "})(jQuery);"
}
}
View
2 example/index.html
@@ -4,7 +4,7 @@
<head>
<title>livecache example</title>
<script type="text/javascript" src="../ext/jquery-1.7.1.js"></script>
- <script type="text/javascript" src="../lib/livecache.js"></script>
+ <script type="text/javascript" src="../lib/standard/livecache.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
View
59 lib/amd/livecache.js
@@ -0,0 +1,59 @@
+define(['jquery'], function($) {
+/*
+ livecache
+ Author: Jim Cowart
+ License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
+ Version 0.1.0
+*/
+
+var subject = {},
+ monitoringDom = false,
+ config = {
+ monitorDom: false,
+ invalidateOnly: true
+ },
+ // Caveat Emptor: live updates of the cache from DOM mutation WILL get **expensive**
+ // Seriously - this callback is named bruteForce for a reason, so only use this feature
+ // if your DOM updates are small and infrequent, and you don't have critical operations
+ // to apply after and during each update. Some might even go as far as to say that I'm
+ // Doing It Wrong™ by including this feature. Maybe I'm including it to poke them in the eye. :-)
+ bruteForce = function(obj) {
+ var elems = [$(obj.target)].concat($("*", obj.target));
+ $.each(elems, function(idx, elem){
+ $.each(subject, function(key) {
+ if($(elem).is($(key))) {
+ if(config.invalidateOnly)
+ delete subject[key];
+ else
+ subject[key] = $(key);
+ }
+ });
+ });
+ };
+
+function applyConfig(cfg) {
+ config = $.extend(config, cfg);
+ if(config.monitorDom && ! monitoringDom) {
+ $(document).on("DOMNodeInserted DOMNodeRemoved", bruteForce);
+ monitoringDom = true;
+ }
+ else if(!config.monitorDom && monitoringDom) {
+ $(document).off("DOMNodeInserted DOMNodeRemoved", bruteForce);
+ monitoringDom = false;
+ }
+}
+
+$.extend({
+ "livecache" : function( selector, forceRefresh ) {
+ if(typeof selector === "string") {
+ if(forceRefresh || !subject[selector] || subject[selector].length === 0) {
+ subject[selector] = $( selector );
+ }
+ return subject[selector];
+ }
+ else if (typeof selector === "object") {
+ applyConfig(selector);
+ }
+ }
+});
+});
View
BIN lib/amd/livecache.min.gz.js
Binary file not shown.
View
1 lib/amd/livecache.min.js
@@ -0,0 +1 @@
+define(["jquery"],function(a){function f(b){d=a.extend(d,b),d.monitorDom&&!c?(a(document).on("DOMNodeInserted DOMNodeRemoved",e),c=!0):!d.monitorDom&&c&&(a(document).off("DOMNodeInserted DOMNodeRemoved",e),c=!1)}var b={},c=!1,d={monitorDom:!1,invalidateOnly:!0},e=function(c){var e=[a(c.target)].concat(a("*",c.target));a.each(e,function(c,e){a.each(b,function(c){a(e).is(a(c))&&(d.invalidateOnly?delete b[c]:b[c]=a(c))})})};a.extend({livecache:function(c,d){if(typeof c=="string"){if(d||!b[c]||b[c].length===0)b[c]=a(c);return b[c]}typeof c=="object"&&f(c)}})})
View
48 lib/livecache.js
@@ -1,48 +0,0 @@
-(function($) {
- var subject = {},
- monitoringDom = false,
- config = {
- monitorDom: false,
- invalidateOnly: true
- },
- // Caveat Emptor: live updates of the cache from DOM mutation could get **expensive**
- bruteForce = function(obj) {
- var elems = [$(obj.target)].concat($("*", obj.target));
- $.each(elems, function(idx, elem){
- $.each(subject, function(key) {
- if($(elem).is($(key))) {
- if(config.invalidateOnly)
- delete subject[key];
- else
- subject[key] = $(key);
- }
- });
- });
- };
-
- function applyConfig(cfg) {
- config = $.extend(config, cfg);
- if(config.monitorDom && ! monitoringDom) {
- $(document).on("DOMNodeInserted DOMNodeRemoved", bruteForce);
- monitoringDom = true;
- }
- else if(!config.monitorDom && monitoringDom) {
- $(document).off("DOMNodeInserted DOMNodeRemoved", bruteForce);
- monitoringDom = false;
- }
- }
-
- $.extend({
- "livecache" : function( selector, forceRefresh ) {
- if(typeof selector === "string") {
- if(forceRefresh || !subject[selector] || subject[selector].length === 0) {
- subject[selector] = $( selector );
- }
- return subject[selector];
- }
- else if (typeof selector === "object") {
- applyConfig(selector);
- }
- }
- });
-})(jQuery);
View
59 lib/standard/livecache.js
@@ -0,0 +1,59 @@
+(function($) {
+/*
+ livecache
+ Author: Jim Cowart
+ License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
+ Version 0.1.0
+*/
+
+var subject = {},
+ monitoringDom = false,
+ config = {
+ monitorDom: false,
+ invalidateOnly: true
+ },
+ // Caveat Emptor: live updates of the cache from DOM mutation WILL get **expensive**
+ // Seriously - this callback is named bruteForce for a reason, so only use this feature
+ // if your DOM updates are small and infrequent, and you don't have critical operations
+ // to apply after and during each update. Some might even go as far as to say that I'm
+ // Doing It Wrong™ by including this feature. Maybe I'm including it to poke them in the eye. :-)
+ bruteForce = function(obj) {
+ var elems = [$(obj.target)].concat($("*", obj.target));
+ $.each(elems, function(idx, elem){
+ $.each(subject, function(key) {
+ if($(elem).is($(key))) {
+ if(config.invalidateOnly)
+ delete subject[key];
+ else
+ subject[key] = $(key);
+ }
+ });
+ });
+ };
+
+function applyConfig(cfg) {
+ config = $.extend(config, cfg);
+ if(config.monitorDom && ! monitoringDom) {
+ $(document).on("DOMNodeInserted DOMNodeRemoved", bruteForce);
+ monitoringDom = true;
+ }
+ else if(!config.monitorDom && monitoringDom) {
+ $(document).off("DOMNodeInserted DOMNodeRemoved", bruteForce);
+ monitoringDom = false;
+ }
+}
+
+$.extend({
+ "livecache" : function( selector, forceRefresh ) {
+ if(typeof selector === "string") {
+ if(forceRefresh || !subject[selector] || subject[selector].length === 0) {
+ subject[selector] = $( selector );
+ }
+ return subject[selector];
+ }
+ else if (typeof selector === "object") {
+ applyConfig(selector);
+ }
+ }
+});
+})(jQuery);
View
BIN lib/livecache.min.gz.js → lib/standard/livecache.min.gz.js
Binary file not shown.
View
0 lib/livecache.min.js → lib/standard/livecache.min.js
File renamed without changes.
View
6 src/VersionHeader.js
@@ -0,0 +1,6 @@
+/*
+ livecache
+ Author: Jim Cowart
+ License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
+ Version 0.1.0
+*/
View
87 src/livecache.js
@@ -1,48 +1,51 @@
-(function($) {
- var subject = {},
- monitoringDom = false,
- config = {
- monitorDom: false,
- invalidateOnly: true
- },
- // Caveat Emptor: live updates of the cache from DOM mutation could get **expensive**
- bruteForce = function(obj) {
- var elems = [$(obj.target)].concat($("*", obj.target));
- $.each(elems, function(idx, elem){
- $.each(subject, function(key) {
- if($(elem).is($(key))) {
- if(config.invalidateOnly)
- delete subject[key];
- else
- subject[key] = $(key);
- }
- });
+//import("VersionHeader.js");
+var subject = {},
+ monitoringDom = false,
+ config = {
+ monitorDom: false,
+ invalidateOnly: true
+ },
+ // Caveat Emptor: live updates of the cache from DOM mutation WILL get **expensive**
+ // Seriously - this callback is named bruteForce for a reason, so only use this feature
+ // if your DOM updates are small and infrequent, and you don't have critical operations
+ // to apply after and during each update. Some might even go as far as to say that I'm
+ // Doing It Wrong™ by including this feature. Maybe I'm including it to poke them in the eye. :-)
+ bruteForce = function(obj) {
+ var elems = [$(obj.target)].concat($("*", obj.target));
+ $.each(elems, function(idx, elem){
+ $.each(subject, function(key) {
+ if($(elem).is($(key))) {
+ if(config.invalidateOnly)
+ delete subject[key];
+ else
+ subject[key] = $(key);
+ }
});
- };
+ });
+ };
- function applyConfig(cfg) {
- config = $.extend(config, cfg);
- if(config.monitorDom && ! monitoringDom) {
- $(document).on("DOMNodeInserted DOMNodeRemoved", bruteForce);
- monitoringDom = true;
- }
- else if(!config.monitorDom && monitoringDom) {
- $(document).off("DOMNodeInserted DOMNodeRemoved", bruteForce);
- monitoringDom = false;
- }
+function applyConfig(cfg) {
+ config = $.extend(config, cfg);
+ if(config.monitorDom && ! monitoringDom) {
+ $(document).on("DOMNodeInserted DOMNodeRemoved", bruteForce);
+ monitoringDom = true;
+ }
+ else if(!config.monitorDom && monitoringDom) {
+ $(document).off("DOMNodeInserted DOMNodeRemoved", bruteForce);
+ monitoringDom = false;
}
+}
- $.extend({
- "livecache" : function( selector, forceRefresh ) {
- if(typeof selector === "string") {
- if(forceRefresh || !subject[selector] || subject[selector].length === 0) {
- subject[selector] = $( selector );
- }
- return subject[selector];
- }
- else if (typeof selector === "object") {
- applyConfig(selector);
+$.extend({
+ "livecache" : function( selector, forceRefresh ) {
+ if(typeof selector === "string") {
+ if(forceRefresh || !subject[selector] || subject[selector].length === 0) {
+ subject[selector] = $( selector );
}
+ return subject[selector];
}
- });
-})(jQuery);
+ else if (typeof selector === "object") {
+ applyConfig(selector);
+ }
+ }
+});

0 comments on commit fdee3e3

Please sign in to comment.
Something went wrong with that request. Please try again.