diff options
author | Morris Jobke <hey@morrisjobke.de> | 2014-07-03 11:07:02 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2014-07-03 11:07:02 +0200 |
commit | b8ced7025e170c98ba43992a81e39465f1e4f97b (patch) | |
tree | 164e1f72039008d3f101f15aeaacf3503fbbffe3 /core | |
parent | e9f616b0a03e830ae3ec723a8eff511220810eca (diff) | |
parent | 8ef8ef10968c1ec2300a8ba9668b03e6b5f2a3a2 (diff) | |
download | nextcloud-server-b8ced7025e170c98ba43992a81e39465f1e4f97b.tar.gz nextcloud-server-b8ced7025e170c98ba43992a81e39465f1e4f97b.zip |
Merge pull request #9384 from owncloud/eventsrc-fixfallbackwhenclosed
Fixed event source fallback after close
Diffstat (limited to 'core')
-rw-r--r-- | core/js/eventsource.js | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/core/js/eventsource.js b/core/js/eventsource.js index ce2a13d4676..46bd9f60bb5 100644 --- a/core/js/eventsource.js +++ b/core/js/eventsource.js @@ -28,6 +28,8 @@ * server */ +/* global EventSource */ + /** * Create a new event source * @param {string} src @@ -35,7 +37,10 @@ */ OC.EventSource=function(src,data){ var dataStr=''; + var name; + var joinChar; this.typelessListeners=[]; + this.closed = false; this.listeners={}; if(data){ for(name in data){ @@ -43,25 +48,25 @@ OC.EventSource=function(src,data){ } } dataStr+='requesttoken='+oc_requesttoken; - if(!this.useFallBack && typeof EventSource !='undefined'){ - var joinChar = '&'; + if(!this.useFallBack && typeof EventSource !== 'undefined'){ + joinChar = '&'; if(src.indexOf('?') === -1) { joinChar = '?'; } - this.source=new EventSource(src+joinChar+dataStr); + this.source= new EventSource(src+joinChar+dataStr); this.source.onmessage=function(e){ for(var i=0;i<this.typelessListeners.length;i++){ this.typelessListeners[i](JSON.parse(e.data)); } }.bind(this); }else{ - iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount; + var iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount; OC.EventSource.fallBackSources[OC.EventSource.iframeCount]=this; this.iframe=$('<iframe/>'); this.iframe.attr('id',iframeId); this.iframe.hide(); - var joinChar = '&'; + joinChar = '&'; if(src.indexOf('?') === -1) { joinChar = '?'; } @@ -72,7 +77,7 @@ OC.EventSource=function(src,data){ } //add close listener this.listen('__internal__',function(data){ - if(data=='close'){ + if(data === 'close'){ this.close(); } }.bind(this)); @@ -88,14 +93,19 @@ OC.EventSource.prototype={ listeners:{},//only for fallback useFallBack:false, fallBackCallBack:function(type,data){ + var i; + // ignore messages that might appear after closing + if (this.closed) { + return; + } if(type){ - if (typeof this.listeners['done'] != 'undefined') { - for(var i=0;i<this.listeners[type].length;i++){ + if (typeof this.listeners.done !== 'undefined') { + for(i=0;i<this.listeners[type].length;i++){ this.listeners[type][i](data); } } }else{ - for(var i=0;i<this.typelessListeners.length;i++){ + for(i=0;i<this.typelessListeners.length;i++){ this.typelessListeners[i](data); } } @@ -112,7 +122,7 @@ OC.EventSource.prototype={ this.listeners[type].push(callback); }else{ this.source.addEventListener(type,function(e){ - if (typeof e.data != 'undefined') { + if (typeof e.data !== 'undefined') { callback(JSON.parse(e.data)); } else { callback(''); @@ -120,12 +130,13 @@ OC.EventSource.prototype={ },false); } }else{ - typelessListeners.push(callback); + this.typelessListeners.push(callback); } } }, close:function(){ - if (typeof this.source !='undefined') { + this.closed = true; + if (typeof this.source !== 'undefined') { this.source.close(); } } |