changeset 24:e32ac29df320

Don’t timeout two times.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 16 Jul 2011 16:10:54 +0200
parents d63a2784564d
children ba0edeb9ba05
files avatar.js
diffstat 1 files changed, 11 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/avatar.js
+++ b/avatar.js
@@ -109,7 +109,10 @@ var makeError = function(response) {
 }
 
 if (config.useGravatar)
-	var noAvatar = function(res, to, message) {
+	var noAvatar = function(res, to, message, id) {
+		if (id && sent[id])
+			clearTimeout(sent[id].timeout);
+
 		var options = {
 			host: 'gravatar.com',
 			port: 80,
@@ -152,7 +155,10 @@ if (config.useGravatar)
 		return r.end();
 	};
 else
-	var noAvatar = function(res, to, message) {
+	var noAvatar = function(res, to, message, id) {
+		if (id && sent[id])
+			clearTimeout(sent[id].timeout);
+
 		util.log('No XMPP avatar for ' + to + '.');
 		return svgError(res, message);
 	};
@@ -170,7 +176,8 @@ function onIq(stanza) {
 	if (!sent[id])
 		return conn.send(makeError(response));
 
-	var res = sent[id];
+	clearTimeout(sent[id].timeout);
+	var res = sent[id].res;
 	delete sent[id];
 
 	if (type == 'error') {
@@ -245,9 +252,8 @@ var getVCard = function(jid, res) {
 	.up();
 
 	conn.send(toSend);
-	setTimeout(noAvatar, config.timeout, res, jid, 'Error: XMPP timeout.');
 
-	sent[id] = res;
+	sent[id] = {res: res, timeout: setTimeout(noAvatar, config.timeout, res, jid, 'Error: XMPP timeout.', id)};
 }
 
 var showImage = function(jid, res) {