javascript - How to handle multiple stores of same type in Flux / ReactJS? -
i'm new flux/react , i'm having hard time understanding of fundamental architecture decisions:
- i know stores meant singletons, created @ app start, or can lifetime of store smaller, specific user's actions?
- can have multiple instances of same store type, each initialized different context?
unfortunately, examples i've seen seem simplistic answer these questions. let's start facebook's chat app example. there multiple threads each messages. messagestore holds messages entire app , method called getallforthread(id) returns filtered subset of messages. when message comes thread, emits change notification causes messagesection react component re-fetch data (regardless of thread user viewing). doesn't scale. if had 10,000 threads each lots of message activity? here's how decided solve issue:
- each messagestore initialized thread id.
- create singleton messagestorefactory creates , manages messagestores.
- when user clicks on thread, instead of react component subscribing global messagestore, asks messagestorefactory messagestore that specific thread.
- if factory has messagestore thread, returns it. otherwise creates one, kicks off async task fetch initial data it, , returns it.
- when react component torn down (let's user navigates away it), notifies factory it's done store. using reference counting or other cache logic allow factory prune unused stores.
how far off base approach? there simpler approach still scales?
it seems easier make smarter data fetching according thread user viewing. see facebook's example @ blog-post or presentation?
Comments
Post a Comment