-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMessages.elm
More file actions
84 lines (67 loc) · 2.21 KB
/
Copy pathMessages.elm
File metadata and controls
84 lines (67 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Messages exposing (Model, Msg(..), init, main, update, updateDebouncer, view)
{-| This does exactly the same thing as the `Basic` example, but it
uses `Debouncer.Messages` instead of `Debouncer.Basic`. This simplifies
your code in the (common) case where what you're debouncing is your
own `Msg` type. (You would want `Debouncer.Basic` in other cases, since
it is more general).
-}
import Browser
import Debouncer.Messages as Debouncer exposing (Debouncer, fromSeconds, provideInput, settleWhenQuietFor, toDebouncer)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
type alias Model =
{ quietForOneSecond : Debouncer Msg
, messages : List String
}
init : ( Model, Cmd Msg )
init =
( { quietForOneSecond =
Debouncer.manual
|> settleWhenQuietFor (Just <| fromSeconds 1)
|> toDebouncer
, messages = []
}
, Cmd.none
)
type Msg
= MsgQuietForOneSecond (Debouncer.Msg Msg)
| DoSomething
updateDebouncer : Debouncer.UpdateConfig Msg Model
updateDebouncer =
{ mapMsg = MsgQuietForOneSecond
, getDebouncer = .quietForOneSecond
, setDebouncer = \debouncer model -> { model | quietForOneSecond = debouncer }
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
MsgQuietForOneSecond subMsg ->
Debouncer.update update updateDebouncer subMsg model
DoSomething ->
( { model | messages = model.messages ++ [ "I did something" ] }
, Cmd.none
)
view : Model -> Html Msg
view model =
div [ style "margin" "1em" ]
[ button
[ DoSomething
|> provideInput
|> MsgQuietForOneSecond
|> onClick
]
[ text "Click here repeatedly." ]
, p [] [ text " I'll add a message below once you stop clicking for one second." ]
, model.messages
|> List.map (\message -> p [] [ text message ])
|> div []
]
main : Program () Model Msg
main =
Browser.element
{ init = always init
, view = view
, update = update
, subscriptions = always Sub.none
}