1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00
Files
go-inbucket/ui/src/Timer.elm
James Hillyerd 2162a4caaa ui: Add an Effect system to handle global state and Elm Cmds (#176)
All pages now leverage Effects for most of their Session and Cmd requests. More work required for routing and other lingering Cmd use.
2020-09-12 19:45:14 -07:00

61 lines
1.0 KiB
Elm

module Timer exposing (Timer, cancel, empty, replace, schedule)
import Process
import Task
{-| Implements an identity to track an asynchronous timer.
-}
type Timer
= Empty
| Idle Int
| Timer Int
empty : Timer
empty =
Empty
schedule : (Timer -> msg) -> Timer -> Float -> Cmd msg
schedule message timer millis =
Task.perform (always (message timer)) (Process.sleep millis)
{-| Replaces the provided timer with a newly created one.
-}
replace : Timer -> Timer
replace previous =
case previous of
Empty ->
Timer 0
Idle index ->
Timer (next index)
Timer index ->
Timer (next index)
{-| Cancels the provided timer without creating a replacement.
-}
cancel : Timer -> Timer
cancel previous =
case previous of
Timer index ->
Idle index
_ ->
previous
{-| Increments the timer identity, preventing integer overflow.
-}
next : Int -> Int
next index =
if index > 2 ^ 30 then
0
else
index + 1