DEV Community

Discussion on: Smart Event Listeners

Collapse
 
dwayne profile image
Dwayne Crooks • Edited

Update: I've found the following function to be useful when implementing smart event listeners.

fromMaybe : Maybe a -> JD.Decoder a
fromMaybe ma =
    case ma of
        Just a ->
            JD.succeed a

        Nothing ->
            JD.fail "ignored"
Enter fullscreen mode Exit fullscreen mode

Then, onVolumeInput becomes

onVolumeInput : (Volume -> msg) -> H.Attribute msg
onVolumeInput onVolume =
    HE.targetValue
        |> JD.andThen (Volume.fromString >> Maybe.map onVolume >> JD.fromMaybe)
        |> HE.on "input"
Enter fullscreen mode Exit fullscreen mode

See this commit for a full example.