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: Created Jan. 27, 2016, 6:57 p.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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 frames["" + opacity + whitelisted] = imageData; 111 frames["" + opacity + whitelisted] = imageData;
111 } 112 }
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)
Sebastian Noack 2016/01/27 19:09:20 If you don't mind, I'd also like to move that func
kzar 2016/01/28 09:51:04 Sure I don't mind if you'd like to do that. (The f
Sebastian Noack 2016/01/28 10:14:21 Done.
121 { 122 {
122 let animationStep = 0;
Sebastian Noack 2016/01/27 19:09:20 Unrelated: We always define variables where we nee
kzar 2016/01/28 09:51:03 Acknowledged.
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 { 126 let opacity = 0;
127
128 onActivated = page => {
kzar 2016/01/28 09:51:04 Arrow functions are for anonymous functions, if i
Sebastian Noack 2016/01/28 10:14:21 We have to assign to the global variable onActivat
kzar 2016/01/28 10:19:57 Acknowledged.
127 pages.push(page); 129 pages.push(page);
128 } 130 setIcon(page, notificationType, opacity, frames);
129 ext.pages.onActivated.addListener(appendActivePage); 131 };
132 ext.pages.onActivated.addListener(onActivated);
130 133
131 frameInterval = setInterval(() => 134 frameInterval = setInterval(() =>
132 { 135 {
133 let opacity = frameOpacities[animationStep++]; 136 let oldOpacity = opacity;
134 for (let page of pages) 137 opacity = frameOpacities[animationStep++];
138
139 if (opacity != oldOpacity)
Sebastian Noack 2016/01/27 19:09:20 On my notebook (X230 with i7), I observed signific
kzar 2016/01/28 09:51:04 Of course, good idea.
135 { 140 {
136 if (whitelistedState.has(page)) 141 for (let page of pages)
137 setIcon(page, notificationType, opacity, frames); 142 {
138 }; 143 if (whitelistedState.has(page))
Sebastian Noack 2016/01/27 19:09:20 Unrelated: Removed redundant semicolon.
kzar 2016/01/28 09:51:04 Acknowledged.
144 setIcon(page, notificationType, opacity, frames);
145 }
146 }
139 147
140 if (animationStep > numberOfFrames) 148 if (animationStep > numberOfFrames)
141 { 149 {
142 animationStep = 0;
Sebastian Noack 2016/01/27 19:09:20 Unrelated: Resetting that value is unneeded, as th
kzar 2016/01/28 09:51:04 Acknowledged.
143 clearInterval(frameInterval); 150 clearInterval(frameInterval);
144 frameInterval = null; 151 ext.pages.onActivated.removeListener(onActivated);
145 ext.pages.onActivated.removeListener(appendActivePage); 152 frameInterval = onActivated = null;
146 } 153 }
147 }, 100); 154 }, 100);
148 }); 155 });
149 } 156 }
150 157
151 renderFrames(notificationType).then(frames => 158 renderFrames(notificationType).then(frames =>
152 { 159 {
153 playAnimation(frames); 160 playAnimation(frames);
154 animationInterval = setInterval(() => { playAnimation(frames); }, 10000); 161 animationInterval = setInterval(() => { playAnimation(frames); }, 10000);
155 }); 162 });
(...skipping 16 matching lines...) Expand all
172 let stopIconAnimation = 179 let stopIconAnimation =
173 /** 180 /**
174 * Stops to animate the browser action icon. 181 * Stops to animate the browser action icon.
175 */ 182 */
176 exports.stopIconAnimation = function() 183 exports.stopIconAnimation = function()
177 { 184 {
178 if (frameInterval != null) 185 if (frameInterval != null)
179 clearInterval(frameInterval); 186 clearInterval(frameInterval);
180 if (animationInterval != null) 187 if (animationInterval != null)
181 clearInterval(animationInterval); 188 clearInterval(animationInterval);
182 frameInterval = animationInterval = null; 189 if (onActivated)
Sebastian Noack 2016/01/27 19:09:20 Yeah, that was the memory leak mentioned in the is
kzar 2016/01/28 09:51:04 Dang, good point. (Note, my stopAnimation closure
Sebastian Noack 2016/01/28 10:14:21 (Not really, it doesn't matter where you do the cl
190 ext.pages.onActivated.removeListener(onActivated);
191 frameInterval = animationInterval = onActivated = null;
183 }; 192 };
184 193
185 /** 194 /**
186 * Starts to animate the browser action icon to indicate a pending notifcation. 195 * Starts to animate the browser action icon to indicate a pending notifcation.
187 * 196 *
188 * @param {string} type The notification type (i.e: "information" or "critical" ) 197 * @param {string} type The notification type (i.e: "information" or "critical" )
189 */ 198 */
190 exports.startIconAnimation = function(type) 199 exports.startIconAnimation = function(type)
191 { 200 {
192 stopIconAnimation(); 201 stopIconAnimation();
193 runAnimation(type); 202 runAnimation(type);
194 }; 203 };
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