workaround implemented to avoid IE7 scope reset
[xynt.git] / web / xynt / xynt-http-streaming.js
index cb914e0..3988d1d 100644 (file)
@@ -1,57 +1,94 @@
-function http_streaming(doc, cons)
+function http_streaming(win, cons)
 {
     this.console = cons;
-    this.doc = doc;
+    this.doc = win.document;
     this.keepalive_old = -1;
     this.keepalive_new = -1;
     this.keepalives_equal = 0;
-    this.ifra = doc.createElement("iframe");
+    this.ifra = this.doc.createElement("iframe");
     this.ifra.style.visibility = "hidden";
-    doc.body.appendChild(this.ifra);
-    // this.ifra.contentWindow.onload = function () {
-    //     this.ifra.contentWindow.location.href = 'winstream_rdxxx.php';     
-    // }
-    
+    this.doc.body.appendChild(this.ifra);
 }
 
 http_streaming.prototype = {
     console:           null,
     doc:               null,
     ifra:              null,
+    page:              null,
+    wd_handle:         null,
     keepalive_old:    -1,
     keepalive_new:    -1,
     keepalives_equal:  0,
     keepalives_eq_max: 6,
     watchdog_timeout: 2000,
+    watchable: false,
+    restart_n: 0,
 
     start: function(page) {
-        this.log("http_streaming:start");
+        this.log("http_streaming:start restart: "+this.restart_n);
+        this.page = page;
         this.ifra.contentWindow.location.href = page;
-        setTimeout(function(obj) { obj.watchdog(); }, this.watchdog_timeout, this);
+        this.wd_handle = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
     },
 
     watchdog: function () {
         /* +this.keepalives_equal */
-        this.log("hs::watchdog: start");
+        this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
+        if (!this.watchable) {
+            do {
+                if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
+                    break;
+                /*
+                  on IE7 the the window frame scope is cleaned after the href is set, so we wait 
+                  for a well know variable value before assign this object value to it (OO is a passion)
+                */
+                if (this.ifra.contentWindow.http_streaming == "ready") {
+                    this.ifra.contentWindow.http_streaming = this;
+                    this.watchable = true;
+                }
+            } while (false);
+        }
+        this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
         if (this.keepalive_old == this.keepalive_new) {
             this.keepalives_equal++;
         }
         else {
+            this.keepalive_old = this.keepalive_new;
             this.keepalives_equal = 0;
         }
         
         if (this.keepalives_equal > this.keepalives_eq_max) {
             this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
+            this.reload();
+            return;
         }
         else {
-            setTimeout(function(obj) { obj.watchdog() }, this.watchdog_timeout, this);
+            this.wd_handle = setTimeout(function(obj) { obj.log("tout2"); obj.watchdog(); }, this.watchdog_timeout, this);
         }
     },
     
     keepalive: function () {
+        this.log("hs::keepalive");
         this.keepalive_new++;
     },
 
+    abort: function () {
+        clearTimeout(this.wd_handle);
+        this.restart_n++;
+        this.log("hs::reload");
+        this.watchable = false;
+        delete this.ifra;
+    },
+
+    reload: function () {
+        this.abort();
+        this.keepalives_equal = 0;
+        this.ifra = this.doc.createElement("iframe");
+        this.ifra.style.visibility = "hidden";
+        this.doc.body.appendChild(this.ifra);
+        this.start(this.page);
+    },
+
     log: function (s) {
         if (this.console != null) {
             return (this.console.log(s));