amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.03k stars 1.16k forks source link

.map() or .once() is not working correctly, reproducible code right here #1231

Closed resession closed 2 years ago

resession commented 2 years ago

try the following code, i am on windows

const Gun = require('gun')
// const SEA = Gun.SEA

const gun = Gun({peers: [
  'https://relay.peer.ooo/gun',
  'https://replicant.adamantium.online/gun',
  'http://gun-matrix.herokuapp.com/gun',
  'https://gun-ams1.maddiex.wtf:443/gun',
  'https://gun-sjc1.maddiex.wtf:443/gun',
  'https://shockblox-gun-server.herokuapp.com/gun',
  'https://mg-gun-manhattan.herokuapp.com/gun',
  'https://gunmeetingserver.herokuapp.com/gun',
  'https://gun-eu.herokuapp.com/gun',
  'https://gunjs.herokuapp.com/gun',
  'https://myriad-gundb-relay-peer.herokuapp.com/gun',
  'https://gun-armitro.herokuapp.com/',
  'https://fire-gun.herokuapp.com/gun',
  'http://34.101.247.230:8765/gun',
  'https://gun-manhattan.herokuapp.com/gun',
  'https://us-west.xerberus.net/gun',
  'https://dletta.rig.airfaas.com/gun',
  'https://e2eec.herokuapp.com/gun',
  'https://gun-us.herokuapp.com/gun',
  'https://www.raygun.live/gun'
]})

function test(){
  return new Promise((resolve) => {
    const arr = []
    gun.get('test').get('posts').get({'.': {'<': 'z', '>': 'a', '-': 1}, '%': 100}).once().map().once((found) => {
      if(found !== undefined){
        // delete found['_']
        arr.push(found)
      }
    })
    setTimeout(() => {
      // arr.forEach(data => {if(data !== undefined){delete data['_']}})
      arr.shift()
      resolve(arr)
    }, 10000)
  })
}

test().then(res1 => {
  console.log(res1)
  test().then(res2 => {
    console.log(res2)
    test().then(res3 => {
      console.log(res3)
    }).catch(err => console.error(err))
  }).catch(err => console.error(err))
}).catch(err => console.error(err))

if we do not comment out delete found['_'], then for some reason and somehow a lot of things is logged in the terminal. as you can see we are not logging anything, but it still starts logging gun internals, like console.log(gunInstance).

if we comment out or remove delete found['_'], then only the first promise returns the array with data inside, the second promise and all promises after the first one return an empty array with no data inside.

what it should do is not console log anything, and each promise should returns an array with data inside.

any idea why this is not working correctly?

atordvairn commented 2 years ago

dude thats a lot of relays

On Wed, Apr 27, 2022, 7:11 AM tedd pasta @.***> wrote:

try the following code, i am on windows

const Gun = require('gun') // const SEA = Gun.SEA

const gun = Gun({peers: [ 'https://relay.peer.ooo/gun', 'https://replicant.adamantium.online/gun', 'http://gun-matrix.herokuapp.com/gun', 'https://gun-ams1.maddiex.wtf:443/gun', 'https://gun-sjc1.maddiex.wtf:443/gun', 'https://shockblox-gun-server.herokuapp.com/gun', 'https://mg-gun-manhattan.herokuapp.com/gun', 'https://gunmeetingserver.herokuapp.com/gun', 'https://gun-eu.herokuapp.com/gun', 'https://gunjs.herokuapp.com/gun', 'https://myriad-gundb-relay-peer.herokuapp.com/gun', 'https://gun-armitro.herokuapp.com/', 'https://fire-gun.herokuapp.com/gun', 'http://34.101.247.230:8765/gun', 'https://gun-manhattan.herokuapp.com/gun', 'https://us-west.xerberus.net/gun', 'https://dletta.rig.airfaas.com/gun', 'https://e2eec.herokuapp.com/gun', 'https://gun-us.herokuapp.com/gun', 'https://www.raygun.live/gun' ]})

function test(){ return new Promise((resolve) => { const arr = [] gun.get('test').get('posts').get({'.': {'<': 'z', '>': 'a', '-': 1}, '%': 100}).once().map().once((found) => { if(found !== undefined){ // delete found[''] arr.push(found) } }) setTimeout(() => { // arr.forEach(data => {if(data !== undefined){delete data['']}}) arr.shift() resolve(arr) }, 10000) }) }

test().then(res1 => { console.log(res1) test().then(res2 => { console.log(res2) test().then(res3 => { console.log(res3) }).catch(err => console.error(err)) }).catch(err => console.error(err)) }).catch(err => console.error(err))

if we do not comment out delete found['_'], then for some reason and somehow a lot of things is logged in the terminal. as you can see we are not logging anything, but it still starts logging gun internals, like console.log(gunInstance).

if we comment out or remove delete found['_'], then only the first promise returns the array with data inside, the second promise and all promises after the first one return an empty array with no data inside.

what it should do is not console log anything, and each promise should returns an array with data inside.

any idea why this is not working correctly?

— Reply to this email directly, view it on GitHub https://github.com/amark/gun/issues/1231, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXFW64OFAV2DC7P52EOGOPLVHCLNFANCNFSM5UNSOTXA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

atordvairn commented 2 years ago

instead of this

.once().map().once()

do this

.map().once()
resession commented 2 years ago

instead of this

.once().map().once()

do this

.map().once()

it works, thank you. but i have a question. wouldn't .map().once() mean that any time more keys/data are added, .map().once() will also listen to those changes as well?

"Map iterates over each property and item on a node, passing it down the chain, behaving like a forEach on your data. It also subscribes to every item as well and listens for newly inserted items." "users.map().once(cb) gets each user once, including ones that are added over time."

thats why i was using .once().map().once() because from the docs, it says it should not subscribe to any changes which is what i want "users.once().map().once(cb) gets the user list once, gets each of those users only once (not added ones)."

https://gun.eco/docs/API#-a-name-map-a-gun-map-callback-

resession commented 2 years ago

found the issue

if we have it like how i had it at first .once().map().once(someHandler => {//handle}) then this will only work the first time it runs, after that, it will not work for all other times

i changed it to this and it works now .once(handleThisToo => {//handle this too}).map().once(someHandler => {//handle}) now it works, for some reason we have to handle the data from the first .once(), the docs did not show this. in fact i was following exactly what the docs said and it didn't work, so we need to update the docs on this.

i would still like to keep this issue open since we still don't know why delete data['_'] makes a lot of console logs in the terminal without having any console.log() in the code.

resession commented 2 years ago

closing