-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: can the Store be used by multiple "apps"? #4
Comments
I haven't explored this as a possibility, and I haven't done this with global states elsewhere (for example, in Redux). However, since the purescript-halogen-store/src/Halogen/Store/Monad.purs Lines 57 to 83 in 583430d
The purescript-halogen-store/src/Halogen/Store/Monad.purs Lines 122 to 134 in 583430d
but I believe you could split out the function that creates a purescript-halogen-store/src/Halogen/Store/Monad.purs Lines 130 to 133 in 583430d
and then reuse the same value for every component that you hoist:
So the resulting code might be something like main :: Effect Unit
main = launchAff_ do
store <- liftEffect do
value <- Ref.new initialStore
{ emitter, listener } <- HS.create
pure { value, emitter, listener, reducer }
app1 <- hoist (\(StoreT m) -> runReaderT m hs) App1.component
app2 <- hoist (\(StoreT m) -> runReaderT m hs) App2.component
app3 <- hoist (\(StoreT m) -> runReaderT m hs) App3.component
... |
Oh, thank you so much @thomashoneyman! I had started with a dummy root component, but it seemed like a lot of extra machinery to accomplish the task. Maybe at some point I can circle back and contribute an example (especially if you get in an |
Does this mean Apps 1-3 should be using the whole (Above example should be example, |
They can just connect to the store normally. You’d only use the ReduxTodo approach if you wanted to make multiple stores within the main store. Hoisting refers to taking a component that runs in some non-Aff monad (like StoreT) and transforming it to run in Aff instead. Ultimately Halogen only runs components in Aff. |
Speaking of Aff.launchAff_ do
...
mbookShowingIO ← do
mdialogElem ← HA.selectElement (QuerySelector "#BookShowingDialog .Dialog-container")
case mdialogElem of
Nothing → pure Nothing
Just dialogElem → do
bookShowing ← H.hoist (\(StoreT m) → runReaderT m store) BookShowing.component
Just <$> runUI bookShowing unit dialogElem That component: component ∷
∀ output m.
MonadAff m ⇒
Store.MonadStore Store.Action Store.Store m ⇒
H.Component Query Input output m
component =
Store.connect selectState $ H.mkComponent { ... } Error
|
Can you make a reproducible example on Try PureScript that I could take a look at? Otherwise, I'd recommend giving a type annotation to each line because sometimes in |
Aff.launchAff_ do
-- BookShowing component
(mbookShowingIO ∷ Maybe (H.HalogenIO BookShowing.Query Void Aff)) ← do
mdialogElem ← HA.selectElement (QuerySelector "#BookShowingDialog .Dialog-container")
case mdialogElem of
Nothing → pure Nothing
Just dialogElem → do
let cmpnt = H.hoist (\(StoreT m) → runReaderT m store) BookShowing.component
Just <$> runUI cmpnt unit dialogElem |
I have a question: can Halogen Store be used as a global store for multiple application/components? The examples all have a single root component with sub components rendered inside of it, and not using it with multiple root apps and I'm unclear on if this is supported skimming the code.
To give a more concrete example. I have 2 widgets on a server-rendered, non-SPA page,
A
andB
. BothA
&B
want to subscribe to connectivity of the browser (e.g. are we online of offline?). A simple store would havetype Store = { isOnline ∷ Boolean }
which would be subscribed byA
&B
to know if the browser has gone offline or come online so the event listeners are only set up once and everything is kept in sync. (But I have several similar global state values to share in this manner)Do I need to a create a "root" application/component that has no render to achieve this and it handles the main Store values, or is there a way to initialize and and subscribe to a store for multiple entries? Or is this library the wrong thing for this approach and just use
MonadAsk
, et. al.?The text was updated successfully, but these errors were encountered: