supermedium/superframe

[state]: infinite recursion

Closed this issue · 2 comments

If something is listening to the state updates and modifies the state on state update this will fall into infinite recursion because the copying of the state to old state is happening after notifying subscribers.

Reproduce:

  1. Have something listening to the state (i.e. some custom aframe component with keysToWatch=['anything'])
  2. This custom component will have a onStateUpdate() method. In this method dispatch any action in the state: this.el.sceneEl.emit('updateSomething', payload)
  3. Update state.anything and you will fall into infinite recursion.

The root cause: subscribers are being notified about state update before this.state is copied to this.lastState

for (i = 0; i < toUpdate.length; i++) {

this.copyState(this.lastState, this.state);

I have a fix ready, just want to confirm my understanding is correct before creating a pull request.

That seems plausible, I'd love to see a fix, thanks!

I've written this system in other languages, and evolved how it works a bit. I wait for a subscription change to fully release. If there are nested state changes, instead of immediately notifying subscribers, I pretty much batch them all towards at the end of the subscription loop to notify subscribers.

It's very simple to be honest, but should work :)