on master help sign in
FFI Seems to work
commit44f0d6287fa03ab678d46adbee8bb1fcaad52c4c authoriko <ilyakooo0@gmail.com> dateFri 22 Jul 2022, 19:30 (4 years ago) parent688b4b6
10 files changed+81−34
backend/FFI.js+23−3 backend/FFI.purs+12−2 backend/Main.purs+6−1 build.sh+8−2 elm.json+1−1 frontend/FFI.elm+4 frontend/Main.elm+25−13 spago.dhall+1−1 static/index.html+1 test/Main.purs−11
backend/FFI.js+23−3
@@ -1,5 +1,25 @@
11export function startElm(element) {
2− Elm.Main.init({
3− node: document.getElementById(element)
4− })
2+ return function () {
3+ return Elm.Main.init({
4+ node: document.getElementById(element)
5+ })
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+ }
525}
backend/FFI.purs+12−2
@@ -1,10 +1,20 @@
11module FFI
2− ( startElm
2+ ( Elm
3+ , createElmSubscription
4+ , sendElmEvent
5+ , startElm
36 )
47 where
58
69import Prelude
710
11+import Data.Argonaut (Json)
812import Effect (Effect)
913
10−foreign import startElm :: String -> Effect Unit
14+foreign import data Elm :: Type
15+
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
22
33import Prelude
44
5+import Data.Argonaut (encodeJson)
56import Effect (Effect)
67import Effect.Console (log)
78import FFI as FFI
@@ -9,5 +10,9 @@ import FFI as FFI
910
1011main :: Effect Unit
1112main = 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+
1318 log "🍝"
build.sh+8−2
@@ -4,8 +4,14 @@ cp -r "$SRC_DIR"/static/* "$INSTALL_DIR"
44
55rm "$INSTALL_DIR/elm.js"
66
7−elm make --debug "$SRC_DIR/frontend/Main.elm" --output="$INSTALL_DIR/elm.js"
7+BUILDDIR=$(mktemp -d)
8+
9+cp -r $SRC_DIR/* $BUILDDIR
10+
11+ls $BUILDDIR
12+
13+elm make --debug "$BUILDDIR/frontend/Main.elm" --output="$INSTALL_DIR/elm.js"
814
915rm "$INSTALL_DIR/backend.js"
1016
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 @@
11{
22 "type": "application",
33 "source-directories": [
4− "src"
4+ "frontend"
55 ],
66 "elm-version": "0.19.1",
77 "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 @@
11module Main exposing (..)
22
33import Browser
4+import FFI
45import Html exposing (..)
56
67
7−type alias Msg =
8− ()
8+type Msg
9+ = RecieveMessage String
910
1011
1112type alias Model =
12− ()
13+ { messages : List String
14+ }
1315
1416
1517main : Program () Model Msg
1618main =
17− Browser.sandbox
18− { init = init
19+ Browser.element
20+ { init = \_ -> ( init, Cmd.none )
1921 , view = view
2022 , update = update
23+ , subscriptions = \_ -> FFI.getString RecieveMessage
2124 }
2225
2326
2427init : Model
2528init =
26− ()
29+ { messages = []
30+ }
2731
2832
2933view : Model -> Html Msg
30−view _ =
31− h1 [] [ text "HELLO" ]
32−
33−
34−update : Msg -> Model -> Model
35−update _ model =
36− model
34+view model =
35+ div []
36+ [ h1 [] [ text "HELLO" ]
37+ , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
38+ ]
39+
40+
41+update : Msg -> Model -> ( Model, Cmd Msg )
42+update msg 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
1111to generate this file without the comments in this block.
1212-}
1313{ name = "backend"
14−, dependencies = [ "console", "effect", "prelude" ]
14+, dependencies = [ "argonaut", "console", "effect", "prelude" ]
1515, packages = ./packages.dhall
1616, sources = [ "backend/**/*.purs" ]
1717}
static/index.html+1
@@ -13,6 +13,7 @@
1313
1414<body>
1515 <div id="elm"></div>
16+ <div id="elm-2"></div>
1617 <script src="backend.js"></script>
1718</body>
1819
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."