question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Let effects return the `actions` object

See original GitHub issue

Actions are functions you call like actions.myAction(data). When you call an action, HyperApp checks if it belongs to effects or reducers, then:

  • for reducers
    • applies the reducer to update the model, newModel = reducer(oldModel, data)
  • for effects
    • calls the effect effect(data)

Finally, the action function returns (undefined).

The proposal is, amend the previously described behavior, and return the actions object instead.

This would allow chaining effects / reducers.

Effects

Effects, can be asynchronous, so we must return a Promise to make sure they are executed in order.

actions.myReducer1().myReducer2().myEffect1().then(actions.myEffect2).then(actions.myEffect3)
app({
    effects: {
       bigEffect: async (model, actions, data) => {
           actions.myReducer1().myReducer2()
           await actions.myEffect1()
           await actions.myEffect2()
           actions.myReducer3().myReducer4()
        }
    }
})

Reducers

Reducers are sync, so there’s no need to return a promise in this case.

actions.myReducer1().myReducer2()
Notes
  • Chaining reducers should be limited to reducers, otherwise you’d end up using them like an effect which is wrong. Why? Because reducers cause the view to be re-rendered.
  • There’s no need to re-render after each reducer, so this mechanism should try to be efficient. Also related: https://github.com/hyperapp/hyperapp/issues/90

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
rbiggscommented, Feb 24, 2017

I think sticking as closely as possible to the Elm Architecture is the biggest selling point, otherwise it will become like CycleJS.

2reactions
jorgebucarancommented, Feb 27, 2017

It shouldn’t if we’re using requestAnimationFrame, but TBH I don’t know if I’d like to proceed with this feature, it seems it creates more of an anti-pattern than anything else.

The thing, sometimes you’d like to run several reducers, so instead of:

actions.one()
actions.two()
actions.three()

you could do:

actions.one().two().three()

Effect actions, return what your effect function returns, so you can return a promise which is fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

let - JavaScript - MDN Web Docs
The let declaration declares a block-scoped local variable, optionally initializing it to a value.
Read more >
React useReducer Hook ultimate guide - LogRocket Blog
An action: An object that tells the reducer how to change the state. It must contain a type property and can contain an...
Read more >
Side-effects in Compose - Android Developers
A side-effect is a change to the state of the app that happens outside the scope of a composable function. Due to composables'...
Read more >
Apply multiple animation effects to one object - Microsoft Support
Open the Animation Pane. Select the object on the slide that you want to animate. On the Animations tab, click Animation Pane. Open...
Read more >
API Reference - Redux-Saga
Creates an Effect description that instructs the middleware to wait for a specified action on the Store. The Generator is suspended until an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found