on master help sign in
Got bridge working
26df76eiko
backend/Bridge.pursnew file+16
@@ -0,0 +1,16 @@
1+-- File auto generated by purescript-bridge! --
2+module Bridge where
3+
4+import Data.Generic.Rep (class Generic)
5+import Data.Maybe (Maybe(..))
6+import Data.Newtype (class Newtype)
7+
8+import Prelude
9+
10+data Sub =
11+ Hello
12+
13+derive instance genericSub :: Generic Sub _
14+derive instance eqSub :: Eq Sub
15+derive instance ordSub :: Ord Sub
16+
backend/Elm.pursnew file+14
@@ -0,0 +1,14 @@
1+module Elm
2+ ( sendEvent
3+ )
4+ where
5+
6+import Prelude
7+
8+import Bridge as Bridge
9+import Data.Argonaut.Encode.Generic (genericEncodeJson)
10+import Effect (Effect)
11+import FFI as FFI
12+
13+sendEvent ∷ FFI.Elm → Bridge.Sub → Effect Unit
14+sendEvent elm a = FFI.sendElmEvent elm "bridge" (genericEncodeJson a)
backend/Main.purs+3−3
@@ -2,17 +2,17 @@ module Main where
2
2
3import Prelude
3import Prelude
4
4
5−import Data.Argonaut (encodeJson)
5+import Bridge (Sub(..))
6import Effect (Effect)
6import Effect (Effect)
7import Effect.Console (log)
7import Effect.Console (log)
8+import Elm (sendEvent)
8import FFI as FFI
9import FFI as FFI
9
10
10
11
11main :: Effect Unit
12main :: Effect Unit
12main = do
13main = do
13 app <- FFI.startElm "elm"
14 app <- FFI.startElm "elm"
14− FFI.sendElmEvent app "getString" (encodeJson "hello from elm")
15+ sendEvent app Hello
15− FFI.sendElmEvent app "getString" (encodeJson "hello from elm")
16
16
17
17
18 log "🍝"
18 log "🍝"
bridge.yamlnew file+2
@@ -0,0 +1,2 @@
1+Sub:
2+- Hello
dev/generate-bridge.shnew file+3
@@ -0,0 +1,3 @@
1+#!/bin/sh
2+
3+elm-ps-bridge --input bridge.yaml --elmOutput=Frontend/Bridge.elm --psOutput=backend/Bridge.purs
elm.json+3−2
@@ -6,12 +6,13 @@
6 "elm-version": "0.19.1",
6 "elm-version": "0.19.1",
7 "dependencies": {
7 "dependencies": {
8 "direct": {
8 "direct": {
9+ "bartavelle/json-helpers": "2.0.2",
9 "elm/browser": "1.0.2",
10 "elm/browser": "1.0.2",
10 "elm/core": "1.0.5",
11 "elm/core": "1.0.5",
11− "elm/html": "1.0.0"
12+ "elm/html": "1.0.0",
13+ "elm/json": "1.1.3"
12 },
14 },
13 "indirect": {
15 "indirect": {
14− "elm/json": "1.1.3",
15 "elm/time": "1.0.0",
16 "elm/time": "1.0.0",
16 "elm/url": "1.0.0",
17 "elm/url": "1.0.0",
17 "elm/virtual-dom": "1.0.3"
18 "elm/virtual-dom": "1.0.3"
frontend/Bridge.elmnew file+22
@@ -0,0 +1,22 @@
1+module Bridge exposing(..)
2+
3+import Json.Decode
4+import Json.Encode exposing (Value)
5+-- The following module comes from bartavelle/json-helpers
6+import Json.Helpers exposing (..)
7+import Dict exposing (Dict)
8+import Set exposing (Set)
9+
10+
11+type Sub =
12+ Hello
13+
14+jsonDecSub : Json.Decode.Decoder ( Sub )
15+jsonDecSub =
16+ Json.Decode.lazy (\_ -> Json.Decode.succeed Hello)
17+
18+
19+jsonEncSub : Sub -> Value
20+jsonEncSub (Hello ) =
21+ Json.Encode.list identity []
22+
frontend/FFI.elm+20−2
@@ -1,4 +1,22 @@
1−port module FFI exposing (..)
1+port module FFI exposing (getBridge)
2
2
3+import Bridge
4+import Json.Decode as D
3
5
6+
7+port bridge : (D.Value -> msg) -> Sub msg
8+
9+
4−port getString : (String -> msg) -> Sub msg
10+getBridge : (String -> msg) -> (Bridge.Sub -> msg) -> Sub msg
11+getBridge err f =
12+ bridge
13+ (D.decodeValue Bridge.jsonDecSub
14+ >> (\res ->
15+ case res of
16+ Ok a ->
17+ f a
18+
19+ Err e ->
20+ e |> D.errorToString |> err
21+ )
22+ )
frontend/Main.elm+15−7
@@ -1,16 +1,19 @@
1module Main exposing (..)
1module Main exposing (..)
2
2
3import Browser
3import Browser
4−import FFI
4+import FFI exposing (getBridge)
5import Html exposing (..)
5import Html exposing (..)
6+import Utils exposing (..)
6
7
7
8
8type Msg
9type Msg
9 = RecieveMessage String
10 = RecieveMessage String
11+ | Error String
10
12
11
13
12type alias Model =
14type alias Model =
13 { messages : List String
15 { messages : List String
16+ , error : Maybe String
14 }
17 }
15
18
16
19
@@ -20,29 +23,34 @@ main =
20 { init = \_ -> ( init, Cmd.none )
23 { init = \_ -> ( init, Cmd.none )
21 , view = view
24 , view = view
22 , update = update
25 , update = update
23− , subscriptions = \_ -> FFI.getString RecieveMessage
26+ , subscriptions = \_ -> getBridge Error (always (RecieveMessage "Got it!"))
24 }
27 }
25
28
26
29
27init : Model
30init : Model
28init =
31init =
29 { messages = []
32 { messages = []
33+ , error = Nothing
30 }
34 }
31
35
32
36
33view : Model -> Html Msg
37view : Model -> Html Msg
34view model =
38view model =
35 div []
39 div []
40+ (maybeList model.error (text >> List.singleton >> h2 [])
36− [ h1 [] [ text "HELLO" ]
41+ ++ [ h1 [] [ text "HELLO" ]
37− , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
42+ , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
38− ]
43+ ]
44+ )
39
45
40
46
41update : Msg -> Model -> ( Model, Cmd Msg )
47update : Msg -> Model -> ( Model, Cmd Msg )
42update msg model =
48update msg model =
43 case msg of
49 case msg of
44 RecieveMessage x ->
50 RecieveMessage x ->
45− ( { messages = x :: model.messages
51+ ( { model | messages = x :: model.messages }
46− }
47 , Cmd.none
52 , Cmd.none
48 )
53 )
54+
55+ Error err ->
56+ ( { model | error = Just err }, Cmd.none )
frontend/Utils.elmnew file+52
@@ -0,0 +1,52 @@
1+module Utils exposing (..)
2+
3+import Task
4+
5+
6+flip : (a -> b -> c) -> b -> a -> c
7+flip f b a =
8+ f a b
9+
10+
11+const : a -> b -> a
12+const a _ =
13+ a
14+
15+
16+uncurry : (a -> b -> c) -> ( a, b ) -> c
17+uncurry f ( a, b ) =
18+ f a b
19+
20+
21+pureCmd : a -> Cmd a
22+pureCmd a =
23+ Task.perform identity <| Task.succeed a
24+
25+
26+result : (e -> a) -> (x -> a) -> Result e x -> a
27+result err ok r =
28+ case r of
29+ Result.Ok x ->
30+ ok x
31+
32+ Result.Err e ->
33+ err e
34+
35+
36+optionals : Bool -> a -> List a
37+optionals b a =
38+ if b then
39+ [ a ]
40+
41+ else
42+ []
43+
44+
45+maybeList : Maybe a -> (a -> b) -> List b
46+maybeList m f =
47+ case m of
48+ Just a ->
49+ [ f a ]
50+
51+ Nothing ->
52+ []
spago.dhall+10−1
@@ -11,7 +11,16 @@ 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 = [ "argonaut", "console", "effect", "prelude" ]
14+, dependencies =
15+ [ "argonaut"
16+ , "argonaut-generic"
17+ , "console"
18+ , "effect"
19+ , "foreign"
20+ , "maybe"
21+ , "newtype"
22+ , "prelude"
23+ ]
15, packages = ./packages.dhall
24, packages = ./packages.dhall
16, sources = [ "backend/**/*.purs" ]
25, sources = [ "backend/**/*.purs" ]
17}
26}