bye.js 817 B

1234567891011121314151617181920212223242526272829303132333435
  1. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  2. Gun.on('create', function(root){
  3. this.to.next(root);
  4. var mesh = root.opt.mesh;
  5. if(!mesh){ return }
  6. mesh.hear['bye'] = function(msg, peer){
  7. (peer.byes = peer.byes || []).push(msg.bye);
  8. }
  9. root.on('bye', function(peer){
  10. this.to.next(peer);
  11. if(!peer.byes){ return }
  12. var gun = root.$;
  13. Gun.obj.map(peer.byes, function(data){
  14. Gun.obj.map(data, function(put, soul){
  15. gun.get(soul).put(put);
  16. });
  17. });
  18. peer.byes = [];
  19. });
  20. });
  21. Gun.chain.bye = function(){
  22. var gun = this, bye = gun.chain(), root = gun.back(-1), put = bye.put;
  23. bye.put = function(data){
  24. gun.back(function(at){
  25. if(!at.get){ return }
  26. var tmp = data;
  27. (data = {})[at.get] = tmp;
  28. });
  29. root.on('out', {bye: data});
  30. return gun;
  31. }
  32. return bye;
  33. }