[Pkg-mozext-commits] [wot] 196/226: Reset mytags list when user used scorecard to rate/comment

David Prévot taffit at moszumanska.debian.org
Fri May 1 00:35:51 UTC 2015


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

taffit pushed a commit to branch master
in repository wot.

commit 75481061d2752c0aed414807d41951f106adc872
Author: Sergey Andryukhin <sorgoz at yandex.com>
Date:   Thu Mar 6 13:56:50 2014 +0200

    Reset mytags list when user used scorecard to rate/comment
---
 content/api.js |  1 +
 content/my.js  | 20 +++++++++++++++-----
 content/wg.js  | 31 +++++++++++++++++++++++++++++--
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/content/api.js b/content/api.js
index 35eee16..398d3b8 100644
--- a/content/api.js
+++ b/content/api.js
@@ -1714,6 +1714,7 @@ var wot_api_tags = {
 					wot_tools.log("api.get_tags() failed", err);
 				},
 				function (data) {
+					wot_wg.release_lock(method);
 					wot_api_tags._on_get_tags(data, core_keyword);
 				});
 
diff --git a/content/my.js b/content/my.js
index 07c8564..0a36dd9 100644
--- a/content/my.js
+++ b/content/my.js
@@ -20,6 +20,10 @@
 
 var wot_my_session =
 {
+	last_reset: null,
+	reset_wait: 2000,   // 2 seconds to wait before reset again
+
+
 	domcontentloaded: function(e)
 	{
 		try {
@@ -39,10 +43,16 @@ var wot_my_session =
 
 			if (clear) {
 				clear.addEventListener("click", function() {
-					var target = clear.getAttribute("target");
-					if (target && wot_cache.iscached(target)) {
-						wot_cache.set(target, "status", WOT_QUERY_RETRY);
-                        wot_rw.resetstate();    // tell the Rating Window to reset old user testimonies
+
+					// the event can be triggered several times due to a bug on the website, so we have to skip other events after we got the first
+					if (!wot_my_session.last_reset || wot_my_session.last_reset + wot_my_session.reset_wait < Date.now()) {
+						var target = clear.getAttribute("target");
+						if (target && wot_cache.iscached(target)) {
+							wot_cache.set(target, "status", WOT_QUERY_RETRY);
+							wot_rw.resetstate();            // tell the Rating Window to reset old user testimonies
+							wot_wg.set_mytags([], true);    // reset mytags so the new list will be fetched on next RW opening
+							wot_my_session.last_reset = Date.now();
+						}
 					}
 				});
 			}
@@ -97,7 +107,7 @@ var wot_my_session =
 
 			var mgr = Components.classes["@mozilla.org/cookiemanager;1"].
 						getService(Components.interfaces.nsICookieManager);
-	
+
 			cookies = "";
 			var enumerator = mgr.enumerator;
 
diff --git a/content/wg.js b/content/wg.js
index 9a0a057..b03150d 100644
--- a/content/wg.js
+++ b/content/wg.js
@@ -32,6 +32,9 @@ var wot_wg = {
 	tags_re: /(\s|^)#([a-zä-ö0-9\u0400-\u04FF]{2,})/img,    // Also change the regexp at content/rw/wot.js
 	tags_validate_re: /^\d{2}$/im,
 
+	LOCK_POSTFIX: "_LOCK",
+	LOCK_TIMEOUT: 2000,
+
 	is_enabled: function () {
 		return (wot_hashtable.get("wg_enabled") == true);
 	},
@@ -58,11 +61,13 @@ var wot_wg = {
 		return JSON.parse(json);
 	},
 
-	set_mytags: function (tags) {
+	set_mytags: function (tags, reset) {
 		// this function is called in dynamic way, e.g. by concatenating string "set_" + variable.
 		tags = tags || [];
+		reset = reset || false;
 		wot_hashtable.set("mytags_list", JSON.stringify(tags));
-		wot_hashtable.set("mytags_updated", Date.now());
+		var dt = reset ? null : Date.now();
+		wot_hashtable.set("mytags_updated", dt);
 	},
 
 	set_popular_tags: function (tags) {
@@ -90,6 +95,9 @@ var wot_wg = {
 
 		for (var i = 0; i < tmap.length; i++) {
 			var obj = tmap[i];
+
+			if (!this.lock_api(obj.method)) continue;   // skip the iteration of lock is already acquired
+
 			var last_updated = obj.time_func.apply();
 			if (!last_updated || obj.time + last_updated < Date.now()) {
 				wot_api_tags.get_tags(obj.keyword, obj.method);
@@ -137,6 +145,25 @@ var wot_wg = {
 		}
 		this.tags_re.lastIndex = 0; // reset the last index to avoid using it for the different text
 		return tags;
+	},
+
+	lock_api: function (method) {
+		// this helps to avoid multiple API calls resulted from many update() calls
+
+		var lock_name = method + this.LOCK_POSTFIX,
+			update_lock = wot_hashtable.get(lock_name) || null;
+
+		if (update_lock && update_lock + this.LOCK_TIMEOUT > Date.now()) {
+			return false;   // the lock can't be acquired
+		}
+
+		wot_hashtable.set(lock_name, Date.now());
+
+		return true;    // lock is acquired
+	},
+
+	release_lock: function (method) {
+		wot_hashtable.remove(method + this.LOCK_POSTFIX); // clear the lock
 	}
 };
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/wot.git



More information about the Pkg-mozext-commits mailing list