Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit 79ed661

Browse files
committed
heading toward v1.0: code review
1 parent 1c1b2a9 commit 79ed661

File tree

2 files changed

+91
-82
lines changed

2 files changed

+91
-82
lines changed

js/async.js

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@
2727

2828
HTTPSB.asyncJobs = (function() {
2929

30-
var timeResolution = 200;
31-
var jobs = {};
32-
var jobCount = 0;
33-
var jobJunkyard = [];
34-
var timerId = null;
35-
var timerWhen = Number.MAX_VALUE;
30+
var processJobs = function() {
31+
asyncJobManager.process();
32+
};
3633

3734
var AsyncJobEntry = function(name) {
3835
this.name = name;
@@ -48,121 +45,129 @@ AsyncJobEntry.prototype.destroy = function() {
4845
this.callback = null;
4946
};
5047

51-
var restartTimer = function() {
48+
var AsyncJobManager = function() {
49+
this.timeResolution = 200;
50+
this.jobs = {};
51+
this.jobCount = 0;
52+
this.jobJunkyard = [];
53+
this.timerId = null;
54+
this.timerWhen = Number.MAX_VALUE;
55+
};
56+
57+
AsyncJobManager.prototype.restartTimer = function() {
5258
var when = Number.MAX_VALUE;
5359
var job;
54-
for ( var jobName in jobs ) {
55-
if ( jobs.hasOwnProperty(jobName) === false ) {
60+
for ( var jobName in this.jobs ) {
61+
if ( this.jobs.hasOwnProperty(jobName) === false ) {
5662
continue;
5763
}
58-
job = jobs[jobName];
64+
job = this.jobs[jobName];
5965
if ( job.when < when ) {
6066
when = job.when;
6167
}
6268
}
6369
// Quantize time value
64-
when = Math.floor((when + timeResolution - 1) / timeResolution) * timeResolution;
70+
when = Math.floor((when + this.timeResolution - 1) / this.timeResolution) * this.timeResolution;
6571

66-
if ( when < timerWhen ) {
67-
clearTimeout(timerId);
68-
timerWhen = when;
69-
timerId = setTimeout(processJobs, Math.max(when - Date.now(), 10));
72+
if ( when < this.timerWhen ) {
73+
clearTimeout(this.timerId);
74+
this.timerWhen = when;
75+
this.timerId = setTimeout(processJobs, Math.max(when - Date.now(), 10));
7076
}
7177
};
7278

73-
var addJob = function(name, data, callback, delay, recurrent) {
74-
var job = jobs[name];
79+
AsyncJobManager.prototype.add = function(name, data, callback, delay, recurrent) {
80+
var job = this.jobs[name];
7581
if ( !job ) {
76-
job = jobJunkyard.pop();
82+
job = this.jobJunkyard.pop();
7783
if ( !job ) {
7884
job = new AsyncJobEntry(name);
7985
} else {
8086
job.name = name;
8187
}
82-
jobs[name] = job;
83-
jobCount++;
88+
this.jobs[name] = job;
89+
this.jobCount++;
8490
}
8591
job.data = data;
8692
job.callback = callback;
8793
job.when = Date.now() + delay;
8894
job.period = recurrent ? delay : 0;
89-
restartTimer();
95+
this.restartTimer();
9096
};
9197

92-
var processJobs = function() {
93-
timerId = null;
94-
timerWhen = Number.MAX_VALUE;
98+
AsyncJobManager.prototype.process = function() {
99+
this.timerId = null;
100+
this.timerWhen = Number.MAX_VALUE;
95101
var now = Date.now();
96102
var job;
97-
for ( var jobName in jobs ) {
98-
if ( jobs.hasOwnProperty(jobName) === false ) {
103+
for ( var jobName in this.jobs ) {
104+
if ( this.jobs.hasOwnProperty(jobName) === false ) {
99105
continue;
100106
}
101-
job = jobs[jobName];
107+
job = this.jobs[jobName];
102108
if ( job.when > now ) {
103109
continue;
104110
}
105111
job.callback(job.data);
106112
if ( job.period ) {
107113
job.when = now + job.period;
108114
} else {
109-
delete jobs[jobName];
115+
delete this.jobs[jobName];
110116
job.destroy();
111-
jobCount--;
112-
jobJunkyard.push(job);
117+
this.jobCount--;
118+
this.jobJunkyard.push(job);
113119
}
114120
}
115-
restartTimer();
121+
this.restartTimer();
116122
};
117123

118-
// Publish async jobs module
119-
return {
120-
add: addJob
121-
};
124+
// Only one instance
125+
var asyncJobManager = new AsyncJobManager();
126+
127+
// Publish
128+
return asyncJobManager;
122129

123130
})();
124131

125132
/******************************************************************************/
126133

127134
// Update visual of extension icon.
128-
// A time out is used to coalesce adjacents requests to update badge.
135+
// A time out is used to coalesce adjacent requests to update badge.
129136

130-
function updateBadgeCallback(pageUrl) {
131-
var httpsb = HTTPSB;
132-
if ( pageUrl === httpsb.behindTheSceneURL ) {
133-
return;
134-
}
135-
var tabId = httpsb.tabIdFromPageUrl(pageUrl);
136-
if ( !tabId ) {
137-
return;
138-
}
139-
var pageStats = httpsb.pageStatsFromTabId(tabId);
140-
if ( pageStats ) {
141-
pageStats.updateBadge(tabId);
142-
} else {
143-
chrome.browserAction.setIcon({ tabId: tabId, path: 'img/browsericons/icon19.png' });
144-
chrome.browserAction.setBadgeText({ tabId: tabId, text: '?' });
145-
}
146-
}
137+
HTTPSB.updateBadge = function(pageUrl) {
138+
var updateBadgeCallback = function(pageUrl) {
139+
var httpsb = HTTPSB;
140+
if ( pageUrl === httpsb.behindTheSceneURL ) {
141+
return;
142+
}
143+
var tabId = httpsb.tabIdFromPageUrl(pageUrl);
144+
if ( !tabId ) {
145+
return;
146+
}
147+
var pageStats = httpsb.pageStatsFromTabId(tabId);
148+
if ( pageStats ) {
149+
pageStats.updateBadge(tabId);
150+
} else {
151+
chrome.browserAction.setIcon({ tabId: tabId, path: 'img/browsericons/icon19.png' });
152+
chrome.browserAction.setBadgeText({ tabId: tabId, text: '?' });
153+
}
154+
};
147155

148-
function updateBadge(pageUrl) {
149-
HTTPSB.asyncJobs.add('updateBadge ' + pageUrl, pageUrl, updateBadgeCallback, 250);
150-
}
156+
this.asyncJobs.add('updateBadge ' + pageUrl, pageUrl, updateBadgeCallback, 250);
157+
};
151158

152159
/******************************************************************************/
153160

154161
// Notify whoever care that whitelist/blacklist have changed (they need to
155162
// refresh their matrix).
156163

157-
function permissionChangedCallback() {
158-
chrome.runtime.sendMessage({
159-
'what': 'permissionsChanged'
160-
});
161-
}
164+
HTTPSB.permissionsChanged = function() {
165+
var permissionChangedCallback = function() {
166+
chrome.runtime.sendMessage({ 'what': 'permissionsChanged' });
167+
};
162168

163-
function permissionsChanged() {
164-
HTTPSB.asyncJobs.add('permissionsChanged', null, permissionChangedCallback, 250);
165-
}
169+
this.asyncJobs.add('permissionsChanged', null, permissionChangedCallback, 250);
170+
};
166171

167172
/******************************************************************************/
168173

@@ -228,21 +233,22 @@ function gotoExtensionURL(url) {
228233
// Notify whoever care that url stats have changed (they need to
229234
// rebuild their matrix).
230235

231-
function urlStatsChangedCallback(pageUrl) {
232-
// rhill 2013-11-17: No point in sending this message if the popup menu
233-
// does not exist. I suspect this could be related to
234-
// https://github.com/gorhill/httpswitchboard/issues/58
235-
if ( HTTPSB.port ) {
236-
HTTPSB.port.postMessage({
237-
what: 'urlStatsChanged',
238-
pageURL: pageUrl
239-
});
240-
}
241-
}
236+
HTTPSB.urlStatsChanged = function(pageUrl) {
237+
var httpsb = this;
238+
var urlStatsChangedCallback = function(pageUrl) {
239+
// rhill 2013-11-17: No point in sending this message if the popup menu
240+
// does not exist. I suspect this could be related to
241+
// https://github.com/gorhill/httpswitchboard/issues/58
242+
if ( httpsb.port ) {
243+
httpsb.port.postMessage({
244+
what: 'urlStatsChanged',
245+
pageURL: pageUrl
246+
});
247+
}
248+
};
242249

243-
function urlStatsChanged(pageUrl) {
244-
HTTPSB.asyncJobs.add('urlStatsChanged ' + pageUrl, pageUrl, urlStatsChangedCallback, 1000);
245-
}
250+
this.asyncJobs.add('urlStatsChanged ' + pageUrl, pageUrl, urlStatsChangedCallback, 1000);
251+
};
246252

247253
/******************************************************************************/
248254

js/tab.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
Home: https://github.com/gorhill/httpswitchboard
2020
*/
2121

22+
/* global chrome, HTTPSB */
23+
2224
/******************************************************************************/
2325

2426
PageStatsEntry.junkyard = [];
@@ -83,7 +85,8 @@ PageStatsEntry.prototype.recordRequest = function(type, url, block, reason) {
8385
// rhill 2013-10-26: This needs to be called even if the request is
8486
// already logged, since the request stats are cached for a while after
8587
// the page is no longer visible in a browser tab.
86-
updateBadge(this.pageUrl);
88+
var httpsb = HTTPSB;
89+
httpsb.updateBadge(this.pageUrl);
8790

8891
// Count blocked/allowed requests
8992
this.requestStats.record(type, block);
@@ -100,7 +103,7 @@ PageStatsEntry.prototype.recordRequest = function(type, url, block, reason) {
100103
return;
101104
}
102105

103-
var hostname = HTTPSB.URI.hostnameFromURI(url);
106+
var hostname = httpsb.URI.hostnameFromURI(url);
104107

105108
// https://github.com/gorhill/httpswitchboard/issues/181
106109
if ( type === 'script' && hostname !== this.pageHostname ) {
@@ -122,7 +125,7 @@ PageStatsEntry.prototype.recordRequest = function(type, url, block, reason) {
122125
this.distinctRequestCount++;
123126
this.domains[hostname] = true;
124127

125-
urlStatsChanged(this.pageUrl);
128+
httpsb.urlStatsChanged(this.pageUrl);
126129
// console.debug("HTTP Switchboard > PageStatsEntry.recordRequest() > %o: %s @ %s", this, type, url);
127130
};
128131

@@ -364,7 +367,7 @@ HTTPSB.smartReloadTabs = function(which, tabId) {
364367
};
365368

366369
this.asyncJobs.add('smartReloadTabs', null, getTabs, 500);
367-
}
370+
};
368371

369372
/******************************************************************************/
370373

0 commit comments

Comments
 (0)