nx-js / observer-util

Transparent reactivity with 100% language coverage. Made with ❤️ and ES6 Proxies.
MIT License
1.2k stars 94 forks source link

Mutating observables in reactions is forbidden #29

Closed ghost closed 6 years ago

ghost commented 6 years ago

I don't understand why I am getting this error. The code where I am mutating the observable is not in a reaction? Here is a sample of what is happening in a custom element.

// create observable store
this.store = observable({
  clickCount: 0,
  clickTimestamp: null
});

// re-render component with async/dedupe Scheduler
observe(() => {
  this.renderCallback();
}, {scheduler: new Scheduler()});

// event handler mutating props
this.addEventListener('click', (e) => {
  this.store.clickCount = this.store.clickCount + 1;
  this.store.clickTimestamp = Date.now(); // <== I GET THE ERROR HERE
})

I am getting the error trying to set the second property on the observable.

solkimicreb commented 6 years ago

Can you also send the code in renderCallback()? I don't see any issues the code you sent.

ghost commented 6 years ago

Sorry, I had a circular looping problem with updating attributes from properties which was firing the attributeChangedCallback on customer elements, which was changing the property. I had a guard clause for this but had a type conversion/comparison bug.