define(['app'], function (app) {
  app.factory('stream', ['$window', function(window){
    'use strict';

    if (window.streams === undefined) {
      window.streams = {};
    }

    return function($scope, url, callback) {
      if (window.streams[url]) {
        console.log('[Stream] Closing connection to ' + url);
        window.streams[url].close()
      }
      console.log('[Stream] Initiating connection to ' + url);

      $scope.$on('routeChangeStart', function(e){
        if (window.streams) {
          $.each(window.streams, function(_, stream){
            stream.close();
          });
        }
      });

      window.streams[url] = new EventSource(url + '?_=' + new Date().getTime());
      window.streams[url].onopen = function(e) {
        console.log('[Stream] Connection opened to ' + url);
      }

      return {
        subscribe: function(event, callback){
          window.streams[url].addEventListener(event, function(e) {
            var data = $.parseJSON(e.data);
            callback(data);
          }, false);
        }
      }
    };
  }]);
});

@@ -425,205 +425,8 @@
 );%0A%0A
-      $scope.$on('routeChangeStart', function(e)%7B%0A        if (window.streams) %7B%0A          $.each(window.streams, function(_, stream)%7B%0A            stream.close();%0A          %7D);%0A        %7D%0A      %7D);%0A%0A
     
@@ -617,24 +617,212 @@
 );%0A      %7D%0A%0A
+      $scope.$on(%22$destroy%22, function() %7B%0A        console.log('%5BStream%5D Closing connection to ' + url);%0A        window.streams%5Burl%5D.close();%0A        delete window.streams%5Burl%5D;%0A      %7D);%0A%0A
       return

Fix handling of stream cleanup