Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Created January 8, 2019 08:24
Show Gist options
  • Save d3x0r/5145f71ff4437d6ca71ff809b3fb5e5d to your computer and use it in GitHub Desktop.
Save d3x0r/5145f71ff4437d6ca71ff809b3fb5e5d to your computer and use it in GitHub Desktop.
Demonstration of difference between GunDB's on and once
var Gun = require( ".." );
var gun = new Gun();
var db = gun.get( "db" );
var player1Node = gun.get( "player1" );
var player2Node = gun.get( "player2" );
var players = db.get( "players" );
//players.map().on( onPlayer );
players.map().once( oncePlayer );
function onPlayer(val,key ) {
console.log( "on Player: ", val );
}
function oncePlayer(val,key ) {
console.log( "once Player: ", val );
}
player1Node.put( {name:'bob', x:0, y:0} );
player2Node.put( {name:'tom', x:0, y:0} );
players.set( player1Node );
players.set( player2Node );
setTimeout( ()=>{
player1Node.put( {x:1, y:0} );
}, 500 );
setTimeout( ()=>{
player1Node.put( {x:2, y:0} );
}, 1000 );
setTimeout( ()=>{
player1Node.put( {x:3, y:0} );
}, 1500 );
setTimeout( ()=>{
console.log( "Changing to on instead of once" );
players.map().off();
players.map().on( onPlayer );
player1Node.put( {x:3, y:0} );
}, 2900 );
setTimeout( ()=>{
player1Node.put( {x:4, y:0} );
}, 2500 );
setTimeout( ()=>{
player1Node.put( {x:5, y:0} );
}, 3000 );
setTimeout( ()=>{
player1Node.put( {x:6, y:0} );
}, 3500 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment