list.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var Gun = Gun || require('../gun');
  2. Gun.chain.list = function(cb, opt){
  3. opt = opt || {};
  4. cb = cb || function(){};
  5. var gun = this.put({}); // insert assumes a graph node. So either create it or merge with the existing one.
  6. gun.last = function(obj, cb){
  7. var last = gun.path('last');
  8. if(!arguments.length){ return last }
  9. return gun.path('last').put(null).put(obj).val(function(val){ // warning! these are not transactional! They could be.
  10. console.log("last is", val);
  11. last.path('next').put(this._.node, cb);
  12. });
  13. }
  14. gun.first = function(obj, cb){
  15. var first = gun.path('first');
  16. if(!arguments.length){ return first }
  17. return gun.path('first').put(null).put(obj).val(function(){ // warning! these are not transactional! They could be.
  18. first.path('prev').put(this._.node, cb);
  19. });
  20. }
  21. return gun;
  22. };
  23. (function(){ // list tests
  24. return;
  25. var Gun = require('../index');
  26. var gun = Gun({file: 'data.json'});
  27. Gun.log.verbose = true;
  28. var list = gun.list();
  29. list.last({name: "Mark Nadal", type: "human", age: 23}).val(function(val){
  30. //console.log("oh yes?", val, '\n', this.__.graph);
  31. });
  32. list.last({name: "Amber Cazzell", type: "human", age: 23}).val(function(val){
  33. //console.log("oh yes?", val, '\n', this.__.graph);
  34. });
  35. list.list().last({name: "Hobbes", type: "kitten", age: 4}).val(function(val){
  36. //console.log("oh yes?", val, '\n', this.__.graph);
  37. });
  38. list.list().last({name: "Skid", type: "kitten", age: 2}).val(function(val){
  39. //console.log("oh yes?", val, '\n', this.__.graph);
  40. });
  41. setTimeout(function(){ list.val(function(val){
  42. console.log("the list!", list.__.graph);
  43. return;
  44. list.path('first').val(Gun.log)
  45. .path('next').val(Gun.log)
  46. .path('next').val(Gun.log);
  47. })}, 1000);
  48. return;
  49. gun.list().map(function(val, id){
  50. console.log("each!", id, val);
  51. })
  52. }());