You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
conststate=require('./state')app.locals.state=stateapp.get('/article',(req,res)=>{Article.find((err,article)=>{// IMPORTANT: Make sure to only update your state all at once, right before renderingstate.set({title: article.title})res.render('article')})})
Wait, WAT? Isn't that going to leak state across requests?
Here in lies the big exprimental gamble. We're clearing the state tree out on every event loop tick when used on the server. Since Node's HTTP servers don't handle requests synchronously, we're hoping that's enough to ensure nothing weird happens. We're also turning off Baobab's persistence, immutability, and all that other client-side-useful stuff in hopes it's enough to make performance not dramatically suffer with this approach.
Caveats
Must use only use JSON primative data in tree for clone perf
Only update your state all at once, right before rendering
Contributing
Please fork the project and submit a pull request with tests. Install node modules npm install and run tests with npm test.
License
MIT
About
Tiny wrapper around Baobab for an OOTB universal state tree