<commit_before>angular.module('eoleWs', []).factory('eoleWs', ['$q', 'eoleSession', 'wsseTokenGenerator', '$rootScope', function ($q, eoleSession, wsseTokenGenerator, $rootScope) {
    var generateWsseToken = function () {
        var wsseTokenValues = wsseTokenGenerator.createWsseTokenValues(
            eoleSession.player.username,
            eoleSession.player.password,
            eoleSession.player.salt
        );

        return btoa(JSON.stringify(wsseTokenValues));
    };

    // Override autobahn _connect method to add a new wsse token on connect
    ab._connect_old = ab._connect;

    ab._connect = function (wsuri, onconnect, onhangup, options) {
        var lastPosition = wsuri.wsuri.indexOf('/?wsse_token=');
        if (-1 !== lastPosition) {
            wsuri.wsuri = wsuri.wsuri.substr(0, lastPosition);
        }

        wsuri.wsuri += '/?wsse_token='+generateWsseToken();

        console.log('connect', wsuri);
        return ab._connect_old(wsuri, onconnect, onhangup, options);
    };

    function EoleWs() {
        var that = this;

        this.sessionPromise = null;

        var openSocket = function () {
            that.sessionPromise = $q(function (resolve, reject) {
                ab.connect(
                    'ws://127.0.0.1:8080',
                    function (session) {
                        console.log('websocket open');
                        resolve(session);
                    },
                    function (code, reason, detail) {
                        console.log('socket closed', code, reason, detail);
                        reject([code, reason, detail]);
                        that.reopenSocket();
                    },
                    {
                        maxRetries: 0,
                        retryDelay: 2000
                    }
                );
            });
        };

        this.closeSocket = function () {
            that.sessionPromise.then(function (session) {
                session.close();
            });

            that.sessionPromise = createClosedSocketPromise();
        };

        this.reopenSocket = function () {
            console.log('reopening socket...');
            that.closeSocket();
            openSocket();
        };

        var createClosedSocketPromise = function () {
            return $q(function (resolve, reject) {<commit_msg>Auto resubscribe each topic on socket re-open<commit_after>angular.module('eoleWs', []).factory('eoleWs', ['$q', 'eoleSession', 'wsseTokenGenerator', '$rootScope', function ($q, eoleSession, wsseTokenGenerator, $rootScope) {
    var generateWsseToken = function () {
        var wsseTokenValues = wsseTokenGenerator.createWsseTokenValues(
            eoleSession.player.username,
            eoleSession.player.password,
            eoleSession.player.salt
        );

        return btoa(JSON.stringify(wsseTokenValues));
    };

    // Override autobahn _connect method to add a new wsse token on connect
    ab._connect_old = ab._connect;

    ab._connect = function (wsuri, onconnect, onhangup, options) {
        var lastPosition = wsuri.wsuri.indexOf('/?wsse_token=');
        if (-1 !== lastPosition) {
            wsuri.wsuri = wsuri.wsuri.substr(0, lastPosition);
        }

        wsuri.wsuri += '/?wsse_token='+generateWsseToken();

        console.log('connect', wsuri);
        return ab._connect_old(wsuri, onconnect, onhangup, options);
    };

    function EoleWs() {
        var that = this;

        this.sessionPromise = null;

        var lastSession = null;

        var openSocket = function () {
            that.sessionPromise = $q(function (resolve, reject) {
                ab.connect(
                    'ws://127.0.0.1:8080',
                    function (session) {
                        console.log('websocket open');

                        if (null !== lastSession) {
                            angular.forEach(lastSession._subscriptions, function (callbacks, topic) {
                                session.subscribe(topic, callbacks[0]);
                            });
                        }

                        lastSession = session;
                        resolve(session);
                        window['eoleWsSession'] = session;
                    },
                    function (code, reason, detail) {
                        console.log('socket closed', code, reason, detail);
                        reject([code, reason, detail]);
                        if (0 !== code) {
                            setTimeout(that.reopenSocket, 2000);
                        }
                    },
                    {
                        maxRetries: 0,
                        retryDelay: 2000
                    }
                );
            });
        };

        this.closeSocket = function () {
            that.sessionPromise.then(function (session) {
                session.close();
            });

            that.sessionPromise = createClosedSocketPromise();
        };

        this.reopenSocket = function () {
            console.log('reopening socket...');
            that.closeSocket();
            openSocket();
        };

        var createClosedSocketPromise = function () {
            return $q(function (resolve, reject) {
{'fraction_non_alphanumeric': 0.11294296001516013, 'fraction_numerical': 0.007580064430547659, 'mean_word_length': 1.5002368545712932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4601457', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1291, 'n_words': 262}