on master help sign in
FFI Seems to work
44f0d62iko
backend/FFI.js+23−3
@@ -1,5 +1,25 @@
1export function startElm(element) {
1export function startElm(element) {
2+ return function () {
2− Elm.Main.init({
3+ return Elm.Main.init({
3− node: document.getElementById(element)
4+ node: document.getElementById(element)
5+ })
4− })
6+ }
7+}
8+
9+export function createElmSubscription(elm) {
10+ return function (name) {
11+ return function (callback) {
12+ return function () {
13+ elm.ports[name].subscribe(callback)
14+ }
15+ }
16+ }
17+}
18+
19+export function sendElmEvent(elm) {
20+ return function (name) {
21+ return function (value) {
22+ return function () { elm.ports[name].send(value) }
23+ }
24+ }
5}
25}
backend/FFI.purs+12−2
@@ -1,10 +1,20 @@
1module FFI
1module FFI
2+ ( Elm
3+ , createElmSubscription
4+ , sendElmEvent
2− ( startElm
5+ , startElm
3 )
6 )
4 where
7 where
5
8
6import Prelude
9import Prelude
7
10
11+import Data.Argonaut (Json)
8import Effect (Effect)
12import Effect (Effect)
9
13
14+foreign import data Elm :: Type
15+
10−foreign import startElm :: String -> Effect Unit
16+foreign import startElm :: String -> Effect Elm
17+
18+foreign import createElmSubscription :: Elm -> String -> (Json -> Effect Unit) -> Effect Unit
19+
20+foreign import sendElmEvent :: Elm -> String -> Json -> Effect Unit
backend/Main.purs+6−1
@@ -2,6 +2,7 @@ module Main where
2
2
3import Prelude
3import Prelude
4
4
5+import Data.Argonaut (encodeJson)
5import Effect (Effect)
6import Effect (Effect)
6import Effect.Console (log)
7import Effect.Console (log)
7import FFI as FFI
8import FFI as FFI
@@ -9,5 +10,9 @@ import FFI as FFI
9
10
10main :: Effect Unit
11main :: Effect Unit
11main = do
12main = do
12− FFI.startElm "elm"
13+ app <- FFI.startElm "elm"
14+ FFI.sendElmEvent app "getString" (encodeJson "hello from elm")
15+ FFI.sendElmEvent app "getString" (encodeJson "hello from elm")
16+
17+
13 log "🍝"
18 log "🍝"
build.sh+8−2
@@ -4,8 +4,14 @@ cp -r "$SRC_DIR"/static/* "$INSTALL_DIR"
4
4
5rm "$INSTALL_DIR/elm.js"
5rm "$INSTALL_DIR/elm.js"
6
6
7+BUILDDIR=$(mktemp -d)
8+
9+cp -r $SRC_DIR/* $BUILDDIR
10+
11+ls $BUILDDIR
12+
7−elm make --debug "$SRC_DIR/frontend/Main.elm" --output="$INSTALL_DIR/elm.js"
13+elm make --debug "$BUILDDIR/frontend/Main.elm" --output="$INSTALL_DIR/elm.js"
8
14
9rm "$INSTALL_DIR/backend.js"
15rm "$INSTALL_DIR/backend.js"
10
16
11−(cd "$SRC_DIR" && spago bundle-app --to "$INSTALL_DIR/backend.js")
17+(cd "$BUILDDIR" && spago bundle-app --to "$INSTALL_DIR/backend.js")
elm.json+1−1
@@ -1,7 +1,7 @@
1{
1{
2 "type": "application",
2 "type": "application",
3 "source-directories": [
3 "source-directories": [
4− "src"
4+ "frontend"
5 ],
5 ],
6 "elm-version": "0.19.1",
6 "elm-version": "0.19.1",
7 "dependencies": {
7 "dependencies": {
frontend/FFI.elmnew file+4
@@ -0,0 +1,4 @@
1+port module FFI exposing (..)
2+
3+
4+port getString : (String -> msg) -> Sub msg
frontend/Main.elm+25−13
@@ -1,36 +1,48 @@
1module Main exposing (..)
1module Main exposing (..)
2
2
3import Browser
3import Browser
4+import FFI
4import Html exposing (..)
5import Html exposing (..)
5
6
6
7
7−type alias Msg =
8+type Msg
8− ()
9+ = RecieveMessage String
9
10
10
11
11type alias Model =
12type alias Model =
12− ()
13+ { messages : List String
14+ }
13
15
14
16
15main : Program () Model Msg
17main : Program () Model Msg
16main =
18main =
17− Browser.sandbox
19+ Browser.element
18− { init = init
20+ { init = \_ -> ( init, Cmd.none )
19 , view = view
21 , view = view
20 , update = update
22 , update = update
23+ , subscriptions = \_ -> FFI.getString RecieveMessage
21 }
24 }
22
25
23
26
24init : Model
27init : Model
25init =
28init =
26− ()
29+ { messages = []
30+ }
27
31
28
32
29view : Model -> Html Msg
33view : Model -> Html Msg
30−view _ =
34+view model =
35+ div []
31− h1 [] [ text "HELLO" ]
36+ [ h1 [] [ text "HELLO" ]
32−
37+ , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
33−
38+ ]
39+
40+
34−update : Msg -> Model -> Model
41+update : Msg -> Model -> ( Model, Cmd Msg )
35−update _ model =
42+update msg model =
36− model
43+ case msg of
44+ RecieveMessage x ->
45+ ( { messages = x :: model.messages
46+ }
47+ , Cmd.none
48+ )
spago.dhall+1−1
@@ -11,7 +11,7 @@ When creating a new Spago project, you can use
11to generate this file without the comments in this block.
11to generate this file without the comments in this block.
12-}
12-}
13{ name = "backend"
13{ name = "backend"
14−, dependencies = [ "console", "effect", "prelude" ]
14+, dependencies = [ "argonaut", "console", "effect", "prelude" ]
15, packages = ./packages.dhall
15, packages = ./packages.dhall
16, sources = [ "backend/**/*.purs" ]
16, sources = [ "backend/**/*.purs" ]
17}
17}
static/index.html+1
@@ -13,6 +13,7 @@
13
13
14<body>
14<body>
15 <div id="elm"></div>
15 <div id="elm"></div>
16+ <div id="elm-2"></div>
16 <script src="backend.js"></script>
17 <script src="backend.js"></script>
17</body>
18</body>
18
19
test/Main.pursdeleted−11
@@ -1,11 +0,0 @@
1−module Test.Main where
2−
3−import Prelude
4−
5−import Effect (Effect)
6−import Effect.Class.Console (log)
7−
8−main :: Effect Unit
9−main = do
10− log "🍝"
11− log "You should add some tests."