Skip to content

Commit 0903df1

Browse files
committed
Restructured initial 'if' statement for clarity
This was changed due to feedback by @benschwarz because the '!'(not) operator was easily missed and this method leaves less ambiguity to the intent of this file.
1 parent dd565b2 commit 0903df1

File tree

1 file changed

+60
-58
lines changed

1 file changed

+60
-58
lines changed

matchMedia.addListener.js

+60-58
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,75 @@
11
/*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */
22
(function(){
3-
// monkeypatch unsupported addListener/removeListener
4-
if (window.matchMedia && !window.matchMedia('all').addListener){
5-
var localMatchMedia = window.matchMedia,
6-
hasMediaQueries = localMatchMedia('only all').matches,
7-
isListening = false,
8-
timeoutID = 0, // setTimeout for debouncing 'handleChange'
9-
queries = [], // Contains each 'mql' and associated 'listeners' if 'addListener' is used
10-
handleChange = function(evt) {
11-
// Debounce
12-
clearTimeout(timeoutID);
3+
// Bail out for browsers that have addListener support
4+
if (window.matchMedia && window.matchMedia('all').addListener) {
5+
return false;
6+
}
7+
8+
var localMatchMedia = window.matchMedia,
9+
hasMediaQueries = localMatchMedia('only all').matches,
10+
isListening = false,
11+
timeoutID = 0, // setTimeout for debouncing 'handleChange'
12+
queries = [], // Contains each 'mql' and associated 'listeners' if 'addListener' is used
13+
handleChange = function(evt) {
14+
// Debounce
15+
clearTimeout(timeoutID);
1316

14-
timeoutID = setTimeout(function() {
15-
for (var i = 0, il = queries.length; i < il; i++) {
16-
var mql = queries[i].mql,
17-
listeners = queries[i].listeners || [],
18-
matches = localMatchMedia(mql.media).matches;
17+
timeoutID = setTimeout(function() {
18+
for (var i = 0, il = queries.length; i < il; i++) {
19+
var mql = queries[i].mql,
20+
listeners = queries[i].listeners || [],
21+
matches = localMatchMedia(mql.media).matches;
1922

20-
// Update mql.matches value and call listeners
21-
// Fire listeners only if transitioning to or from matched state
22-
if (matches !== mql.matches) {
23-
mql.matches = matches;
23+
// Update mql.matches value and call listeners
24+
// Fire listeners only if transitioning to or from matched state
25+
if (matches !== mql.matches) {
26+
mql.matches = matches;
2427

25-
for (var j = 0, jl = listeners.length; j < jl; j++) {
26-
listeners[j].call(window, mql);
27-
}
28+
for (var j = 0, jl = listeners.length; j < jl; j++) {
29+
listeners[j].call(window, mql);
2830
}
2931
}
30-
}, 30);
31-
};
32+
}
33+
}, 30);
34+
};
3235

33-
window.matchMedia = function(media) {
34-
var mql = localMatchMedia(media),
35-
listeners = [],
36-
index = 0;
36+
window.matchMedia = function(media) {
37+
var mql = localMatchMedia(media),
38+
listeners = [],
39+
index = 0;
3740

38-
mql.addListener = function(listener) {
39-
// Changes would not occur to css media type so return now (Affects IE <= 8)
40-
if (!hasMediaQueries) {
41-
return;
42-
}
41+
mql.addListener = function(listener) {
42+
// Changes would not occur to css media type so return now (Affects IE <= 8)
43+
if (!hasMediaQueries) {
44+
return;
45+
}
4346

44-
// Set up 'resize' listener for browsers that support CSS3 media queries (Not for IE <= 8)
45-
// There should only ever be 1 resize listener running for performance
46-
if (!isListening) {
47-
isListening = true;
48-
window.addEventListener('resize', handleChange, true);
49-
}
47+
// Set up 'resize' listener for browsers that support CSS3 media queries (Not for IE <= 8)
48+
// There should only ever be 1 resize listener running for performance
49+
if (!isListening) {
50+
isListening = true;
51+
window.addEventListener('resize', handleChange, true);
52+
}
5053

51-
// Push object only if it has not been pushed already
52-
if (index === 0) {
53-
index = queries.push({
54-
mql : mql,
55-
listeners : listeners
56-
});
57-
}
54+
// Push object only if it has not been pushed already
55+
if (index === 0) {
56+
index = queries.push({
57+
mql : mql,
58+
listeners : listeners
59+
});
60+
}
5861

59-
listeners.push(listener);
60-
};
62+
listeners.push(listener);
63+
};
6164

62-
mql.removeListener = function(listener) {
63-
for (var i = 0, il = listeners.length; i < il; i++){
64-
if (listeners[i] === listener){
65-
listeners.splice(i, 1);
66-
}
65+
mql.removeListener = function(listener) {
66+
for (var i = 0, il = listeners.length; i < il; i++){
67+
if (listeners[i] === listener){
68+
listeners.splice(i, 1);
6769
}
68-
};
69-
70-
return mql;
70+
}
7171
};
72-
}
73-
}());
72+
73+
return mql;
74+
};
75+
}());

0 commit comments

Comments
 (0)