bye.js 941 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  2. Gun.on('opt', function(root){
  3. this.to.next(root);
  4. if(root.once){ return }
  5. root.on('in', function(msg){
  6. //Msg did not have a peer property saved before, so nothing ever went further
  7. if(!msg.mesh || !msg.BYE){ return this.to.next(msg) }
  8. var peer = msg.mesh.via;
  9. (peer.bye = peer.bye || []).push(msg.BYE);
  10. })
  11. root.on('bye', function(peer){
  12. this.to.next(peer);
  13. if(!peer.bye){ return }
  14. var gun = root.gun;
  15. Gun.obj.map(peer.bye, function(data){
  16. Gun.obj.map(data, function(put, soul){
  17. gun.get(soul).put(put);
  18. });
  19. });
  20. peer.bye = [];
  21. });
  22. });
  23. Gun.chain.bye = function(){
  24. var gun = this, bye = gun.chain(), root = gun.back(-1), put = bye.put;
  25. bye.put = function(data){
  26. gun.back(function(at){
  27. if(!at.get){ return }
  28. var tmp = data;
  29. (data = {})[at.get] = tmp;
  30. });
  31. root.on('out', {BYE: data});
  32. return gun;
  33. }
  34. return bye;
  35. }