ctrlplusb
ctrlplusb

Reputation: 13147

Is it expected behaviour for "map" to return a node multiple times? (i.e. duplicates)

I was experimenting with using gun in a server side rendering (SSR) context and noticed that I began to receive duplicate items in the map callback. The duplicate count was n, where n was the number of times I had refreshed the page.

I did some poking around and realised that I was spawning a gun instance for every request to my server. So basically a new peer was being created for every request and therefore map was returning a duplicate of each node for each peer in the network.

Is this expected behaviour?

Upvotes: 2

Views: 77

Answers (1)

marknadal
marknadal

Reputation: 7624

Yes, by default gun is peer-to-peer (P2P) which means that every peer (even peers connected through other peers) will try to reply to requests.

Why? If you are not running a centralized server (which you can with gun, but you also don't have to), there is no guarantee that if 1 peer replies that they have the latest or all of the data you want.

However, you are correct, that creating a new gun database instance for every server request... is unnecessary. Does this answer the question?

Also note: map subscribes to the table and the items (as they are added) in it. Meaning that map will get called for every item in the table/list (as they are added even in the future), and when an item updates it will get called again for just that item.

If you only want to get each item once do map().val(cb) however this will still get called for new items that are added. Just each item, only once.

Upvotes: 1

Related Questions