<commit_before>var SongRequestService = angular.module('SongRequestService', ['ngResource']);

/**
 * Handle the sending of requests for songs to the server
 */
SongRequestService.service('SongRequestService', ['$resource','$http','angularPlayer',
    function($resource,$http,angularPlayer) {

        /**
         * Sends a request to the server for songs matching the input features
         * @param featurelist The features to match
         * @returns {*} A promise which contains the songs if successfull
         */
        this.sendRequest = function(featurelist){
            var req = {
                method: 'POST',
                url: '/app/api/songrequest',
                data: {
                    features: featurelist
                }
            };
            return $http(req);
        };

        /**
         * Sends a request to the server for all songs matching the input features
         * and adds them to the playlist.
         *
         * @param featurelist The features to match
         */
        this.playMatchingSongs = function(featurelist){
            var request = this.sendRequest(featurelist);

            request.then(function successCallback(response) {
                var res = response.data;
                for(var i = 0; i<res.length;i++){
                    var id =res[i].songID;
                    angularPlayer.addTrack({"title":id,"id":id,"url":"/app/api/music/"+id+".ogg"});
                }
                angularPlayer.play();
            }, function errorCallback() {
                console.log("Failed to query for songs.");
            });
        }
    }
]);

<commit_msg>Now correctly clears playlist before trying to add more songs.<commit_after>var SongRequestService = angular.module('SongRequestService', ['ngResource']);

/**
 * Handle the sending of requests for songs to the server
 */
SongRequestService.service('SongRequestService', ['$resource','$http','angularPlayer',
    function($resource,$http,angularPlayer) {

        /**
         * Sends a request to the server for songs matching the input features
         * @param featurelist The features to match
         * @returns {*} A promise which contains the songs if successfull
         */
        this.sendRequest = function(featurelist){
            var req = {
                method: 'POST',
                url: '/app/api/songrequest',
                data: {
                    features: featurelist
                }
            };
            return $http(req);
        };

        var addSongs = function(songs){
            var key = null;
            for (var i = 0; i < songs.length; i++) {
                var id = songs[i].songID;
                var tmp = angularPlayer.addTrack({
                    "title": id,
                    "id": id,
                    "url": "/app/api/music/" + id + ".ogg"
                });
                if (key === null) {
                    key = tmp;
                }

            }
            angularPlayer.playTrack(key);
        };
        /**
         * Sends a request to the server for all songs matching the input features
         * and adds them to the playlist.
         *
         * @param featurelist The features to match
         */
        this.playMatchingSongs = function(featurelist){

            var request = this.sendRequest(featurelist);

            request.then(function successCallback(response) {
                var res = response.data;
                if(res.length > 0) {
                    if (angularPlayer.getPlaylist().length > 0) {
                        angularPlayer.clearPlaylist(function(param){
                            addSongs(res);
                        });
                    }
                    else {
                        addSongs(res);
                    }
                }
            }, function errorCallback() {
                console.log("Failed to query for songs.");
            });
        }
    }
]);


{'fraction_non_alphanumeric': 0.1057668093679174, 'fraction_numerical': 0.001007302946361118, 'mean_word_length': 1.438305709023941, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '214531', 'n_tokens_mistral': 987, 'n_tokens_neox': 918, 'n_words': 258}