What can I do to eliminate the unsafePerformIO in this Reflex.Dom code?
main = mainWidget $
el "div" $ do
let fileInputConfig = FileInputConfig (constDyn Map.empty)
fi <- fileInput fileInputConfig
let uploads :: Dynamic t [File] = value fi
upload :: Dynamic t (Maybe File) <- (return . fmap headMay) uploads
getNameAction :: Dynamic t (Maybe (IO Text)) <- (return . fmap (getNameText <$>)) upload
filename :: Dynamic t (Maybe Text) <- (return . fmap (unsafePerformIO <$>)) getNameAction
el "div" $ dynText (show <$> filename)
return ()
where getNameText :: MonadIO m => File -> m Text
getNameText = getName
I did my best to play "connect-the-dots" with types, but I couldn't find a way without using unsafePerformIO
. I think it is safe in this case, but obviously there are other similar things that you might want to do that will not be safe.
source to share
For such things it is usually required performEvent
. I don't have a compiler / REPL on hand to provide more details, and the function is hidden behind some hackers like ( https://github.com/reflex-frp/reflex/blob/9575a5660334fb8a617da1cd9aa1b522e8e4ddb7/src/Reflex/PerformE4ddhvent ), but the bottom line is that if you have a carrier event IO
, you can fire it wherever that event happens.
Now you have Dynamic
, but you can
- extract an event from it
- maybe it shouldn't be dynamic (it doesn't make sense since you don't want to look into the value
IO
)
source to share