-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
57 lines (52 loc) · 1.78 KB
/
list.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
var Gun = Gun || require('../gun');
Gun.chain.list = function(cb, opt){
opt = opt || {};
cb = cb || function(){};
var gun = this.put({}); // insert assumes a graph node. So either create it or merge with the existing one.
gun.last = function(obj, cb){
var last = gun.path('last');
if(!arguments.length){ return last }
return gun.path('last').put(null).put(obj).val(function(val){ // warning! these are not transactional! They could be.
console.log("last is", val);
last.path('next').put(this._.node, cb);
});
}
gun.first = function(obj, cb){
var first = gun.path('first');
if(!arguments.length){ return first }
return gun.path('first').put(null).put(obj).val(function(){ // warning! these are not transactional! They could be.
first.path('prev').put(this._.node, cb);
});
}
return gun;
};
(function(){ // list tests
return;
var Gun = require('../index');
var gun = Gun({file: 'data.json'});
Gun.log.verbose = true;
var list = gun.list();
list.last({name: "Mark Nadal", type: "human", age: 23}).val(function(val){
//console.log("oh yes?", val, '\n', this.__.graph);
});
list.last({name: "Amber Cazzell", type: "human", age: 23}).val(function(val){
//console.log("oh yes?", val, '\n', this.__.graph);
});
list.list().last({name: "Hobbes", type: "kitten", age: 4}).val(function(val){
//console.log("oh yes?", val, '\n', this.__.graph);
});
list.list().last({name: "Skid", type: "kitten", age: 2}).val(function(val){
//console.log("oh yes?", val, '\n', this.__.graph);
});
setTimeout(function(){ list.val(function(val){
console.log("the list!", list.__.graph);
return;
list.path('first').val(Gun.log)
.path('next').val(Gun.log)
.path('next').val(Gun.log);
})}, 1000);
return;
gun.list().map(function(val, id){
console.log("each!", id, val);
})
}());