workaround implemented to avoid IE7 scope reset
[xynt.git] / web / xynt / xynt-http-streaming.js
1 function http_streaming(win, cons)
2 {
3     this.console = cons;
4     this.doc = win.document;
5     this.keepalive_old = -1;
6     this.keepalive_new = -1;
7     this.keepalives_equal = 0;
8     this.ifra = this.doc.createElement("iframe");
9     this.ifra.style.visibility = "hidden";
10     this.doc.body.appendChild(this.ifra);
11 }
12
13 http_streaming.prototype = {
14     console:           null,
15     doc:               null,
16     ifra:              null,
17     page:              null,
18     wd_handle:         null,
19     keepalive_old:    -1,
20     keepalive_new:    -1,
21     keepalives_equal:  0,
22     keepalives_eq_max: 6,
23     watchdog_timeout: 2000,
24     watchable: false,
25     restart_n: 0,
26
27     start: function(page) {
28         this.log("http_streaming:start restart: "+this.restart_n);
29         this.page = page;
30         this.ifra.contentWindow.location.href = page;
31         this.wd_handle = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
32     },
33
34     watchdog: function () {
35         /* +this.keepalives_equal */
36         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
37         if (!this.watchable) {
38             do {
39                 if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
40                     break;
41                 /*
42                   on IE7 the the window frame scope is cleaned after the href is set, so we wait 
43                   for a well know variable value before assign this object value to it (OO is a passion)
44                 */
45                 if (this.ifra.contentWindow.http_streaming == "ready") {
46                     this.ifra.contentWindow.http_streaming = this;
47                     this.watchable = true;
48                 }
49             } while (false);
50         }
51         this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
52         if (this.keepalive_old == this.keepalive_new) {
53             this.keepalives_equal++;
54         }
55         else {
56             this.keepalive_old = this.keepalive_new;
57             this.keepalives_equal = 0;
58         }
59         
60         if (this.keepalives_equal > this.keepalives_eq_max) {
61             this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
62             this.reload();
63             return;
64         }
65         else {
66             this.wd_handle = setTimeout(function(obj) { obj.log("tout2"); obj.watchdog(); }, this.watchdog_timeout, this);
67         }
68     },
69     
70     keepalive: function () {
71         this.log("hs::keepalive");
72         this.keepalive_new++;
73     },
74
75     abort: function () {
76         clearTimeout(this.wd_handle);
77         this.restart_n++;
78         this.log("hs::reload");
79         this.watchable = false;
80         delete this.ifra;
81     },
82
83     reload: function () {
84         this.abort();
85         this.keepalives_equal = 0;
86         this.ifra = this.doc.createElement("iframe");
87         this.ifra.style.visibility = "hidden";
88         this.doc.body.appendChild(this.ifra);
89         this.start(this.page);
90     },
91
92     log: function (s) {
93         if (this.console != null) {
94             return (this.console.log(s));
95         }
96     }
97 }