I’m trying to figure out a really weird bug. In some cases when I run Time.now
, it seems like it consistently gets triggered twice and I can’t figure out any reason why.
Relevant code (I have a Timestamp
data type in my app), so this is the context for the now
function (with some Debug.log statements included):
module Utils.Types.Timestamp exposing (Timestamp(..), now)
type Timestamp
= Timestamp Int
now : (Timestamp -> msg) -> Cmd msg
now msg =
let
_ =
Debug.log "Timestamp.now msg" msg
in
Task.perform
(\posix ->
let
_ =
Debug.log "perform msg" msg
in
msg (fromTime posix)
)
Time.now
fromTime : Posix -> Timestamp
fromTime time =
time
|> Time.posixToMillis
|> Timestamp
When some parts of my code call this function, it prints what you would expect:
Timestamp.now msg: <function>
perform msg: <function>
But in others, this happens:
Timestamp.now msg: <function>
perform msg: <function>
perform msg: <function>
Timestamp.now
is only being called once, but somehow Task.perform
is getting triggered twice (if I print the resulting timestamp, I get two veeeery slightly different timestamps, like 1681681068968
vs 1681681068971
). Is it possible there’s a bug in the Time.now
or Task.perform
implementations or something that causes this to sometimes trigger twice? Or is there another possibility I’m missing? I’m completely bewildered.