matrix-react-sdk
matrix-react-sdk copied to clipboard
Store refactor: use non-global stores in components
This is part of a series of PRs aiming to improve stores. This PR focuses on a pattern for passing stores to components in a way that doesn't involve global singletons.
Added a new kind of class:
- Add God object
Storeswhich will hold refs to all known stores and theMatrixClient. This object is NOT a singleton. - Add
StoresContextto hold onto a ref ofStoresfor use inside components.
StoresContext is created via:
- Create
StoresinMatrixChat, assigning theMatrixClientwhen we have one set. Currently sets the RVS toRoomViewStore.instance. - Wrap
MatrixChatsrender()function in aStoresContext.Providerso it can be used anywhere.
StoresContext is currently only used in RoomView via the following changes:
- Remove the HOC, which redundantly set
mxClientas a prop. We don't need this asRoomViewwas using the client fromthis.context. - Change the type of context accepted from
MatrixClientContexttoStoresContext. - Modify alllll the places where
this.contextis used to interact with the client and suffix.client. - Modify places where we use
RoomViewStore.instanceand replace them withthis.context.roomViewStore.
This makes RoomView use a non-global instance of RVS.
Checklist
- [ ] Tests written for new code (and old code if feasible)
- [ ] Linter and other CI checks pass
- [ ] Sign-off given on the changes (see CONTRIBUTING.md)
This change is marked as an internal change (Task), so will not be included in the changelog.
Agreed the name Stores could be better. I would strongly push for it to be a "general stuff-you-need context" else you end up designing situations where a component may need >1 context which then needs HOC to do and it ends up being more boilerplate for little gain. ReactSDKContext is better but a bit long and probably redundant (given it's a React.Context we needn't mention React, so maybe SDKContext?)
SDKContext sounds sensible to me, fwiw.
Please avoid force-pushing as it makes review harder by not allowing commit-by-commit reviewing
The force push was done to rebase this PR off develop rather than #9290 - sorry about that. There were no additional code changes.
Given we squash merge PRs, you can just merge commit instead of rebasing in most cases
Force merging due to regex refactor not needing as much coverage