From 5f48dd0f0e55acc99cd0c984086e5fb163024cfb Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 27 Jul 2012 23:34:19 -0400 Subject: [PATCH] image subscriptions was not always properly unsubscribing; fixed --- public/javascripts/wardrobe.js | 45 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/public/javascripts/wardrobe.js b/public/javascripts/wardrobe.js index 0fab302b..fd868482 100644 --- a/public/javascripts/wardrobe.js +++ b/public/javascripts/wardrobe.js @@ -1126,7 +1126,8 @@ function Wardrobe() { log("Outfit image still enqueued; will try again soon", outfit); setTimeout(function () { checkSubscription(outfit) }, DELAY); } else { - controller.unsubscribe(outfit); + // Unsubscribe everyone from this outfit and fire ready events + delete outfitSubscriptionTotals[outfit.id]; controller.events.trigger('imageReady', outfit); } } else { @@ -1137,31 +1138,31 @@ function Wardrobe() { } this.subscribe = function (outfit) { - if(outfit.id in outfitSubscriptionTotals) { - // The subscription is already running. Just mark that one more - // consumer is interested in it, and they'll all get a response soon. - outfitSubscriptionTotals[outfit.id] += 1; - } else { - // This is a new subscription! - outfitSubscriptionTotals[outfit.id] = 1; - - if(outfit.image_enqueued) { - // If the image is enqueued, trigger that event and start checking. - controller.events.trigger('imageEnqueued', outfit); - checkSubscription(outfit); + if(outfit.image_enqueued) { + if(outfit.id in outfitSubscriptionTotals) { + // The subscription is already running. Just mark that one more + // consumer is interested in it, and they'll all get a response soon. + outfitSubscriptionTotals[outfit.id] += 1; } else { - // Otherwise, never bother checking: skip straight to the ready phase. - // Give it an instant timeout so that we're sure the consumer is ready - // for the event. (It can be tricky when the consumer assigns this - // return value somewhere to know if it cares about the event, so the - // event can't fire before the return.) - setTimeout(function () { - controller.events.trigger('imageReady', outfit) - }, 0); + // This is a new subscription! Let's start checking it. + outfitSubscriptionTotals[outfit.id] = 1; + checkSubscription(outfit); } - return outfit; + // Regardless, trigger the enqueued event for the new consumer's sake. + controller.events.trigger('imageEnqueued', outfit); + } else { + // Otherwise, never bother checking: skip straight to the ready phase. + // Give it an instant timeout so that we're sure the consumer is ready + // for the event. (It can be tricky when the consumer assigns this + // return value somewhere to know if it cares about the event, so the + // event can't fire before the return.) + setTimeout(function () { + controller.events.trigger('imageReady', outfit) + }, 0); } + + return outfit; } this.unsubscribe = function (outfit) {