What is the current `map()` doing?

102 views Asked by At

( Gun.version 0.9.6 )

Given the following data structure

{
    lights:{
        1:{
            state:{
                on:true,
                color:'red',
                br:254
            }
        },
        2:{
            state:{
                on:true,
                color:'red',
                br:254
            }
        },
        3:{
            state:{
                on:true,
                color:'red',
                br:254
            }
        }
    }
}    

I want to turn the lights on/off at the same time. gun.get('lights').val(cb) does give me {1:{...},2:{...},3:{...}} and gun.get('lights').path('1.state.on').put(false); works perfect.

I thought that by doing gun.get('lights').map().path('1.state.on').put(false) it would 'map' over all lights but instead it only changes the first light.

Question: How do i turn on/off all the lights ?

2

There are 2 answers

0
marknadal On

@stef-de-vries , you've spotted an important TODO bug in gun's source code!

Currently (v0.9.6), put does not work with map() commands. I think it only picks the first item (which may be random) and then turns it off.

This is bad, and needs to be addressed.

For now, the work around is to probably do something like:

gun.get('lights').map().path('state.on').val(function(){
  this.put(false);
});

Which is ugly.

0
Stef de Vries On

Hmm.. okay, but it fails on gun.get('lights').map().val(cb) also if i run it the second time.

Solved my problem with the each() module

gun.get('lights').each(node=>{
          gun.get(node._['#']).path('state.on').put(true)
        })