<commit_before>(function () {
    'use strict';

    var API_SERVER = window.location.hostname + ':9003',
        SITE_URL = 'http://lunasync.ajf.me';

    var mode, socket, player, ytReady = false;

    var state = {
        playing: false,
        current: null,
        playlist: []
    }, haveControl = false, chatNick = null;

    function $(id) {
        return document.getElementById(id);
    }

    window.onYouTubePlayerReady = function () {
        ytReady = true;
        player = $('player');
    };

    window.onload = function () {
        var id, control;

        // get them before IE errors out
        if (!Object.prototype.hasOwnProperty.call(window, 'WebSocket')) {
            document.innerHTML = 'lunasync requires a browser that supports WebSocket, such as Google Chrome, Mozilla Firefox, Apple Safari, Opera or Internet Explorer 10. Sorry :(';
            return;
        }

        // disable document-hiding style
        $('site').className = '';

        // display basic UI elements present on all pages
        initBasic();

        // display correct page for URL
        if (window.location.pathname === '/') {
            mode = 'home';
            initHome();
        } else if (window.location.pathname[0] === '/') {
            mode = 'view';
            id = window.location.pathname.substr(1);
            if (window.location.search.substr(0, 9) === '?control=') {
                control = window.location.search.substr(9);
            }
            initView(id, control);
        } else {
            mode = '404';
            init404();
        }
    };

    function doAJAX(method, url, data, callback, errback) {
        var xhr;

        xhr = new XMLHttpRequest();
        xhr.open(method, 'http://' + API_SERVER + url);
        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    callback(JSON.parse(xhr.responseText));
                } else {
                    errback(xhr);
                }
            }
        };
        if (data) {
            xhr.send(JSON.stringify(data));
        } else {
            xhr.send();
        }
    }

    function send(msg) {
        console.log(msg);
        socket.send(JSON.stringify(msg));
    }

    function initBasic() {
        $('create-new').onclick = function () {
            var title;

            title = prompt('Enter a title:', '') || 'My Stream';
            doAJAX('POST', '/new', {
                title: title
            }, function (response) {
                window.location = '/' + response.stream.id + '?control=' + response.stream.secret;
            }, function (xhr) {
                alert('Error while trying to create new sync:\nResponse code:\n' + xhr.status + '\nError message:' + xhr.responseText);
            });
        };
    }

    // homepage
    function initHome() {
        // unhide home page
        $('homepage').className = '';
    }

    // sync viewing page
    function initView(id, control) {
        // unhide view page
        $('viewpage').className = '';

        if (!ytReady) {
            window.onYouTubePlayerReady = function () {
                player = $('player');
                initRestView(id, control);
            };
        } else {
            initRestView(id, control);
        }
    }

    function initRestView(id, control) {
        var url;

        window.onStateChange = function (pstate) {
            var cueIndex;

            // if we are controlling
            if (haveControl) {
                // if video paused and it was playing according to known state
                if (pstate === 2 /* YT.PlayerState.PAUSED */ && state.playing) {
                    // broadcast state change
                    send({
                        type: 'stop',
                        time: player.getCurrentTime()
                    });
                    state.playing = false;
                    console.log('stop!');
                // if video started playing and it was paused according to known state
                } else if (pstate === 1 /*YT.PlayerState.PLAYING*/ && !state.playing) {
                    // broadcast state change
                    send({
                        type: 'play',
                        time: player.getCurrentTime()
                    });
                    state.playing = true;
                    console.log('play!');
                // if the video ended
                } else if (pstate === 0 /*YT.PlayerState.ENDED*/) {
                    // if we haven't reached the end of the playlist
                    if (state.current + 1 < state.playlist.length) {
                        // cue next video
                        cueIndex = state.current + 1;
                    } else {
                        // cue first video
                        cueIndex = 0;
                    }

                    state.current = cueIndex;
                    state.playing = true;

                    send({
                        type: 'cue',
                        current: cueIndex
                    });

                    player.loadVideoById(state.playlist[cueIndex].id);

                    updatePlaylist();
                }
            }
        };
        player.addEventListener('onStateChange', 'onStateChange');

        socket = new WebSocket('ws://' + API_SERVER, ['lunasync']);
        socket.onopen = function () {
            // set our stream (subscribe to events) and get info on it
            send({
                type: 'set_stream',
                id: id,
                control: control
            });
        };
        socket.onerror = socket.onclose = function (err) {
            $('viewpage').innerHTML = 'Error communicating with server, lost connection (server may be down, lunasync may have updated, try refreshing):\n' + err;
        };
        socket.onmessage = function (event) {
            var msg, stream, elem;

            msg = JSON.parse(event.data);

            console.log(msg.type);

            switch (msg.type) {
                case 'stream_info':
                    stream = msg.stream;
<commit_msg>prevent error messages overwriting eachother<commit_after>(function () {
    'use strict';

    var API_SERVER = window.location.hostname + ':9003',
        SITE_URL = 'http://lunasync.ajf.me';

    var mode, socket, player, ytReady = false, errored = false;

    var state = {
        playing: false,
        current: null,
        playlist: []
    }, haveControl = false, chatNick = null;

    function $(id) {
        return document.getElementById(id);
    }

    window.onYouTubePlayerReady = function () {
        ytReady = true;
        player = $('player');
    };

    window.onload = function () {
        var id, control;

        // get them before IE errors out
        if (!Object.prototype.hasOwnProperty.call(window, 'WebSocket')) {
            document.innerHTML = 'lunasync requires a browser that supports WebSocket, such as Google Chrome, Mozilla Firefox, Apple Safari, Opera or Internet Explorer 10. Sorry :(';
            return;
        }

        // disable document-hiding style
        $('site').className = '';

        // display basic UI elements present on all pages
        initBasic();

        // display correct page for URL
        if (window.location.pathname === '/') {
            mode = 'home';
            initHome();
        } else if (window.location.pathname[0] === '/') {
            mode = 'view';
            id = window.location.pathname.substr(1);
            if (window.location.search.substr(0, 9) === '?control=') {
                control = window.location.search.substr(9);
            }
            initView(id, control);
        } else {
            mode = '404';
            init404();
        }
    };

    function doAJAX(method, url, data, callback, errback) {
        var xhr;

        xhr = new XMLHttpRequest();
        xhr.open(method, 'http://' + API_SERVER + url);
        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    callback(JSON.parse(xhr.responseText));
                } else {
                    errback(xhr);
                }
            }
        };
        if (data) {
            xhr.send(JSON.stringify(data));
        } else {
            xhr.send();
        }
    }

    function send(msg) {
        console.log(msg);
        socket.send(JSON.stringify(msg));
    }

    function initBasic() {
        $('create-new').onclick = function () {
            var title;

            title = prompt('Enter a title:', '') || 'My Stream';
            doAJAX('POST', '/new', {
                title: title
            }, function (response) {
                window.location = '/' + response.stream.id + '?control=' + response.stream.secret;
            }, function (xhr) {
                alert('Error while trying to create new sync:\nResponse code:\n' + xhr.status + '\nError message:' + xhr.responseText);
            });
        };
    }

    // homepage
    function initHome() {
        // unhide home page
        $('homepage').className = '';
    }

    // sync viewing page
    function initView(id, control) {
        // unhide view page
        $('viewpage').className = '';

        if (!ytReady) {
            window.onYouTubePlayerReady = function () {
                player = $('player');
                initRestView(id, control);
            };
        } else {
            initRestView(id, control);
        }
    }

    function initRestView(id, control) {
        var url;

        window.onStateChange = function (pstate) {
            var cueIndex;

            // if we are controlling
            if (haveControl) {
                // if video paused and it was playing according to known state
                if (pstate === 2 /* YT.PlayerState.PAUSED */ && state.playing) {
                    // broadcast state change
                    send({
                        type: 'stop',
                        time: player.getCurrentTime()
                    });
                    state.playing = false;
                    console.log('stop!');
                // if video started playing and it was paused according to known state
                } else if (pstate === 1 /*YT.PlayerState.PLAYING*/ && !state.playing) {
                    // broadcast state change
                    send({
                        type: 'play',
                        time: player.getCurrentTime()
                    });
                    state.playing = true;
                    console.log('play!');
                // if the video ended
                } else if (pstate === 0 /*YT.PlayerState.ENDED*/) {
                    // if we haven't reached the end of the playlist
                    if (state.current + 1 < state.playlist.length) {
                        // cue next video
                        cueIndex = state.current + 1;
                    } else {
                        // cue first video
                        cueIndex = 0;
                    }

                    state.current = cueIndex;
                    state.playing = true;

                    send({
                        type: 'cue',
                        current: cueIndex
                    });

                    player.loadVideoById(state.playlist[cueIndex].id);

                    updatePlaylist();
                }
            }
        };
        player.addEventListener('onStateChange', 'onStateChange');

        socket = new WebSocket('ws://' + API_SERVER, ['lunasync']);
        socket.onopen = function () {
            // set our stream (subscribe to events) and get info on it
            send({
                type: 'set_stream',
                id: id,
                control: control
            });
        };
        socket.onerror = socket.onclose = function (err) {
            if (errored) {
                return;
            }
            $('viewpage').innerHTML = 'Error communicating with server, lost connection (server may be down, lunasync may have updated, try refreshing):\n' + err;
            errored = true;
        };
        socket.onmessage = function (event) {
            var msg, stream, elem;

            msg = JSON.parse(event.data);

            console.log(msg.type);

            switch (msg.type) {
                case 'stream_info':
                    stream = msg.stream;

{'fraction_non_alphanumeric': 0.11876728565784275, 'fraction_numerical': 0.004425128407743974, 'mean_word_length': 1.3103322380430815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '872612', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3113, 'n_words': 809}