Skip to content

Commit 8ffc3aa

Browse files
committed
- monitor ops state and disconnect if submitop fails
- bump version
1 parent 5b7241d commit 8ffc3aa

5 files changed

Lines changed: 108 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Logs
22

3+
## v0.5.6
4+
5+
- monitor ops state and disconnect if submitop fails
6+
7+
38
## v0.5.5
49

510
- sharehub: fix bug: error should be thrown if no handler registered.

dist/sharehub.js

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var hub, sharehub;
33
hub = typeof module != 'undefined' && module !== null ? require("./datahub") : datahub;
44
sharehub = function(o){
5-
var this$ = this;
5+
var watchdog, this$ = this;
66
o == null && (o = {});
77
this.evthdr = {};
88
this.data = {};
@@ -11,11 +11,78 @@
1111
this._create = o.create || null;
1212
this._watch = o.watch || null;
1313
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+
};
1480
hub.src.call(this, import$(import$({}, o), {
1581
opsOut: function(ops){
16-
var _id;
82+
var _id, tid;
1783
_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));
1986
return this$.opsIn(ops);
2087
},
2188
get: function(){

dist/sharehub.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dist/**/*"
99
],
1010
"description": "access scoped data via piped operational transformation",
11-
"version": "0.5.5",
11+
"version": "0.5.6",
1212
"homepage": "https://github.com/plotdb/datahub",
1313
"repository": {
1414
"type": "git",

src/sharehub.ls

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,42 @@ sharehub = (o={}) ->
88
@_create = o.create or null
99
@_watch = o.watch or null
1010
@ews = o.ews
11+
12+
watchdog =
13+
timeout: 20000
14+
count: 0, hash: {}, hdr: null
15+
fire: ~>
16+
if watchdog.hdr =>
17+
clearTimeout watchdog.hdr
18+
watchdog.hdr = null
19+
if @ews and @ews.disconnect and @ews.status and @ews.status! == 2 => @ews.disconnect!
20+
watchdog.hash = {}
21+
check: ->
22+
[now, min] = [Date.now!, -1]
23+
for k,v of @hash =>
24+
if (now - v) >= @timeout => return @fire!
25+
if min < 0 or (@timeout - (now - v)) < min => min = ((@timeout - (now - v)) >? 0)
26+
if @hdr =>
27+
clearTimeout @hdr
28+
@hdr = null
29+
if min >= 0 => @hdr = setTimeout (->@check!), min
30+
track: ~>
31+
if @ews and @ews.status and @ews.status! != 2 => return 0
32+
tid = ++watchdog.count
33+
watchdog.hash[tid] = Date.now!
34+
if !watchdog.hdr => watchdog.hdr = setTimeout((~>watchdog.check!), watchdog.timeout)
35+
return tid
36+
untrack: (tid) ->
37+
if !tid => return (->)
38+
(e) ~> if e => @fire! else delete @hash[tid]
39+
1140
hub.src.call @, {} <<< o <<< do
1241
ops-out: (ops) ~>
1342
_id = ops._id
1443
# DATA: we only have to apply if we decide to make a clone of remote obj when init
1544
#@data = json0.type.apply @data, ops
16-
@doc.submitOp JSON.parse(JSON.stringify(ops))
45+
tid = watchdog.track!
46+
@doc.submitOp JSON.parse(JSON.stringify(ops)), watchdog.untrack(tid)
1747
# reflect to other subtree in hub
1848
@ops-in ops
1949
get: ~> @data

0 commit comments

Comments
 (0)