Blame view
resources/js/livescores/livescores.js
3.74 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import jsonpatch from 'fast-json-patch/src/json-patch'; angular.module('footyroom') .factory('livescores', ['$filter', '$rootScope', function ($filter, $rootScope) { var socket; var dayReportCb; var matchReportCb; var updateMatchCb; var updateDayCb; var subscribedMatchId; var subscribedMatch; var subscribedDay; var timezone; var dayReportDataVersion = 0; var matchReportDataVersion = 0; var subscribedDayDate; return { connect: connect, subMatch: subMatch, subDay: subDay, }; // ////////// function connect() { if (!socket) { socket = io(window.SITEURL, { path: '/livescores/socket.io/ws' }); socket.on('full-data', onFullData); socket.on('match-report', onMatchReport); socket.on('update', onUpdate); } return socket; } function subMatch(matchId, reportCb, updateCb) { matchReportCb = reportCb; updateMatchCb = updateCb; subscribedMatchId = matchId; socket.emit('subscribe-match', { matchId: matchId }); } function subDay(date, tz, reportCb, updateCb) { dayReportCb = reportCb; updateDayCb = updateCb; subscribedDayDate = date; timezone = tz; socket.emit('subscribe-day', { date: $filter('date')(date, 'yyyy-MM-dd'), timezone }); } function preprocessMatches(matches) { _(matches).each(function (match) { if (match.incidents === undefined) { match.incidents = []; } if (match.lineup === undefined) { match.lineup = []; } }); } function onFullData(data) { subscribedDay = data; dayReportDataVersion = data.version; preprocessMatches(subscribedDay.matchesById); dayReportCb(subscribedDay); $rootScope.$apply(); } function onMatchReport(matchReport) { if (subscribedMatchId !== matchReport.match.matchId) { return; } subscribedMatch = matchReport.match; matchReportDataVersion = matchReport.version; matchReportCb && matchReportCb(subscribedMatch); $rootScope.$broadcast('formation-update'); $rootScope.$apply(); } function onUpdate(data) { if (data.matches) { onDayUpdate(data); } else if (subscribedMatch) { onMatchUpdate(data); } } function onDayUpdate(data) { if (data.version !== dayReportDataVersion + 1) { // Resubscribe to get full data socket.emit('subscribe-day', { date: $filter('date')(subscribedDayDate, 'yyyy-MM-dd'), timezone, }); } else { dayReportDataVersion++; // Increment current data version } var oldMatchesLength = subscribedDay.matchesById.length; jsonpatch.apply(subscribedDay.matchesById, data.matches); if (subscribedDay.matchesById.length != oldMatchesLength) { preprocessMatches(subscribedDay.matchesById); } updateDayCb && updateDayCb(subscribedDay, data); $rootScope.$apply(); } function onMatchUpdate(data) { if (data.version !== 'undefined' && data.version !== matchReportDataVersion + 1) { // Emit subscribe match event again to get full match report data again socket.emit('subscribe-match', { matchId: subscribedMatch.matchId }); } else { matchReportDataVersion++; } jsonpatch.apply(subscribedMatch, data.patches); updateMatchCb && updateMatchCb(subscribedMatch, data.patches); $rootScope.$broadcast('formation-update'); $rootScope.$apply(); } }]); |