gun/lib/super.js
Adriano Rogowski 680b2d115a AXE - peer/soul subscribe. In this version the peers receive only data subscribed with gun.get(...soul...).once(...)
TODO:
 1) if the peer change the id, will lost the subscribes. Workaround for this is set the pid (unique) in: Gun(pid: 'mypeerid', peers:[...])
 2) If the superpeer die, the subscribes will be lost. Each peer must resubscribe all souls.
2018-11-21 19:28:28 -02:00

30 lines
931 B
JavaScript

;(function(){
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
var Rad = (Gun.window||{}).Radix || require('./radix');
function input(msg){
var at = this.as, to = this.to, peer = (msg.mesh||empty).via;
var get = msg.get, soul, key;
if(!peer || !get){ return to.next(msg) }
console.log("super", msg);
if(soul = get['#']){
if(key = get['.']){
} else {
}
subscribe(soul, peer, msg);
}
to.next(msg);
}
/// Store the subscribes
Gun.subscribe = {}; /// TODO: use Rad instead of plain object?
function subscribe(soul, peer, msg) {
if (!peer.id) { console.log('super jump peer without id: ', peer, msg); return; } /// TODO: this occurs in first subscription. Use peer reference or peer.wire.id?
Gun.subscribe[soul] = Gun.subscribe[soul] || {};
Gun.subscribe[soul][peer.id] = 1;
}
var empty = {}, u;
if(Gun.window){ return }
try{module.exports = input}catch(e){}
}());