-
Notifications
You must be signed in to change notification settings - Fork 1
/
synchronous.js
58 lines (56 loc) · 1.15 KB
/
synchronous.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
(function (env) {
var Gun;
if(typeof module !== "undefined" && module.exports){ Gun = require('gun/gun') }
if(typeof window !== "undefined"){ Gun = window.Gun }
Gun.chain.sync = function (obj, opt, cb, o) {
var gun = this;
if (!Gun.obj.is(obj)) {
console.log('First param is not an object');
return gun;
}
if (Gun.bi.is(opt)) {
opt = {
meta: opt
};
}
if(Gun.fn.is(opt)){
cb = opt;
opt = null;
}
cb = cb || function(){};
opt = opt || {};
opt.ctx = opt.ctx || {};
gun.on(function (change, field) {
Gun.obj.map(change, function (val, field) {
if (!obj) {
return;
}
if (field === '_' || field === '#') {
if (opt.meta) {
obj[field] = val;
}
return;
}
if (Gun.obj.is(val)) {
var soul = Gun.val.rel.is(val);
if (opt.ctx[soul + field]) {
// don't re-subscribe.
return;
}
// unique subscribe!
opt.ctx[soul + field] = true;
this.path(field).sync(
obj[field] = (obj[field] || {}),
Gun.obj.copy(opt),
cb,
o || obj
);
return;
}
obj[field] = val;
}, this);
cb(o || obj);
});
return gun;
};
}());