Pattern to handle reactive data changes in Meteor using sagas, perhaps using runSaga()
See original GitHub issueI’m trying to come up with a pattern for handling changes to reactive data. Without using sagas, I might be tempted to write something like:
Meteor.autorun(() => {
const user = Meteor.user();
dispatch({
type: "USER_CHANGED",
payload: user
});
});
This will fire the action anytime the user changes. I can’t help but think that there is a much better way to do this, possibly using runSaga()
but I really need some help seeing the way forward. The problem is essentially: How do I integrate reactive changes into the redux universe? Ideally, the pattern would support starting and stopping the monitoring of the reactive data. Being able to discern what exactly changed in the data (USER_LOGOUT, USER_DELETED, etc) would be grand, but mostly what I’m wanting is to have all the code that does this kind of thing built around the same infrastructure as the rest of the side-effect generating code. In other words, built on sagas and generators. Thanks for any help.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8
Top GitHub Comments
Here is my personal approach to sync Meteor Collections with the Redux store. The
meteorDataSaga
generatorfunction*
takes anArray
ofMongo.Collection
as its first parameter:The
meteorDataSaga
dispatches an action of typeMETEOR_FETCH_DATA
withdata
andentityType
properties whenever the reactive computation for a Collection reruns. It’s then up to thereducer
to handle these actions. I normalize the data into an Immutable data structure with anentities
reducer:This way my Redux store is always in sync with all the local MiniMongo Collections i provide to the Saga. Hope this helps and please let me know if there is a better and more elegant way to do this…
That pretty much depends on your requirements. The simple
fetch
works well as long as you don’t have to deal with really large data sets. But also of course if you want a more fine grained tracking of data changes thenobserve
or evenobserveChanges
is the preferred path…