[Pkg-javascript-commits] [less.js] 157/285: Seperate out the browser cache

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:49 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit 3450e336b8ac4d5157f24d88e53a78885bd35c8f
Author: Luke Page <luke.a.page at gmail.com>
Date:   Tue Oct 7 23:20:47 2014 +0100

    Seperate out the browser cache
---
 lib/less-browser/browser.js |  2 +-
 lib/less-browser/cache.js   | 35 +++++++++++++++++++++++++++++++++++
 lib/less-browser/index.js   | 37 +++++++------------------------------
 3 files changed, 43 insertions(+), 31 deletions(-)

diff --git a/lib/less-browser/browser.js b/lib/less-browser/browser.js
index 09eb7b9..c289a15 100644
--- a/lib/less-browser/browser.js
+++ b/lib/less-browser/browser.js
@@ -1,6 +1,6 @@
 var utils = require("./utils");
 module.exports = {
-    createCSS: function (document, styles, sheet, lastModified) {
+    createCSS: function (document, styles, sheet) {
         // Strip the query-string
         var href = sheet.href || '';
 
diff --git a/lib/less-browser/cache.js b/lib/less-browser/cache.js
new file mode 100644
index 0000000..fd7a454
--- /dev/null
+++ b/lib/less-browser/cache.js
@@ -0,0 +1,35 @@
+// Cache system is a bit outdated and could do with work
+
+module.exports = function(window, options, logger) {
+    var cache = null;
+    if (options.env !== 'development') {
+        try {
+            cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
+        } catch (_) {}
+    }
+    return {
+        setCSS: function(path, lastModified, styles) {
+            if (cache) {
+                logger.info('saving ' + path+ ' to cache.');
+                try {
+                    cache.setItem(path, styles);
+                    cache.setItem(path + ':timestamp', lastModified);
+                } catch(e) {
+                    //TODO - could do with adding more robust error handling
+                    logger.error('failed to save');
+                }
+            }
+        },
+        getCSS: function(path, webInfo) {
+            var css       = cache && cache.getItem(path),
+                timestamp = cache && cache.getItem(path + ':timestamp');
+
+            if (timestamp && webInfo.lastModified &&
+                (new(Date)(webInfo.lastModified).valueOf() ===
+                    new(Date)(timestamp).valueOf())) {
+                // Use local copy
+                return css;
+            }
+        }
+    };
+};
diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js
index 3494fb0..801c5bd 100644
--- a/lib/less-browser/index.js
+++ b/lib/less-browser/index.js
@@ -23,6 +23,7 @@ require("./log-listener")(less, options);
 //var utils = require("./utils");
 var errors = require("./error-reporting")(window, less, options);
 var browser = require("./browser");
+var cache = options.cache || require("./cache")(window, options, less.logger);
 
 options.env = options.env || (window.location.hostname == '127.0.0.1' ||
                               window.location.hostname == '0.0.0.0'   ||
@@ -55,7 +56,6 @@ if (dumpLineNumbers) {
 }
 
 var typePattern = /^text\/(x-)?less$/;
-var cache = null;
 
 function postProcessCSS(styles) {
     if (options.postProcessor && typeof options.postProcessor === 'function') {
@@ -64,20 +64,6 @@ function postProcessCSS(styles) {
     return styles;
 }
 
-// Replace the cache system
-// Don't update the local store if the file wasn't modified
-//if (lastModified && cache) {
-//    logger.info('saving ' + href + ' to cache.');
-//    try {
-//        cache.setItem(href, styles);
-//        cache.setItem(href + ':timestamp', lastModified);
-//    } catch(e) {
-//        //TODO - could do with adding more robust error handling
-//        logger.error('failed to save');
-//    }//
-//}
-
-
 function clone(obj) {
     var cloned = {};
     for(var prop in obj) {
@@ -162,13 +148,8 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
         if (webInfo) {
             webInfo.remaining = remaining;
 
-            var css       = cache && cache.getItem(path),
-                timestamp = cache && cache.getItem(path + ':timestamp');
-
-            if (!reload && timestamp && webInfo.lastModified &&
-                (new(Date)(webInfo.lastModified).valueOf() ===
-                    new(Date)(timestamp).valueOf())) {
-                // Use local copy
+            var css = cache.getCSS(path, webInfo);
+            if (!reload && css) {
                 browser.createCSS(window.document, css, sheet);
                 webInfo.local = true;
                 callback(null, null, data, sheet, webInfo, path);
@@ -209,7 +190,8 @@ function initRunningMode(){
                         errors.add(e, e.href || sheet.href);
                     } else if (css) {
                         css = postProcessCSS(css);
-                        browser.createCSS(window.document, css, sheet, context.lastModified);
+                        browser.createCSS(window.document, css, sheet);
+                        cache.setCSS(sheet.href, context.lastModified, css);
                     }
                 });
             }
@@ -235,12 +217,6 @@ if (/!watch/.test(location.hash)) {
     less.watch();
 }
 
-if (less.env != 'development') {
-    try {
-        cache = (typeof(window.localStorage) === 'undefined') ? null : window.localStorage;
-    } catch (_) {}
-}
-
 //
 // Get all <link> tags with the 'rel' attribute set to "stylesheet/less"
 //
@@ -276,7 +252,8 @@ less.refresh = function (reload, modifyVars) {
         } else {
             less.logger.info("rendered " + sheet.href + " successfully.");
             css = postProcessCSS(css);
-            browser.createCSS(window.document, css, sheet, webInfo.lastModified);
+            browser.createCSS(window.document, css, sheet);
+            cache.setCSS(sheet.href, webInfo.lastModified, css);
         }
         less.logger.info("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms');
         if (webInfo.remaining === 0) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list