Yomguithereal / baobab-react

React integration for Baobab.
MIT License
310 stars 38 forks source link

Middleware implementation #137

Closed soulprovidr closed 4 years ago

soulprovidr commented 4 years ago

Just wondering if anyone has successfully implemented Redux-esque middleware in an application using baobab-react.

Currently I am using a emmett instance to fire events manually, but it would be nice to be able to listen to dispatch-ed actions without needing to add extra code.

One solution I was considering is to override the dispatch implementation so an event is automatically emitted, and then add an extra method to the Baobab object to allow observers to subscribe to specific actions.

Any advice here? Is there another option that doesn't require modification of baobab and baobab-react?

(If there is interest, I may try to submit a PR implementing this idea).

Yomguithereal commented 4 years ago

Hello @soulprovidr,

Baobab indeed only lets you listen to any change on the tree at each tick, so if you need to know which specific action was fired it is a bit tricky indeed.

One solution I was considering is to override the dispatch implementation so an event is automatically emitted, and then add an extra method to the Baobab object to allow observers to subscribe to specific actions.

Your solution seems to be a good one that does not require to change anything to the libraries.

soulprovidr commented 4 years ago

Hi @Yomguithereal,

Thanks for your feedback, I will try to add this functionality without modifying the library code.

For now I will close this issue and re-open it if I need more direction.

👍