Create mem.js
Showing
1 changed file
with
62 additions
and
0 deletions
mem.js
0 → 100644
1 | (function() { | ||
2 | var Gun = typeof window !== "undefined" ? window.Gun : require("gun"); | ||
3 | |||
4 | Gun.on("opt", function(ctx) { | ||
5 | this.to.next(ctx); | ||
6 | var opt = ctx.opt; | ||
7 | if (ctx.once) { | ||
8 | return; | ||
9 | } | ||
10 | |||
11 | var graph = ctx.graph, | ||
12 | acks = {}, | ||
13 | count = 0, | ||
14 | to; | ||
15 | |||
16 | ctx.on("put", function(at) { | ||
17 | this.to.next(at); | ||
18 | Gun.graph.is(at.put, null, null); | ||
19 | if (!at["@"]) { | ||
20 | acks[at["#"]] = true; | ||
21 | } // only ack non-acks. | ||
22 | count += 1; | ||
23 | if (count >= (opt.batch || 10000)) { | ||
24 | return flush(); | ||
25 | } | ||
26 | if (to) { | ||
27 | return; | ||
28 | } | ||
29 | to = setTimeout(flush, opt.wait || 1); | ||
30 | }); | ||
31 | |||
32 | ctx.on("get", function(at) { | ||
33 | // this.to.next(at); //What does this do? | ||
34 | var lex = at.get, | ||
35 | soul, | ||
36 | data, | ||
37 | opt, | ||
38 | u; | ||
39 | |||
40 | if (!lex || !(soul = lex["#"])) { | ||
41 | return; | ||
42 | } | ||
43 | var field = lex["."]; | ||
44 | if (data && field) { | ||
45 | data = Gun.state.to(data, field); | ||
46 | } | ||
47 | ctx.on("in", { "@": at["#"], put: Gun.graph.node(data) }); | ||
48 | }); | ||
49 | |||
50 | var wait; | ||
51 | var flush = function() { | ||
52 | if (wait) { | ||
53 | return; | ||
54 | } | ||
55 | wait = true; | ||
56 | clearTimeout(to); | ||
57 | to = false; | ||
58 | var ack = acks; | ||
59 | acks = {}; | ||
60 | }; | ||
61 | }); | ||
62 | })(); |
-
Please register or sign in to post a comment