Skip to content

Conversation

demian51
Copy link
Contributor

This adds a new enhancer called with_thunk, which enables dispatching not only plain actions but also thunk functions that take a lager::context.

This allows deferring logic such as conditional dispatch or side effects until runtime, similar to what redux-thunk provides in Redux.

Example usage:

store.dispatch([](lager::context<action> ctx) {
    // perform some effectful logic, then dispatch
    ctx.dispatch(some_action{});
});

Copy link
Owner

@arximboldi arximboldi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you a lot for this contribution!

It looks interesting, I'd be happy to merge it. Can you add a comment to with_thunk explaining this enhancer a little bit more?

Some questions:

  1. I wonder, how does this affect the covariance that is allowed between action types, i.e. normally one can convert context<A> to context<B> as long as B is a subset of actions of A ?

  2. Also, just out of curiosity, what is the use-case that motivated you to implement this? And why was using ctx.loop().post(...) not a viable alternative?

@demian51
Copy link
Contributor Author

Thanks for the feedback!

  1. I believe with_thunk does not affect it. The context is passed through unchanged, so conversions like context<A> to context<B> should still work as before.

  2. It was mostly curiosity. While ctx.loop().post(...) can handle similar cases, it requires capturing the context manually. With with_thunk, the context is passed in, so it's easier to reuse named functions and keep the dispatch flow a bit more explicit. That said, I agree it might be overkill for simpler use cases.

Also, I have been thinking that this could make it possible to build something like Redux Toolkit's createAsyncThunk on top, though I'm not sure yet how useful that would really be.

I'll add a brief comment to with_thunk as suggested.

Let me know if you see any issues or if there's a better direction to consider.

@demian51 demian51 force-pushed the demian51/add_thunk branch from 6a31dce to 3a3f43f Compare March 27, 2025 15:35
@arximboldi
Copy link
Owner

Cool. Thank you for the clarifications. One last nitpick before merging, can you make the comment have this form, so that it can be referenced in the HTML docs later on:

/*!
 * The comment...
 */

Adds an enhancer similar to redux-thunk that allows dispatching either
plain actions or thunks (functions taking a lager::context). This
enables deferring logic such as side effects or conditional dispatches
to runtime.
@demian51 demian51 force-pushed the demian51/add_thunk branch from 3a3f43f to 5ce48fe Compare March 28, 2025 03:11
@demian51
Copy link
Contributor Author

Sure! I've updated the comment to follow the Doxygen format.

Thanks for the thorough review!

@arximboldi arximboldi merged commit 4218503 into arximboldi:master Mar 28, 2025
@demian51 demian51 deleted the demian51/add_thunk branch April 4, 2025 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants