Commits

Anton Afanasyev committed 2b29e01

rewrote SITEINFO_IMPORT_URLS loading in iced coffee script for simplicity
(compiling the result to JS, of course). this is the first step towards better
caching of the results.

Comments (0)

Files changed (1)

autopagerize.user.js

 // @exclude        http://b.hatena.ne.jp/*
 // @exclude        http://www.facebook.com/plugins/like.php*
 // @exclude        http://api.tweetmeme.com/button.js*
-// @version        0.1.02
+// @version        0.1.03
 // @updateURL      https://bitbucket.org/aasoft/autopagerize-extended/raw/tip/autopagerize.user.js
 // @icon           http://autopagerize.net/img/icons/icon_032.png
 // @grant          GM_getValue
 var launchAutoPager = function(list)
 {
     if (list.length == 0)
-        return;
+        return false;
 
     if(ap)
-        return;
+        return false;
     for (var i = 0; i < list.length; i++) {
         try {
             var li = list[i];
                 if (lnkT) {
                     //log(lnkT);
                     ap = new AutoPager(li);
-                    return;
+                    return true;
                 }
             }
         } catch(e) {
             continue;
         }
     }
+
+    return false;
 }
 
 
         log("getCacheCallback("+res + ", " + info + ") error:\r\n"+e);
         info = [];
     }
-    if (info.length > 0) {
-        var withoutUrl = info.filter(function(i) { return ('urls' in i) || ('urlFunc' in i); });
+    if (info.length < 0) {
+        return getCacheErrorCallback(urlInfo);
+    }
 
-        var withUrl = info.filter(function(i) { return ('url' in i); });
-        withUrl.sort(function(a, b) { return (b.url.length - a.url.length); });
+    var withoutUrl = info.filter(function(i) { return ('urls' in i) || ('urlFunc' in i); });
 
-        info = withoutUrl.concat(withUrl);
-        /*info = info.filter(function(i) { return ('url' in i) || ('urls' in i) || ('urlFunc' in i); });
-        info.sort(function(a, b) { return (b.url.length - a.url.length); });*/
+    var withUrl = info.filter(function(i) { return ('url' in i); });
+    withUrl.sort(function(a, b) { return (b.url.length - a.url.length); });
 
-        /*
-        var r_keys = ['url', 'nextLink', 'insertBefore', 'pageElement'];
-        info = info.map(function(i) {
-            var item = {};
-            r_keys.forEach(function(key) {
-                if (i[key])
-                {
-                    item[key] = i[key];
-                }
-            });
-            return item;
-        });*/
+    info = withoutUrl.concat(withUrl);
+    /*info = info.filter(function(i) { return ('url' in i) || ('urls' in i) || ('urlFunc' in i); });
+    info.sort(function(a, b) { return (b.url.length - a.url.length); });*/
 
-        cacheInfo[urlInfo.url] = {
-            url: urlInfo.url,
-            expire: new Date(new Date().getTime() + (urlInfo.expire || CACHE_EXPIRE)),
-            info: info
-        };
-        //try {
-        //  GM_setValue('cacheInfo', JSON.stringify(cacheInfo))
-        //} catch(e) {
-        //  log("couldnt convert to json for GM_setValue in getCacheCallback("+ e.toString() + "). trying toSource()");
-            GM_setValue('cacheInfo', cacheInfo.toSource());
-        //}
-        launchAutoPager(info);
-    } else {
-        getCacheErrorCallback(urlInfo);
-    }
+    /*
+    var r_keys = ['url', 'nextLink', 'insertBefore', 'pageElement'];
+    info = info.map(function(i) {
+        var item = {};
+        r_keys.forEach(function(key) {
+            if (i[key])
+            {
+                item[key] = i[key];
+            }
+        });
+        return item;
+    });*/
+
+    cacheInfo[urlInfo.url] = {
+        url: urlInfo.url,
+        expire: new Date(new Date().getTime() + (urlInfo.expire || CACHE_EXPIRE)),
+        info: info
+    };
+    //try {
+    //  GM_setValue('cacheInfo', JSON.stringify(cacheInfo))
+    //} catch(e) {
+    //  log("couldnt convert to json for GM_setValue in getCacheCallback("+ e.toString() + "). trying toSource()");
+        GM_setValue('cacheInfo', cacheInfo.toSource());
+    //}
 }
 var getCacheErrorCallback = function(urlInfo) {
     var expire = new Date(new Date().getTime() + (urlInfo.expire || CACHE_EXPIRE));
     if (cacheInfo[urlInfo.url]) {
         cacheInfo[urlInfo.url].expire = expire;
-        launchAutoPager(cacheInfo[urlInfo.url].info);
     } else {
         cacheInfo[urlInfo.url] = {
             url: urlInfo.url,
             info: []
         };
     }
+
     GM_setValue('cacheInfo', cacheInfo.toSource());
 }
 
 GM_registerMenuCommand('AutoPagerize - YES mouse holding wont load', function() { enableMouseHolding( true ); });
 GM_registerMenuCommand('AutoPagerize - NO  mouse holding wont load', function() { enableMouseHolding( false ); });
 
