1
0
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:
James Hillyerd
2020-04-05 16:22:16 -07:00
parent 8a3d291ff3
commit e8e506f870
2 changed files with 57 additions and 34 deletions

51
ui/src/Timer.elm Normal file
View 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