topMatchesComponent.js 1.13 KB
import jsonpatch from 'fast-json-patch/src/json-patch';

angular.module('footyroom')

.component('topMatches', {
    controller: ['livescores', '$rootScope', function (livescores, $rootScope) {
        var ctrl = this;

        ctrl.$onInit = init;

        function init() {
            var lsSocket = livescores.connect();

            lsSocket.on('top-matches', onFullData);
            lsSocket.on('top-matches-patches', onPatches);

            lsSocket.on('connect', function () {
                lsSocket.emit('subscribe-top');
            });
        }

        function onFullData(data) {
            ctrl.matchesById = data.matchesById;

            processMatches();
        }

        function onPatches(data) {
            jsonpatch.apply(ctrl.matchesById, data.patches);

            processMatches();
        }

        function processMatches() {
            ctrl.matches = _.sortBy(ctrl.matchesById, function (match) {
                return match.datetime;
            });

            ctrl.show = ctrl.matches.length > 0;

            $rootScope.$apply();
        }
    }],

    templateUrl: '/views/ng/topMatches/topMatches.html?3',
});