import socket from '../../socket';
import settings from '../../settings';
import { lobby as lobbyXhr } from '../../xhr';
import throttle from 'lodash/throttle';
import { featured as featuredXhr, dailyPuzzle as dailyPuzzleXhr, topPlayersOfTheWeek as topPlayersOfTheWeekXhr } from './homeXhr';
import { hasNetwork, noop, handleXhrError } from '../../utils';
import m from 'mithril';

export default function homeCtrl() {

  const featured = m.prop();

  const nbConnectedPlayers = m.prop();
  const nbGamesInPlay = m.prop();
  const dailyPuzzle = m.prop();
  const weekTopPlayers = m.prop([]);

  function onFeatured() {
    return throttle(featuredXhr, 1000)()
    .then(data => {
      featured(data);
      m.redraw();
    });
  }

  function init() {

    lobbyXhr(true).then(data => {
      socket.createLobby(data.lobby.version, noop, {
        n: (_, d) => {
          nbConnectedPlayers(d.d);
          nbGamesInPlay(d.r);
          m.redraw();
        },
        featured: onFeatured
      });
    });

    Promise.all([
      featuredXhr(true),
      dailyPuzzleXhr(),
      topPlayersOfTheWeekXhr()
    ]).then(results => {
      const [featuredData, dailyData, topPlayersData] = results;

      // featured game
      featured(featuredData);
      const featuredFeed = new EventSource(`http://${window.lichess.apiEndPoint}/tv/feed`);
        featuredFeed.onmessage = function(ev) {
          const obj = JSON.parse(ev.data);
          featured().game.fen = obj.d.fen;
          featured().game.lastMove = obj.d.lm;
          m.redraw();
        };

        // daily puzzle
        dailyPuzzle(dailyData.puzzle);

        // week top players
        weekTopPlayers(topPlayersData);
    })
    .catch(handleXhrError);

  }

  if (hasNetwork()) {
    init();
  }

  document.addEventListener('online', init);

  return {
    featured,
    nbConnectedPlayers,
    nbGamesInPlay,
    dailyPuzzle,
    weekTopPlayers,
    goToFeatured() {
      settings.tv.channel('best');
      m.route('/tv');
    },
    onunload() {
      document.removeEventListener('online', init);
    }
  };
}

@@ -413,16 +413,37 @@
 rl() %7B%0A%0A
+  var featuredFeed;%0A%0A
   const 
@@ -1272,22 +1272,16 @@
 ;%0A      
-const 
 featured
@@ -1352,24 +1352,23 @@
 feed%60);%0A
+%0A
       
-  
 featured
@@ -1403,26 +1403,24 @@
 ) %7B%0A        
-  
 const obj = 
@@ -1436,26 +1436,24 @@
 e(ev.data);%0A
-  
         feat
@@ -1485,26 +1485,24 @@
 en;%0A        
-  
 featured().g
@@ -1526,34 +1526,32 @@
 j.d.lm;%0A        
-  
 m.redraw();%0A    
@@ -1552,18 +1552,16 @@
 ;%0A      
-  
 %7D;%0A%0A    
@@ -1562,18 +1562,16 @@
 %0A%0A      
- 
- 
 // daily
@@ -1578,18 +1578,16 @@
  puzzle%0A
-  
       da
@@ -1622,18 +1622,16 @@
 %0A%0A      
-  
 // week 
@@ -1648,18 +1648,16 @@
 s%0A      
- 
- 
 weekTopP
@@ -1721,16 +1721,88 @@
 ;%0A%0A  %7D%0A%0A
+  function onPause() %7B%0A    if (featuredFeed) featuredFeed.close();%0A  %7D%0A%0A
   if (ha
@@ -1876,16 +1876,122 @@
 , init);
+%0A  document.addEventListener('pause', onPause, false);%0A  document.addEventListener('resume', init, false);
 %0A%0A  retu
@@ -2201,51 +2201,203 @@
     
-document.removeEventListener('online', init
+if (featuredFeed) featuredFeed.close();%0A      document.removeEventListener('online', init);%0A      document.removeEventListener('resume', init);%0A      document.removeEventListener('pause', onPause
 );%0A 

Fix event source not closed in home