mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
All pages now leverage Effects for most of their Session and Cmd requests. More work required for routing and other lingering Cmd use.
61 lines
1.0 KiB
Elm
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
|