Skip to content

Instantly share code, notes, and snippets.

@slightlyoff
Last active December 12, 2015 09:59
Show Gist options
  • Save slightlyoff/4755944 to your computer and use it in GitHub Desktop.
Save slightlyoff/4755944 to your computer and use it in GitHub Desktop.
connectionchange event properties
class ConnectionInfo {
constructor(media="unknown",
className="unknown",
classId=0) {
this.media = media;
this.className = className;
this.classId = classId;
}
}
var typeGenerator = function(type) {
var id = 0;
return function(className) {
return new ConnectionInfo(type, className, ++id);
};
};
var e = typeGenerator("ethernet");
var w = typeGenerator("wifi");
var c = typeGenerator("cellular");
// Some connection classes, grouped by type only for readability
// These are all rough, and the cellular group in particular
// omits tons of details. I'm not sure if I should be bothered.
var connectionClasses = {
ethernet: [
e("10 Mbit"),
e("100 Mbit"),
e("1 Gbit"),
e("10 Gbit"),
e("100 Gbit")
],
wifi: [
w("a"),
w("b"),
w("g"),
w("n"),
w("ac"), // draft
w("ad") // future
],
cellular: [
c("2G"),
c("GPRS"),
c("EDGE"),
c("3G"),
c("HSPA"),
c("3GPP-LTE"),
c("4G"),
// FIXME(where does wimax go?)
]
};
// Example usage:
// We add a "connectionInfo" property to navigator
if(navigator.connectionInfo.media == "wifi") {
// ...
}
var fetchHighResCatVideos = true;
window.addEvenListener("connectionchange", function(e) {
// Use navigator.connectionInfo to determine what to do:
var ci = navigator.connectionInfo;
switch (ci.media) {
case "cellular":
if (ci.classId < 5) { // worse than 4G
fetchHighResCatVideos = false;
break;
}
default:
fetchHighResCatVideos = true;
break;
}
// Carry on fetching cat videos here.
});
@igrigorik
Copy link

@slightlyoff descriptive names would be nice, but it gets a bit tricky.. we would have to standardize those as well. I guess between 3GPP and 3GPP2 there are only so many.. But effectively, you're asking for a subclass of the generation. FWIW, within the generation, there is some variability in technical parameters, but mostly on par, so this is more of a nice to have (I think..).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment