on master help sign in
Got bridge working
commit26df76e71d2b29cd7bd9ae65c43fb25d4cc2c4df authoriko <ilyakooo0@gmail.com> dateSat 23 Jul 2022, 05:42 (4 years ago) parent44f0d62
11 files changed+160−15
backend/Bridge.purs+16 backend/Elm.purs+14 backend/Main.purs+3−3 bridge.yaml+2 dev/generate-bridge.sh+3 elm.json+3−2 frontend/Bridge.elm+22 frontend/FFI.elm+20−2 frontend/Main.elm+15−7 frontend/Utils.elm+52 spago.dhall+10−1
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
22
33import Prelude
44
5−import Data.Argonaut (encodeJson)
5+import Bridge (Sub(..))
66import Effect (Effect)
77import Effect.Console (log)
8+import Elm (sendEvent)
89import FFI as FFI
910
1011
1112main :: Effect Unit
1213main = do
1314 app <- FFI.startElm "elm"
14− FFI.sendElmEvent app "getString" (encodeJson "hello from elm")
15− FFI.sendElmEvent app "getString" (encodeJson "hello from elm")
15+ sendEvent app Hello
1616
1717
1818 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 @@
66 "elm-version": "0.19.1",
77 "dependencies": {
88 "direct": {
9+ "bartavelle/json-helpers": "2.0.2",
910 "elm/browser": "1.0.2",
1011 "elm/core": "1.0.5",
11− "elm/html": "1.0.0"
12+ "elm/html": "1.0.0",
13+ "elm/json": "1.1.3"
1214 },
1315 "indirect": {
14− "elm/json": "1.1.3",
1516 "elm/time": "1.0.0",
1617 "elm/url": "1.0.0",
1718 "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)
22
3+import Bridge
4+import Json.Decode as D
35
4−port getString : (String -> msg) -> Sub msg
6+
7+port bridge : (D.Value -> msg) -> Sub msg
8+
9+
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 @@
11module Main exposing (..)
22
33import Browser
4−import FFI
4+import FFI exposing (getBridge)
55import Html exposing (..)
6+import Utils exposing (..)
67
78
89type Msg
910 = RecieveMessage String
11+ | Error String
1012
1113
1214type alias Model =
1315 { messages : List String
16+ , error : Maybe String
1417 }
1518
1619
@@ -20,29 +23,34 @@ main =
2023 { init = \_ -> ( init, Cmd.none )
2124 , view = view
2225 , update = update
23− , subscriptions = \_ -> FFI.getString RecieveMessage
26+ , subscriptions = \_ -> getBridge Error (always (RecieveMessage "Got it!"))
2427 }
2528
2629
2730init : Model
2831init =
2932 { messages = []
33+ , error = Nothing
3034 }
3135
3236
3337view : Model -> Html Msg
3438view model =
3539 div []
36− [ h1 [] [ text "HELLO" ]
37− , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
38− ]
40+ (maybeList model.error (text >> List.singleton >> h2 [])
41+ ++ [ h1 [] [ text "HELLO" ]
42+ , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
43+ ]
44+ )
3945
4046
4147update : Msg -> Model -> ( Model, Cmd Msg )
4248update msg model =
4349 case msg of
4450 RecieveMessage x ->
45− ( { messages = x :: model.messages
46− }
51+ ( { model | messages = x :: model.messages }
4752 , Cmd.none
4853 )
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
1111to generate this file without the comments in this block.
1212-}
1313{ 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+ ]
1524, packages = ./packages.dhall
1625, sources = [ "backend/**/*.purs" ]
1726}