621198f3 by Lorenzo Mangani Committed by GitHub

Create mem.js

1 parent d47cdfa9
Showing 1 changed file with 62 additions and 0 deletions
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 })();
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!