Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Created March 11, 2018 01:45
Show Gist options
  • Save d3x0r/e84218f47ad58d0025b18889a81ce241 to your computer and use it in GitHub Desktop.
Save d3x0r/e84218f47ad58d0025b18889a81ce241 to your computer and use it in GitHub Desktop.
after (10?) seconds, makes gun crash trying to handle circular reference... this USED to work.
/* let's define an appropriate deep default database... */
var dfltSansUsers = {
1: {
name : "org1",
sites : {
1: {
name : "site1",
},
2: {
name : "site2",
},
}
},
3: {
name : "org3",
sites : {
5: {
name : "site5",
},
7: {
name : "site7",
}
}
}
}
var dflt = {
1: {
name : "org1",
sites : {
1: {
name : "site1",
users : {
1: {
name: "alice",
boss : {}, // ** referenced later to betty; as an object
siblings : {
mark : {} // ** see users org3, site 5
}
},
2: {
name: "betty"
}
}
},
2: {
name : "site2",
users : {
1 : {
name : "charlie",
boss : {} // ** referenced later to david }
},
2: {
name : "david"
}
}
},
}
},
3: {
name : "org3",
sites : {
5: {
name : "site5",
users : {
1 : {
name : "edward"
},
2 : {
name : "frank"
},
3 : {
name : "mark"
}
}
},
7: {
name : "site7",
users : {
1 : {
name : "georgia"
},
2 : {
name : "hank"
}
}
}
}
}
}
/*
// these relations also exist.
dflt[1].sites[1].users[1].boss = dflt[1].sites[1].users[2];
dflt[1].sites[1].users[1].siblings.mark = dflt[3].sites[5].users[3];
dflt[1].sites[2].users[1].boss = dflt[1].sites[2].users[2];
*/
/* This can be done with...
var drvr = Gun();
drvr.get( "root" );
drvr.put( dflt );
*/
//var Gun = require( "gun" );
var Gun = require( "gun/gun" );
//require( '.' );
var gun = Gun();
var root2 = gun.get( "root2" );
root2.put( dflt );
root2.map().get( "sites" ).map().get( "users" ).map().on( showMeTheData );
var root = gun.get( "root" );
root.map( getData );
root.put( dfltSansUsers );
//console.log( "data:", root._ )
function getData( val, field, alsoThis ) {
if( typeof( val ) === "object" ) {
var _this = gun.get( val )
_this.map( getData );
console.trace( "path event:", field, val );
} else
console.trace( "field event:", field, val );
}
var alice = gun.get( "alice" ).put( dflt[1].sites[1].users[1] );
var betty = gun.get( "betty" ).put( dflt[1].sites[1].users[2] );
var charlie = gun.get( "charlie" ).put( dflt[1].sites[2].users[1] );
var david = gun.get( "david" ).put( dflt[1].sites[2].users[2] );
var edward = gun.get( "david" ).put( dflt[3].sites[5].users[1] );
var frank = gun.get( "david" ).put( dflt[3].sites[5].users[2] );
var mark = gun.get( "mark" ).put( dflt[3].sites[5].users[3] );
var georgia = gun.get( "david" ).put( dflt[3].sites[7].users[1] );
var hank = gun.get( "hank" ).put( dflt[3].sites[7].users[2] );
console.log( "putting user into a site" );
root.get( "1").get("sites").get("1").get("users" ).put( { 1: alice } );
console.log( "putting user into a site" );
root.get( "1").get("sites").get("1").get("users" ).put( { 2: betty } );
console.log( "putting user into a site" );
root.get( "1").get("sites").get("2").get("users" ).put( { 1: charlie } );
console.log( "putting user into a site" );
root.get( "1").get("sites").get("2").get("users" ).put( { 2: david } );
root.get( "3").get("sites").get("5").get("users" ).put( { 1: edward } );
root.get( "3").get("sites").get("5").get("users" ).put( { 2: frank } );
root.get( "3").get("sites").get("5").get("users" ).put( { 3: mark } );
root.get( "3").get("sites").get("7").get("users" ).put( { 1: georgia } );
root.get( "3").get("sites").get("7").get("users" ).put( { 2: hank } );
//console.log( "data:", root._ )
root.map().get( "sites" ).map().get( "users" ).map().on( showMeTheData );
function showMeTheData( val, field ) {
console.log( "Data!:", field,"=", val );
}
function showMeTheData1( val, field ) {
console.log( "Data1:", field,"=", val );
}
function showMeTheData2( val, field ) {
console.log( "Data2:", field,"=", val );
}
//gun.
//console.log( "put default data..." );
var bossref = gun.get( "root" ).get( "1").get("sites").get("1").get("users").get("2" );
//console.log( "got a ref?" );
gun.get( "root" ).get( "1").get("sites").get("1").get("users").get("1").get("boss" ).put( bossref );
//console.log( "put the ref out?" );
// or
//gun.get( "root" ).get( "1.sites.1.users.1.boss" ).put( bossref );
var bossref = gun.get( "root" ).get( "1").get("sites").get("2").get("users").get("2" );
gun.get( "root" ).get( "1").get("sites").get("2").get("users").get("1" ).put( { boss : bossref } );
var bossref = gun.get( "root" ).get( "3").get("sites").get("5").get("users.3" );
gun.get( "root" ).get( "1").get("sites").get("1").get("users").get("1").get("siblings" ).put( { mark : bossref } );
console.log( "Done now... but should we get a map" );
function Tick() {
console.log( "Tick.." );
setTimeout( Tick, 1000 );
}
Tick();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment