Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test.async();
// Now we want to connect to every gun server peer...
var peers = [], i = env.config.servers;
while(i--){
// For the total number of servers listed in the configuration
// Add their URL into an array.
peers.push('http://'+ env.config.IP + ':' + (env.config.port + (i + 1)) + '/gun');
}
// Pass all the servers we want to connect to into gun.
//var gun = Gun();
var gun = Gun(peers);
// Now we want to create a list
// of all the messages that WILL be sent
// according to the expected configuration.
// This is equal to...
var num = 0, total = 0, check = Gun.obj.map(env.ids, function(v,id,t){
// for each browser ID
// they will be saving X number of messages each.
var i = env.config.each;
while(i--){
// So add a deterministic key we can check against.
t(id + (i + 1), 1);
// And count up the total number of messages we expect for all.
total += 1;
}
});
// Note, this `check` hash table now looks something like this:
// {alice1: 1, alice2: 1, alice3: 1, bob1: 1, bob2: 1, bob3: 1}
var report = $("<div>").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(num +" / "+ total +" Verified").prependTo('body');
// Add a nifty UI that tells us how many messages have been verified.
// FINALLY, tell gun to subscribe to every record
// that is is/will be saved to this table.</div>
//$(log).append(el.attr('id', key).text(key +": "+ data));
$(log).text(key +": "+ data); // DOM updates thrash performance, try this.
// Scroll down with the logging.
//$('body').stop(true).animate({scrollTop: $(log).height()});
// Now, make sure the received data
// matches exactly the data we EXPECT
if(("Hello world, "+key+"!") === data){
// if it does, we can "check off" record
// Bump the total number of verified items and update the UI.
if(check[key]){ num += 1 }
// from our verify todo list.
check[key] = 0;
report.text(num +" / "+ total +" Verified");
}
// This next part is important:
if(Gun.obj.map(check, function(still){
// IF THERE ARE ANY RECORDS STILL LEFT TO BE VERIFIED
if(still){ return true }
})){ return } // return, PREVENTING the test from being finished.
// IF HOWEVER, every single message
// that we EXPECTED to see
// has now been seen
// then THIS ONE BROWSER PEER (of many peers)
// is finally done.
test.done();
});
// But we have to actually tell the browser to save data!
tests.push(client.run(function(test){
Gun.state.drift = Math.random() * 10000;
localStorage.clear();
var env = test.props;
test.async();
var peers = [], i = env.i;
//while(i--){
peers.push('http://'+ env.config.IP + ':' + (env.config.port + (i)) + '/gun');
//}
console.log("Connect to", peers);
var gun = Gun(peers);
var num = 0, total = 0, check = Gun.obj.map(env.ids, function(v,id,t){
var i = env.config.each;
while(i--){
t(id + (i + 1), 1);
total += 1;
}
});
var report = $("<div>").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(num +" / "+ total +" Verified").prependTo('body');
var wait;
gun.get('test').map().on(function(data, key){
//$(log).text(key +": "+ data);
if(("Hello world, "+key+"!") === data){
if(check[key]){ num += 1 }
check[key] = 0;
report.text(num +" / "+ total +" Verified");
}
if(wait){ return }</div>
wait = setTimeout(function(){
wait = false;
if(Gun.obj.map(check, function(still){
if(still){ return true }
})){ return }
console.log("SUCCESS");
test.done();
},10);
});
ref._.root.opt.mesh.hear = function(raw, peer){
var msg = Gun.obj.ify(raw);
console.log('hear:', msg);
hear(raw, peer);
ref.hear.push(msg);
}
var say = ref._.root.opt.mesh.say;