+
+
+function AP_MakeAjaxRequest( url, callback ) {
+    var state = 0;
+
+    var opt = {
+        method: 'GET',
+        url: url,
+        onload: function(res) {
+            state = 1;
+            callback( {
+                result: res,
+                ok: true
+            });
+        },
+        onerror: function(res) {
+            state = 2;
+            callback( {
+                result: res,
+                ok: false
+            });
+        },
+    };
+    setTimeout( function() {
+        if ( state == 0) {
+            callback( {
+                result: null,
+                ok: false
+            });
+        }
+    }, XHR_TIMEOUT);
+
+    GM_xmlhttpRequest(opt);
+}
+
+
 var ap = null;
 launchAutoPager(SITEINFO);
 var cacheInfo = getCache();
-var xhrStates = {};
 var dt = new Date();
-SITEINFO_IMPORT_URLS.forEach(function(i) {
-    if (!cacheInfo[i.url] || new Date(cacheInfo[i.url].expire) < dt) {
-        var opt = {
-            method: 'GET',
-            url: i.url,
-            onload: function(res) {
-                xhrStates[i] = 'loaded';
-                getCacheCallback(res, i);
-            },
-            onerror: function(res) {
-                xhrStates[i] = 'error';
-                getCacheErrorCallback(i);
-            },
+
+/*
+refreshSingleSiteInfoImportUrl = (importItem) ->
+    cacheItem = cacheInfo[importItem.url]
+    if (cacheItem && new Date(cacheItem.expire) > dt)
+        return
+
+    await AP_MakeAjaxRequest importItem.url, defer result
+
+    if( result.ok )
+        getCacheCallback( result.result, importItem );
+    else
+        getCacheErrorCallback( importItem );
+
+checkUpdateSiteInfoImportUrls = () ->
+    await
+        for importItem,i in SITEINFO_IMPORT_URLS
+            refreshSingleSiteInfoImportUrl importItem
+
+tryLaunchSiteInfoImportUrls = () ->
+    for importItem,i in SITEINFO_IMPORT_URLS
+        cacheItem = cacheInfo[importItem.url]
+        if ( launchAutoPager cacheItem.info )
+            return true
+    return false
+*/
+// Generated by IcedCoffeeScript 1.4.0a
+var checkUpdateSiteInfoImportUrls, iced, refreshSingleSiteInfoImportUrl, tryLaunchSiteInfoImportUrls, __iced_k, __iced_k_noop,
+  __slice = [].slice;
+
+iced = {
+  Deferrals: (function() {
+
+    function _Class(_arg) {
+      this.continuation = _arg;
+      this.count = 1;
+      this.ret = null;
+    }
+
+    _Class.prototype._fulfill = function() {
+      if (!--this.count) return this.continuation(this.ret);
+    };
+
+    _Class.prototype.defer = function(defer_params) {
+      var _this = this;
+      ++this.count;
+      return function() {
+        var inner_params, _ref;
+        inner_params = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+        if (defer_params != null) {
+          if ((_ref = defer_params.assign_fn) != null) {
+            _ref.apply(null, inner_params);
+          }
+        }
+        return _this._fulfill();
+      };
+    };
+
+    return _Class;
+
+  })(),
+  findDeferral: function() {
+    return null;
+  },
+  trampoline: function(_fn) {
+    return _fn();
+  }
+};
+__iced_k = __iced_k_noop = function() {};
+
+refreshSingleSiteInfoImportUrl = function(importItem) {
+  var cacheItem, result, ___iced_passed_deferral, __iced_deferrals, __iced_k,
+    _this = this;
+  __iced_k = __iced_k_noop;
+  ___iced_passed_deferral = iced.findDeferral(arguments);
+  cacheItem = cacheInfo[importItem.url];
+  if (cacheItem && new Date(cacheItem.expire) > dt) return;
+  (function(__iced_k) {
+    __iced_deferrals = new iced.Deferrals(__iced_k, {
+      parent: ___iced_passed_deferral,
+      filename: "test.ics",
+      funcname: "refreshSingleSiteInfoImportUrl"
+    });
+    AP_MakeAjaxRequest(importItem.url, __iced_deferrals.defer({
+      assign_fn: (function() {
+        return function() {
+          return result = arguments[0];
         };
-        xhrStates[i] = 'start';
-        GM_xmlhttpRequest(opt);
-        setTimeout(function() {
-            if (xhrStates[i] == 'start') {
-                getCacheErrorCallback(i);
-            }
-        }, XHR_TIMEOUT);
+      })(),
+      lineno: 7
+    }));
+    __iced_deferrals._fulfill();
+  })(function() {
+    if (result.ok) {
+      return getCacheCallback(result.result, importItem);
     } else {
-        launchAutoPager(cacheInfo[i.url].info);
+      return getCacheErrorCallback(importItem);
     }
-});
-launchAutoPager([MICROFORMAT]);
+  });
+};
+
+checkUpdateSiteInfoImportUrls = function() {
+  var i, importItem, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len;
+  __iced_k = __iced_k_noop;
+  ___iced_passed_deferral = iced.findDeferral(arguments);
+  __iced_deferrals = new iced.Deferrals(__iced_k, {
+    parent: ___iced_passed_deferral,
+    filename: "test.ics",
+    funcname: "checkUpdateSiteInfoImportUrls"
+  });
+  for (i = _i = 0, _len = SITEINFO_IMPORT_URLS.length; _i < _len; i = ++_i) {
+    importItem = SITEINFO_IMPORT_URLS[i];
+    refreshSingleSiteInfoImportUrl(importItem);
+  }
+  __iced_deferrals._fulfill();
+};
+
+tryLaunchSiteInfoImportUrls = function() {
+  var cacheItem, i, importItem, _i, _len;
+  for (i = _i = 0, _len = SITEINFO_IMPORT_URLS.length; _i < _len; i = ++_i) {
+    importItem = SITEINFO_IMPORT_URLS[i];
+    cacheItem = cacheInfo[importItem.url];
+    if (launchAutoPager(cacheItem.info)) return true;
+  }
+  return false;
+};
+
+
+checkUpdateSiteInfoImportUrls();
+if( !tryLaunchSiteInfoImportUrls() )
+    launchAutoPager([MICROFORMAT]);
+
 
 // new google search sucks!
 //if (location.href.match('^http://[^.]+\.google\.(?:[^.]{2,3}\.)?[^./]{2,3}/.*(&fp=)')) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.