Add higher-order component for connecting to global state #45
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rationale
Conduit has a global state which contains the currently logged in user via a mutable reference. That information is in global state because several components (the router, the home page, and the editor) need to know about the current user in order to function correctly. Those components need to take several actions to properly hook up to the global state:
HandleUserBus
, which updates their state when the value of the current user changesThis is all a bit tedious and largely irrelevant to the components other than having an always up-to-date reference to the current user. Ideally, we could just have that reference and not worry about the mechanics of getting it and updating it.
This PR introduces a new higher-order component,
Connect.component
, which takes care of all of this behind the scenes. With this change, components can remove all the steps I described.Instead, they now have
currentUser :: Maybe Profile
inserted into their input type and can use the ordinaryreceive
function from Halogen to update their state when this value changes. Parent components can use the child components as usual, without even knowing that the higher order component is in place. As far as the parent is concerned the component has its old input; as far as the child is concerned, its input hascurrentUser
inserted. Querying, messages, and so on all behave as usual.Other notes
This module could use some extra explanatory comments about higher-order components in Halogen, but I'm wary of stuffing it full of information not relevant for folks already versed in Halogen and who have used higher order components before.
This build has not been tested locally yet and will require that before merging.