ThomasBurleson
11/21/2013 - 4:25 AM

Demonstration of refactor of DownloadRatioRules.js > transform deep nesting of promise chains to an easily-maintained, flattened, sequential

Demonstration of refactor of DownloadRatioRules.js > transform deep nesting of promise chains to an easily-maintained, flattened, sequential chain.

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));
            }
        }
    );
}