Skip to content

Instantly share code, notes, and snippets.

@mjc-gh
Created October 5, 2013 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjc-gh/6843004 to your computer and use it in GitHub Desktop.
Save mjc-gh/6843004 to your computer and use it in GitHub Desktop.
Simple EventSource factory object for angular.js
!function(factories){
factories.factory('$eventSource', ['$window', function($window){
var EventSource = $window.EventSource;
function parse(obj){
try { var json = JSON.parse(obj); }
catch(e) { json = {}; }
finally { return json; }
}
return function(url){
var esrc = new EventSource(url);
return {
on:function(type, callback){
esrc.addEventListener(type, function(ev){
callback(parse(ev.data));
});
}
};
}
}]);
}(angular.module('gp.Factories'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment