mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 10:07:02 +00:00
ui: Refactor Timer into it's own module.
This commit is contained in:
51
ui/src/Timer.elm
Normal file
51
ui/src/Timer.elm
Normal file
@@ -0,0 +1,51 @@
|
||||
module Timer exposing (Timer, cancel, empty, replace)
|
||||
|
||||
{-| Implements an identity to track an asynchronous timer.
|
||||
-}
|
||||
|
||||
|
||||
type Timer
|
||||
= Empty
|
||||
| Idle Int
|
||||
| Timer Int
|
||||
|
||||
|
||||
empty : Timer
|
||||
empty =
|
||||
Empty
|
||||
|
||||
|
||||
{-| 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
|
||||
|
||||
|
||||
next : Int -> Int
|
||||
next index =
|
||||
if index > 2 ^ 30 then
|
||||
0
|
||||
|
||||
else
|
||||
index + 1
|
||||
Reference in New Issue
Block a user