Skip to content

Instantly share code, notes, and snippets.

@evdama
Last active June 17, 2020 13:25
Show Gist options
  • Save evdama/11b98ece9706d5415c65e57f652fb02d to your computer and use it in GitHub Desktop.
Save evdama/11b98ece9706d5415c65e57f652fb02d to your computer and use it in GitHub Desktop.
Gun.chain.subscribe = function( publish ) { // arrow function would loose scope i.e. its this so doesn't work
let gun = this
let at = gun._
let isMap = !!at && !!at.back && !!at.back.each // check if the map() function has been called
if ( isMap ) {
let store = new Map()
publish( Array.from( store ) )
gun = gun.on( (data, _key, as) => {
let key = _key || ( ( data || {} )._ || {} )[ '#' ] || as.via.soul
if ( data === null ) {
store.delete(key)
} else {
store.set(key, data)
}
publish( Array.from(store) )
})
} else {
gun = gun.on( data => publish(data) )
}
return gun.off()
}
gun = Gun( {
peers: [
// location.origin + '/gun',
// 'https://everyday-market-eu.herokuapp.com/gun',
// 'https://everyday-market-us.herokuapp.com/gun'
],
// localStorage: false
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment