jorgebucaran / hyperapp

1kB-ish JavaScript framework for building hypertext applications
MIT License
19.07k stars 779 forks source link

What about setState? #159

Closed FlorianWendelborn closed 7 years ago

FlorianWendelborn commented 7 years ago

Recently effects and reducers got merged into actions. Now the way to set the state is by just retuning an object.

What about exposing a actions.setState function which can be called from everywhere where actions are available instead? This way an effect could still return whatever it wants when called by a different action (I actually used that a lot) and code like this becomes easier to do:

function example () {
    actions.room.set(room) // reducer
    actions.room.socket.join() // effect
    // the key here is that socket.join depends on model.room
}
function example () {
    actions.setState({room})
    actions.room.socket.join()
}

This would also fix use-cases where big actions need to be split into multiple actions, but need to return data that is not a promise. Hard to describe, but I actually also used that for WebRTC SDP patching. I worked around this, but overall I think that this might make hyperapp easier to use for more advanced apps.

And yes, I'm aware that setState() would basically be implemented as setState: (_, data) => data.

jorgebucaran commented 7 years ago

Sorry, I don't understand. There is only one way to update the model.

actions: {
   updateTheModel: _ => newModel
}