Skip to content

Commit

Permalink
Add responseIf and responseMaybe helpers (#250)
Browse files Browse the repository at this point in the history
* Add responseIf and responseMaybe helpers

* Move helper functions into UserUtil, where other similar functions reside
  • Loading branch information
fjvallarino authored Feb 4, 2023
1 parent 0bdd378 commit 63189b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/tutorials/01-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ application (you can check the [lens](external/01-optics.md) tutorial to better
understand those operators). If the model changed, this will trigger a call to
the build UI function.

Similar to `widgetIf` and `widgetMaybe`, a couple of helper functions exist for
event responses: `responseIf` and `responseMaybe`. Their usage pattern is the
same as in the widget helpers.

## Configuration

By default, the starter app sets a few configuration options, including window
Expand Down
28 changes: 28 additions & 0 deletions src/Monomer/Main/UserUtil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,31 @@ Useful for conditionally setting a configuration value.
configMaybe :: Monoid a => Maybe b -> (b -> a) -> a
configMaybe Nothing _ = mempty
configMaybe (Just val) fn = fn val

{-|
Returns the provided 'EventResponse' when True, otherwise returns a no-op.
Useful for conditionally returning a response.
@
...
_ -> [Model newModel, responseIf isValid (SetFocusOnKey "widgetKey")]
@
-}
responseIf :: Bool -> EventResponse s e sp ep -> EventResponse s e sp ep
responseIf True resp = resp
responseIf False _ = NoOpResponse

{-|
Returns the provided 'EventResponse' when 'Just', otherwise returns a no-op.
Useful for conditionally returning a response.
@
...
_ -> [Model newModel, responseMaybe maybeResp]
@
-}
responseMaybe :: Maybe (EventResponse s e sp ep) -> EventResponse s e sp ep
responseMaybe (Just resp) = resp
responseMaybe Nothing = NoOpResponse
3 changes: 3 additions & 0 deletions src/Monomer/Widgets/Composite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ data EventResponse s e sp ep
producer crashes without sending a value, composite will not know about it.
-}
| Producer (ProducerHandler e)
-- | Does nothing. Useful with 'responseIf' and 'responseMaybe' helpers.
| NoOpResponse

{-|
Configuration options for composite:
Expand Down Expand Up @@ -942,6 +944,7 @@ evtResponseToRequest widgetComp widgetKeys response = case response of
Message key msg -> Just $ sendMsgTo widgetComp (CompMsgMessage key msg)
Task task -> Just $ RunTask widgetId path task
Producer producer -> Just $ RunProducer widgetId path producer
NoOpResponse -> Nothing
where
widgetId = widgetComp ^. L.info . L.widgetId
path = widgetComp ^. L.info . L.path
Expand Down

0 comments on commit 63189b7

Please sign in to comment.