Last active
December 12, 2015 09:59
-
-
Save slightlyoff/4755944 to your computer and use it in GitHub Desktop.
connectionchange event properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
}); |
- I'd cleanup the ethernet examples to just stick with single unit (mbps)
- Wi-Fi: lgtm
- Instead of listing out the technology names, just stick to generations.. Each generation has different variants and implementations under 3GPP / 3GPP2. The list below should cover them all.
- Re: WiMax.. Great question. How about bluetooth, and anyone else? I don't know enough about WiMax to say if it deserves subclasses under it, but I'd just propose to keep it as another top level entry: we're grouping technology by physical layer, and WiMax and Bluetooth are fundamentally different from all others.
ethernet: [
e("10"),
e("100"),
e("1000"),
e("10000"),
e("100000")
],
wifi: [
w("a"),
w("b"),
w("g"),
w("n"),
w("ac"), // draft
w("ad") // future
],
cellular: [
c("2"),
c("2.5"), // GPRS
c("2.75"), // EDGE
c("3"), // UMTS, CDMA2000 1X
c("3.5"), // HSPA, EV-DO
c("3.75"), // HSPA+, EV-DO Advanced
c("3.9"), // LTE
c("4G") // LTE Advanced
],
wimax: [],
bluetooth: [1, 1.1, 1.2, 2.0, 2.1, 3.0]
@jakearchibald: yeah, that's the idea. Making them globally unique felt like an invitation to the sort of meaingless and misleading cross-class comparisons we talked so much about.
@igrigorik: that sounds good, although I was hoping for descriptive names...perhaps as an additional field? I love the cleanup cellular
. Will iterate and incorporate.
@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
So
classId
is unique to themedia
but not unique acrossmedia
s?