|
2 | 2 | var hub, sharehub; |
3 | 3 | hub = typeof module != 'undefined' && module !== null ? require("./datahub") : datahub; |
4 | 4 | sharehub = function(o){ |
5 | | - var this$ = this; |
| 5 | + var watchdog, this$ = this; |
6 | 6 | o == null && (o = {}); |
7 | 7 | this.evthdr = {}; |
8 | 8 | this.data = {}; |
|
11 | 11 | this._create = o.create || null; |
12 | 12 | this._watch = o.watch || null; |
13 | 13 | this.ews = o.ews; |
| 14 | + watchdog = { |
| 15 | + timeout: 20000, |
| 16 | + count: 0, |
| 17 | + hash: {}, |
| 18 | + hdr: null, |
| 19 | + fire: function(){ |
| 20 | + if (watchdog.hdr) { |
| 21 | + clearTimeout(watchdog.hdr); |
| 22 | + watchdog.hdr = null; |
| 23 | + } |
| 24 | + if (this$.ews && this$.ews.disconnect && this$.ews.status && this$.ews.status() === 2) { |
| 25 | + this$.ews.disconnect(); |
| 26 | + } |
| 27 | + return watchdog.hash = {}; |
| 28 | + }, |
| 29 | + check: function(){ |
| 30 | + var ref$, now, min, k, v, ref1$; |
| 31 | + ref$ = [Date.now(), -1], now = ref$[0], min = ref$[1]; |
| 32 | + for (k in ref$ = this.hash) { |
| 33 | + v = ref$[k]; |
| 34 | + if (now - v >= this.timeout) { |
| 35 | + return this.fire(); |
| 36 | + } |
| 37 | + if (min < 0 || this.timeout - (now - v) < min) { |
| 38 | + min = (ref1$ = this.timeout - (now - v)) > 0 ? ref1$ : 0; |
| 39 | + } |
| 40 | + } |
| 41 | + if (this.hdr) { |
| 42 | + clearTimeout(this.hdr); |
| 43 | + this.hdr = null; |
| 44 | + } |
| 45 | + if (min >= 0) { |
| 46 | + return this.hdr = setTimeout(function(){ |
| 47 | + return this.check(); |
| 48 | + }, min); |
| 49 | + } |
| 50 | + }, |
| 51 | + track: function(){ |
| 52 | + var tid; |
| 53 | + if (this$.ews && this$.ews.status && this$.ews.status() !== 2) { |
| 54 | + return 0; |
| 55 | + } |
| 56 | + tid = ++watchdog.count; |
| 57 | + watchdog.hash[tid] = Date.now(); |
| 58 | + if (!watchdog.hdr) { |
| 59 | + watchdog.hdr = setTimeout(function(){ |
| 60 | + return watchdog.check(); |
| 61 | + }, watchdog.timeout); |
| 62 | + } |
| 63 | + return tid; |
| 64 | + }, |
| 65 | + untrack: function(tid){ |
| 66 | + var this$ = this; |
| 67 | + if (!tid) { |
| 68 | + return function(){}; |
| 69 | + } |
| 70 | + return function(e){ |
| 71 | + var ref$, ref1$; |
| 72 | + if (e) { |
| 73 | + return this$.fire(); |
| 74 | + } else { |
| 75 | + return ref1$ = (ref$ = this$.hash)[tid], delete ref$[tid], ref1$; |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + }; |
14 | 80 | hub.src.call(this, import$(import$({}, o), { |
15 | 81 | opsOut: function(ops){ |
16 | | - var _id; |
| 82 | + var _id, tid; |
17 | 83 | _id = ops._id; |
18 | | - this$.doc.submitOp(JSON.parse(JSON.stringify(ops))); |
| 84 | + tid = watchdog.track(); |
| 85 | + this$.doc.submitOp(JSON.parse(JSON.stringify(ops)), watchdog.untrack(tid)); |
19 | 86 | return this$.opsIn(ops); |
20 | 87 | }, |
21 | 88 | get: function(){ |
|
0 commit comments