forked from halfzebra/elm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
86 lines (49 loc) · 1.56 KB
/
Main.elm
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
85
86
port module Main exposing (..)
import String
import Task
import Html exposing (..)
import Html.Attributes exposing (..)
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- SUBSCRIPTIONS
{-| With an outgoing port, I want to tell JavaScript to send some value to Elm.
That does not require sending data to JavaScript, so I send an empty Tuple.
Note, that you can not specify the exact message type of port commands.
Both port functions can be exported and used outside of the module.
Since output does not emit any messages, we have type annotation definition with a generic `msg`,
so it's easier to use it outside of this module
and you don't have to use Cmd.map to re-map a non-existing message type.
The subscription as well can be used outside of this module,
you can use any message type to receive messages from the port.
-}
port output : () -> Cmd msg
port input : (Int -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
input Get
-- MODEL
type alias Model =
{ number : Int }
-- VIEW
view : Model -> Html Msg
view model =
text (toString model.number)
-- UPDATE
type Msg
= Get Int
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case (Debug.log "MESSAGE: " msg) of
Get x ->
( Model x, Cmd.none )
-- INIT
init : ( Model, Cmd Msg )
init =
{- Send a message through port upon initialization. -}
( Model 0, output () )