From fb456d28f227f4bd903c7ec736425ba64a5700f3 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 2 Feb 2009 15:02:10 +0300 Subject: [PATCH] experimental gears cache work --- manifest.json | 19 ++++++-- tt-rss.js | 40 +++++++++++++++- viewfeed.js | 128 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 146 insertions(+), 41 deletions(-) diff --git a/manifest.json b/manifest.json index e349d960..6dd6ecc9 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,18 @@ { "betaManifestVersion": 1, - "version": "my_version_string", + "version": "0", "entries": [ - { "url": "go_offline.html"}, - { "url": "go_offline.js"}, - { "url": "../gears_init.js"} + { "url": "tt-rss.php"}, + { "url": "tt-rss.css"}, + { "url": "viewfeed.js"}, + { "url": "feedlist.js"}, + { "url": "functions.js"}, + { "url": "tt-rss.js"}, + { "url": "lib/scriptaculous/effects.js"}, + { "url": "lib/scriptaculous/controls.js"}, + { "url": "lib/scriptaculous/dragdrop.js"}, + { "url": "lib/scriptaculous/scriptaculous.js"}, + { "url": "lib/prototype.js"}, + { "url": "gears_init.js"} ] -} \ No newline at end of file +} diff --git a/tt-rss.js b/tt-rss.js index 17e33bf8..d49eaa6b 100644 --- a/tt-rss.js +++ b/tt-rss.js @@ -19,6 +19,10 @@ var ver_offset = 0; var hor_offset = 0; var feeds_sort_by_unread = false; var feedlist_sortable_enabled = false; +var offline_mode = false; +var store = false; +var localServer = false; +var db = false; function activeFeedIsCat() { return _active_feed_is_cat; @@ -131,7 +135,11 @@ function backend_sanity_check_callback(transport) { } if (!transport.responseXML) { - fatalError(3, "Sanity check: Received reply is not XML", transport.responseText); + if (!google.gears) { + fatalError(3, "Sanity check: Received reply is not XML", transport.responseText); + } else { + init_offline(); + } return; } @@ -369,6 +377,8 @@ function init() { if (arguments.callee.done) return; arguments.callee.done = true; + init_gears(); + disableContainerChildren("headlinesToolbar", true); Form.disable("main_toolbar_form"); @@ -1451,3 +1461,31 @@ function feedBrowserSubscribe() { } } +function init_gears() { + try { + + if (google.gears) { + localServer = google.gears.factory.create("beta.localserver"); + store = localServer.createManagedStore("tt-rss"); + db = google.gears.factory.create('beta.database'); + db.open('tt-rss'); + + db.execute("CREATE TABLE IF NOT EXISTS cache (id text, article text, param text, added text)"); + } + + } catch (e) { + exception_error("init_gears", e); + } +} + +function init_offline() { + try { + offline_mode = true; + + remove_splash(); + + } catch (e) { + exception_error("init_offline", e); + } +} + diff --git a/viewfeed.js b/viewfeed.js index 828a2e89..615f366f 100644 --- a/viewfeed.js +++ b/viewfeed.js @@ -1466,65 +1466,116 @@ function cdmWatchdog() { function cache_inject(id, article, param) { - if (!cache_check_param(id, param)) { - debug("cache_article: miss: " + id + " [p=" + param + "]"); - - var cache_obj = new Array(); - - cache_obj["id"] = id; - cache_obj["data"] = article; - cache_obj["param"] = param; + try { + if (!cache_check_param(id, param)) { + debug("cache_article: miss: " + id + " [p=" + param + "]"); + + + if (db) { - article_cache.push(cache_obj); + var date = new Date(); + var ts = Math.round(date.getTime() / 1000); - } else { - debug("cache_article: hit: " + id + " [p=" + param + "]"); + db.execute("INSERT INTO cache (id, article, param, added) VALUES (?, ?, ?, ?)", + [id, article, param, ts]); + } else { + + var cache_obj = new Array(); + + cache_obj["id"] = id; + cache_obj["data"] = article; + cache_obj["param"] = param; + + article_cache.push(cache_obj); + } + + } else { + debug("cache_article: hit: " + id + " [p=" + param + "]"); + } + } catch (e) { + exception_error("cache_inject", e); } } function cache_find(id) { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id) { - return article_cache[i]["data"]; + + if (db) { + + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id) { + return article_cache[i]["data"]; + } } } return false; } function cache_find_param(id, param) { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { - return article_cache[i]["data"]; + + if (db) { + var rs = db.execute("SELECT article FROM cache WHERE id = ? AND param = ?", + [id, param]); + + if (rs.isValidRow()) { + return rs.field(0); + } + + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { + return article_cache[i]["data"]; + } } } return false; } function cache_check(id) { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id) { - return true; + + if (db) { + var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ?", + [id]); + + if (rs.isValidRow()) { + return rs.field(0) != "0"; + } + + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id) { + return true; + } } } return false; } function cache_check_param(id, param) { - for (var i = 0; i < article_cache.length; i++) { -// debug("cache_check_param " + article_cache[i]["id"] + ":" + -// article_cache[i]["param"] + " vs " + id + ":" + param); + if (db) { + var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ? AND param = ?", + [id, param]); - if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { - return true; + if (rs.isValidRow()) { + return rs.field(0) != "0"; + } + + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { + return true; + } } } return false; } function cache_expire() { - while (article_cache.length > 25) { - article_cache.shift(); + if (!db) { + while (article_cache.length > 25) { + article_cache.shift(); + } } } @@ -1533,18 +1584,25 @@ function cache_empty() { } function cache_invalidate(id) { - var i = 0 - try { - while (i < article_cache.length) { - if (article_cache[i]["id"] == id) { - debug("cache_invalidate: removed id " + id); - article_cache.splice(i, 1); - return true; + if (db) { + rs = db.execute("DELETE FROM cache WHERE id = ?", [id]); + return rs.rowsAffected != 0; + } else { + + var i = 0 + + while (i < article_cache.length) { + if (article_cache[i]["id"] == id) { + debug("cache_invalidate: removed id " + id); + article_cache.splice(i, 1); + return true; + } + i++; } - i++; } + debug("cache_invalidate: id not found: " + id); return false; } catch (e) { -- 2.39.5