1 |
|
2 | var Gun = require('./core');
|
3 | Gun.chain.map = function(cb, opt, t){
|
4 | var gun = this, cat = gun._, chain;
|
5 | if(!cb){
|
6 | if(chain = cat.fields){ return chain }
|
7 | chain = cat.fields = gun.chain();
|
8 | chain._.val = gun.back('val');
|
9 | gun.on('in', map, chain._);
|
10 | return chain;
|
11 | }
|
12 | Gun.log.once("mapfn", "Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
|
13 | chain = gun.chain();
|
14 | gun.map().on(function(data, key, at, ev){
|
15 | var next = (cb||noop).call(this, data, key, at, ev);
|
16 | if(u === next){ return }
|
17 | if(Gun.is(next)){
|
18 | chain._.on('in', next._);
|
19 | return;
|
20 | }
|
21 | chain._.on('in', {get: key, put: next, gun: chain});
|
22 | });
|
23 | return chain;
|
24 | }
|
25 | function map(at){
|
26 | if(!at.put || Gun.val.is(at.put)){ return }
|
27 | if(this.as.val){ this.off() }
|
28 | obj_map(at.put, each, {cat: this.as, gun: at.gun});
|
29 | this.to.next(at);
|
30 | }
|
31 | function each(v,f){
|
32 | if(n_ === f){ return }
|
33 | var cat = this.cat, gun = this.gun.get(f), at = (gun._);
|
34 | (at.echo || (at.echo = {}))[cat.id] = cat;
|
35 | }
|
36 | var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u;
|
37 | |
\ | No newline at end of file |