Skip to content

Commit a0fa93a

Browse files
committed
Add checkMail function.
This is a utility function to aid in process 'Mail' received from other 'Component'.
1 parent 44bf05e commit a0fa93a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Miso.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Miso
3131
, Sink
3232
-- ** Component
3333
, mail
34+
, checkMail
3435
, parent
3536
, broadcast
3637
-- ** Subscriptions

src/Miso/Runtime.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Miso.Runtime
3939
, ComponentState
4040
-- ** Communication
4141
, mail
42+
, checkMail
4243
, broadcast
4344
, parent
4445
) where
@@ -930,6 +931,30 @@ mail vcompId message = io_ $
930931
pure ()
931932
Just ComponentState {..} ->
932933
liftIO $ sendMail componentMailbox (toJSON message)
934+
----------------------------------------------------------------------------
935+
-- | Helper function for processing 'Mail' from 'mail'.
936+
--
937+
-- @
938+
--
939+
-- data Action
940+
-- = ParsedMail Message
941+
-- | ErrorMail MisoString
942+
--
943+
-- main = app { mailbox = checkMail ParsedMail ErrorMail }
944+
--
945+
-- @
946+
--
947+
-- @since 1.9.0.0
948+
checkMail
949+
:: FromJSON value
950+
=> (value -> action)
951+
-> (MisoString -> action)
952+
-> Value
953+
-> Maybe action
954+
checkMail successful errorful value =
955+
pure $ case fromJSON value of
956+
Success x -> successful x
957+
Error err -> errorful (ms err)
933958
-----------------------------------------------------------------------------
934959
-- | Fetches the parent `model` from the child.
935960
--

0 commit comments

Comments
 (0)