| Index: lib/icon.js |
| =================================================================== |
| --- a/lib/icon.js |
| +++ b/lib/icon.js |
| @@ -25,9 +25,9 @@ |
| const numberOfFrames = frameOpacities.length; |
| const safariPlatform = require("info").platform == "safari"; |
| -let frameInterval = null; |
| -let animationInterval = null; |
| -let onActivated = null; |
| +let stopRequested = false; |
| +let canUpdateIcon = true; |
| +let notRunning = Promise.resolve(); |
| let whitelistedState = new ext.PageMap(); |
| function loadImage(url) |
| @@ -123,14 +123,15 @@ |
| let animationStep = 0; |
| let opacity = 0; |
| - onActivated = page => |
| + let onActivated = page => |
| { |
| pages.push(page); |
| setIcon(page, notificationType, opacity, frames); |
| }; |
| ext.pages.onActivated.addListener(onActivated); |
| - frameInterval = setInterval(() => |
| + canUpdateIcon = false; |
| + let frameInterval = setInterval(() => |
| { |
| let oldOpacity = opacity; |
| opacity = frameOpacities[animationStep++]; |
| @@ -148,24 +149,12 @@ |
| { |
| clearInterval(frameInterval); |
| ext.pages.onActivated.removeListener(onActivated); |
| - frameInterval = onActivated = null; |
| + canUpdateIcon = true; |
| } |
| }, 100); |
| }); |
| } |
| -function runAnimationLoop(notificationType) |
| -{ |
| - renderFrames(notificationType).then(frames => |
| - { |
| - animateIcon(notificationType, frames); |
| - animationInterval = setInterval(() => |
| - { |
| - animateIcon(notificationType, frames); |
| - }, 10000); |
| - }); |
| -} |
| - |
| /** |
| * Set the browser action icon for the given page, indicating whether |
| * adblocking is active there, and considering the icon animation. |
| @@ -176,23 +165,23 @@ |
| exports.updateIcon = function(page, whitelisted) |
| { |
| whitelistedState.set(page, whitelisted); |
| - if (frameInterval == null) |
| + if (canUpdateIcon) |
| setIcon(page); |
| }; |
| let stopIconAnimation = |
| /** |
| * Stops to animate the browser action icon. |
| + * |
| + * @return {Promise} |
| */ |
| exports.stopIconAnimation = function() |
| { |
| - if (frameInterval != null) |
| - clearInterval(frameInterval); |
| - if (animationInterval != null) |
| - clearInterval(animationInterval); |
| - if (onActivated) |
| - ext.pages.onActivated.removeListener(onActivated); |
| - frameInterval = animationInterval = onActivated = null; |
| + stopRequested = true; |
| + return notRunning.then(() => |
| + { |
| + stopRequested = false; |
| + }); |
| }; |
| /** |
| @@ -202,6 +191,30 @@ |
| */ |
| exports.startIconAnimation = function(type) |
| { |
| - stopIconAnimation(); |
| - runAnimationLoop(type); |
| + notRunning = new Promise(resolve => |
| + { |
| + Promise.all([renderFrames(type), stopIconAnimation()]).then(results => |
| + { |
| + if (stopRequested) |
| + { |
| + resolve(); |
| + return; |
| + } |
| + |
| + let frames = results[0]; |
| + animateIcon(type, frames); |
| + |
| + let animationInterval = setInterval(() => |
| + { |
| + if (stopRequested) |
| + { |
| + clearInterval(animationInterval); |
| + resolve(); |
| + return; |
| + } |
| + |
| + animateIcon(type, frames); |
| + }, 10000); |
| + }); |
| + }); |
| }; |