I'm trying to create a set of classes that all need to access each other. Following the MobX guide, I would like to have my structure as such:
RootStore
├─ ApplicationStore
├─ UserStore
├─ EventStore
└─ ...Store
Now if I construct each Store with RootStore parameter, I can look up events for user etc. All is good, application works but Jest test explodes when I want to use type hinting:
RootStore.js
import UserStore from './UserStore.mobx'
import ApplicationStore from './ApplicationStore.mobx'
export default class RootStore {
userStore: UserStore
appStore: ApplicationStore
}
UserStore.js
import RidersProvider from '../providers/RidersProvider'
import RootStore from './RootStore.mobx'
export default class UserStore {
provider: RidersProvider
rootStore: RootStore
@observable _users = []
constructor (rootStore: ?RootStore, userProvider: ?RidersProvider) {
this.rootStore = rootStore
this.provider = userProvider
}
...
Now if I remove the imports in UserStore, it all works fine, but then I get no type hinting. Is there a good solution to this? I've spent days going through articles on circular references (and losing my mind) but I couldn't find a clue how to properly fix this.
The reference issue I have is as follows:
Require cycle: src/Connection.js -> stores/RootStore.singleton.js -> stores/RootStore.mobx.js -> stores/UserStore.mobx.js -> providers/RidersProvider.js -> src/Connection.js
So Connection
uses RootStore
to access ApplicationStore.accessToken
, which is required to get user detail in UserStore
. At this point I could just get rid of RootStore
which is mentioned in MobX best practices though, unless I misunderstood the concept.
But the problem isn't as much with the RootStore but the fact that if I want to type hint using provider: RidersProvider
it fails. In PHP I would have just used a ProviderInterface
(and StoreInterface) and never run into a circular dependency but in JS I don't see an alternative other than TypeScript, but is there one?