mem.js
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(function() {
var Gun = typeof window !== "undefined" ? window.Gun : require("gun/gun");
Gun.on("opt", function(ctx) {
this.to.next(ctx);
var opt = ctx.opt;
if (ctx.once) {
return;
}
var graph = ctx.graph,
acks = {},
count = 0,
to;
ctx.on("put", function(at) {
this.to.next(at);
Gun.graph.is(at.put, null, null);
if (!at["@"]) {
acks[at["#"]] = true;
} // only ack non-acks.
count += 1;
if (count >= (opt.batch || 10000)) {
return flush();
}
if (to) {
return;
}
to = setTimeout(flush, opt.wait || 1);
});
ctx.on("put", function(at) {
this.to.next(at);
Gun.graph.is(at.put, null, null);
if (!at["@"]) {
acks[at["#"]] = true;
} // only ack non-acks.
count += 1;
if (count >= (opt.batch || 10000)) {
return flush();
}
if (to) {
return;
}
to = setTimeout(flush, opt.wait || 1);
var id = at['#']
ctx.on('in', {"@": id, ok:1})
});
ctx.on("get", function(at) {
// this.to.next(at); //What does this do?
var lex = at.get,
soul,
data,
opt,
u;
if (!lex || !(soul = lex["#"])) {
return;
}
var field = lex["."];
if (data && field) {
data = Gun.state.to(data, field);
}
ctx.on("in", { "@": at["#"], put: Gun.graph.node(data) });
});
var wait;
var flush = function() {
if (wait) {
return;
}
wait = true;
clearTimeout(to);
to = false;
var ack = acks;
acks = {};
};
});
})();