Last active
September 23, 2019 14:12
-
-
Save ThinkingJoules/8328f56d54c7f748e85ba4ca44e2031f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mesh.hear = function(raw, peer){ | |
if(!raw){ return } | |
var msg, id, hash, tmp = raw[0]; | |
if(opt.pack <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) } | |
try{msg = msg || decode(raw); | |
}catch(e){return opt.log('DAM JSON parse error', e)} | |
if(msg && Array.isArray(msg)){ | |
var i = 0, m; | |
while(m = msg[i++]){ | |
mesh.hear(m, peer); | |
} | |
return; | |
} | |
if(msg && msg instanceof Object){ | |
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) } | |
if(dup.check(id)){ return } | |
dup.track(id, true).it = msg; // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore? | |
if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) } | |
if(hash && (tmp = msg['@'] || (msg.get && id))){ // Reduces backward daisy in case varying hashes at different daisy depths are the same. | |
if(dup.check(tmp+hash)){ return } | |
dup.track(tmp+hash, true).it = msg; // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore? | |
} | |
(msg._ = function(){}).via = peer; | |
if(tmp = msg['><']){ (msg._).to = Type.obj.map(tmp.split(','), tomap) } | |
if(msg.dam){ | |
if(tmp = mesh.hear[msg.dam]){ | |
tmp(msg, peer, root); | |
} | |
return; | |
} | |
root.on('in', msg); | |
return; | |
} | |
//OPTIONAL VVV Just switching to Mpack should be ~2x faster for decoding, even if it is base64 and you do a second conversion. | |
if(msg && msg instanceof Uint8Array){//trick is being able to send the message through GUN.... Will look in to this bit more. | |
root.on('stream', msg);//would need to add another listener to grab this before it goes in to GUN core. | |
} | |
} | |
var tomap = function(k,i,m){m(k,true)}; | |
mesh.hear.c = mesh.hear.d = 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment