Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/icon.js

Issue 29334778: Issue 3532 - Improved preformance of icon animation and fixed a memory leak (Closed)
Patch Set: Added missing newline Created Jan. 28, 2016, 10:02 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module icon */ 18 /** @module icon */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 22 const frameOpacities = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
23 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 23 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
24 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]; 24 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0];
25 const numberOfFrames = frameOpacities.length; 25 const numberOfFrames = frameOpacities.length;
26 const safariPlatform = require("info").platform == "safari"; 26 const safariPlatform = require("info").platform == "safari";
27 27
28 let frameInterval = null; 28 let frameInterval = null;
29 let animationInterval = null; 29 let animationInterval = null;
30 let onActivated = null;
30 let whitelistedState = new ext.PageMap(); 31 let whitelistedState = new ext.PageMap();
31 32
32 function loadImage(url) 33 function loadImage(url)
33 { 34 {
34 return new Promise((resolve, reject) => 35 return new Promise((resolve, reject) =>
35 { 36 {
36 let image = new Image(); 37 let image = new Image();
37 image.src = url; 38 image.src = url;
38 image.addEventListener("load", () => 39 image.addEventListener("load", () =>
39 { 40 {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 } 113 }
113 114
114 return frames; 115 return frames;
115 }); 116 });
116 } 117 }
117 118
118 function runAnimation(notificationType) 119 function runAnimation(notificationType)
119 { 120 {
120 function playAnimation(frames) 121 function playAnimation(frames)
121 { 122 {
122 let animationStep = 0;
123 ext.pages.query({active: true}, pages => 123 ext.pages.query({active: true}, pages =>
124 { 124 {
125 function appendActivePage(page) 125 let animationStep = 0;
126 let opacity = 0;
127
128 onActivated = page =>
126 { 129 {
127 pages.push(page); 130 pages.push(page);
128 } 131 setIcon(page, notificationType, opacity, frames);
129 ext.pages.onActivated.addListener(appendActivePage); 132 };
133 ext.pages.onActivated.addListener(onActivated);
130 134
131 frameInterval = setInterval(() => 135 frameInterval = setInterval(() =>
132 { 136 {
133 let opacity = frameOpacities[animationStep++]; 137 let oldOpacity = opacity;
134 for (let page of pages) 138 opacity = frameOpacities[animationStep++];
139
140 if (opacity != oldOpacity)
135 { 141 {
136 if (whitelistedState.has(page)) 142 for (let page of pages)
137 setIcon(page, notificationType, opacity, frames); 143 {
138 }; 144 if (whitelistedState.has(page))
145 setIcon(page, notificationType, opacity, frames);
146 }
147 }
139 148
140 if (animationStep > numberOfFrames) 149 if (animationStep > numberOfFrames)
141 { 150 {
142 animationStep = 0;
143 clearInterval(frameInterval); 151 clearInterval(frameInterval);
144 frameInterval = null; 152 ext.pages.onActivated.removeListener(onActivated);
145 ext.pages.onActivated.removeListener(appendActivePage); 153 frameInterval = onActivated = null;
146 } 154 }
147 }, 100); 155 }, 100);
148 }); 156 });
149 } 157 }
150 158
151 renderFrames(notificationType).then(frames => 159 renderFrames(notificationType).then(frames =>
152 { 160 {
153 playAnimation(frames); 161 playAnimation(frames);
154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000); 162 animationInterval = setInterval(() => { playAnimation(frames); }, 10000);
155 }); 163 });
(...skipping 16 matching lines...) Expand all
172 let stopIconAnimation = 180 let stopIconAnimation =
173 /** 181 /**
174 * Stops to animate the browser action icon. 182 * Stops to animate the browser action icon.
175 */ 183 */
176 exports.stopIconAnimation = function() 184 exports.stopIconAnimation = function()
177 { 185 {
178 if (frameInterval != null) 186 if (frameInterval != null)
179 clearInterval(frameInterval); 187 clearInterval(frameInterval);
180 if (animationInterval != null) 188 if (animationInterval != null)
181 clearInterval(animationInterval); 189 clearInterval(animationInterval);
182 frameInterval = animationInterval = null; 190 if (onActivated)
191 ext.pages.onActivated.removeListener(onActivated);
192 frameInterval = animationInterval = onActivated = null;
183 }; 193 };
184 194
185 /** 195 /**
186 * Starts to animate the browser action icon to indicate a pending notifcation. 196 * Starts to animate the browser action icon to indicate a pending notifcation.
187 * 197 *
188 * @param {string} type The notification type (i.e: "information" or "critical" ) 198 * @param {string} type The notification type (i.e: "information" or "critical" )
189 */ 199 */
190 exports.startIconAnimation = function(type) 200 exports.startIconAnimation = function(type)
191 { 201 {
192 stopIconAnimation(); 202 stopIconAnimation();
193 runAnimation(type); 203 runAnimation(type);
194 }; 204 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld