I have a type alias like this:
type alias Comment =
{ content : String
, timestamp : Time.Posix
}
The problem when creating a port like this…
port receiveComments : (List Comment -> msg) -> Sub msg
is that I get the error The 'receiveComments' port is trying to transmit a 'Posix' value:
I can successfully decode a value from the port using port receiveComments : (Json.Encode.Value -> msg) -> Sub msg and a little help with some decoder utils like these…
timeDecoder : Json.Decode.Decoder Time.Posix
timeDecoder =
Json.Decode.int |> Json.Decode.andThen timeHelper
timeHelper : Int -> Json.Decode.Decoder Time.Posix
timeHelper time =
if time < 0 then
Json.Decode.fail "Cannot decode time less than zero."
else
Json.Decode.succeed <| Time.millisToPosix time
commentDecoder : Json.Decode.Decoder Comment
commentDecoder =
Json.Decode.map2 Comment
(Json.Decode.field "content" Json.Decode.string)
(Json.Decode.field "timestamp" timeDecoder)
However, I’d like to hide all the gritty details of decoding a Comment from JSON and allow other users of this port to subscribe to something with the equivalent of receiveComments : (List Comment -> msg) -> Sub msg. Is it somehow possible to wrap my port in a function or otherwise achieve this result?