Last active
October 31, 2018 19:54
-
-
Save ThomasBurleson/7576083 to your computer and use it in GitHub Desktop.
Demonstration of refactor of DownloadRatioRules.js > transform deep nesting of promise chains to an easily-maintained, flattened, sequential chain.
This file contains 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
if (downloadRatio < 1.0) { | |
self.debug.log("Download ratio is poor."); | |
if (current > 0) { | |
self.debug.log("We are not at the lowest bitrate, so switch down."); | |
self.manifestExt.getRepresentationFor(current - 1, data).then( | |
function (representation1) { | |
self.manifestExt.getBandwidth(representation1).then( | |
function (oneDownBandwidth) { | |
self.manifestExt.getRepresentationFor(current, data).then( | |
function (representation2) { | |
self.manifestExt.getBandwidth(representation2).then( | |
function (currentBandwidth) { | |
switchRatio = oneDownBandwidth / currentBandwidth; | |
self.debug.log("Switch ratio: " + switchRatio); | |
if (downloadRatio < switchRatio) { | |
self.debug.log("Things must be going pretty bad, switch all the way down."); | |
deferred.resolve(new MediaPlayer.rules.SwitchRequest(0)); | |
} else { | |
self.debug.log("Things could be better, so just switch down one index."); | |
deferred.resolve(new MediaPlayer.rules.SwitchRequest(current - 1)); | |
} | |
} | |
); | |
} | |
); | |
} | |
); | |
} | |
); | |
} else { | |
self.debug.log("We are at the lowest bitrate and cannot switch down, use current."); | |
deferred.resolve(new MediaPlayer.rules.SwitchRequest(current)); | |
} | |
} else { | |
self.debug.log("Download ratio is good."); | |
self.manifestExt.getRepresentationCount(data).then( | |
function (max) { | |
max -= 1; // 0 based | |
if (current < max) { | |
self.debug.log("We are not at the highest bitrate, so switch up."); | |
self.manifestExt.getRepresentationFor(current + 1, data).then( | |
function (representation1) { | |
self.manifestExt.getBandwidth(representation1).then( | |
function (oneUpBandwidth) { | |
self.manifestExt.getRepresentationFor(current, data).then( | |
function (representation2) { | |
self.manifestExt.getBandwidth(representation2).then( | |
function (currentBandwidth) { | |
} | |
); | |
} | |
); | |
} | |
); | |
} | |
); | |
} else { | |
self.debug.log("We are at the highest bitrate and cannot switch up, use current."); | |
deferred.resolve(new MediaPlayer.rules.SwitchRequest(max)); | |
} | |
} | |
); | |
} |
Thanks for this example, Thomas. Keep up the good work, I don't think any of this was disrespectful to the people behind Dash.js or could be misconstrued as such -- I'm not sure John was a neutral party in this discussion, but his input certainly got the ball rolling.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting close: