<commit_before>'use strict';

$(function() {
    var clickChannel = function (view) {
        return function (channel) {
            view.channelName = channel.name;
            if (!(view.channelName in view.userNames)) {
                updateUserList();
            }
            view.update();
            return false;
        };
    }
    var clickChannelDel = function (view) {
        return function (channel) {
            var channelName = channel.name;
            var msg = "Are you sure you want to delete subscribing '" +
                channelName + "'?"
            if (!confirm(msg)) {
                return false;
            }
            var url = '/subscribings?' +
                'channel_name=' + encodeURIComponent(channelName) + ';' +
                'user_name=' + encodeURIComponent(view.session.userName());
            var callbacks = {
                success: updateChannelList,
                logOut: logOut,
            }
            starChat.ajax(view.session.userName(), view.session.password(),
                          url,
                          'DELETE',
                          callbacks);
            return false;
        };
    }
    var session = new starChat.Session();
    // TODO: channel page state (dirty)
    var views = {};
    function getView() {
        if (!views[session.id()]) {
            var view = new starChat.View(session);
            view.clickChannel(clickChannel(view)).clickChannelDel(clickChannelDel(view));
            views[session.id()] = view;
        }
        return views[session.id()];
    }
    function processPacketMessage(obj, view) {
        var channelName = obj.channel_name;
        var message = obj.message;
        if (channelName && message) {
            if (!view.newMessages[channelName]) {
                view.newMessages[channelName] = [];
            }
            view.newMessages[channelName].push(message);
            var view = getView();
            if (channelName === view.channelName) {
                view.update();
            }
        }
    }
    function processPacketSubscribing(obj, view) {
        var channelName = obj.channel_name;
        if (!(channelName in view.userNames)) {
            view.userNames[channelName] = {};
        }
        var userNames = view.userNames[channelName];
        userNames[obj.user_name] = true;
        var view = getView();
        if (channelName === view.channelName) {
            view.update();
        }
    }
    function processPacketDeleteSubscribing(obj, view) {
        var channelName = obj.channel_name;
        if (!(channelName in view.userNames)) {
            view.userNames[channelName] = {};
            return;
        }
        var userNames = view.userNames[channelName];
        delete userNames[obj.user_name];
        var view = getView();
        if (channelName === view.channelName) {
            view.update();
        }
    }
    function startStream() {
        var view = getView();
        if (view.stream) {
            view.stream.abort();
        }
        view.stream = null;
        var streamReadIndex = 0;
        var url = '/users/' + encodeURIComponent(session.userName()) + '/stream';
        var callbacks = {
            onprogress: function () {
                // TODO: Reconnecting if overflow
                var xhr = this;
                var text = xhr.responseText;
                var subText = text.substring(streamReadIndex);
                while (true) {
                    var tokenLength = subText.search("\n");
                    if (tokenLength === -1) {
                        break;
                    }
                    streamReadIndex += tokenLength + 1;
                    var token = subText.substring(0, tokenLength);
                    subText = subText.substring(tokenLength + 1);
                    try {
                        var obj = JSON.parse(token);
                    } catch (e) {
                        console.log(e);
                        continue;
                    }
                    if (obj.type === 'message') {
                        processPacketMessage(obj, view);
                    } else if (obj.type === 'subscribing') {
                        processPacketSubscribing(obj, view);
                    } else if (obj.type === 'delete_subscribing') {
                        processPacketDeleteSubscribing(obj, view);
                    }
                }
            },
            success: function (data, textStatus, jqXHR) {
                view.streamContinuingErrorNum = 0;
                setTimeout(startStream, 0);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus);
                view.streamContinuingErrorNum++;
                if (10 <= view.streamContinuingErrorNum) {
                    console.log('Too many errors!');
                    // TODO: implement showing error message
                    return;
                }
                setTimeout(startStream, 10000);
            },
        };
        starChat.ajax(session.userName(), session.password(),
                      url,
                      'GET',
                      callbacks);
    }
    function stopStream() {
        var view = getView();
        if (view.stream) {
            view.stream.abort();
        }
        view.stream = null;
        view.streamContinuingErrorNum = 0;
    }
    function logIn(userName, password) {
        localStorage.userName = userName;
        localStorage.password = password;
        session = new starChat.Session((new Date()).getTime(), userName, password);
        updateChannelList();
        var view = getView();
        view.update();
        startStream();
    }
    function logOut() {
        delete localStorage.userName;<commit_msg>Use $.now()<commit_after>'use strict';

$(function() {
    var clickChannel = function (view) {
        return function (channel) {
            view.channelName = channel.name;
            if (!(view.channelName in view.userNames)) {
                updateUserList();
            }
            view.update();
            return false;
        };
    }
    var clickChannelDel = function (view) {
        return function (channel) {
            var channelName = channel.name;
            var msg = "Are you sure you want to delete subscribing '" +
                channelName + "'?"
            if (!confirm(msg)) {
                return false;
            }
            var url = '/subscribings?' +
                'channel_name=' + encodeURIComponent(channelName) + ';' +
                'user_name=' + encodeURIComponent(view.session.userName());
            var callbacks = {
                success: updateChannelList,
                logOut: logOut,
            }
            starChat.ajax(view.session.userName(), view.session.password(),
                          url,
                          'DELETE',
                          callbacks);
            return false;
        };
    }
    var session = new starChat.Session();
    // TODO: channel page state (dirty)
    var views = {};
    function getView() {
        if (!views[session.id()]) {
            var view = new starChat.View(session);
            view.clickChannel(clickChannel(view)).clickChannelDel(clickChannelDel(view));
            views[session.id()] = view;
        }
        return views[session.id()];
    }
    function processPacketMessage(obj, view) {
        var channelName = obj.channel_name;
        var message = obj.message;
        if (channelName && message) {
            if (!view.newMessages[channelName]) {
                view.newMessages[channelName] = [];
            }
            view.newMessages[channelName].push(message);
            var view = getView();
            if (channelName === view.channelName) {
                view.update();
            }
        }
    }
    function processPacketSubscribing(obj, view) {
        var channelName = obj.channel_name;
        if (!(channelName in view.userNames)) {
            view.userNames[channelName] = {};
        }
        var userNames = view.userNames[channelName];
        userNames[obj.user_name] = true;
        var view = getView();
        if (channelName === view.channelName) {
            view.update();
        }
    }
    function processPacketDeleteSubscribing(obj, view) {
        var channelName = obj.channel_name;
        if (!(channelName in view.userNames)) {
            view.userNames[channelName] = {};
            return;
        }
        var userNames = view.userNames[channelName];
        delete userNames[obj.user_name];
        var view = getView();
        if (channelName === view.channelName) {
            view.update();
        }
    }
    function startStream() {
        var view = getView();
        if (view.stream) {
            view.stream.abort();
        }
        view.stream = null;
        var streamReadIndex = 0;
        var url = '/users/' + encodeURIComponent(session.userName()) + '/stream';
        var callbacks = {
            onprogress: function () {
                // TODO: Reconnecting if overflow
                var xhr = this;
                var text = xhr.responseText;
                var subText = text.substring(streamReadIndex);
                while (true) {
                    var tokenLength = subText.search("\n");
                    if (tokenLength === -1) {
                        break;
                    }
                    streamReadIndex += tokenLength + 1;
                    var token = subText.substring(0, tokenLength);
                    subText = subText.substring(tokenLength + 1);
                    try {
                        var obj = JSON.parse(token);
                    } catch (e) {
                        console.log(e);
                        continue;
                    }
                    if (obj.type === 'message') {
                        processPacketMessage(obj, view);
                    } else if (obj.type === 'subscribing') {
                        processPacketSubscribing(obj, view);
                    } else if (obj.type === 'delete_subscribing') {
                        processPacketDeleteSubscribing(obj, view);
                    }
                }
            },
            success: function (data, textStatus, jqXHR) {
                view.streamContinuingErrorNum = 0;
                setTimeout(startStream, 0);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus);
                view.streamContinuingErrorNum++;
                if (10 <= view.streamContinuingErrorNum) {
                    console.log('Too many errors!');
                    // TODO: implement showing error message
                    return;
                }
                setTimeout(startStream, 10000);
            },
        };
        starChat.ajax(session.userName(), session.password(),
                      url,
                      'GET',
                      callbacks);
    }
    function stopStream() {
        var view = getView();
        if (view.stream) {
            view.stream.abort();
        }
        view.stream = null;
        view.streamContinuingErrorNum = 0;
    }
    function logIn(userName, password) {
        localStorage.userName = userName;
        localStorage.password = password;
        session = new starChat.Session($.now(), userName, password);
        updateChannelList();
        var view = getView();
        view.update();
        startStream();
    }
    function logOut() {
        delete localStorage.userName;
{'fraction_non_alphanumeric': 0.10862647286488346, 'fraction_numerical': 0.0025802012556979444, 'mean_word_length': 1.3798608268522308, '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': '2730305', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2837, 'n_words': 615}