-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
70 lines (62 loc) · 2.12 KB
/
index.js
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
(function (env) {
var Gun;
if(typeof module !== "undefined" && module.exports){ Gun = require('gun/gun') }
if(typeof window !== "undefined"){ Gun = window.Gun }
let globalOpt = {
skipnull: true,
log: false
}
module.exports = function(opt) {
Object.assign(globalOpt, opt)
}
Gun.chain.load = function(cb, opt) {
opt = Object.assign({}, globalOpt, opt)
const gun = this
const root = gun.back(-1)
return this.val((obj, key) => {
// if null or undefined (but shouldnt be able to be that, right ?).. skip it if opt allows
if (obj == null && opt.skipnull) {
if (opt.log) {
console.log('skipping null');
}
return
}
obj = Gun.obj.copy(obj);
const queue = {}
let doc
let done
function expand(o) {
if (!doc) {
doc = o
}
Gun.obj.map(o, (val, prop) => {
const soul = Gun.val.rel.is(val)
if (soul) {
queue[soul] = true
root.get(soul).val(loadedValue => {
queue[soul] = false
loadedValue = Gun.obj.copy(loadedValue)
o[prop] = loadedValue
expand(loadedValue)
})
return
}
// if it doesnt have a soul, just attach the value as is
o[prop] = val
})
const wait = Gun.obj.map(queue, wait => {
if (wait) {
return true
}
});
if (done || wait) {
// dont send the document back yet / or again, we are either already done and have sent the doc, or are still waiting for it load completely
return
}
done = true;
cb(doc, key)
}
expand(obj)
})
}
})()